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"
28 #include "pathnames.h"
31 A_CREATE
= 1, /* setup a new device */
32 A_DELETE
, /* delete given device(s) */
33 A_DELETE_ALL
, /* delete all devices */
34 A_SHOW
, /* list devices */
35 A_SHOW_ONE
, /* print info about one device */
36 A_FIND_FREE
, /* find first unused */
37 A_SET_CAPACITY
, /* set device capacity */
53 /* basic output flags */
54 static int no_headings
;
64 static struct colinfo infos
[] = {
65 [COL_AUTOCLR
] = { "AUTOCLEAR", 1, SCOLS_FL_RIGHT
, N_("autoclear flag set")},
66 [COL_BACK_FILE
] = { "BACK-FILE", 0.3, 0, N_("device backing file")},
67 [COL_BACK_INO
] = { "BACK-INO", 4, SCOLS_FL_RIGHT
, N_("backing file inode number")},
68 [COL_BACK_MAJMIN
] = { "BACK-MAJ:MIN", 6, 0, N_("backing file major:minor device number")},
69 [COL_NAME
] = { "NAME", 0.25, 0, N_("loop device name")},
70 [COL_OFFSET
] = { "OFFSET", 5, SCOLS_FL_RIGHT
, N_("offset from the beginning")},
71 [COL_PARTSCAN
] = { "PARTSCAN", 1, SCOLS_FL_RIGHT
, N_("partscan flag set")},
72 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device")},
73 [COL_SIZELIMIT
] = { "SIZELIMIT", 5, SCOLS_FL_RIGHT
, N_("size limit of the file in bytes")},
74 [COL_MAJMIN
] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")},
77 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
78 static size_t ncolumns
;
80 static int get_column_id(int num
)
83 assert((size_t) num
< ncolumns
);
84 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
88 static struct colinfo
*get_column_info(int num
)
90 return &infos
[ get_column_id(num
) ];
93 static int column_name_to_id(const char *name
, size_t namesz
)
97 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
98 const char *cn
= infos
[i
].name
;
100 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
103 warnx(_("unknown column: %s"), name
);
107 static int printf_loopdev(struct loopdev_cxt
*lc
)
115 fname
= loopcxt_get_backing_file(lc
);
119 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
120 loopcxt_get_backing_inode(lc
, &ino
);
124 * Probably non-root user (no permissions to
125 * call LOOP_GET_STATUS ioctls).
127 printf("%s: []: (%s)",
128 loopcxt_get_device(lc
), fname
);
130 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
131 printf(_(", offset %ju"), x
);
133 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
134 printf(_(", sizelimit %ju"), x
);
139 printf("%s: [%04d]:%" PRIu64
" (%s)",
140 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
142 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
143 printf(_(", offset %ju"), x
);
145 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
146 printf(_(", sizelimit %ju"), x
);
148 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
149 const char *e
= loopcxt_get_crypt_name(lc
);
151 if ((!e
|| !*e
) && type
== 1)
154 printf(_(", encryption %s (type %u)"), e
, type
);
160 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
161 uint64_t offset
, int flags
)
163 struct stat sbuf
, *st
= &sbuf
;
164 char *cn_file
= NULL
;
166 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
169 if (!file
|| stat(file
, st
))
172 while (loopcxt_next(lc
) == 0) {
175 const char *bf
= cn_file
? cn_file
: file
;
177 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
178 if (!used
&& !cn_file
) {
179 bf
= cn_file
= canonicalize_path(file
);
180 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
187 loopcxt_deinit_iterator(lc
);
192 static int delete_loop(struct loopdev_cxt
*lc
)
194 if (loopcxt_delete_device(lc
))
195 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
202 static int delete_all_loops(struct loopdev_cxt
*lc
)
206 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
209 while (loopcxt_next(lc
) == 0)
210 res
+= delete_loop(lc
);
212 loopcxt_deinit_iterator(lc
);
216 static int set_scols_data(struct loopdev_cxt
*lc
, struct libscols_line
*ln
)
220 for (i
= 0; i
< ncolumns
; i
++) {
221 const char *p
= NULL
; /* external data */
222 char *np
= NULL
; /* allocated here */
225 switch(get_column_id(i
)) {
227 p
= loopcxt_get_device(lc
);
230 p
= loopcxt_get_backing_file(lc
);
233 if (loopcxt_get_offset(lc
, &x
) == 0)
234 xasprintf(&np
, "%jd", x
);
237 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
238 xasprintf(&np
, "%jd", x
);
240 case COL_BACK_MAJMIN
:
243 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
244 xasprintf(&np
, "%8u:%-3u", major(dev
), minor(dev
));
251 if (loopcxt_get_device(lc
)
252 && stat(loopcxt_get_device(lc
), &st
) == 0
253 && S_ISBLK(st
.st_mode
)
254 && major(st
.st_rdev
) == LOOPDEV_MAJOR
)
255 xasprintf(&np
, "%3u:%-3u", major(st
.st_rdev
),
262 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
263 xasprintf(&np
, "%ju", ino
);
267 p
= loopcxt_is_autoclear(lc
) ? "1" : "0";
270 p
= loopcxt_is_readonly(lc
) ? "1" : "0";
273 p
= loopcxt_is_partscan(lc
) ? "1" : "0";
281 scols_line_set_data(ln
, i
, p
); /* calls strdup() */
283 scols_line_refer_data(ln
, i
, np
); /* only refers */
289 static int show_table(struct loopdev_cxt
*lc
,
294 struct stat sbuf
, *st
= &sbuf
;
295 struct libscols_table
*tb
;
296 struct libscols_line
*ln
;
302 if (!(tb
= scols_new_table()))
303 err(EXIT_FAILURE
, _("failed to initialize output table"));
304 scols_table_enable_raw(tb
, raw
);
305 scols_table_enable_noheadings(tb
, no_headings
);
307 for (i
= 0; i
< ncolumns
; i
++) {
308 struct colinfo
*ci
= get_column_info(i
);
310 if (!scols_table_new_column(tb
, ci
->name
, ci
->whint
, ci
->flags
))
311 err(EXIT_FAILURE
, _("failed to initialize output column"));
314 /* only one loopdev requested (already assigned to loopdev_cxt) */
315 if (loopcxt_get_device(lc
)) {
316 ln
= scols_table_new_line(tb
, NULL
);
318 err(EXIT_FAILURE
, _("failed to initialize output line"));
319 rc
= set_scols_data(lc
, ln
);
321 /* list all loopdevs */
323 char *cn_file
= NULL
;
325 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
328 if (!file
|| stat(file
, st
))
331 while (loopcxt_next(lc
) == 0) {
334 const char *bf
= cn_file
? cn_file
: file
;
336 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
337 if (!used
&& !cn_file
) {
338 bf
= cn_file
= canonicalize_path(file
);
339 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
345 ln
= scols_table_new_line(tb
, NULL
);
347 err(EXIT_FAILURE
, _("failed to initialize output column"));
348 rc
= set_scols_data(lc
, ln
);
353 loopcxt_deinit_iterator(lc
);
358 rc
= scols_print_table(tb
);
359 scols_unref_table(tb
);
363 static void usage(FILE *out
)
367 fputs(USAGE_HEADER
, out
);
370 _(" %1$s [options] [<loopdev>]\n"
371 " %1$s [options] -f | <loopdev> <file>\n"),
372 program_invocation_short_name
);
374 fputs(USAGE_SEPARATOR
, out
);
375 fputs(_("Set up and control loop devices.\n"), out
);
377 fputs(USAGE_OPTIONS
, out
);
378 fputs(_(" -a, --all list all used devices\n"), out
);
379 fputs(_(" -d, --detach <loopdev>... detach one or more devices\n"), out
);
380 fputs(_(" -D, --detach-all detach all used devices\n"), out
);
381 fputs(_(" -f, --find find first unused device\n"), out
);
382 fputs(_(" -c, --set-capacity <loopdev> resize the device\n"), out
);
383 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out
);
385 fputs(USAGE_SEPARATOR
, out
);
387 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out
);
388 fputs(_(" --sizelimit <num> device is limited to <num> bytes of the file\n"), out
);
389 fputs(_(" -P, --partscan create a partitioned loop device\n"), out
);
390 fputs(_(" -r, --read-only set up a read-only loop device\n"), out
);
391 fputs(_(" --show print device name after setup (with -f)\n"), out
);
392 fputs(_(" -v, --verbose verbose mode\n"), out
);
394 fputs(USAGE_SEPARATOR
, out
);
396 fputs(_(" -l, --list list info about all or specified (default)\n"), out
);
397 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out
);
398 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out
);
399 fputs(_(" --raw use raw --list output format\n"), out
);
401 fputs(USAGE_SEPARATOR
, out
);
402 fputs(USAGE_HELP
, out
);
403 fputs(USAGE_VERSION
, out
);
405 fputs(_("\nAvailable --list columns:\n"), out
);
406 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
407 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
409 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
411 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
414 static void warn_size(const char *filename
, uint64_t size
)
419 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
425 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
426 "may be useless or invisible for system tools."),
429 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
430 "the end of the file will be ignored."),
434 int main(int argc
, char **argv
)
436 struct loopdev_cxt lc
;
437 int act
= 0, flags
= 0, c
;
439 uint64_t offset
= 0, sizelimit
= 0;
440 int res
= 0, showdev
= 0, lo_flags
= 0;
445 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
449 static const struct option longopts
[] = {
450 { "all", 0, 0, 'a' },
451 { "set-capacity", 1, 0, 'c' },
452 { "detach", 1, 0, 'd' },
453 { "detach-all", 0, 0, 'D' },
454 { "find", 0, 0, 'f' },
455 { "help", 0, 0, 'h' },
456 { "associated", 1, 0, 'j' },
457 { "list", 0, 0, 'l' },
458 { "noheadings", 0, 0, 'n' },
459 { "offset", 1, 0, 'o' },
460 { "output", 1, 0, 'O' },
461 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
462 { "partscan", 0, 0, 'P' },
463 { "read-only", 0, 0, 'r' },
464 { "raw", 0, 0, OPT_RAW
},
465 { "show", 0, 0, OPT_SHOW
},
466 { "verbose", 0, 0, 'v' },
467 { "version", 0, 0, 'V' },
471 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
472 { 'D','a','c','d','f','j' },
473 { 'D','c','d','f','l' },
474 { 'D','c','d','f','O' },
477 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
479 setlocale(LC_ALL
, "");
480 bindtextdomain(PACKAGE
, LOCALEDIR
);
482 atexit(close_stdout
);
484 if (loopcxt_init(&lc
, 0))
485 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
487 while ((c
= getopt_long(argc
, argv
, "ac:d:Dfhj:lno:O:PrvV",
488 longopts
, NULL
)) != -1) {
490 err_exclusive_options(c
, longopts
, excl
, excl_st
);
497 act
= A_SET_CAPACITY
;
498 if (!is_loopdev(optarg
) ||
499 loopcxt_set_device(&lc
, optarg
))
500 err(EXIT_FAILURE
, _("%s: failed to use device"),
504 lo_flags
|= LO_FLAGS_READ_ONLY
;
508 if (!is_loopdev(optarg
) ||
509 loopcxt_set_device(&lc
, optarg
))
510 err(EXIT_FAILURE
, _("%s: failed to use device"),
536 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
537 flags
|= LOOPDEV_FL_OFFSET
;
544 lo_flags
|= LO_FLAGS_PARTSCAN
;
552 printf(UTIL_LINUX_VERSION
);
554 case OPT_SIZELIMIT
: /* --sizelimit */
555 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
556 flags
|= LOOPDEV_FL_SIZELIMIT
;
563 /* default is --list --all */
569 /* default --list output columns */
570 if (list
&& !ncolumns
) {
571 columns
[ncolumns
++] = COL_NAME
;
572 columns
[ncolumns
++] = COL_SIZELIMIT
;
573 columns
[ncolumns
++] = COL_OFFSET
;
574 columns
[ncolumns
++] = COL_AUTOCLR
;
575 columns
[ncolumns
++] = COL_RO
;
576 columns
[ncolumns
++] = COL_BACK_FILE
;
579 if (act
== A_FIND_FREE
&& optind
< argc
) {
581 * losetup -f <backing_file>
584 file
= argv
[optind
++];
587 if (list
&& !act
&& optind
== argc
)
589 * losetup --list defaults to --all
593 if (!act
&& optind
+ 1 == argc
) {
595 * losetup [--list] <device>
598 if (!is_loopdev(argv
[optind
]) ||
599 loopcxt_set_device(&lc
, argv
[optind
]))
600 err(EXIT_FAILURE
, _("%s: failed to use device"),
606 * losetup <loopdev> <backing_file>
611 errx(EXIT_FAILURE
, _("no loop device specified"));
612 /* don't use is_loopdev() here, the device does not have exist yet */
613 if (loopcxt_set_device(&lc
, argv
[optind
]))
614 err(EXIT_FAILURE
, _("%s: failed to use device"),
619 errx(EXIT_FAILURE
, _("no file specified"));
620 file
= argv
[optind
++];
623 if (act
!= A_CREATE
&&
624 (sizelimit
|| lo_flags
|| showdev
))
626 _("the options %s are allowed during loop device setup only"),
627 "--{sizelimit,read-only,show}");
629 if ((flags
& LOOPDEV_FL_OFFSET
) &&
630 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
631 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context"));
633 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
634 &ncolumns
, column_name_to_id
) < 0)
640 int hasdev
= loopcxt_has_device(&lc
);
642 if (hasdev
&& !is_loopdev(loopcxt_get_device(&lc
)))
643 loopcxt_add_device(&lc
);
647 /* Note that loopcxt_{find_unused,set_device}() resets
650 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
651 warnx(_("cannot find an unused loop device"));
654 if (flags
& LOOPDEV_FL_OFFSET
)
655 loopcxt_set_offset(&lc
, offset
);
656 if (flags
& LOOPDEV_FL_SIZELIMIT
)
657 loopcxt_set_sizelimit(&lc
, sizelimit
);
659 loopcxt_set_flags(&lc
, lo_flags
);
660 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
661 warn(_("%s: failed to use backing file"), file
);
665 res
= loopcxt_setup_device(&lc
);
668 if (errno
== EBUSY
&& !hasdev
)
672 errpre
= hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
673 loopcxt_get_device(&lc
) : file
;
674 warn(_("%s: failed to set up loop device"), errpre
);
676 } while (hasdev
== 0);
680 printf("%s\n", loopcxt_get_device(&lc
));
681 warn_size(file
, sizelimit
);
686 res
= delete_loop(&lc
);
687 while (optind
< argc
) {
688 if (!is_loopdev(argv
[optind
]) ||
689 loopcxt_set_device(&lc
, argv
[optind
]))
690 warn(_("%s: failed to use device"),
693 res
+= delete_loop(&lc
);
697 res
= delete_all_loops(&lc
);
700 res
= loopcxt_find_unused(&lc
);
704 if (access(_PATH_DEV_LOOPCTL
, F_OK
) == 0 &&
705 access(_PATH_DEV_LOOPCTL
, W_OK
) != 0)
710 warn(_("cannot find an unused loop device"));
712 printf("%s\n", loopcxt_get_device(&lc
));
716 res
= show_table(&lc
, file
, offset
, flags
);
718 res
= show_all_loops(&lc
, file
, offset
, flags
);
722 res
= show_table(&lc
, NULL
, 0, 0);
724 res
= printf_loopdev(&lc
);
726 warn("%s", loopcxt_get_device(&lc
));
729 res
= loopcxt_set_capacity(&lc
);
731 warn(_("%s: set capacity failed"),
732 loopcxt_get_device(&lc
));
740 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;