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 */
38 A_SET_DIRECT_IO
, /* set accessing backing file by direct io */
39 A_SET_BLOCKSIZE
, /* set logical block size of the loop device */
62 /* basic output flags */
63 static int no_headings
;
68 const char * const name
;
73 int json_type
; /* default is string */
76 static const struct colinfo infos
[] = {
77 [COL_AUTOCLR
] = { "AUTOCLEAR", 1, SCOLS_FL_RIGHT
, N_("autoclear flag set"), SCOLS_JSON_BOOLEAN
},
78 [COL_BACK_FILE
] = { "BACK-FILE", 0.3, SCOLS_FL_NOEXTREMES
, N_("device backing file")},
79 [COL_BACK_INO
] = { "BACK-INO", 4, SCOLS_FL_RIGHT
, N_("backing file inode number"), SCOLS_JSON_NUMBER
},
80 [COL_BACK_MAJMIN
] = { "BACK-MAJ:MIN", 6, 0, N_("backing file major:minor device number")},
81 [COL_BACK_MAJ
] = { "BACK-MAJ", 6, 0, N_("backing file major device number")},
82 [COL_BACK_MIN
] = { "BACK-MIN", 6, 0, N_("backing file minor device number")},
83 [COL_NAME
] = { "NAME", 0.25, 0, N_("loop device name")},
84 [COL_OFFSET
] = { "OFFSET", 5, SCOLS_FL_RIGHT
, N_("offset from the beginning"), SCOLS_JSON_NUMBER
},
85 [COL_PARTSCAN
] = { "PARTSCAN", 1, SCOLS_FL_RIGHT
, N_("partscan flag set"), SCOLS_JSON_BOOLEAN
},
86 [COL_REF
] = { "REF", 0.1, 0, N_("loop device reference string")},
87 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), SCOLS_JSON_BOOLEAN
},
88 [COL_SIZELIMIT
] = { "SIZELIMIT", 5, SCOLS_FL_RIGHT
, N_("size limit of the file in bytes"), SCOLS_JSON_NUMBER
},
89 [COL_MAJMIN
] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")},
90 [COL_MAJ
] = { "MAJ", 1, SCOLS_FL_RIGHT
, N_("loop device major number"), SCOLS_JSON_NUMBER
},
91 [COL_MIN
] = { "MIN", 1, SCOLS_FL_RIGHT
, N_("loop device minor number"), SCOLS_JSON_NUMBER
},
92 [COL_DIO
] = { "DIO", 1, SCOLS_FL_RIGHT
, N_("access backing file with direct-io"), SCOLS_JSON_BOOLEAN
},
93 [COL_LOGSEC
] = { "LOG-SEC", 4, SCOLS_FL_RIGHT
, N_("logical sector size in bytes"), SCOLS_JSON_NUMBER
},
96 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
97 static size_t ncolumns
;
99 static int get_column_id(int num
)
102 assert((size_t) num
< ncolumns
);
103 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
107 static const struct colinfo
*get_column_info(int num
)
109 return &infos
[ get_column_id(num
) ];
112 static int column_name_to_id(const char *name
, size_t namesz
)
116 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
117 const char *cn
= infos
[i
].name
;
119 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
122 warnx(_("unknown column: %s"), name
);
126 static int printf_loopdev(struct loopdev_cxt
*lc
)
134 fname
= loopcxt_get_backing_file(lc
);
138 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
139 loopcxt_get_backing_inode(lc
, &ino
);
143 * Probably non-root user (no permissions to
144 * call LOOP_GET_STATUS ioctls).
146 printf("%s%s: []: (%s)",
147 loopcxt_get_device(lc
),
148 loopcxt_is_lost(lc
) ? " (lost)" : "",
151 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
152 printf(_(", offset %ju"), x
);
153 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
154 printf(_(", sizelimit %ju"), x
);
159 printf("%s%s: [%04jd]:%ju (%s)",
160 loopcxt_get_device(lc
),
161 loopcxt_is_lost(lc
) ? " (lost)" : "",
162 (intmax_t) dev
, (uintmax_t) ino
, fname
);
164 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
165 printf(_(", offset %ju"), x
);
166 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
167 printf(_(", sizelimit %ju"), x
);
169 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
170 const char *e
= loopcxt_get_crypt_name(lc
);
172 if ((!e
|| !*e
) && type
== 1)
175 printf(_(", encryption %s (type %u)"), e
, type
);
184 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
185 uint64_t offset
, int flags
)
187 struct stat sbuf
, *st
= &sbuf
;
188 char *cn_file
= NULL
;
190 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
193 if (!file
|| stat(file
, st
))
196 while (loopcxt_next(lc
) == 0) {
199 const char *bf
= cn_file
? cn_file
: file
;
201 used
= loopcxt_is_used(lc
, st
, bf
, offset
, 0, flags
);
202 if (!used
&& !cn_file
) {
203 bf
= cn_file
= canonicalize_path(file
);
204 used
= loopcxt_is_used(lc
, st
, bf
, offset
, 0, flags
);
211 loopcxt_deinit_iterator(lc
);
216 static void warn_lost(struct loopdev_cxt
*lc
)
218 dev_t devno
= loopcxt_get_devno(lc
);
223 warnx(("device node %s (%u:%u) is lost. You may use mknod(1) to recover it."),
224 loopcxt_get_device(lc
), major(devno
), minor(devno
));
227 static int delete_loop(struct loopdev_cxt
*lc
)
229 if (loopcxt_delete_device(lc
)) {
230 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
231 if (loopcxt_is_lost(lc
))
239 static int delete_all_loops(struct loopdev_cxt
*lc
)
243 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
246 while (loopcxt_next(lc
) == 0)
247 res
+= delete_loop(lc
);
249 loopcxt_deinit_iterator(lc
);
253 static int set_scols_data(struct loopdev_cxt
*lc
, struct libscols_line
*ln
)
257 for (i
= 0; i
< ncolumns
; i
++) {
258 const char *p
= NULL
; /* external data */
259 char *np
= NULL
; /* allocated here */
263 switch(get_column_id(i
)) {
265 p
= loopcxt_get_device(lc
);
266 if (loopcxt_is_lost(lc
)) {
267 xasprintf(&np
, "%s (lost)", p
);
272 np
= loopcxt_get_backing_file(lc
);
275 if (loopcxt_get_offset(lc
, &x
) == 0)
276 xasprintf(&np
, "%jd", x
);
279 if (loopcxt_get_sizelimit(lc
, &x
) == 0)
280 xasprintf(&np
, "%jd", x
);
282 case COL_BACK_MAJMIN
:
285 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
286 xasprintf(&np
, raw
|| json
? "%u:%u" : "%8u:%-3u",
287 major(dev
), minor(dev
));
293 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
294 xasprintf(&np
, "%u", major(dev
));
300 if (loopcxt_get_backing_devno(lc
, &dev
) == 0 && dev
)
301 xasprintf(&np
, "%u", minor(dev
));
306 dev_t dev
= loopcxt_get_devno(lc
);
308 xasprintf(&np
, raw
|| json
? "%u:%u" :"%3u:%-3u",
309 major(dev
), minor(dev
));
313 dev_t dev
= loopcxt_get_devno(lc
);
315 xasprintf(&np
, "%u", major(dev
));
319 dev_t dev
= loopcxt_get_devno(lc
);
321 xasprintf(&np
, "%u", minor(dev
));
327 if (loopcxt_get_backing_inode(lc
, &ino
) == 0 && ino
)
328 xasprintf(&np
, "%ju", ino
);
332 p
= loopcxt_is_autoclear(lc
) ? "1" : "0";
335 p
= loopcxt_is_readonly(lc
) ? "1" : "0";
338 p
= loopcxt_is_dio(lc
) ? "1" : "0";
341 p
= loopcxt_is_partscan(lc
) ? "1" : "0";
344 if (loopcxt_get_blocksize(lc
, &x
) == 0)
345 xasprintf(&np
, "%jd", x
);
348 np
= loopcxt_get_refname(lc
);
356 rc
= scols_line_set_data(ln
, i
, p
); /* calls strdup() */
358 rc
= scols_line_refer_data(ln
, i
, np
); /* only refers */
361 err(EXIT_FAILURE
, _("failed to add output data"));
367 static int show_table(struct loopdev_cxt
*lc
,
372 struct stat sbuf
, *st
= &sbuf
;
373 struct libscols_table
*tb
;
374 struct libscols_line
*ln
;
380 if (!(tb
= scols_new_table()))
381 err(EXIT_FAILURE
, _("failed to allocate output table"));
382 scols_table_enable_raw(tb
, raw
);
383 scols_table_enable_json(tb
, json
);
384 scols_table_enable_noheadings(tb
, no_headings
);
387 scols_table_set_name(tb
, "loopdevices");
389 for (i
= 0; i
< ncolumns
; i
++) {
390 const struct colinfo
*ci
= get_column_info(i
);
391 struct libscols_column
*cl
;
393 cl
= scols_table_new_column(tb
, ci
->name
, ci
->whint
, ci
->flags
);
395 err(EXIT_FAILURE
, _("failed to allocate output column"));
397 scols_column_set_json_type(cl
, ci
->json_type
);
400 /* only one loopdev requested (already assigned to loopdev_cxt) */
401 if (loopcxt_get_device(lc
)) {
402 ln
= scols_table_new_line(tb
, NULL
);
404 err(EXIT_FAILURE
, _("failed to allocate output line"));
405 rc
= set_scols_data(lc
, ln
);
407 /* list all loopdevs */
409 char *cn_file
= NULL
;
411 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
414 if (!file
|| stat(file
, st
))
417 while (loopcxt_next(lc
) == 0) {
420 const char *bf
= cn_file
? cn_file
: file
;
422 used
= loopcxt_is_used(lc
, st
, bf
, offset
, 0, flags
);
423 if (!used
&& !cn_file
) {
424 bf
= cn_file
= canonicalize_path(file
);
425 used
= loopcxt_is_used(lc
, st
, bf
, offset
, 0, flags
);
431 ln
= scols_table_new_line(tb
, NULL
);
433 err(EXIT_FAILURE
, _("failed to allocate output line"));
434 rc
= set_scols_data(lc
, ln
);
439 loopcxt_deinit_iterator(lc
);
444 rc
= scols_print_table(tb
);
445 scols_unref_table(tb
);
449 static void __attribute__((__noreturn__
)) usage(void)
454 fputs(USAGE_HEADER
, out
);
457 _(" %1$s [options] [<loopdev>]\n"
458 " %1$s [options] -f | <loopdev> <file>\n"),
459 program_invocation_short_name
);
461 fputs(USAGE_SEPARATOR
, out
);
462 fputs(_("Set up and control loop devices.\n"), out
);
465 fputs(USAGE_OPTIONS
, out
);
466 fputs(_(" -a, --all list all used devices\n"), out
);
467 fputs(_(" -d, --detach <loopdev>... detach one or more devices\n"), out
);
468 fputs(_(" -D, --detach-all detach all used devices\n"), out
);
469 fputs(_(" -f, --find find first unused device\n"), out
);
470 fputs(_(" -c, --set-capacity <loopdev> resize the device\n"), out
);
471 fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out
);
472 fputs(_(" -L, --nooverlap avoid possible conflict between devices\n"), out
);
474 /* commands options */
475 fputs(USAGE_SEPARATOR
, out
);
476 fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out
);
477 fputs(_(" --sizelimit <num> device is limited to <num> bytes of the file\n"), out
);
478 fputs(_(" -b, --sector-size <num> set the logical sector size to <num>\n"), out
);
479 fputs(_(" -P, --partscan create a partitioned loop device\n"), out
);
480 fputs(_(" -r, --read-only set up a read-only loop device\n"), out
);
481 fputs(_(" --direct-io[=<on|off>] open backing file with O_DIRECT\n"), out
);
482 fputs(_(" --loop-ref <string> loop device reference\n"), out
);
483 fputs(_(" --show print device name after setup (with -f)\n"), out
);
484 fputs(_(" -v, --verbose verbose mode\n"), out
);
487 fputs(USAGE_SEPARATOR
, out
);
488 fputs(_(" -J, --json use JSON --list output format\n"), out
);
489 fputs(_(" -l, --list list info about all or specified (default)\n"), out
);
490 fputs(_(" -n, --noheadings don't print headings for --list output\n"), out
);
491 fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out
);
492 fputs(_(" --output-all output all columns\n"), out
);
493 fputs(_(" --raw use raw --list output format\n"), out
);
495 fputs(USAGE_SEPARATOR
, out
);
496 fprintf(out
, USAGE_HELP_OPTIONS(31));
498 fputs(USAGE_COLUMNS
, out
);
499 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
500 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
502 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
507 static void warn_size(const char *filename
, uint64_t size
, uint64_t offset
, int flags
)
512 if (stat(filename
, &st
) || S_ISBLK(st
.st_mode
))
516 if (flags
& LOOPDEV_FL_OFFSET
)
521 warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device "
522 "may be useless or invisible for system tools."),
525 warnx(_("%s: Warning: file does not fit into a 512-byte sector; "
526 "the end of the file will be ignored."),
530 static int find_unused(struct loopdev_cxt
*lc
)
534 rc
= loopcxt_find_unused(lc
);
538 if (access(_PATH_DEV_LOOPCTL
, F_OK
) == 0 &&
539 access(_PATH_DEV_LOOPCTL
, W_OK
) != 0)
544 warn(_("cannot find an unused loop device"));
549 static int create_loop(struct loopdev_cxt
*lc
,
550 int nooverlap
, int lo_flags
, int flags
,
551 const char *file
, const char *refname
,
552 uint64_t offset
, uint64_t sizelimit
,
555 int hasdev
= loopcxt_has_device(lc
);
556 int rc
= 0, ntries
= 0;
558 /* losetup --find --noverlap file.img */
559 if (!hasdev
&& nooverlap
) {
560 rc
= loopcxt_find_overlap(lc
, file
, offset
, sizelimit
);
562 case 0: /* not found */
565 case 1: /* overlap */
567 errx(EXIT_FAILURE
, _("%s: overlapping loop device exists"), file
);
569 case 2: /* overlap -- full size and offset match (reuse) */
571 uint32_t lc_encrypt_type
;
573 /* Once a loop is initialized RO, there is no
574 * way to change its parameters. */
575 if (loopcxt_is_readonly(lc
)
576 && !(lo_flags
& LO_FLAGS_READ_ONLY
)) {
578 errx(EXIT_FAILURE
, _("%s: overlapping read-only loop device exists"), file
);
581 /* This is no more supported, but check to be safe. */
582 if (loopcxt_get_encrypt_type(lc
, &lc_encrypt_type
) == 0
583 && lc_encrypt_type
!= LO_CRYPT_NONE
) {
585 errx(EXIT_FAILURE
, _("%s: overlapping encrypted loop device exists"), file
);
588 lc
->config
.info
.lo_flags
&= ~LO_FLAGS_AUTOCLEAR
;
589 if (loopcxt_ioctl_status(lc
)) {
591 errx(EXIT_FAILURE
, _("%s: failed to re-use loop device"), file
);
593 return 0; /* success, re-use */
597 errx(EXIT_FAILURE
, _("failed to inspect loop devices"));
603 loopcxt_add_device(lc
);
605 /* losetup --noverlap /dev/loopN file.img */
606 if (hasdev
&& nooverlap
) {
607 struct loopdev_cxt lc2
;
609 if (loopcxt_init(&lc2
, 0)) {
611 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
613 rc
= loopcxt_find_overlap(&lc2
, file
, offset
, sizelimit
);
614 loopcxt_deinit(&lc2
);
619 errx(EXIT_FAILURE
, _("%s: overlapping loop device exists"), file
);
620 err(EXIT_FAILURE
, _("%s: failed to check for conflicting loop devices"), file
);
624 /* Create a new device */
628 /* Note that loopcxt_{find_unused,set_device}() resets
631 if (!hasdev
&& (rc
= find_unused(lc
)))
633 if (flags
& LOOPDEV_FL_OFFSET
)
634 loopcxt_set_offset(lc
, offset
);
635 if (flags
& LOOPDEV_FL_SIZELIMIT
)
636 loopcxt_set_sizelimit(lc
, sizelimit
);
638 loopcxt_set_flags(lc
, lo_flags
);
640 loopcxt_set_blocksize(lc
, blocksize
);
641 if (refname
&& (rc
= loopcxt_set_refname(lc
, refname
))) {
642 warnx(_("cannot set loop reference string"));
645 if ((rc
= loopcxt_set_backing_file(lc
, file
))) {
646 warn(_("%s: failed to use backing file"), file
);
650 rc
= loopcxt_setup_device(lc
);
654 if ((errno
== EBUSY
|| errno
== EAGAIN
) && !hasdev
&& ntries
< 64) {
661 errpre
= hasdev
&& lc
->fd
< 0 ?
662 loopcxt_get_device(lc
) : file
;
663 warn(_("%s: failed to set up loop device"), errpre
);
665 } while (hasdev
== 0);
670 int main(int argc
, char **argv
)
672 struct loopdev_cxt lc
;
673 int act
= 0, flags
= 0, no_overlap
= 0, c
;
674 char *file
= NULL
, *refname
= NULL
;
675 uint64_t offset
= 0, sizelimit
= 0, blocksize
= 0;
676 int res
= 0, showdev
= 0, lo_flags
= 0;
679 unsigned long use_dio
= 0, set_dio
= 0, set_blocksize
= 0;
682 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
689 static const struct option longopts
[] = {
690 { "all", no_argument
, NULL
, 'a' },
691 { "set-capacity", required_argument
, NULL
, 'c' },
692 { "detach", required_argument
, NULL
, 'd' },
693 { "detach-all", no_argument
, NULL
, 'D' },
694 { "find", no_argument
, NULL
, 'f' },
695 { "nooverlap", no_argument
, NULL
, 'L' },
696 { "help", no_argument
, NULL
, 'h' },
697 { "associated", required_argument
, NULL
, 'j' },
698 { "json", no_argument
, NULL
, 'J' },
699 { "list", no_argument
, NULL
, 'l' },
700 { "sector-size", required_argument
, NULL
, 'b' },
701 { "noheadings", no_argument
, NULL
, 'n' },
702 { "offset", required_argument
, NULL
, 'o' },
703 { "output", required_argument
, NULL
, 'O' },
704 { "output-all", no_argument
, NULL
, OPT_OUTPUT_ALL
},
705 { "sizelimit", required_argument
, NULL
, OPT_SIZELIMIT
},
706 { "partscan", no_argument
, NULL
, 'P' },
707 { "read-only", no_argument
, NULL
, 'r' },
708 { "direct-io", optional_argument
, NULL
, OPT_DIO
},
709 { "raw", no_argument
, NULL
, OPT_RAW
},
710 { "loop-ref", required_argument
, NULL
, OPT_REF
, },
711 { "show", no_argument
, NULL
, OPT_SHOW
},
712 { "verbose", no_argument
, NULL
, 'v' },
713 { "version", no_argument
, NULL
, 'V' },
717 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
718 { 'D','a','c','d','f','j' },
719 { 'D','c','d','f','l' },
720 { 'D','c','d','f','O' },
724 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
726 setlocale(LC_ALL
, "");
727 bindtextdomain(PACKAGE
, LOCALEDIR
);
729 close_stdout_atexit();
731 if (loopcxt_init(&lc
, 0))
732 err(EXIT_FAILURE
, _("failed to initialize loopcxt"));
734 while ((c
= getopt_long(argc
, argv
, "ab:c:d:Dfhj:JlLno:O:PrvV",
735 longopts
, NULL
)) != -1) {
737 err_exclusive_options(c
, longopts
, excl
, excl_st
);
745 blocksize
= strtosize_or_err(optarg
, _("failed to parse logical block size"));
748 act
= A_SET_CAPACITY
;
749 if (loopcxt_set_device(&lc
, optarg
))
750 err(EXIT_FAILURE
, _("%s: failed to use device"),
754 lo_flags
|= LO_FLAGS_READ_ONLY
;
761 if (loopcxt_set_device(&lc
, optarg
))
762 err(EXIT_FAILURE
, _("%s: failed to use device"),
791 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
792 flags
|= LOOPDEV_FL_OFFSET
;
800 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
801 columns
[ncolumns
] = ncolumns
;
804 lo_flags
|= LO_FLAGS_PARTSCAN
;
810 use_dio
= set_dio
= 1;
812 use_dio
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
814 lo_flags
|= LO_FLAGS_DIRECT_IO
;
818 case OPT_SIZELIMIT
: /* --sizelimit */
819 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
820 flags
|= LOOPDEV_FL_SIZELIMIT
;
826 print_version(EXIT_SUCCESS
);
828 errtryhelp(EXIT_FAILURE
);
832 ul_path_init_debug();
833 ul_sysfs_init_debug();
835 /* default is --list --all */
841 if (!act
&& argc
== 2 && (raw
|| json
)) {
846 /* default --list output columns */
847 if (list
&& !ncolumns
) {
848 columns
[ncolumns
++] = COL_NAME
;
849 columns
[ncolumns
++] = COL_SIZELIMIT
;
850 columns
[ncolumns
++] = COL_OFFSET
;
851 columns
[ncolumns
++] = COL_AUTOCLR
;
852 columns
[ncolumns
++] = COL_RO
;
853 columns
[ncolumns
++] = COL_BACK_FILE
;
854 columns
[ncolumns
++] = COL_DIO
;
855 columns
[ncolumns
++] = COL_LOGSEC
;
858 if (act
== A_FIND_FREE
&& optind
< argc
) {
860 * losetup -f <backing_file>
863 file
= argv
[optind
++];
866 errx(EXIT_FAILURE
, _("unexpected arguments"));
869 if (list
&& !act
&& optind
== argc
)
871 * losetup --list defaults to --all
875 if (!act
&& optind
+ 1 == argc
) {
877 * losetup [--list] <device>
879 * losetup {--direct-io[=off]|--logical-blocksize=size}... <device>
882 act
= A_SET_DIRECT_IO
;
883 lo_flags
&= ~LO_FLAGS_DIRECT_IO
;
884 } else if (set_blocksize
)
885 act
= A_SET_BLOCKSIZE
;
889 if (loopcxt_set_device(&lc
, argv
[optind
]))
890 err(EXIT_FAILURE
, _("%s: failed to use device"),
896 * losetup <loopdev> <backing_file>
901 errx(EXIT_FAILURE
, _("no loop device specified"));
902 /* don't use is_loopdev() here, the device does not have exist yet */
903 if (loopcxt_set_device(&lc
, argv
[optind
]))
904 err(EXIT_FAILURE
, _("%s: failed to use device"),
909 errx(EXIT_FAILURE
, _("no file specified"));
910 file
= argv
[optind
++];
913 if (act
!= A_CREATE
&&
914 (sizelimit
|| lo_flags
|| showdev
))
916 _("the options %s are allowed during loop device setup only"),
917 "--{sizelimit,partscan,read-only,show}");
919 if ((flags
& LOOPDEV_FL_OFFSET
) &&
920 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
921 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context"));
923 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
924 &ncolumns
, column_name_to_id
) < 0)
929 res
= create_loop(&lc
, no_overlap
, lo_flags
, flags
, file
, refname
,
930 offset
, sizelimit
, blocksize
);
933 printf("%s\n", loopcxt_get_device(&lc
));
934 warn_size(file
, sizelimit
, offset
, flags
);
938 res
= delete_loop(&lc
);
939 while (optind
< argc
) {
940 if (loopcxt_set_device(&lc
, argv
[optind
]))
941 warn(_("%s: failed to use device"),
944 res
+= delete_loop(&lc
);
948 res
= delete_all_loops(&lc
);
951 res
= find_unused(&lc
);
953 printf("%s\n", loopcxt_get_device(&lc
));
957 res
= show_table(&lc
, file
, offset
, flags
);
959 res
= show_all_loops(&lc
, file
, offset
, flags
);
963 res
= show_table(&lc
, NULL
, 0, 0);
965 res
= printf_loopdev(&lc
);
967 warn("%s", loopcxt_get_device(&lc
));
970 res
= loopcxt_ioctl_capacity(&lc
);
972 warn(_("%s: set capacity failed"),
973 loopcxt_get_device(&lc
));
975 case A_SET_DIRECT_IO
:
976 res
= loopcxt_ioctl_dio(&lc
, use_dio
);
978 warn(_("%s: set direct io failed"),
979 loopcxt_get_device(&lc
));
981 case A_SET_BLOCKSIZE
:
982 res
= loopcxt_ioctl_blocksize(&lc
, blocksize
);
984 warn(_("%s: set logical block size failed"),
985 loopcxt_get_device(&lc
));
988 warnx(_("bad usage"));
989 errtryhelp(EXIT_FAILURE
);
993 if (res
&& (act
== A_SET_CAPACITY
994 || act
== A_SET_DIRECT_IO
995 || act
== A_SET_BLOCKSIZE
)
996 && loopcxt_is_lost(&lc
))
1000 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;