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>
18 #include <libsmartcols.h>
24 #include "closestream.h"
27 #include "canonicalize.h"
30 A_CREATE
= 1, /* setup a new device */
31 A_DELETE
, /* delete given device(s) */
32 A_DELETE_ALL
, /* delete all devices */
33 A_SHOW
, /* list devices */
34 A_SHOW_ONE
, /* print info about one device */
35 A_FIND_FREE
, /* find first unused */
36 A_SET_CAPACITY
, /* set device capacity */
52 /* basic output flags */
53 static int no_headings
;
63 static struct colinfo infos
[] = {
64 [COL_AUTOCLR
] = { "AUTOCLEAR", 1, SCOLS_FL_RIGHT
, N_("autoclear flag set")},
65 [COL_BACK_FILE
] = { "BACK-FILE", 0.3, 0, N_("device backing file")},
66 [COL_BACK_INO
] = { "BACK-INO", 4, SCOLS_FL_RIGHT
, N_("backing file inode number")},
67 [COL_BACK_MAJMIN
] = { "BACK-MAJ:MIN", 6, 0, N_("backing file major:minor device number")},
68 [COL_NAME
] = { "NAME", 0.25, 0, N_("loop device name")},
69 [COL_OFFSET
] = { "OFFSET", 5, SCOLS_FL_RIGHT
, N_("offset from the beginning")},
70 [COL_PARTSCAN
] = { "PARTSCAN", 1, SCOLS_FL_RIGHT
, N_("partscan flag set")},
71 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device")},
72 [COL_SIZELIMIT
] = { "SIZELIMIT", 5, SCOLS_FL_RIGHT
, N_("size limit of the file in bytes")},
73 [COL_MAJMIN
] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")},
76 #define NCOLS ARRAY_SIZE(infos)
78 static int columns
[NCOLS
] = {-1};
82 static int get_column_id(int num
)
84 assert(ARRAY_SIZE(columns
) == NCOLS
);
85 assert(num
< ncolumns
);
86 assert(columns
[num
] < (int) NCOLS
);
90 static struct colinfo
*get_column_info(int num
)
92 return &infos
[ get_column_id(num
) ];
95 static int column_name_to_id(const char *name
, size_t namesz
)
99 for (i
= 0; i
< NCOLS
; i
++) {
100 const char *cn
= infos
[i
].name
;
102 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
105 warnx(_("unknown column: %s"), name
);
109 static int printf_loopdev(struct loopdev_cxt
*lc
)
117 fname
= loopcxt_get_backing_file(lc
);
121 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
122 loopcxt_get_backing_inode(lc
, &ino
);
126 * Probably non-root user (no permissions to
127 * call LOOP_GET_STATUS ioctls).
129 printf("%s: []: (%s)",
130 loopcxt_get_device(lc
), fname
);
132 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
133 printf(_(", offset %ju"), x
);
135 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
136 printf(_(", sizelimit %ju"), x
);
141 printf("%s: [%04d]:%" PRIu64
" (%s)",
142 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
144 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
145 printf(_(", offset %ju"), x
);
147 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
148 printf(_(", sizelimit %ju"), x
);
150 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
151 const char *e
= loopcxt_get_crypt_name(lc
);
153 if ((!e
|| !*e
) && type
== 1)
156 printf(_(", encryption %s (type %u)"), e
, type
);
162 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
163 uint64_t offset
, int flags
)
165 struct stat sbuf
, *st
= &sbuf
;
166 char *cn_file
= NULL
;
168 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
171 if (!file
|| stat(file
, st
))
174 while (loopcxt_next(lc
) == 0) {
177 const char *bf
= cn_file
? cn_file
: file
;
179 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
180 if (!used
&& !cn_file
) {
181 bf
= cn_file
= canonicalize_path(file
);
182 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
189 loopcxt_deinit_iterator(lc
);
194 static int delete_loop(struct loopdev_cxt
*lc
)
196 if (loopcxt_delete_device(lc
))
197 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
204 static int delete_all_loops(struct loopdev_cxt
*lc
)
208 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
211 while (loopcxt_next(lc
) == 0)
212 res
+= delete_loop(lc
);
214 loopcxt_deinit_iterator(lc
);
218 static int set_scols_data(struct loopdev_cxt
*lc
, struct libscols_line
*ln
)
222 for (i
= 0; i
< ncolumns
; i
++) {
223 const char *p
= NULL
; /* external data */
224 char *np
= NULL
; /* allocated here */
227 switch(get_column_id(i
)) {
229 p
= loopcxt_get_device(lc
);
232 p
= loopcxt_get_backing_file(lc
);
235 if (loopcxt_get_offset(lc
, &x
) == 0)
236 xasprintf(&np
, "%jd", x
);
239 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
240 xasprintf(&np
, "%jd", x
);
242 case COL_BACK_MAJMIN
:
245 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
246 xasprintf(&np
, "%8u:%-3u", major(dev
), minor(dev
));
253 if (loopcxt_get_device(lc
)
254 && stat(loopcxt_get_device(lc
), &st
) == 0
255 && S_ISBLK(st
.st_mode
)
256 && major(st
.st_rdev
) == LOOPDEV_MAJOR
)
257 xasprintf(&np
, "%3u:%-3u", major(st
.st_rdev
),
264 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
265 xasprintf(&np
, "%ju", ino
);
269 p
= loopcxt_is_autoclear(lc
) ? "1" : "0";
272 p
= loopcxt_is_readonly(lc
) ? "1" : "0";
275 p
= loopcxt_is_partscan(lc
) ? "1" : "0";
283 scols_line_set_data(ln
, i
, p
); /* calls strdup() */
285 scols_line_refer_data(ln
, i
, np
); /* only refers */
291 static int show_table(struct loopdev_cxt
*lc
,
296 struct stat sbuf
, *st
= &sbuf
;
297 struct libscols_table
*tb
;
298 struct libscols_line
*ln
;
301 if (!(tb
= scols_new_table()))
302 err(EXIT_FAILURE
, _("failed to initialize output table"));
303 scols_table_enable_raw(tb
, raw
);
304 scols_table_enable_noheadings(tb
, no_headings
);
306 for (i
= 0; i
< ncolumns
; i
++) {
307 struct colinfo
*ci
= get_column_info(i
);
309 if (!scols_table_new_column(tb
, ci
->name
, ci
->whint
, ci
->flags
))
310 err(EXIT_FAILURE
, _("failed to initialize output column"));
313 /* only one loopdev requested (already assigned to loopdev_cxt) */
314 if (loopcxt_get_device(lc
)) {
315 ln
= scols_table_new_line(tb
, NULL
);
317 err(EXIT_FAILURE
, _("failed to initialize output line"));
318 rc
= set_scols_data(lc
, ln
);
320 /* list all loopdevs */
322 char *cn_file
= NULL
;
324 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
327 if (!file
|| stat(file
, st
))
330 while (loopcxt_next(lc
) == 0) {
333 const char *bf
= cn_file
? cn_file
: file
;
335 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
336 if (!used
&& !cn_file
) {
337 bf
= cn_file
= canonicalize_path(file
);
338 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
344 ln
= scols_table_new_line(tb
, NULL
);
346 err(EXIT_FAILURE
, _("failed to initialize output column"));
347 rc
= set_scols_data(lc
, ln
);
352 loopcxt_deinit_iterator(lc
);
357 rc
= scols_print_table(tb
);
358 scols_unref_table(tb
);
362 static void usage(FILE *out
)
366 fputs(USAGE_HEADER
, out
);
369 _(" %1$s [options] [<loopdev>]\n"
370 " %1$s [options] -f | <loopdev> <file>\n"),
371 program_invocation_short_name
);
373 fputs(USAGE_OPTIONS
, out
);
374 fputs(_(" -a, --all list all used devices\n"), out
);
375 fputs(_(" -d, --detach <loopdev> [...] detach one or more devices\n"), out
);
376 fputs(_(" -D, --detach-all detach all used devices\n"), out
);
377 fputs(_(" -f, --find find first unused device\n"), out
);
378 fputs(_(" -c, --set-capacity <loopdev> resize device\n"), out
);
379 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out
);
381 fputs(USAGE_SEPARATOR
, out
);
383 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out
);
384 fputs(_(" --sizelimit <num> device limited to <num> bytes of the file\n"), out
);
385 fputs(_(" -P, --partscan create partitioned loop device\n"), out
);
386 fputs(_(" -r, --read-only setup read-only loop device\n"), out
);
387 fputs(_(" --show print device name after setup (with -f)\n"), out
);
388 fputs(_(" -v, --verbose verbose mode\n"), out
);
390 fputs(USAGE_SEPARATOR
, out
);
392 fputs(_(" -l, --list list info about all or specified (default)\n"), out
);
393 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out
);
394 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out
);
395 fputs(_(" --raw use raw --list output format\n"), out
);
397 fputs(USAGE_SEPARATOR
, out
);
398 fputs(USAGE_HELP
, out
);
399 fputs(USAGE_VERSION
, out
);
401 fputs(_("\nAvailable --list columns:\n"), out
);
402 for (i
= 0; i
< NCOLS
; i
++)
403 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
405 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
407 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
410 static void warn_size(const char *filename
, uint64_t size
)
415 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
421 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
422 "may be useless or invisible for system tools."),
425 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
426 "the end of the file will be ignored."),
430 int main(int argc
, char **argv
)
432 struct loopdev_cxt lc
;
433 int act
= 0, flags
= 0, c
;
435 uint64_t offset
= 0, sizelimit
= 0;
436 int res
= 0, showdev
= 0, lo_flags
= 0;
441 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
445 static const struct option longopts
[] = {
446 { "all", 0, 0, 'a' },
447 { "set-capacity", 1, 0, 'c' },
448 { "detach", 1, 0, 'd' },
449 { "detach-all", 0, 0, 'D' },
450 { "find", 0, 0, 'f' },
451 { "help", 0, 0, 'h' },
452 { "associated", 1, 0, 'j' },
453 { "list", 0, 0, 'l' },
454 { "noheadings", 0, 0, 'n' },
455 { "offset", 1, 0, 'o' },
456 { "output", 1, 0, 'O' },
457 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
458 { "partscan", 0, 0, 'P' },
459 { "read-only", 0, 0, 'r' },
460 { "raw", 0, 0, OPT_RAW
},
461 { "show", 0, 0, OPT_SHOW
},
462 { "verbose", 0, 0, 'v' },
463 { "version", 0, 0, 'V' },
467 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
468 { 'D','a','c','d','f','j' },
469 { 'D','c','d','f','l' },
470 { 'D','c','d','f','O' },
473 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
475 setlocale(LC_ALL
, "");
476 bindtextdomain(PACKAGE
, LOCALEDIR
);
478 atexit(close_stdout
);
480 if (loopcxt_init(&lc
, 0))
481 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
483 while ((c
= getopt_long(argc
, argv
, "ac:d:Dfhj:lno:O:PrvV",
484 longopts
, NULL
)) != -1) {
486 err_exclusive_options(c
, longopts
, excl
, excl_st
);
493 act
= A_SET_CAPACITY
;
494 if (!is_loopdev(optarg
) ||
495 loopcxt_set_device(&lc
, optarg
))
496 err(EXIT_FAILURE
, _("%s: failed to use device"),
500 lo_flags
|= LO_FLAGS_READ_ONLY
;
504 if (!is_loopdev(optarg
) ||
505 loopcxt_set_device(&lc
, optarg
))
506 err(EXIT_FAILURE
, _("%s: failed to use device"),
532 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
533 flags
|= LOOPDEV_FL_OFFSET
;
540 lo_flags
|= LO_FLAGS_PARTSCAN
;
549 printf(UTIL_LINUX_VERSION
);
551 case OPT_SIZELIMIT
: /* --sizelimit */
552 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
553 flags
|= LOOPDEV_FL_SIZELIMIT
;
560 /* default is --list --all */
566 /* default --list output columns */
567 if (list
&& !ncolumns
) {
568 columns
[ncolumns
++] = COL_NAME
;
569 columns
[ncolumns
++] = COL_SIZELIMIT
;
570 columns
[ncolumns
++] = COL_OFFSET
;
571 columns
[ncolumns
++] = COL_AUTOCLR
;
572 columns
[ncolumns
++] = COL_RO
;
573 columns
[ncolumns
++] = COL_BACK_FILE
;
576 if (act
== A_FIND_FREE
&& optind
< argc
) {
578 * losetup -f <backing_file>
581 file
= argv
[optind
++];
584 if (list
&& !act
&& optind
== argc
)
586 * losetup --list defaults to --all
590 if (!act
&& optind
+ 1 == argc
) {
592 * losetup [--list] <device>
595 if (!is_loopdev(argv
[optind
]) ||
596 loopcxt_set_device(&lc
, argv
[optind
]))
597 err(EXIT_FAILURE
, _("%s: failed to use device"),
603 * losetup <loopdev> <backing_file>
608 errx(EXIT_FAILURE
, _("no loop device specified"));
609 /* don't use is_loopdev() here, the device does not have exist yet */
610 if (loopcxt_set_device(&lc
, argv
[optind
]))
611 err(EXIT_FAILURE
, _("%s: failed to use device"),
616 errx(EXIT_FAILURE
, _("no file specified"));
617 file
= argv
[optind
++];
620 if (act
!= A_CREATE
&&
621 (sizelimit
|| lo_flags
|| showdev
))
623 _("the options %s are allowed during loop device setup only"),
624 "--{sizelimit,read-only,show}");
626 if ((flags
& LOOPDEV_FL_OFFSET
) &&
627 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
628 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context"));
630 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
631 &ncolumns
, column_name_to_id
) < 0)
637 int hasdev
= loopcxt_has_device(&lc
);
639 if (hasdev
&& !is_loopdev(loopcxt_get_device(&lc
)))
640 loopcxt_add_device(&lc
);
644 /* Note that loopcxt_{find_unused,set_device}() resets
647 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
648 warnx(_("cannot find an unused loop device"));
651 if (flags
& LOOPDEV_FL_OFFSET
)
652 loopcxt_set_offset(&lc
, offset
);
653 if (flags
& LOOPDEV_FL_SIZELIMIT
)
654 loopcxt_set_sizelimit(&lc
, sizelimit
);
656 loopcxt_set_flags(&lc
, lo_flags
);
657 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
658 warn(_("%s: failed to use backing file"), file
);
662 res
= loopcxt_setup_device(&lc
);
669 errpre
= hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
670 loopcxt_get_device(&lc
) : file
;
671 warn(_("%s: failed to set up loop device"), errpre
);
673 } while (hasdev
== 0);
677 printf("%s\n", loopcxt_get_device(&lc
));
678 warn_size(file
, sizelimit
);
683 res
= delete_loop(&lc
);
684 while (optind
< argc
) {
685 if (!is_loopdev(argv
[optind
]) ||
686 loopcxt_set_device(&lc
, argv
[optind
]))
687 warn(_("%s: failed to use device"),
690 res
+= delete_loop(&lc
);
694 res
= delete_all_loops(&lc
);
697 if (loopcxt_find_unused(&lc
))
698 warn(_("cannot find an unused loop device"));
700 printf("%s\n", loopcxt_get_device(&lc
));
704 res
= show_table(&lc
, file
, offset
, flags
);
706 res
= show_all_loops(&lc
, file
, offset
, flags
);
710 res
= show_table(&lc
, NULL
, 0, 0);
712 res
= printf_loopdev(&lc
);
714 warn("%s", loopcxt_get_device(&lc
));
717 res
= loopcxt_set_capacity(&lc
);
719 warn(_("%s: set capacity failed"),
720 loopcxt_get_device(&lc
));
728 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;