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 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
79 static int get_column_id(int num
)
81 assert(num
< ncolumns
);
82 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
86 static struct colinfo
*get_column_info(int num
)
88 return &infos
[ get_column_id(num
) ];
91 static int column_name_to_id(const char *name
, size_t namesz
)
95 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
96 const char *cn
= infos
[i
].name
;
98 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
101 warnx(_("unknown column: %s"), name
);
105 static int printf_loopdev(struct loopdev_cxt
*lc
)
113 fname
= loopcxt_get_backing_file(lc
);
117 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
118 loopcxt_get_backing_inode(lc
, &ino
);
122 * Probably non-root user (no permissions to
123 * call LOOP_GET_STATUS ioctls).
125 printf("%s: []: (%s)",
126 loopcxt_get_device(lc
), fname
);
128 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
129 printf(_(", offset %ju"), x
);
131 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
132 printf(_(", sizelimit %ju"), x
);
137 printf("%s: [%04d]:%" PRIu64
" (%s)",
138 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
140 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
141 printf(_(", offset %ju"), x
);
143 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
144 printf(_(", sizelimit %ju"), x
);
146 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
147 const char *e
= loopcxt_get_crypt_name(lc
);
149 if ((!e
|| !*e
) && type
== 1)
152 printf(_(", encryption %s (type %u)"), e
, type
);
158 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
159 uint64_t offset
, int flags
)
161 struct stat sbuf
, *st
= &sbuf
;
162 char *cn_file
= NULL
;
164 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
167 if (!file
|| stat(file
, st
))
170 while (loopcxt_next(lc
) == 0) {
173 const char *bf
= cn_file
? cn_file
: file
;
175 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
176 if (!used
&& !cn_file
) {
177 bf
= cn_file
= canonicalize_path(file
);
178 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
185 loopcxt_deinit_iterator(lc
);
190 static int delete_loop(struct loopdev_cxt
*lc
)
192 if (loopcxt_delete_device(lc
))
193 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
200 static int delete_all_loops(struct loopdev_cxt
*lc
)
204 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
207 while (loopcxt_next(lc
) == 0)
208 res
+= delete_loop(lc
);
210 loopcxt_deinit_iterator(lc
);
214 static int set_scols_data(struct loopdev_cxt
*lc
, struct libscols_line
*ln
)
218 for (i
= 0; i
< ncolumns
; i
++) {
219 const char *p
= NULL
; /* external data */
220 char *np
= NULL
; /* allocated here */
223 switch(get_column_id(i
)) {
225 p
= loopcxt_get_device(lc
);
228 p
= loopcxt_get_backing_file(lc
);
231 if (loopcxt_get_offset(lc
, &x
) == 0)
232 xasprintf(&np
, "%jd", x
);
235 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
236 xasprintf(&np
, "%jd", x
);
238 case COL_BACK_MAJMIN
:
241 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
242 xasprintf(&np
, "%8u:%-3u", major(dev
), minor(dev
));
249 if (loopcxt_get_device(lc
)
250 && stat(loopcxt_get_device(lc
), &st
) == 0
251 && S_ISBLK(st
.st_mode
)
252 && major(st
.st_rdev
) == LOOPDEV_MAJOR
)
253 xasprintf(&np
, "%3u:%-3u", major(st
.st_rdev
),
260 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
261 xasprintf(&np
, "%ju", ino
);
265 p
= loopcxt_is_autoclear(lc
) ? "1" : "0";
268 p
= loopcxt_is_readonly(lc
) ? "1" : "0";
271 p
= loopcxt_is_partscan(lc
) ? "1" : "0";
279 scols_line_set_data(ln
, i
, p
); /* calls strdup() */
281 scols_line_refer_data(ln
, i
, np
); /* only refers */
287 static int show_table(struct loopdev_cxt
*lc
,
292 struct stat sbuf
, *st
= &sbuf
;
293 struct libscols_table
*tb
;
294 struct libscols_line
*ln
;
299 if (!(tb
= scols_new_table()))
300 err(EXIT_FAILURE
, _("failed to initialize output table"));
301 scols_table_enable_raw(tb
, raw
);
302 scols_table_enable_noheadings(tb
, no_headings
);
304 for (i
= 0; i
< ncolumns
; i
++) {
305 struct colinfo
*ci
= get_column_info(i
);
307 if (!scols_table_new_column(tb
, ci
->name
, ci
->whint
, ci
->flags
))
308 err(EXIT_FAILURE
, _("failed to initialize output column"));
311 /* only one loopdev requested (already assigned to loopdev_cxt) */
312 if (loopcxt_get_device(lc
)) {
313 ln
= scols_table_new_line(tb
, NULL
);
315 err(EXIT_FAILURE
, _("failed to initialize output line"));
316 rc
= set_scols_data(lc
, ln
);
318 /* list all loopdevs */
320 char *cn_file
= NULL
;
322 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
325 if (!file
|| stat(file
, st
))
328 while (loopcxt_next(lc
) == 0) {
331 const char *bf
= cn_file
? cn_file
: file
;
333 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
334 if (!used
&& !cn_file
) {
335 bf
= cn_file
= canonicalize_path(file
);
336 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
342 ln
= scols_table_new_line(tb
, NULL
);
344 err(EXIT_FAILURE
, _("failed to initialize output column"));
345 rc
= set_scols_data(lc
, ln
);
350 loopcxt_deinit_iterator(lc
);
355 rc
= scols_print_table(tb
);
356 scols_unref_table(tb
);
360 static void usage(FILE *out
)
364 fputs(USAGE_HEADER
, out
);
367 _(" %1$s [options] [<loopdev>]\n"
368 " %1$s [options] -f | <loopdev> <file>\n"),
369 program_invocation_short_name
);
371 fputs(USAGE_OPTIONS
, out
);
372 fputs(_(" -a, --all list all used devices\n"), out
);
373 fputs(_(" -d, --detach <loopdev>... detach one or more devices\n"), out
);
374 fputs(_(" -D, --detach-all detach all used devices\n"), out
);
375 fputs(_(" -f, --find find first unused device\n"), out
);
376 fputs(_(" -c, --set-capacity <loopdev> resize the device\n"), out
);
377 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out
);
379 fputs(USAGE_SEPARATOR
, out
);
381 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out
);
382 fputs(_(" --sizelimit <num> device is limited to <num> bytes of the file\n"), out
);
383 fputs(_(" -P, --partscan create a partitioned loop device\n"), out
);
384 fputs(_(" -r, --read-only set up a read-only loop device\n"), out
);
385 fputs(_(" --show print device name after setup (with -f)\n"), out
);
386 fputs(_(" -v, --verbose verbose mode\n"), out
);
388 fputs(USAGE_SEPARATOR
, out
);
390 fputs(_(" -l, --list list info about all or specified (default)\n"), out
);
391 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out
);
392 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out
);
393 fputs(_(" --raw use raw --list output format\n"), out
);
395 fputs(USAGE_SEPARATOR
, out
);
396 fputs(USAGE_HELP
, out
);
397 fputs(USAGE_VERSION
, out
);
399 fputs(_("\nAvailable --list columns:\n"), out
);
400 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
401 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
403 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
405 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
408 static void warn_size(const char *filename
, uint64_t size
)
413 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
419 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
420 "may be useless or invisible for system tools."),
423 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
424 "the end of the file will be ignored."),
428 int main(int argc
, char **argv
)
430 struct loopdev_cxt lc
;
431 int act
= 0, flags
= 0, c
;
433 uint64_t offset
= 0, sizelimit
= 0;
434 int res
= 0, showdev
= 0, lo_flags
= 0;
439 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
443 static const struct option longopts
[] = {
444 { "all", 0, 0, 'a' },
445 { "set-capacity", 1, 0, 'c' },
446 { "detach", 1, 0, 'd' },
447 { "detach-all", 0, 0, 'D' },
448 { "find", 0, 0, 'f' },
449 { "help", 0, 0, 'h' },
450 { "associated", 1, 0, 'j' },
451 { "list", 0, 0, 'l' },
452 { "noheadings", 0, 0, 'n' },
453 { "offset", 1, 0, 'o' },
454 { "output", 1, 0, 'O' },
455 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
456 { "partscan", 0, 0, 'P' },
457 { "read-only", 0, 0, 'r' },
458 { "raw", 0, 0, OPT_RAW
},
459 { "show", 0, 0, OPT_SHOW
},
460 { "verbose", 0, 0, 'v' },
461 { "version", 0, 0, 'V' },
465 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
466 { 'D','a','c','d','f','j' },
467 { 'D','c','d','f','l' },
468 { 'D','c','d','f','O' },
471 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
473 setlocale(LC_ALL
, "");
474 bindtextdomain(PACKAGE
, LOCALEDIR
);
476 atexit(close_stdout
);
478 if (loopcxt_init(&lc
, 0))
479 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
481 while ((c
= getopt_long(argc
, argv
, "ac:d:Dfhj:lno:O:PrvV",
482 longopts
, NULL
)) != -1) {
484 err_exclusive_options(c
, longopts
, excl
, excl_st
);
491 act
= A_SET_CAPACITY
;
492 if (!is_loopdev(optarg
) ||
493 loopcxt_set_device(&lc
, optarg
))
494 err(EXIT_FAILURE
, _("%s: failed to use device"),
498 lo_flags
|= LO_FLAGS_READ_ONLY
;
502 if (!is_loopdev(optarg
) ||
503 loopcxt_set_device(&lc
, optarg
))
504 err(EXIT_FAILURE
, _("%s: failed to use device"),
530 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
531 flags
|= LOOPDEV_FL_OFFSET
;
538 lo_flags
|= LO_FLAGS_PARTSCAN
;
546 printf(UTIL_LINUX_VERSION
);
548 case OPT_SIZELIMIT
: /* --sizelimit */
549 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
550 flags
|= LOOPDEV_FL_SIZELIMIT
;
557 /* default is --list --all */
563 /* default --list output columns */
564 if (list
&& !ncolumns
) {
565 columns
[ncolumns
++] = COL_NAME
;
566 columns
[ncolumns
++] = COL_SIZELIMIT
;
567 columns
[ncolumns
++] = COL_OFFSET
;
568 columns
[ncolumns
++] = COL_AUTOCLR
;
569 columns
[ncolumns
++] = COL_RO
;
570 columns
[ncolumns
++] = COL_BACK_FILE
;
573 if (act
== A_FIND_FREE
&& optind
< argc
) {
575 * losetup -f <backing_file>
578 file
= argv
[optind
++];
581 if (list
&& !act
&& optind
== argc
)
583 * losetup --list defaults to --all
587 if (!act
&& optind
+ 1 == argc
) {
589 * losetup [--list] <device>
592 if (!is_loopdev(argv
[optind
]) ||
593 loopcxt_set_device(&lc
, argv
[optind
]))
594 err(EXIT_FAILURE
, _("%s: failed to use device"),
600 * losetup <loopdev> <backing_file>
605 errx(EXIT_FAILURE
, _("no loop device specified"));
606 /* don't use is_loopdev() here, the device does not have exist yet */
607 if (loopcxt_set_device(&lc
, argv
[optind
]))
608 err(EXIT_FAILURE
, _("%s: failed to use device"),
613 errx(EXIT_FAILURE
, _("no file specified"));
614 file
= argv
[optind
++];
617 if (act
!= A_CREATE
&&
618 (sizelimit
|| lo_flags
|| showdev
))
620 _("the options %s are allowed during loop device setup only"),
621 "--{sizelimit,read-only,show}");
623 if ((flags
& LOOPDEV_FL_OFFSET
) &&
624 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
625 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context"));
627 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
628 &ncolumns
, column_name_to_id
) < 0)
634 int hasdev
= loopcxt_has_device(&lc
);
636 if (hasdev
&& !is_loopdev(loopcxt_get_device(&lc
)))
637 loopcxt_add_device(&lc
);
641 /* Note that loopcxt_{find_unused,set_device}() resets
644 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
645 warnx(_("cannot find an unused loop device"));
648 if (flags
& LOOPDEV_FL_OFFSET
)
649 loopcxt_set_offset(&lc
, offset
);
650 if (flags
& LOOPDEV_FL_SIZELIMIT
)
651 loopcxt_set_sizelimit(&lc
, sizelimit
);
653 loopcxt_set_flags(&lc
, lo_flags
);
654 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
655 warn(_("%s: failed to use backing file"), file
);
659 res
= loopcxt_setup_device(&lc
);
662 if (errno
== EBUSY
&& !hasdev
)
666 errpre
= hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
667 loopcxt_get_device(&lc
) : file
;
668 warn(_("%s: failed to set up loop device"), errpre
);
670 } while (hasdev
== 0);
674 printf("%s\n", loopcxt_get_device(&lc
));
675 warn_size(file
, sizelimit
);
680 res
= delete_loop(&lc
);
681 while (optind
< argc
) {
682 if (!is_loopdev(argv
[optind
]) ||
683 loopcxt_set_device(&lc
, argv
[optind
]))
684 warn(_("%s: failed to use device"),
687 res
+= delete_loop(&lc
);
691 res
= delete_all_loops(&lc
);
694 res
= loopcxt_find_unused(&lc
);
696 warn(_("cannot find an unused loop device"));
698 printf("%s\n", loopcxt_get_device(&lc
));
702 res
= show_table(&lc
, file
, offset
, flags
);
704 res
= show_all_loops(&lc
, file
, offset
, flags
);
708 res
= show_table(&lc
, NULL
, 0, 0);
710 res
= printf_loopdev(&lc
);
712 warn("%s", loopcxt_get_device(&lc
));
715 res
= loopcxt_set_capacity(&lc
);
717 warn(_("%s: set capacity failed"),
718 loopcxt_get_device(&lc
));
726 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;