2 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
3 * Originally from Ted's losetup.c
5 * losetup.c - setup and control loop devices
14 #include <sys/ioctl.h>
17 #include <sys/sysmacros.h>
25 #include "pathnames.h"
28 #include "canonicalize.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 */
43 * A function to read the passphrase either from the terminal or from
44 * an open file descriptor.
46 static char *xgetpass(int pfd
, const char *prompt
)
51 if (pfd
< 0) /* terminal */
52 return getpass(prompt
);
58 /* we're running out of space in the buffer.
62 pass
= realloc(tmppass
, buflen
);
64 /* realloc failed. Stop reading. */
65 warn(_("Out of memory while reading passphrase"));
66 pass
= tmppass
; /* the old buffer hasn't changed */
70 if (read(pfd
, pass
+i
, 1) != 1 ||
71 pass
[i
] == '\n' || pass
[i
] == 0)
82 static int printf_loopdev(struct loopdev_cxt
*lc
)
90 fname
= loopcxt_get_backing_file(lc
);
94 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
95 loopcxt_get_backing_inode(lc
, &ino
);
99 * Probably non-root user (no permissions to
100 * call LOOP_GET_STATUS ioctls).
102 printf("%s: []: (%s)",
103 loopcxt_get_device(lc
), fname
);
105 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
106 printf(_(", offset %ju"), x
);
108 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
109 printf(_(", sizelimit %ju"), x
);
114 printf("%s: [%04d]:%" PRIu64
" (%s)",
115 loopcxt_get_device(lc
), dev
, ino
, fname
);
117 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
118 printf(_(", offset %ju"), x
);
120 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
121 printf(_(", sizelimit %ju"), x
);
123 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
124 const char *e
= loopcxt_get_crypt_name(lc
);
126 if ((!e
|| !*e
) && type
== 1)
129 printf(_(", encryption %s (type %ju)"), e
, type
);
135 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
136 uint64_t offset
, int flags
)
138 struct stat sbuf
, *st
= &sbuf
;
140 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
143 if (!file
|| stat(file
, st
))
146 while (loopcxt_next(lc
) == 0) {
148 if (file
&& !loopcxt_is_used(lc
, st
, file
, offset
, flags
))
153 loopcxt_deinit_iterator(lc
);
157 static int set_capacity(struct loopdev_cxt
*lc
)
159 int fd
= loopcxt_get_fd(lc
);
162 warn(_("%s: open failed"), loopcxt_get_device(lc
));
163 else if (ioctl(fd
, LOOP_SET_CAPACITY
) != 0)
164 warnx(_("%s: set capacity failed"), loopcxt_get_device(lc
));
171 static int delete_loop(struct loopdev_cxt
*lc
)
173 if (loopcxt_delete_device(lc
))
174 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
181 static int delete_all_loops(struct loopdev_cxt
*lc
)
185 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
188 while (loopcxt_next(lc
) == 0)
189 res
+= delete_loop(lc
);
191 loopcxt_deinit_iterator(lc
);
195 static void usage(FILE *out
)
197 fputs(USAGE_HEADER
, out
);
200 _(" %1$s [options] [<loopdev>]\n"
201 " %1$s [options] -f | <loopdev> <file>\n"),
202 program_invocation_short_name
);
204 fputs(USAGE_OPTIONS
, out
);
205 fputs(_(" -a, --all list all used devices\n"
206 " -d, --detach <loopdev> [...] detach one or more devices\n"
207 " -D, --detach-all detach all used devices\n"
208 " -f, --find find first unused device\n"
209 " -c, --set-capacity <loopdev> resize device\n"
210 " -j, --associated <file> list all devices associated with <file>\n"), out
);
211 fputs(USAGE_SEPARATOR
, out
);
213 fputs(_(" -e, --encryption <type> enable encryption with specified <name/num>\n"
214 " -o, --offset <num> start at offset <num> into file\n"
215 " --sizelimit <num> device limited to <num> bytes of the file\n"
216 " -p, --pass-fd <num> read passphrase from file descriptor <num>\n"
217 " -r, --read-only setup read-only loop device\n"
218 " --show print device name after setup (with -f)\n"
219 " -v, --verbose verbose mode\n"), out
);
221 fputs(USAGE_SEPARATOR
, out
);
222 fputs(USAGE_HELP
, out
);
223 fputs(USAGE_VERSION
, out
);
225 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
227 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
230 int main(int argc
, char **argv
)
232 struct loopdev_cxt lc
;
233 int act
= 0, flags
= 0, passfd
= -1, c
;
234 char *file
= NULL
, *encryption
= NULL
;
235 uint64_t offset
= 0, sizelimit
= 0;
236 int res
= 0, showdev
= 0, lo_flags
= 0;
239 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
242 static const struct option longopts
[] = {
243 { "all", 0, 0, 'a' },
244 { "set-capacity", 1, 0, 'c' },
245 { "detach", 1, 0, 'd' },
246 { "detach-all", 0, 0, 'D' },
247 { "encryption", 1, 0, 'e' },
248 { "find", 0, 0, 'f' },
249 { "help", 0, 0, 'h' },
250 { "associated", 1, 0, 'j' },
251 { "offset", 1, 0, 'o' },
252 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
253 { "pass-fd", 1, 0, 'p' },
254 { "read-only", 0, 0, 'r' },
255 { "show", 0, 0, OPT_SHOW
},
256 { "verbose", 0, 0, 'v' },
257 { "version", 0, 0, 'V' },
261 setlocale(LC_ALL
, "");
262 bindtextdomain(PACKAGE
, LOCALEDIR
);
265 loopcxt_init(&lc
, 0);
266 loopcxt_enable_debug(&lc
, getenv("LOOPDEV_DEBUG") ? TRUE
: FALSE
);
268 while ((c
= getopt_long(argc
, argv
, "ac:d:De:E:fhj:o:p:rvV",
269 longopts
, NULL
)) != -1) {
271 if (act
&& strchr("acdDfj", c
))
273 _("the options %s are mutually exclusive"),
274 "--{all,associated,set-capacity,detach,detach-all,find}");
281 act
= A_SET_CAPACITY
;
282 loopcxt_set_device(&lc
, optarg
);
285 lo_flags
|= LO_FLAGS_READ_ONLY
;
289 loopcxt_set_device(&lc
, optarg
);
309 if (strtosize(optarg
, &offset
))
311 _("invalid offset '%s' specified"), optarg
);
312 flags
|= LOOPDEV_FL_OFFSET
;
315 passfd
= strtol_or_err(optarg
,
316 _("invalid passphrase file descriptor"));
325 printf(UTIL_LINUX_VERSION
);
327 case OPT_SIZELIMIT
: /* --sizelimit */
328 if (strtosize(optarg
, &sizelimit
))
330 _("invalid size '%s' specified"), optarg
);
331 flags
|= LOOPDEV_FL_SIZELIMIT
;
341 if (act
== A_FIND_FREE
&& optind
< argc
) {
343 * losetup -f <backing_file>
346 file
= argv
[optind
++];
348 if (!act
&& optind
+ 1 == argc
) {
353 loopcxt_set_device(&lc
, argv
[optind
++]);
357 * losetup <loopdev> <backing_file>
362 errx(EXIT_FAILURE
, _("no loop device specified"));
363 loopcxt_set_device(&lc
, argv
[optind
++]);
366 errx(EXIT_FAILURE
, _("no file specified"));
367 file
= argv
[optind
++];
370 if (act
!= A_CREATE
&&
371 (encryption
|| sizelimit
|| passfd
!= -1 || lo_flags
|| showdev
))
373 _("the options %s are allowed to loop device setup only"),
374 "--{encryption,sizelimit,pass-fd,read-only,show}");
376 if (act
!= A_CREATE
&& act
!= A_SHOW
&& (flags
& LOOPDEV_FL_OFFSET
))
377 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context."));
383 int hasdev
= loopcxt_has_device(&lc
);
387 if(mlockall(MCL_CURRENT
| MCL_FUTURE
))
388 err(EXIT_FAILURE
, _("couldn't lock into memory"));
390 pass
= xgetpass(passfd
, _("Password: "));
393 /* Note that loopcxt_{find_unused,set_device}() resets
396 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
397 warnx(_("not found unused device"));
400 if (encryption
&& pass
)
401 loopcxt_set_encryption(&lc
, encryption
, pass
);
402 if (flags
& LOOPDEV_FL_OFFSET
)
403 loopcxt_set_offset(&lc
, offset
);
404 if (flags
& LOOPDEV_FL_SIZELIMIT
)
405 loopcxt_set_offset(&lc
, sizelimit
);
407 loopcxt_set_flags(&lc
, lo_flags
);
408 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
409 warn(_("%s: failed to use backing file"), file
);
413 res
= loopcxt_setup_device(&lc
);
416 if (errno
!= EBUSY
) {
417 warn(_("failed to setup loop device"));
420 } while (hasdev
== 0);
424 if (showdev
&& res
== 0)
425 printf("%s\n", loopcxt_get_device(&lc
));
429 res
= delete_loop(&lc
);
430 while (optind
< argc
) {
431 loopcxt_set_device(&lc
, argv
[optind
++]);
432 res
+= delete_loop(&lc
);
436 res
= delete_all_loops(&lc
);
439 if (loopcxt_find_unused(&lc
))
440 warn(_("find unused loop device failed"));
442 printf("%s\n", loopcxt_get_device(&lc
));
445 res
= show_all_loops(&lc
, file
, offset
, flags
);
448 res
= printf_loopdev(&lc
);
450 warn(_("%s"), loopcxt_get_device(&lc
));
453 res
= set_capacity(&lc
);
461 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;