2 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
3 * Originally from Ted's losetup.c
5 * losetup.c - setup and control loop devices
12 #include <sys/ioctl.h>
22 #include "closestream.h"
25 A_CREATE
= 1, /* setup a new device */
26 A_DELETE
, /* delete given device(s) */
27 A_DELETE_ALL
, /* delete all devices */
28 A_SHOW
, /* list devices */
29 A_SHOW_ONE
, /* print info about one device */
30 A_FIND_FREE
, /* find first unused */
31 A_SET_CAPACITY
, /* set device capacity */
37 static int printf_loopdev(struct loopdev_cxt
*lc
)
45 fname
= loopcxt_get_backing_file(lc
);
49 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
50 loopcxt_get_backing_inode(lc
, &ino
);
54 * Probably non-root user (no permissions to
55 * call LOOP_GET_STATUS ioctls).
57 printf("%s: []: (%s)",
58 loopcxt_get_device(lc
), fname
);
60 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
61 printf(_(", offset %ju"), x
);
63 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
64 printf(_(", sizelimit %ju"), x
);
69 printf("%s: [%04d]:%" PRIu64
" (%s)",
70 loopcxt_get_device(lc
), (int) dev
, ino
, fname
);
72 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
73 printf(_(", offset %ju"), x
);
75 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
76 printf(_(", sizelimit %ju"), x
);
78 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
79 const char *e
= loopcxt_get_crypt_name(lc
);
81 if ((!e
|| !*e
) && type
== 1)
84 printf(_(", encryption %s (type %u)"), e
, type
);
90 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
91 uint64_t offset
, int flags
)
93 struct stat sbuf
, *st
= &sbuf
;
95 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
98 if (!file
|| stat(file
, st
))
101 while (loopcxt_next(lc
) == 0) {
103 if (file
&& !loopcxt_is_used(lc
, st
, file
, offset
, flags
))
108 loopcxt_deinit_iterator(lc
);
112 static int set_capacity(struct loopdev_cxt
*lc
)
114 int fd
= loopcxt_get_fd(lc
);
117 warn(_("%s: open failed"), loopcxt_get_device(lc
));
118 else if (ioctl(fd
, LOOP_SET_CAPACITY
) != 0)
119 warnx(_("%s: set capacity failed"), loopcxt_get_device(lc
));
126 static int delete_loop(struct loopdev_cxt
*lc
)
128 if (loopcxt_delete_device(lc
))
129 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
136 static int delete_all_loops(struct loopdev_cxt
*lc
)
140 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
143 while (loopcxt_next(lc
) == 0)
144 res
+= delete_loop(lc
);
146 loopcxt_deinit_iterator(lc
);
150 static void usage(FILE *out
)
152 fputs(USAGE_HEADER
, out
);
155 _(" %1$s [options] [<loopdev>]\n"
156 " %1$s [options] -f | <loopdev> <file>\n"),
157 program_invocation_short_name
);
159 fputs(USAGE_OPTIONS
, out
);
160 fputs(_(" -a, --all list all used devices\n"
161 " -d, --detach <loopdev> [...] detach one or more devices\n"
162 " -D, --detach-all detach all used devices\n"
163 " -f, --find find first unused device\n"
164 " -c, --set-capacity <loopdev> resize device\n"
165 " -j, --associated <file> list all devices associated with <file>\n"), out
);
166 fputs(USAGE_SEPARATOR
, out
);
168 fputs(_(" -e, --encryption <type> enable encryption with specified <name/num>\n"
169 " -o, --offset <num> start at offset <num> into file\n"
170 " --sizelimit <num> device limited to <num> bytes of the file\n"
171 " -p, --pass-fd <num> read passphrase from file descriptor <num>\n"
172 " -P, --partscan create partitioned loop device\n"
173 " -r, --read-only setup read-only loop device\n"
174 " --show print device name after setup (with -f)\n"
175 " -v, --verbose verbose mode\n"), out
);
177 fputs(USAGE_SEPARATOR
, out
);
178 fputs(USAGE_HELP
, out
);
179 fputs(USAGE_VERSION
, out
);
181 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
183 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
186 int main(int argc
, char **argv
)
188 struct loopdev_cxt lc
;
189 int act
= 0, flags
= 0, passfd
= -1, c
;
190 char *file
= NULL
, *encryption
= NULL
;
191 uint64_t offset
= 0, sizelimit
= 0;
192 int res
= 0, showdev
= 0, lo_flags
= 0;
195 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
198 static const struct option longopts
[] = {
199 { "all", 0, 0, 'a' },
200 { "set-capacity", 1, 0, 'c' },
201 { "detach", 1, 0, 'd' },
202 { "detach-all", 0, 0, 'D' },
203 { "encryption", 1, 0, 'e' },
204 { "find", 0, 0, 'f' },
205 { "help", 0, 0, 'h' },
206 { "associated", 1, 0, 'j' },
207 { "offset", 1, 0, 'o' },
208 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
209 { "pass-fd", 1, 0, 'p' },
210 { "partscan", 0, 0, 'P' },
211 { "read-only", 0, 0, 'r' },
212 { "show", 0, 0, OPT_SHOW
},
213 { "verbose", 0, 0, 'v' },
214 { "version", 0, 0, 'V' },
218 setlocale(LC_ALL
, "");
219 bindtextdomain(PACKAGE
, LOCALEDIR
);
221 atexit(close_stdout
);
223 loopcxt_init(&lc
, 0);
224 loopcxt_enable_debug(&lc
, getenv("LOOPDEV_DEBUG") ? TRUE
: FALSE
);
226 while ((c
= getopt_long(argc
, argv
, "ac:d:De:E:fhj:o:p:PrvV",
227 longopts
, NULL
)) != -1) {
229 if (act
&& strchr("acdDfj", c
))
231 _("the options %s are mutually exclusive"),
232 "--{all,associated,set-capacity,detach,detach-all,find}");
239 act
= A_SET_CAPACITY
;
240 loopcxt_set_device(&lc
, optarg
);
243 lo_flags
|= LO_FLAGS_READ_ONLY
;
247 loopcxt_set_device(&lc
, optarg
);
267 offset
= strtosize_or_err(optarg
, _("failed to parse offset"));
268 flags
|= LOOPDEV_FL_OFFSET
;
271 passfd
= strtou32_or_err(optarg
,
272 _("invalid passphrase file descriptor"));
275 lo_flags
|= LO_FLAGS_PARTSCAN
;
284 printf(UTIL_LINUX_VERSION
);
286 case OPT_SIZELIMIT
: /* --sizelimit */
287 sizelimit
= strtosize_or_err(optarg
, _("failed to parse size"));
288 flags
|= LOOPDEV_FL_SIZELIMIT
;
298 if (act
== A_FIND_FREE
&& optind
< argc
) {
300 * losetup -f <backing_file>
303 file
= argv
[optind
++];
305 if (!act
&& optind
+ 1 == argc
) {
310 loopcxt_set_device(&lc
, argv
[optind
++]);
314 * losetup <loopdev> <backing_file>
319 errx(EXIT_FAILURE
, _("no loop device specified"));
320 loopcxt_set_device(&lc
, argv
[optind
++]);
323 errx(EXIT_FAILURE
, _("no file specified"));
324 file
= argv
[optind
++];
327 if (act
!= A_CREATE
&&
328 (encryption
|| sizelimit
|| passfd
!= -1 || lo_flags
|| showdev
))
330 _("the options %s are allowed to loop device setup only"),
331 "--{encryption,sizelimit,pass-fd,read-only,show}");
333 if ((flags
& LOOPDEV_FL_OFFSET
) &&
334 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
335 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context."));
341 int hasdev
= loopcxt_has_device(&lc
);
345 if(mlockall(MCL_CURRENT
| MCL_FUTURE
))
346 err(EXIT_FAILURE
, _("couldn't lock into memory"));
348 pass
= xgetpass(passfd
, _("Password: "));
351 /* Note that loopcxt_{find_unused,set_device}() resets
354 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
355 warnx(_("not found unused device"));
358 if (encryption
&& pass
)
359 loopcxt_set_encryption(&lc
, encryption
, pass
);
360 if (flags
& LOOPDEV_FL_OFFSET
)
361 loopcxt_set_offset(&lc
, offset
);
362 if (flags
& LOOPDEV_FL_SIZELIMIT
)
363 loopcxt_set_sizelimit(&lc
, sizelimit
);
365 loopcxt_set_flags(&lc
, lo_flags
);
366 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
367 warn(_("%s: failed to use backing file"), file
);
371 res
= loopcxt_setup_device(&lc
);
374 if (errno
!= EBUSY
) {
375 warn(_("%s: failed to setup loop device"),
376 hasdev
&& loopcxt_get_fd(&lc
) < 0 ?
377 loopcxt_get_device(&lc
) : file
);
380 } while (hasdev
== 0);
384 if (showdev
&& res
== 0)
385 printf("%s\n", loopcxt_get_device(&lc
));
389 res
= delete_loop(&lc
);
390 while (optind
< argc
) {
391 loopcxt_set_device(&lc
, argv
[optind
++]);
392 res
+= delete_loop(&lc
);
396 res
= delete_all_loops(&lc
);
399 if (loopcxt_find_unused(&lc
))
400 warn(_("find unused loop device failed"));
402 printf("%s\n", loopcxt_get_device(&lc
));
405 res
= show_all_loops(&lc
, file
, offset
, flags
);
408 res
= printf_loopdev(&lc
);
410 warn(_("%s"), loopcxt_get_device(&lc
));
413 res
= set_capacity(&lc
);
421 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;