]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/losetup.c
losetup: remove warning for unsupported -e and -E
[thirdparty/util-linux.git] / sys-utils / losetup.c
1 /*
2 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
3 * Originally from Ted's losetup.c
4 *
5 * losetup.c - setup and control loop devices
6 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/ioctl.h>
14 #include <sys/stat.h>
15 #include <inttypes.h>
16 #include <getopt.h>
17
18 #include "c.h"
19 #include "tt.h"
20 #include "nls.h"
21 #include "strutils.h"
22 #include "loopdev.h"
23 #include "closestream.h"
24 #include "optutils.h"
25 #include "xalloc.h"
26 #include "canonicalize.h"
27
28 enum {
29 A_CREATE = 1, /* setup a new device */
30 A_DELETE, /* delete given device(s) */
31 A_DELETE_ALL, /* delete all devices */
32 A_SHOW, /* list devices */
33 A_SHOW_ONE, /* print info about one device */
34 A_FIND_FREE, /* find first unused */
35 A_SET_CAPACITY, /* set device capacity */
36 };
37
38 enum {
39 COL_NAME = 0,
40 COL_AUTOCLR,
41 COL_BACK_FILE,
42 COL_BACK_INO,
43 COL_BACK_MAJMIN,
44 COL_MAJMIN,
45 COL_OFFSET,
46 COL_PARTSCAN,
47 COL_RO,
48 COL_SIZELIMIT,
49 };
50
51 struct tt *tt;
52
53 struct colinfo {
54 const char *name;
55 double whint;
56 int flags;
57 const char *help;
58 };
59
60 static struct colinfo infos[] = {
61 [COL_AUTOCLR] = { "AUTOCLEAR", 1, TT_FL_RIGHT, N_("autoclear flag set")},
62 [COL_BACK_FILE] = { "BACK-FILE", 0.3, 0, N_("device backing file")},
63 [COL_BACK_INO] = { "BACK-INO", 4, TT_FL_RIGHT, N_("backing file inode number")},
64 [COL_BACK_MAJMIN] = { "BACK-MAJ:MIN", 6, 0, N_("backing file major:minor device number")},
65 [COL_NAME] = { "NAME", 0.25, 0, N_("loop device name")},
66 [COL_OFFSET] = { "OFFSET", 5, TT_FL_RIGHT, N_("offset from the beginning")},
67 [COL_PARTSCAN] = { "PARTSCAN", 1, TT_FL_RIGHT, N_("partscan flag set")},
68 [COL_RO] = { "RO", 1, TT_FL_RIGHT, N_("read-only device")},
69 [COL_SIZELIMIT] = { "SIZELIMIT", 5, TT_FL_RIGHT, N_("size limit of the file in bytes")},
70 [COL_MAJMIN] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")},
71 };
72
73 #define NCOLS ARRAY_SIZE(infos)
74
75 static int columns[NCOLS] = {-1};
76 static int ncolumns;
77 static int verbose;
78
79 static int get_column_id(int num)
80 {
81 assert(ARRAY_SIZE(columns) == NCOLS);
82 assert(num < ncolumns);
83 assert(columns[num] < (int) NCOLS);
84 return columns[num];
85 }
86
87 static struct colinfo *get_column_info(int num)
88 {
89 return &infos[ get_column_id(num) ];
90 }
91
92 static int column_name_to_id(const char *name, size_t namesz)
93 {
94 size_t i;
95
96 for (i = 0; i < NCOLS; i++) {
97 const char *cn = infos[i].name;
98
99 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
100 return i;
101 }
102 warnx(_("unknown column: %s"), name);
103 return -1;
104 }
105
106 static int printf_loopdev(struct loopdev_cxt *lc)
107 {
108 uint64_t x;
109 dev_t dev = 0;
110 ino_t ino = 0;
111 char *fname = NULL;
112 uint32_t type;
113
114 fname = loopcxt_get_backing_file(lc);
115 if (!fname)
116 return -EINVAL;
117
118 if (loopcxt_get_backing_devno(lc, &dev) == 0)
119 loopcxt_get_backing_inode(lc, &ino);
120
121 if (!dev && !ino) {
122 /*
123 * Probably non-root user (no permissions to
124 * call LOOP_GET_STATUS ioctls).
125 */
126 printf("%s: []: (%s)",
127 loopcxt_get_device(lc), fname);
128
129 if (loopcxt_get_offset(lc, &x) == 0 && x)
130 printf(_(", offset %ju"), x);
131
132 if (loopcxt_get_sizelimit(lc, &x) == 0 && x)
133 printf(_(", sizelimit %ju"), x);
134 printf("\n");
135 return 0;
136 }
137
138 printf("%s: [%04d]:%" PRIu64 " (%s)",
139 loopcxt_get_device(lc), (int) dev, ino, fname);
140
141 if (loopcxt_get_offset(lc, &x) == 0 && x)
142 printf(_(", offset %ju"), x);
143
144 if (loopcxt_get_sizelimit(lc, &x) == 0 && x)
145 printf(_(", sizelimit %ju"), x);
146
147 if (loopcxt_get_encrypt_type(lc, &type) == 0) {
148 const char *e = loopcxt_get_crypt_name(lc);
149
150 if ((!e || !*e) && type == 1)
151 e = "XOR";
152 if (e && *e)
153 printf(_(", encryption %s (type %u)"), e, type);
154 }
155 printf("\n");
156 return 0;
157 }
158
159 static int show_all_loops(struct loopdev_cxt *lc, const char *file,
160 uint64_t offset, int flags)
161 {
162 struct stat sbuf, *st = &sbuf;
163 char *cn_file = NULL;
164
165 if (loopcxt_init_iterator(lc, LOOPITER_FL_USED))
166 return -1;
167
168 if (!file || stat(file, st))
169 st = NULL;
170
171 while (loopcxt_next(lc) == 0) {
172 if (file) {
173 int used;
174 const char *bf = cn_file ? cn_file : file;
175
176 used = loopcxt_is_used(lc, st, bf, offset, flags);
177 if (!used && !cn_file) {
178 bf = cn_file = canonicalize_path(file);
179 used = loopcxt_is_used(lc, st, bf, offset, flags);
180 }
181 if (!used)
182 continue;
183 }
184 printf_loopdev(lc);
185 }
186 loopcxt_deinit_iterator(lc);
187 free(cn_file);
188 return 0;
189 }
190
191 static int delete_loop(struct loopdev_cxt *lc)
192 {
193 if (loopcxt_delete_device(lc))
194 warn(_("%s: detach failed"), loopcxt_get_device(lc));
195 else
196 return 0;
197
198 return -1;
199 }
200
201 static int delete_all_loops(struct loopdev_cxt *lc)
202 {
203 int res = 0;
204
205 if (loopcxt_init_iterator(lc, LOOPITER_FL_USED))
206 return -1;
207
208 while (loopcxt_next(lc) == 0)
209 res += delete_loop(lc);
210
211 loopcxt_deinit_iterator(lc);
212 return res;
213 }
214
215 static int set_tt_data(struct loopdev_cxt *lc, struct tt_line *ln)
216 {
217 int i;
218
219 for (i = 0; i < ncolumns; i++) {
220 const char *p = NULL;
221 char *np = NULL;
222 uint64_t x = 0;
223
224 switch(get_column_id(i)) {
225 case COL_NAME:
226 p = loopcxt_get_device(lc);
227 if (p)
228 tt_line_set_data(ln, i, xstrdup(p));
229 break;
230 case COL_BACK_FILE:
231 p = loopcxt_get_backing_file(lc);
232 if (p)
233 tt_line_set_data(ln, i, xstrdup(p));
234 break;
235 case COL_OFFSET:
236 if (loopcxt_get_offset(lc, &x) == 0)
237 xasprintf(&np, "%jd", x);
238 if (np)
239 tt_line_set_data(ln, i, np);
240 break;
241 case COL_SIZELIMIT:
242 if (loopcxt_get_sizelimit(lc, &x) == 0)
243 xasprintf(&np, "%jd", x);
244 if (np)
245 tt_line_set_data(ln, i, np);
246 break;
247 case COL_BACK_MAJMIN:
248 {
249 dev_t dev = 0;
250 if (loopcxt_get_backing_devno(lc, &dev) == 0 && dev)
251 xasprintf(&np, "%8u:%-3u", major(dev), minor(dev));
252 if (np)
253 tt_line_set_data(ln, i, np);
254 break;
255 }
256 case COL_MAJMIN:
257 {
258 struct stat st;
259
260 if (loopcxt_get_device(lc)
261 && stat(loopcxt_get_device(lc), &st) == 0
262 && S_ISBLK(st.st_mode)
263 && major(st.st_rdev) == LOOPDEV_MAJOR)
264 xasprintf(&np, "%3u:%-3u", major(st.st_rdev),
265 minor(st.st_rdev));
266 if (np)
267 tt_line_set_data(ln, i, np);
268 break;
269 }
270 case COL_BACK_INO:
271 {
272 ino_t ino = 0;
273 if (loopcxt_get_backing_inode(lc, &ino) == 0 && ino)
274 xasprintf(&np, "%ju", ino);
275 if (np)
276 tt_line_set_data(ln, i, np);
277 break;
278 }
279 case COL_AUTOCLR:
280 tt_line_set_data(ln, i,
281 xstrdup(loopcxt_is_autoclear(lc) ? "1" : "0"));
282 break;
283 case COL_RO:
284 tt_line_set_data(ln, i,
285 xstrdup(loopcxt_is_readonly(lc) ? "1" : "0"));
286 break;
287 case COL_PARTSCAN:
288 tt_line_set_data(ln, i,
289 xstrdup(loopcxt_is_partscan(lc) ? "1" : "0"));
290 break;
291 default:
292 return -EINVAL;
293 }
294 }
295 return 0;
296 }
297
298 static int make_table(struct loopdev_cxt *lc,
299 const char *file,
300 uint64_t offset,
301 int flags,
302 int tt_flags)
303 {
304 struct stat sbuf, *st = &sbuf;
305 struct tt_line *ln;
306 char *cn_file = NULL;
307 int i;
308
309 if (!(tt = tt_new_table(tt_flags | TT_FL_FREEDATA)))
310 errx(EXIT_FAILURE, _("failed to initialize output table"));
311
312 for (i = 0; i < ncolumns; i++) {
313 struct colinfo *ci = get_column_info(i);
314
315 if (!tt_define_column(tt, ci->name, ci->whint, ci->flags))
316 warn(_("failed to initialize output column"));
317 }
318
319 /* only one loopdev requested (already assigned to loopdev_cxt) */
320 if (loopcxt_get_device(lc)) {
321 ln = tt_add_line(tt, NULL);
322 if (set_tt_data(lc, ln))
323 return -EINVAL;
324 return 0;
325 }
326
327 /* list all loopdevs */
328 if (loopcxt_init_iterator(lc, LOOPITER_FL_USED))
329 return -1;
330 if (!file || stat(file, st))
331 st = NULL;
332
333 while (loopcxt_next(lc) == 0) {
334 if (file) {
335 int used;
336 const char *bf = cn_file ? cn_file : file;
337
338 used = loopcxt_is_used(lc, st, bf, offset, flags);
339 if (!used && !cn_file) {
340 bf = cn_file = canonicalize_path(file);
341 used = loopcxt_is_used(lc, st, bf, offset, flags);
342 }
343 if (!used)
344 continue;
345 }
346
347 ln = tt_add_line(tt, NULL);
348 if (set_tt_data(lc, ln))
349 return -EINVAL;
350 }
351
352 loopcxt_deinit_iterator(lc);
353 free(cn_file);
354 return 0;
355 }
356
357 static void usage(FILE *out)
358 {
359 size_t i;
360
361 fputs(USAGE_HEADER, out);
362
363 fprintf(out,
364 _(" %1$s [options] [<loopdev>]\n"
365 " %1$s [options] -f | <loopdev> <file>\n"),
366 program_invocation_short_name);
367
368 fputs(USAGE_OPTIONS, out);
369 fputs(_(" -a, --all list all used devices\n"), out);
370 fputs(_(" -d, --detach <loopdev> [...] detach one or more devices\n"), out);
371 fputs(_(" -D, --detach-all detach all used devices\n"), out);
372 fputs(_(" -f, --find find first unused device\n"), out);
373 fputs(_(" -c, --set-capacity <loopdev> resize device\n"), out);
374 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out);
375
376 fputs(USAGE_SEPARATOR, out);
377
378 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out);
379 fputs(_(" --sizelimit <num> device limited to <num> bytes of the file\n"), out);
380 fputs(_(" -P, --partscan create partitioned loop device\n"), out);
381 fputs(_(" -r, --read-only setup read-only loop device\n"), out);
382 fputs(_(" --show print device name after setup (with -f)\n"), out);
383 fputs(_(" -v, --verbose verbose mode\n"), out);
384
385 fputs(USAGE_SEPARATOR, out);
386
387 fputs(_(" -l, --list list info about all or specified\n"), out);
388 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out);
389 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out);
390 fputs(_(" --raw use raw --list output format\n"), out);
391
392 fputs(USAGE_SEPARATOR, out);
393 fputs(USAGE_HELP, out);
394 fputs(USAGE_VERSION, out);
395
396 fputs(_("\nAvailable --list columns:\n"), out);
397 for (i = 0; i < NCOLS; i++)
398 fprintf(out, " %12s %s\n", infos[i].name, _(infos[i].help));
399
400 fprintf(out, USAGE_MAN_TAIL("losetup(8)"));
401
402 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
403 }
404
405 static void warn_size(const char *filename, uint64_t size)
406 {
407 struct stat st;
408
409 if (!size) {
410 if (stat(filename, &st) || S_ISBLK(st.st_mode))
411 return;
412 size = st.st_size;
413 }
414
415 if (size < 512)
416 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
417 "may be useless or invisible for system tools."),
418 filename);
419 else if (size % 512)
420 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
421 "the end of the file will be ignored."),
422 filename);
423 }
424
425 int main(int argc, char **argv)
426 {
427 struct loopdev_cxt lc;
428 int act = 0, flags = 0, c;
429 char *file = NULL;
430 uint64_t offset = 0, sizelimit = 0;
431 int res = 0, showdev = 0, lo_flags = 0, tt_flags = 0;
432 char *outarg = NULL;
433 int list = 0;
434
435 enum {
436 OPT_SIZELIMIT = CHAR_MAX + 1,
437 OPT_SHOW,
438 OPT_RAW
439 };
440 static const struct option longopts[] = {
441 { "all", 0, 0, 'a' },
442 { "set-capacity", 1, 0, 'c' },
443 { "detach", 1, 0, 'd' },
444 { "detach-all", 0, 0, 'D' },
445 { "find", 0, 0, 'f' },
446 { "help", 0, 0, 'h' },
447 { "associated", 1, 0, 'j' },
448 { "list", 0, 0, 'l' },
449 { "noheadings", 0, 0, 'n' },
450 { "offset", 1, 0, 'o' },
451 { "output", 1, 0, 'O' },
452 { "sizelimit", 1, 0, OPT_SIZELIMIT },
453 { "partscan", 0, 0, 'P' },
454 { "read-only", 0, 0, 'r' },
455 { "raw", 0, 0, OPT_RAW },
456 { "show", 0, 0, OPT_SHOW },
457 { "verbose", 0, 0, 'v' },
458 { "version", 0, 0, 'V' },
459 { NULL, 0, 0, 0 }
460 };
461
462 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
463 { 'D','a','c','d','f','j' },
464 { 'D','c','d','f','l' },
465 { 'D','c','d','f','O' },
466 { 0 }
467 };
468 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
469
470 setlocale(LC_ALL, "");
471 bindtextdomain(PACKAGE, LOCALEDIR);
472 textdomain(PACKAGE);
473 atexit(close_stdout);
474
475 if (loopcxt_init(&lc, 0))
476 err(EXIT_FAILURE, _("failed to initialize loopcxt"));
477
478 while ((c = getopt_long(argc, argv, "ac:d:Dfhj:lno:O:PrvV",
479 longopts, NULL)) != -1) {
480
481 err_exclusive_options(c, longopts, excl, excl_st);
482
483 switch (c) {
484 case 'a':
485 act = A_SHOW;
486 break;
487 case 'c':
488 act = A_SET_CAPACITY;
489 if (!is_loopdev(optarg) ||
490 loopcxt_set_device(&lc, optarg))
491 err(EXIT_FAILURE, _("%s: failed to use device"),
492 optarg);
493 break;
494 case 'r':
495 lo_flags |= LO_FLAGS_READ_ONLY;
496 break;
497 case 'd':
498 act = A_DELETE;
499 if (!is_loopdev(optarg) ||
500 loopcxt_set_device(&lc, optarg))
501 err(EXIT_FAILURE, _("%s: failed to use device"),
502 optarg);
503 break;
504 case 'D':
505 act = A_DELETE_ALL;
506 break;
507 case 'f':
508 act = A_FIND_FREE;
509 break;
510 case 'h':
511 usage(stdout);
512 break;
513 case 'j':
514 act = A_SHOW;
515 file = optarg;
516 break;
517 case 'l':
518 list = 1;
519 break;
520 case 'n':
521 tt_flags |= TT_FL_NOHEADINGS;
522 break;
523 case OPT_RAW:
524 tt_flags |= TT_FL_RAW;
525 break;
526 case 'o':
527 offset = strtosize_or_err(optarg, _("failed to parse offset"));
528 flags |= LOOPDEV_FL_OFFSET;
529 break;
530 case 'O':
531 outarg = optarg;
532 list = 1;
533 break;
534 case 'P':
535 lo_flags |= LO_FLAGS_PARTSCAN;
536 break;
537 case OPT_SHOW:
538 showdev = 1;
539 break;
540 case 'v':
541 verbose = 1;
542 break;
543 case 'V':
544 printf(UTIL_LINUX_VERSION);
545 return EXIT_SUCCESS;
546 case OPT_SIZELIMIT: /* --sizelimit */
547 sizelimit = strtosize_or_err(optarg, _("failed to parse size"));
548 flags |= LOOPDEV_FL_SIZELIMIT;
549 break;
550 default:
551 usage(stderr);
552 }
553 }
554
555 /* default is --list --all */
556 if (argc == 1) {
557 act = A_SHOW;
558 list = 1;
559 }
560
561 /* default --list output columns */
562 if (list && !ncolumns) {
563 columns[ncolumns++] = COL_NAME;
564 columns[ncolumns++] = COL_SIZELIMIT;
565 columns[ncolumns++] = COL_OFFSET;
566 columns[ncolumns++] = COL_AUTOCLR;
567 columns[ncolumns++] = COL_RO;
568 columns[ncolumns++] = COL_BACK_FILE;
569 }
570
571 if (act == A_FIND_FREE && optind < argc) {
572 /*
573 * losetup -f <backing_file>
574 */
575 act = A_CREATE;
576 file = argv[optind++];
577 }
578
579 if (list && !act && optind == argc)
580 /*
581 * losetup --list defaults to --all
582 */
583 act = A_SHOW;
584
585 if (!act && optind + 1 == argc) {
586 /*
587 * losetup [--list] <device>
588 */
589 act = A_SHOW_ONE;
590 if (!is_loopdev(argv[optind]) ||
591 loopcxt_set_device(&lc, argv[optind]))
592 err(EXIT_FAILURE, _("%s: failed to use device"),
593 argv[optind]);
594 optind++;
595 }
596 if (!act) {
597 /*
598 * losetup <loopdev> <backing_file>
599 */
600 act = A_CREATE;
601
602 if (optind >= argc)
603 errx(EXIT_FAILURE, _("no loop device specified"));
604 /* don't use is_loopdev() here, the device does not have exist yet */
605 if (loopcxt_set_device(&lc, argv[optind]))
606 err(EXIT_FAILURE, _("%s: failed to use device"),
607 argv[optind]);
608 optind++;
609
610 if (optind >= argc)
611 errx(EXIT_FAILURE, _("no file specified"));
612 file = argv[optind++];
613 }
614
615 if (act != A_CREATE &&
616 (sizelimit || lo_flags || showdev))
617 errx(EXIT_FAILURE,
618 _("the options %s are allowed during loop device setup only"),
619 "--{sizelimit,read-only,show}");
620
621 if ((flags & LOOPDEV_FL_OFFSET) &&
622 act != A_CREATE && (act != A_SHOW || !file))
623 errx(EXIT_FAILURE, _("the option --offset is not allowed in this context"));
624
625 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
626 &ncolumns, column_name_to_id) < 0)
627 return EXIT_FAILURE;
628
629 switch (act) {
630 case A_CREATE:
631 {
632 int hasdev = loopcxt_has_device(&lc);
633
634 if (hasdev && !is_loopdev(loopcxt_get_device(&lc)))
635 loopcxt_add_device(&lc);
636 do {
637 const char *errpre;
638
639 /* Note that loopcxt_{find_unused,set_device}() resets
640 * loopcxt struct.
641 */
642 if (!hasdev && (res = loopcxt_find_unused(&lc))) {
643 warnx(_("cannot find an unused loop device"));
644 break;
645 }
646 if (flags & LOOPDEV_FL_OFFSET)
647 loopcxt_set_offset(&lc, offset);
648 if (flags & LOOPDEV_FL_SIZELIMIT)
649 loopcxt_set_sizelimit(&lc, sizelimit);
650 if (lo_flags)
651 loopcxt_set_flags(&lc, lo_flags);
652 if ((res = loopcxt_set_backing_file(&lc, file))) {
653 warn(_("%s: failed to use backing file"), file);
654 break;
655 }
656 errno = 0;
657 res = loopcxt_setup_device(&lc);
658 if (res == 0)
659 break; /* success */
660 if (errno == EBUSY)
661 continue;
662
663 /* errors */
664 errpre = hasdev && loopcxt_get_fd(&lc) < 0 ?
665 loopcxt_get_device(&lc) : file;
666 warn(_("%s: failed to set up loop device"), errpre);
667 break;
668 } while (hasdev == 0);
669
670 if (res == 0) {
671 if (showdev)
672 printf("%s\n", loopcxt_get_device(&lc));
673 warn_size(file, sizelimit);
674 }
675 break;
676 }
677 case A_DELETE:
678 res = delete_loop(&lc);
679 while (optind < argc) {
680 if (!is_loopdev(argv[optind]) ||
681 loopcxt_set_device(&lc, argv[optind]))
682 warn(_("%s: failed to use device"),
683 argv[optind]);
684 optind++;
685 res += delete_loop(&lc);
686 }
687 break;
688 case A_DELETE_ALL:
689 res = delete_all_loops(&lc);
690 break;
691 case A_FIND_FREE:
692 if (loopcxt_find_unused(&lc))
693 warn(_("cannot find an unused loop device"));
694 else
695 printf("%s\n", loopcxt_get_device(&lc));
696 break;
697 case A_SHOW:
698 if (list)
699 res = make_table(&lc, file, offset, flags, tt_flags);
700 else
701 res = show_all_loops(&lc, file, offset, flags);
702 break;
703 case A_SHOW_ONE:
704 if (list)
705 res = make_table( &lc, NULL, 0, 0, tt_flags);
706 else
707 res = printf_loopdev(&lc);
708 if (res)
709 warn("%s", loopcxt_get_device(&lc));
710 break;
711 case A_SET_CAPACITY:
712 res = loopcxt_set_capacity(&lc);
713 if (res)
714 warn(_("%s: set capacity failed"),
715 loopcxt_get_device(&lc));
716 break;
717 default:
718 usage(stderr);
719 break;
720 }
721 if (tt) {
722 if (!res)
723 tt_print_table(tt);
724 tt_free_table(tt);
725 }
726
727 loopcxt_deinit(&lc);
728 return res ? EXIT_FAILURE : EXIT_SUCCESS;
729 }
730