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>
23 A_CREATE
= 1, /* setup a new device */
24 A_DELETE
, /* delete given device(s) */
25 A_DELETE_ALL
, /* delete all devices */
26 A_SHOW
, /* list devices */
27 A_SHOW_ONE
, /* print info about one device */
28 A_FIND_FREE
, /* find first unused */
29 A_SET_CAPACITY
, /* set device capacity */
35 * A function to read the passphrase either from the terminal or from
36 * an open file descriptor.
38 static char *xgetpass(int pfd
, const char *prompt
)
43 if (pfd
< 0) /* terminal */
44 return getpass(prompt
);
50 /* we're running out of space in the buffer.
54 pass
= realloc(tmppass
, buflen
);
56 /* realloc failed. Stop reading. */
57 warn(_("Out of memory while reading passphrase"));
58 pass
= tmppass
; /* the old buffer hasn't changed */
62 if (read(pfd
, pass
+i
, 1) != 1 ||
63 pass
[i
] == '\n' || pass
[i
] == 0)
74 static int printf_loopdev(struct loopdev_cxt
*lc
)
82 fname
= loopcxt_get_backing_file(lc
);
86 if (loopcxt_get_backing_devno(lc
, &dev
) == 0)
87 loopcxt_get_backing_inode(lc
, &ino
);
91 * Probably non-root user (no permissions to
92 * call LOOP_GET_STATUS ioctls).
94 printf("%s: []: (%s)",
95 loopcxt_get_device(lc
), fname
);
97 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
98 printf(_(", offset %ju"), x
);
100 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
101 printf(_(", sizelimit %ju"), x
);
106 printf("%s: [%04d]:%" PRIu64
" (%s)",
107 loopcxt_get_device(lc
), dev
, ino
, fname
);
109 if (loopcxt_get_offset(lc
, &x
) == 0 && x
)
110 printf(_(", offset %ju"), x
);
112 if (loopcxt_get_sizelimit(lc
, &x
) == 0 && x
)
113 printf(_(", sizelimit %ju"), x
);
115 if (loopcxt_get_encrypt_type(lc
, &type
) == 0) {
116 const char *e
= loopcxt_get_crypt_name(lc
);
118 if ((!e
|| !*e
) && type
== 1)
121 printf(_(", encryption %s (type %ju)"), e
, type
);
127 static int show_all_loops(struct loopdev_cxt
*lc
, const char *file
,
128 uint64_t offset
, int flags
)
130 struct stat sbuf
, *st
= &sbuf
;
132 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
135 if (!file
|| stat(file
, st
))
138 while (loopcxt_next(lc
) == 0) {
140 if (file
&& !loopcxt_is_used(lc
, st
, file
, offset
, flags
))
145 loopcxt_deinit_iterator(lc
);
149 static int set_capacity(struct loopdev_cxt
*lc
)
151 int fd
= loopcxt_get_fd(lc
);
154 warn(_("%s: open failed"), loopcxt_get_device(lc
));
155 else if (ioctl(fd
, LOOP_SET_CAPACITY
) != 0)
156 warnx(_("%s: set capacity failed"), loopcxt_get_device(lc
));
163 static int delete_loop(struct loopdev_cxt
*lc
)
165 if (loopcxt_delete_device(lc
))
166 warn(_("%s: detach failed"), loopcxt_get_device(lc
));
173 static int delete_all_loops(struct loopdev_cxt
*lc
)
177 if (loopcxt_init_iterator(lc
, LOOPITER_FL_USED
))
180 while (loopcxt_next(lc
) == 0)
181 res
+= delete_loop(lc
);
183 loopcxt_deinit_iterator(lc
);
187 static void usage(FILE *out
)
189 fputs(USAGE_HEADER
, out
);
192 _(" %1$s [options] [<loopdev>]\n"
193 " %1$s [options] -f | <loopdev> <file>\n"),
194 program_invocation_short_name
);
196 fputs(USAGE_OPTIONS
, out
);
197 fputs(_(" -a, --all list all used devices\n"
198 " -d, --detach <loopdev> [...] detach one or more devices\n"
199 " -D, --detach-all detach all used devices\n"
200 " -f, --find find first unused device\n"
201 " -c, --set-capacity <loopdev> resize device\n"
202 " -j, --associated <file> list all devices associated with <file>\n"), out
);
203 fputs(USAGE_SEPARATOR
, out
);
205 fputs(_(" -e, --encryption <type> enable encryption with specified <name/num>\n"
206 " -o, --offset <num> start at offset <num> into file\n"
207 " --sizelimit <num> device limited to <num> bytes of the file\n"
208 " -p, --pass-fd <num> read passphrase from file descriptor <num>\n"
209 " -r, --read-only setup read-only loop device\n"
210 " --show print device name after setup (with -f)\n"
211 " -v, --verbose verbose mode\n"), out
);
213 fputs(USAGE_SEPARATOR
, out
);
214 fputs(USAGE_HELP
, out
);
215 fputs(USAGE_VERSION
, out
);
217 fprintf(out
, USAGE_MAN_TAIL("losetup(8)"));
219 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
222 int main(int argc
, char **argv
)
224 struct loopdev_cxt lc
;
225 int act
= 0, flags
= 0, passfd
= -1, c
;
226 char *file
= NULL
, *encryption
= NULL
;
227 uint64_t offset
= 0, sizelimit
= 0;
228 int res
= 0, showdev
= 0, lo_flags
= 0;
231 OPT_SIZELIMIT
= CHAR_MAX
+ 1,
234 static const struct option longopts
[] = {
235 { "all", 0, 0, 'a' },
236 { "set-capacity", 1, 0, 'c' },
237 { "detach", 1, 0, 'd' },
238 { "detach-all", 0, 0, 'D' },
239 { "encryption", 1, 0, 'e' },
240 { "find", 0, 0, 'f' },
241 { "help", 0, 0, 'h' },
242 { "associated", 1, 0, 'j' },
243 { "offset", 1, 0, 'o' },
244 { "sizelimit", 1, 0, OPT_SIZELIMIT
},
245 { "pass-fd", 1, 0, 'p' },
246 { "read-only", 0, 0, 'r' },
247 { "show", 0, 0, OPT_SHOW
},
248 { "verbose", 0, 0, 'v' },
249 { "version", 0, 0, 'V' },
253 setlocale(LC_ALL
, "");
254 bindtextdomain(PACKAGE
, LOCALEDIR
);
257 loopcxt_init(&lc
, 0);
258 loopcxt_enable_debug(&lc
, getenv("LOOPDEV_DEBUG") ? TRUE
: FALSE
);
260 while ((c
= getopt_long(argc
, argv
, "ac:d:De:E:fhj:o:p:rvV",
261 longopts
, NULL
)) != -1) {
263 if (act
&& strchr("acdDfj", c
))
265 _("the options %s are mutually exclusive"),
266 "--{all,associated,set-capacity,detach,detach-all,find}");
273 act
= A_SET_CAPACITY
;
274 loopcxt_set_device(&lc
, optarg
);
277 lo_flags
|= LO_FLAGS_READ_ONLY
;
281 loopcxt_set_device(&lc
, optarg
);
301 if (strtosize(optarg
, &offset
))
303 _("invalid offset '%s' specified"), optarg
);
304 flags
|= LOOPDEV_FL_OFFSET
;
307 passfd
= strtol_or_err(optarg
,
308 _("invalid passphrase file descriptor"));
317 printf(UTIL_LINUX_VERSION
);
319 case OPT_SIZELIMIT
: /* --sizelimit */
320 if (strtosize(optarg
, &sizelimit
))
322 _("invalid size '%s' specified"), optarg
);
323 flags
|= LOOPDEV_FL_SIZELIMIT
;
333 if (act
== A_FIND_FREE
&& optind
< argc
) {
335 * losetup -f <backing_file>
338 file
= argv
[optind
++];
340 if (!act
&& optind
+ 1 == argc
) {
345 loopcxt_set_device(&lc
, argv
[optind
++]);
349 * losetup <loopdev> <backing_file>
354 errx(EXIT_FAILURE
, _("no loop device specified"));
355 loopcxt_set_device(&lc
, argv
[optind
++]);
358 errx(EXIT_FAILURE
, _("no file specified"));
359 file
= argv
[optind
++];
362 if (act
!= A_CREATE
&&
363 (encryption
|| sizelimit
|| passfd
!= -1 || lo_flags
|| showdev
))
365 _("the options %s are allowed to loop device setup only"),
366 "--{encryption,sizelimit,pass-fd,read-only,show}");
368 if ((flags
& LOOPDEV_FL_OFFSET
) &&
369 act
!= A_CREATE
&& (act
!= A_SHOW
|| !file
))
370 errx(EXIT_FAILURE
, _("the option --offset is not allowed in this context."));
376 int hasdev
= loopcxt_has_device(&lc
);
380 if(mlockall(MCL_CURRENT
| MCL_FUTURE
))
381 err(EXIT_FAILURE
, _("couldn't lock into memory"));
383 pass
= xgetpass(passfd
, _("Password: "));
386 /* Note that loopcxt_{find_unused,set_device}() resets
389 if (!hasdev
&& (res
= loopcxt_find_unused(&lc
))) {
390 warnx(_("not found unused device"));
393 if (encryption
&& pass
)
394 loopcxt_set_encryption(&lc
, encryption
, pass
);
395 if (flags
& LOOPDEV_FL_OFFSET
)
396 loopcxt_set_offset(&lc
, offset
);
397 if (flags
& LOOPDEV_FL_SIZELIMIT
)
398 loopcxt_set_offset(&lc
, sizelimit
);
400 loopcxt_set_flags(&lc
, lo_flags
);
401 if ((res
= loopcxt_set_backing_file(&lc
, file
))) {
402 warn(_("%s: failed to use backing file"), file
);
406 res
= loopcxt_setup_device(&lc
);
409 if (errno
!= EBUSY
) {
410 warn(_("failed to setup loop device"));
413 } while (hasdev
== 0);
417 if (showdev
&& res
== 0)
418 printf("%s\n", loopcxt_get_device(&lc
));
422 res
= delete_loop(&lc
);
423 while (optind
< argc
) {
424 loopcxt_set_device(&lc
, argv
[optind
++]);
425 res
+= delete_loop(&lc
);
429 res
= delete_all_loops(&lc
);
432 if (loopcxt_find_unused(&lc
))
433 warn(_("find unused loop device failed"));
435 printf("%s\n", loopcxt_get_device(&lc
));
438 res
= show_all_loops(&lc
, file
, offset
, flags
);
441 res
= printf_loopdev(&lc
);
443 warn(_("%s"), loopcxt_get_device(&lc
));
446 res
= set_capacity(&lc
);
454 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;