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};
81 static int get_column_id(int num
)
83 assert(ARRAY_SIZE(columns
) == NCOLS
);
84 assert(num
< ncolumns
);
85 assert(columns
[num
] < (int) NCOLS
);
89 static struct colinfo
*get_column_info(int num
)
91 return &infos
[ get_column_id(num
) ];
94 static int column_name_to_id(const char *name
, size_t namesz
)
98 for (i
= 0; i
< NCOLS
; i
++) {
99 const char *cn
= infos
[i
].name
;
101 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
104 warnx(_("unknown column: %s"), name
);
108 static int printf_loopdev(struct loopdev_cxt
*lc
)
116 fname
= loopcxt_get_backing_file(lc
);
120 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
121 loopcxt_get_backing_inode(lc
, &ino
);
125 * Probably non-root user (no permissions to
126 * call LOOP_GET_STATUS ioctls).
128 printf("%s: []: (%s)",
129 loopcxt_get_device(lc
), fname
);
131 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
132 printf(_(", offset %ju"), x
);
134 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
135 printf(_(", sizelimit %ju"), x
);
140 printf("%s: [%04d]:%" PRIu64
" (%s)",
141 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
143 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
144 printf(_(", offset %ju"), x
);
146 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
147 printf(_(", sizelimit %ju"), x
);
149 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
150 const char *e
= loopcxt_get_crypt_name(lc
);
152 if ((!e
|| !*e
) && type
== 1)
155 printf(_(", encryption %s (type %u)"), e
, type
);
161 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
162 uint64_t offset
, int flags
)
164 struct stat sbuf
, *st
= &sbuf
;
165 char *cn_file
= NULL
;
167 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
170 if (!file
|| stat(file
, st
))
173 while (loopcxt_next(lc
) == 0) {
176 const char *bf
= cn_file
? cn_file
: file
;
178 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
179 if (!used
&& !cn_file
) {
180 bf
= cn_file
= canonicalize_path(file
);
181 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
188 loopcxt_deinit_iterator(lc
);
193 static int delete_loop(struct loopdev_cxt
*lc
)
195 if (loopcxt_delete_device(lc
))
196 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
203 static int delete_all_loops(struct loopdev_cxt
*lc
)
207 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
210 while (loopcxt_next(lc
) == 0)
211 res
+= delete_loop(lc
);
213 loopcxt_deinit_iterator(lc
);
217 static int set_scols_data(struct loopdev_cxt
*lc
, struct libscols_line
*ln
)
221 for (i
= 0; i
< ncolumns
; i
++) {
222 const char *p
= NULL
; /* external data */
223 char *np
= NULL
; /* allocated here */
226 switch(get_column_id(i
)) {
228 p
= loopcxt_get_device(lc
);
231 p
= loopcxt_get_backing_file(lc
);
234 if (loopcxt_get_offset(lc
, &x
) == 0)
235 xasprintf(&np
, "%jd", x
);
238 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
239 xasprintf(&np
, "%jd", x
);
241 case COL_BACK_MAJMIN
:
244 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
245 xasprintf(&np
, "%8u:%-3u", major(dev
), minor(dev
));
252 if (loopcxt_get_device(lc
)
253 && stat(loopcxt_get_device(lc
), &st
) == 0
254 && S_ISBLK(st
.st_mode
)
255 && major(st
.st_rdev
) == LOOPDEV_MAJOR
)
256 xasprintf(&np
, "%3u:%-3u", major(st
.st_rdev
),
263 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
264 xasprintf(&np
, "%ju", ino
);
268 p
= loopcxt_is_autoclear(lc
) ? "1" : "0";
271 p
= loopcxt_is_readonly(lc
) ? "1" : "0";
274 p
= loopcxt_is_partscan(lc
) ? "1" : "0";
282 scols_line_set_data(ln
, i
, p
); /* calls strdup() */
284 scols_line_refer_data(ln
, i
, np
); /* only refers */
290 static int show_table(struct loopdev_cxt
*lc
,
295 struct stat sbuf
, *st
= &sbuf
;
296 struct libscols_table
*tb
;
297 struct libscols_line
*ln
;
300 if (!(tb
= scols_new_table()))
301 err(EXIT_FAILURE
, _("failed to initialize output table"));
302 scols_table_enable_raw(tb
, raw
);
303 scols_table_enable_noheadings(tb
, no_headings
);
305 for (i
= 0; i
< ncolumns
; i
++) {
306 struct colinfo
*ci
= get_column_info(i
);
308 if (!scols_table_new_column(tb
, ci
->name
, ci
->whint
, ci
->flags
))
309 err(EXIT_FAILURE
, _("failed to initialize output column"));
312 /* only one loopdev requested (already assigned to loopdev_cxt) */
313 if (loopcxt_get_device(lc
)) {
314 ln
= scols_table_new_line(tb
, NULL
);
316 err(EXIT_FAILURE
, _("failed to initialize output line"));
317 rc
= set_scols_data(lc
, ln
);
319 /* list all loopdevs */
321 char *cn_file
= NULL
;
323 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
326 if (!file
|| stat(file
, st
))
329 while (loopcxt_next(lc
) == 0) {
332 const char *bf
= cn_file
? cn_file
: file
;
334 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
335 if (!used
&& !cn_file
) {
336 bf
= cn_file
= canonicalize_path(file
);
337 used
= loopcxt_is_used(lc
, st
, bf
, offset
, flags
);
343 ln
= scols_table_new_line(tb
, NULL
);
345 err(EXIT_FAILURE
, _("failed to initialize output column"));
346 rc
= set_scols_data(lc
, ln
);
351 loopcxt_deinit_iterator(lc
);
356 rc
= scols_print_table(tb
);
357 scols_unref_table(tb
);
361 static void usage(FILE *out
)
365 fputs(USAGE_HEADER
, out
);
368 _(" %1$s [options] [<loopdev>]\n"
369 " %1$s [options] -f | <loopdev> <file>\n"),
370 program_invocation_short_name
);
372 fputs(USAGE_OPTIONS
, out
);
373 fputs(_(" -a, --all list all used devices\n"), out
);
374 fputs(_(" -d, --detach <loopdev>... detach one or more devices\n"), out
);
375 fputs(_(" -D, --detach-all detach all used devices\n"), out
);
376 fputs(_(" -f, --find find first unused device\n"), out
);
377 fputs(_(" -c, --set-capacity <loopdev> resize the device\n"), out
);
378 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out
);
380 fputs(USAGE_SEPARATOR
, out
);
382 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out
);
383 fputs(_(" --sizelimit <num> device is limited to <num> bytes of the file\n"), out
);
384 fputs(_(" -P, --partscan create a partitioned loop device\n"), out
);
385 fputs(_(" -r, --read-only set up a read-only loop device\n"), out
);
386 fputs(_(" --show print device name after setup (with -f)\n"), out
);
387 fputs(_(" -v, --verbose verbose mode\n"), out
);
389 fputs(USAGE_SEPARATOR
, out
);
391 fputs(_(" -l, --list list info about all or specified (default)\n"), out
);
392 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out
);
393 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out
);
394 fputs(_(" --raw use raw --list output format\n"), out
);
396 fputs(USAGE_SEPARATOR
, out
);
397 fputs(USAGE_HELP
, out
);
398 fputs(USAGE_VERSION
, out
);
400 fputs(_("\nAvailable --list columns:\n"), out
);
401 for (i
= 0; i
< NCOLS
; i
++)
402 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
404 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
406 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
409 static void warn_size(const char *filename
, uint64_t size
)
414 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
420 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
421 "may be useless or invisible for system tools."),
424 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
425 "the end of the file will be ignored."),
429 int main(int argc
, char **argv
)
431 struct loopdev_cxt lc
;
432 int act
= 0, flags
= 0, c
;
434 uint64_t offset
= 0, sizelimit
= 0;
435 int res
= 0, showdev
= 0, lo_flags
= 0;
440 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
444 static const struct option longopts
[] = {
445 { "all", 0, 0, 'a' },
446 { "set-capacity", 1, 0, 'c' },
447 { "detach", 1, 0, 'd' },
448 { "detach-all", 0, 0, 'D' },
449 { "find", 0, 0, 'f' },
450 { "help", 0, 0, 'h' },
451 { "associated", 1, 0, 'j' },
452 { "list", 0, 0, 'l' },
453 { "noheadings", 0, 0, 'n' },
454 { "offset", 1, 0, 'o' },
455 { "output", 1, 0, 'O' },
456 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
457 { "partscan", 0, 0, 'P' },
458 { "read-only", 0, 0, 'r' },
459 { "raw", 0, 0, OPT_RAW
},
460 { "show", 0, 0, OPT_SHOW
},
461 { "verbose", 0, 0, 'v' },
462 { "version", 0, 0, 'V' },
466 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
467 { 'D','a','c','d','f','j' },
468 { 'D','c','d','f','l' },
469 { 'D','c','d','f','O' },
472 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
474 setlocale(LC_ALL
, "");
475 bindtextdomain(PACKAGE
, LOCALEDIR
);
477 atexit(close_stdout
);
479 if (loopcxt_init(&lc
, 0))
480 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
482 while ((c
= getopt_long(argc
, argv
, "ac:d:Dfhj:lno:O:PrvV",
483 longopts
, NULL
)) != -1) {
485 err_exclusive_options(c
, longopts
, excl
, excl_st
);
492 act
= A_SET_CAPACITY
;
493 if (!is_loopdev(optarg
) ||
494 loopcxt_set_device(&lc
, optarg
))
495 err(EXIT_FAILURE
, _("%s: failed to use device"),
499 lo_flags
|= LO_FLAGS_READ_ONLY
;
503 if (!is_loopdev(optarg
) ||
504 loopcxt_set_device(&lc
, optarg
))
505 err(EXIT_FAILURE
, _("%s: failed to use device"),
531 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
532 flags
|= LOOPDEV_FL_OFFSET
;
539 lo_flags
|= LO_FLAGS_PARTSCAN
;
547 printf(UTIL_LINUX_VERSION
);
549 case OPT_SIZELIMIT
: /* --sizelimit */
550 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
551 flags
|= LOOPDEV_FL_SIZELIMIT
;
558 /* default is --list --all */
564 /* default --list output columns */
565 if (list
&& !ncolumns
) {
566 columns
[ncolumns
++] = COL_NAME
;
567 columns
[ncolumns
++] = COL_SIZELIMIT
;
568 columns
[ncolumns
++] = COL_OFFSET
;
569 columns
[ncolumns
++] = COL_AUTOCLR
;
570 columns
[ncolumns
++] = COL_RO
;
571 columns
[ncolumns
++] = COL_BACK_FILE
;
574 if (act
== A_FIND_FREE
&& optind
< argc
) {
576 * losetup -f <backing_file>
579 file
= argv
[optind
++];
582 if (list
&& !act
&& optind
== argc
)
584 * losetup --list defaults to --all
588 if (!act
&& optind
+ 1 == argc
) {
590 * losetup [--list] <device>
593 if (!is_loopdev(argv
[optind
]) ||
594 loopcxt_set_device(&lc
, argv
[optind
]))
595 err(EXIT_FAILURE
, _("%s: failed to use device"),
601 * losetup <loopdev> <backing_file>
606 errx(EXIT_FAILURE
, _("no loop device specified"));
607 /* don't use is_loopdev() here, the device does not have exist yet */
608 if (loopcxt_set_device(&lc
, argv
[optind
]))
609 err(EXIT_FAILURE
, _("%s: failed to use device"),
614 errx(EXIT_FAILURE
, _("no file specified"));
615 file
= argv
[optind
++];
618 if (act
!= A_CREATE
&&
619 (sizelimit
|| lo_flags
|| showdev
))
621 _("the options %s are allowed during loop device setup only"),
622 "--{sizelimit,read-only,show}");
624 if ((flags
& LOOPDEV_FL_OFFSET
) &&
625 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
626 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context"));
628 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
629 &ncolumns
, column_name_to_id
) < 0)
635 int hasdev
= loopcxt_has_device(&lc
);
637 if (hasdev
&& !is_loopdev(loopcxt_get_device(&lc
)))
638 loopcxt_add_device(&lc
);
642 /* Note that loopcxt_{find_unused,set_device}() resets
645 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
646 warnx(_("cannot find an unused loop device"));
649 if (flags
& LOOPDEV_FL_OFFSET
)
650 loopcxt_set_offset(&lc
, offset
);
651 if (flags
& LOOPDEV_FL_SIZELIMIT
)
652 loopcxt_set_sizelimit(&lc
, sizelimit
);
654 loopcxt_set_flags(&lc
, lo_flags
);
655 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
656 warn(_("%s: failed to use backing file"), file
);
660 res
= loopcxt_setup_device(&lc
);
663 if (errno
== EBUSY
&& !hasdev
)
667 errpre
= hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
668 loopcxt_get_device(&lc
) : file
;
669 warn(_("%s: failed to set up loop device"), errpre
);
671 } while (hasdev
== 0);
675 printf("%s\n", loopcxt_get_device(&lc
));
676 warn_size(file
, sizelimit
);
681 res
= delete_loop(&lc
);
682 while (optind
< argc
) {
683 if (!is_loopdev(argv
[optind
]) ||
684 loopcxt_set_device(&lc
, argv
[optind
]))
685 warn(_("%s: failed to use device"),
688 res
+= delete_loop(&lc
);
692 res
= delete_all_loops(&lc
);
695 res
= loopcxt_find_unused(&lc
);
697 warn(_("cannot find an unused loop device"));
699 printf("%s\n", loopcxt_get_device(&lc
));
703 res
= show_table(&lc
, file
, offset
, flags
);
705 res
= show_all_loops(&lc
, file
, offset
, flags
);
709 res
= show_table(&lc
, NULL
, 0, 0);
711 res
= printf_loopdev(&lc
);
713 warn("%s", loopcxt_get_device(&lc
));
716 res
= loopcxt_set_capacity(&lc
);
718 warn(_("%s: set capacity failed"),
719 loopcxt_get_device(&lc
));
727 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;