2 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
3 * Originally from Ted's losetup.c
5 * losetup.c - setup and control loop devices
13 #include <sys/ioctl.h>
23 #include "closestream.h"
26 #include "canonicalize.h"
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 */
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")},
73 #define NCOLS ARRAY_SIZE(infos)
75 static int columns
[NCOLS
] = {-1};
79 static int get_column_id(int num
)
81 assert(ARRAY_SIZE(columns
) == NCOLS
);
82 assert(num
< ncolumns
);
83 assert(columns
[num
] < (int) NCOLS
);
87 static struct colinfo
*get_column_info(int num
)
89 return &infos
[ get_column_id(num
) ];
92 static int column_name_to_id(const char *name
, size_t namesz
)
96 for (i
= 0; i
< NCOLS
; i
++) {
97 const char *cn
= infos
[i
].name
;
99 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
102 warnx(_("unknown column: %s"), name
);
106 static int printf_loopdev(struct loopdev_cxt
*lc
)
114 fname
= loopcxt_get_backing_file(lc
);
118 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
119 loopcxt_get_backing_inode(lc
, &ino
);
123 * Probably non-root user (no permissions to
124 * call LOOP_GET_STATUS ioctls).
126 printf("%s: []: (%s)",
127 loopcxt_get_device(lc
), fname
);
129 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
130 printf(_(", offset %ju"), x
);
132 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
133 printf(_(", sizelimit %ju"), x
);
138 printf("%s: [%04d]:%" PRIu64
" (%s)",
139 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
141 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
142 printf(_(", offset %ju"), x
);
144 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
145 printf(_(", sizelimit %ju"), x
);
147 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
148 const char *e
= loopcxt_get_crypt_name(lc
);
150 if ((!e
|| !*e
) && type
== 1)
153 printf(_(", encryption %s (type %u)"), e
, type
);
159 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
160 uint64_t offset
, int flags
)
162 struct stat sbuf
, *st
= &sbuf
;
163 char *cn_file
= NULL
;
165 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
168 if (!file
|| stat(file
, st
))
171 while (loopcxt_next(lc
) == 0) {
174 const char *bf
= cn_file
? cn_file
: file
;
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
);
186 loopcxt_deinit_iterator(lc
);
191 static int delete_loop(struct loopdev_cxt
*lc
)
193 if (loopcxt_delete_device(lc
))
194 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
201 static int delete_all_loops(struct loopdev_cxt
*lc
)
205 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
208 while (loopcxt_next(lc
) == 0)
209 res
+= delete_loop(lc
);
211 loopcxt_deinit_iterator(lc
);
215 static int set_tt_data(struct loopdev_cxt
*lc
, struct tt_line
*ln
)
219 for (i
= 0; i
< ncolumns
; i
++) {
220 const char *p
= NULL
;
224 switch(get_column_id(i
)) {
226 p
= loopcxt_get_device(lc
);
228 tt_line_set_data(ln
, i
, xstrdup(p
));
231 p
= loopcxt_get_backing_file(lc
);
233 tt_line_set_data(ln
, i
, xstrdup(p
));
236 if (loopcxt_get_offset(lc
, &x
) == 0)
237 xasprintf(&np
, "%jd", x
);
239 tt_line_set_data(ln
, i
, np
);
242 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
243 xasprintf(&np
, "%jd", x
);
245 tt_line_set_data(ln
, i
, np
);
247 case COL_BACK_MAJMIN
:
250 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
251 xasprintf(&np
, "%8u:%-3u", major(dev
), minor(dev
));
253 tt_line_set_data(ln
, i
, np
);
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
),
267 tt_line_set_data(ln
, i
, np
);
273 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
274 xasprintf(&np
, "%ju", ino
);
276 tt_line_set_data(ln
, i
, np
);
280 tt_line_set_data(ln
, i
,
281 xstrdup(loopcxt_is_autoclear(lc
) ? "1" : "0"));
284 tt_line_set_data(ln
, i
,
285 xstrdup(loopcxt_is_readonly(lc
) ? "1" : "0"));
288 tt_line_set_data(ln
, i
,
289 xstrdup(loopcxt_is_partscan(lc
) ? "1" : "0"));
298 static int make_table(struct loopdev_cxt
*lc
,
304 struct stat sbuf
, *st
= &sbuf
;
306 char *cn_file
= NULL
;
309 if (!(tt
= tt_new_table(tt_flags
| TT_FL_FREEDATA
)))
310 errx(EXIT_FAILURE
, _("failed to initialize output table"));
312 for (i
= 0; i
< ncolumns
; i
++) {
313 struct colinfo
*ci
= get_column_info(i
);
315 if (!tt_define_column(tt
, ci
->name
, ci
->whint
, ci
->flags
))
316 warn(_("failed to initialize output column"));
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
))
327 /* list all loopdevs */
328 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
330 if (!file
|| stat(file
, st
))
333 while (loopcxt_next(lc
) == 0) {
336 const char *bf
= cn_file
? cn_file
: file
;
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
);
347 ln
= tt_add_line(tt
, NULL
);
348 if (set_tt_data(lc
, ln
))
352 loopcxt_deinit_iterator(lc
);
357 static void usage(FILE *out
)
361 fputs(USAGE_HEADER
, out
);
364 _(" %1$s [options] [<loopdev>]\n"
365 " %1$s [options] -f | <loopdev> <file>\n"),
366 program_invocation_short_name
);
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
);
376 fputs(USAGE_SEPARATOR
, out
);
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
);
385 fputs(USAGE_SEPARATOR
, out
);
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
);
392 fputs(USAGE_SEPARATOR
, out
);
393 fputs(USAGE_HELP
, out
);
394 fputs(USAGE_VERSION
, out
);
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
));
400 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
402 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
405 static void warn_size(const char *filename
, uint64_t size
)
410 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
416 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
417 "may be useless or invisible for system tools."),
420 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
421 "the end of the file will be ignored."),
425 int main(int argc
, char **argv
)
427 struct loopdev_cxt lc
;
428 int act
= 0, flags
= 0, c
;
430 uint64_t offset
= 0, sizelimit
= 0;
431 int res
= 0, showdev
= 0, lo_flags
= 0, tt_flags
= 0;
436 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
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' },
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' },
468 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
470 setlocale(LC_ALL
, "");
471 bindtextdomain(PACKAGE
, LOCALEDIR
);
473 atexit(close_stdout
);
475 if (loopcxt_init(&lc
, 0))
476 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
478 while ((c
= getopt_long(argc
, argv
, "ac:d:Dfhj:lno:O:PrvV",
479 longopts
, NULL
)) != -1) {
481 err_exclusive_options(c
, longopts
, excl
, excl_st
);
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"),
495 lo_flags
|= LO_FLAGS_READ_ONLY
;
499 if (!is_loopdev(optarg
) ||
500 loopcxt_set_device(&lc
, optarg
))
501 err(EXIT_FAILURE
, _("%s: failed to use device"),
521 tt_flags
|= TT_FL_NOHEADINGS
;
524 tt_flags
|= TT_FL_RAW
;
527 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
528 flags
|= LOOPDEV_FL_OFFSET
;
535 lo_flags
|= LO_FLAGS_PARTSCAN
;
544 printf(UTIL_LINUX_VERSION
);
546 case OPT_SIZELIMIT
: /* --sizelimit */
547 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
548 flags
|= LOOPDEV_FL_SIZELIMIT
;
555 /* default is --list --all */
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
;
571 if (act
== A_FIND_FREE
&& optind
< argc
) {
573 * losetup -f <backing_file>
576 file
= argv
[optind
++];
579 if (list
&& !act
&& optind
== argc
)
581 * losetup --list defaults to --all
585 if (!act
&& optind
+ 1 == argc
) {
587 * losetup [--list] <device>
590 if (!is_loopdev(argv
[optind
]) ||
591 loopcxt_set_device(&lc
, argv
[optind
]))
592 err(EXIT_FAILURE
, _("%s: failed to use device"),
598 * losetup <loopdev> <backing_file>
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"),
611 errx(EXIT_FAILURE
, _("no file specified"));
612 file
= argv
[optind
++];
615 if (act
!= A_CREATE
&&
616 (sizelimit
|| lo_flags
|| showdev
))
618 _("the options %s are allowed during loop device setup only"),
619 "--{sizelimit,read-only,show}");
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"));
625 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
626 &ncolumns
, column_name_to_id
) < 0)
632 int hasdev
= loopcxt_has_device(&lc
);
634 if (hasdev
&& !is_loopdev(loopcxt_get_device(&lc
)))
635 loopcxt_add_device(&lc
);
639 /* Note that loopcxt_{find_unused,set_device}() resets
642 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
643 warnx(_("cannot find an unused loop device"));
646 if (flags
& LOOPDEV_FL_OFFSET
)
647 loopcxt_set_offset(&lc
, offset
);
648 if (flags
& LOOPDEV_FL_SIZELIMIT
)
649 loopcxt_set_sizelimit(&lc
, sizelimit
);
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
);
657 res
= loopcxt_setup_device(&lc
);
664 errpre
= hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
665 loopcxt_get_device(&lc
) : file
;
666 warn(_("%s: failed to set up loop device"), errpre
);
668 } while (hasdev
== 0);
672 printf("%s\n", loopcxt_get_device(&lc
));
673 warn_size(file
, sizelimit
);
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"),
685 res
+= delete_loop(&lc
);
689 res
= delete_all_loops(&lc
);
692 if (loopcxt_find_unused(&lc
))
693 warn(_("cannot find an unused loop device"));
695 printf("%s\n", loopcxt_get_device(&lc
));
699 res
= make_table(&lc
, file
, offset
, flags
, tt_flags
);
701 res
= show_all_loops(&lc
, file
, offset
, flags
);
705 res
= make_table( &lc
, NULL
, 0, 0, tt_flags
);
707 res
= printf_loopdev(&lc
);
709 warn("%s", loopcxt_get_device(&lc
));
712 res
= loopcxt_set_capacity(&lc
);
714 warn(_("%s: set capacity failed"),
715 loopcxt_get_device(&lc
));
728 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;