blob: 9b7c49f503599ba07dace4fe9f1a21e595ddb9ab [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Matt Kraai83788da2002-03-20 17:38:37 +00002/*
3 * Mini losetup implementation for busybox
4 *
5 * Copyright (C) 2002 Matt Kraai.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Matt Kraai83788da2002-03-20 17:38:37 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define losetup_trivial_usage
11//usage: "[-o OFS] LOOPDEV FILE - associate loop devices\n"
12//usage: " losetup -d LOOPDEV - disassociate\n"
13//usage: " losetup [-f] - show"
14//usage:#define losetup_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020015//usage: " -o OFS Start OFS bytes into FILE"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -f Show first free loop device"
17//usage:
18//usage:#define losetup_notes_usage
19//usage: "No arguments will display all current associations.\n"
20//usage: "One argument (losetup /dev/loop1) will display the current association\n"
21//usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
22//usage: "and filename of the file the loop device is currently bound to.\n\n"
23//usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
24//usage: "with an optional offset (-o 12345). Encryption is not yet supported.\n"
25//usage: "losetup -f will show the first loop free loop device\n\n"
26
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Matt Kraai83788da2002-03-20 17:38:37 +000028
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000029int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko92510142010-05-19 00:39:17 +020030int losetup_main(int argc UNUSED_PARAM, char **argv)
Matt Kraai83788da2002-03-20 17:38:37 +000031{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000032 unsigned opt;
Denys Vlasenko92510142010-05-19 00:39:17 +020033 int n;
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000034 char *opt_o;
Denis Vlasenko13858992006-10-08 12:49:22 +000035 unsigned long long offset = 0;
Denys Vlasenko92510142010-05-19 00:39:17 +020036 enum {
37 OPT_d = (1 << 0),
38 OPT_o = (1 << 1),
39 OPT_f = (1 << 2),
40 };
Matt Kraai83788da2002-03-20 17:38:37 +000041
Bernhard Reutner-Fischer1d4cd602008-07-21 15:29:36 +000042 /* max 2 args, all opts are mutually exclusive */
Pascal Bellardb82b34e2010-06-07 01:16:45 +020043 opt_complementary = "?2:d--of:o--df:f--do";
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000044 opt = getopt32(argv, "do:f", &opt_o);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000045 argv += optind;
Matt Kraai83788da2002-03-20 17:38:37 +000046
Denys Vlasenko92510142010-05-19 00:39:17 +020047 if (opt == OPT_o)
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000048 offset = xatoull(opt_o);
49
Denys Vlasenko92510142010-05-19 00:39:17 +020050 if (opt == OPT_d) {
51 /* -d BLOCKDEV */
52 if (!argv[0] || argv[1])
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000053 bb_show_usage();
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000054 if (del_loop(argv[0]))
Denis Vlasenko94e33652007-12-22 15:44:23 +000055 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000056 return EXIT_SUCCESS;
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000057 }
58
Denys Vlasenko92510142010-05-19 00:39:17 +020059 if (argv[0]) {
60 char *s;
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000061
Denys Vlasenko92510142010-05-19 00:39:17 +020062 if (opt == OPT_f) /* -f should not have arguments */
63 bb_show_usage();
64
65 if (argv[1]) {
66 /* [-o OFS] BLOCKDEV FILE */
67 if (set_loop(&argv[0], argv[1], offset) < 0)
68 bb_simple_perror_msg_and_die(argv[0]);
69 return EXIT_SUCCESS;
70 }
71 /* [-o OFS] BLOCKDEV */
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000072 s = query_loop(argv[0]);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000073 if (!s)
Denis Vlasenko94e33652007-12-22 15:44:23 +000074 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000075 printf("%s: %s\n", argv[0], s);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000076 if (ENABLE_FEATURE_CLEAN_UP)
77 free(s);
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000078 return EXIT_SUCCESS;
79 }
80
Denys Vlasenko92510142010-05-19 00:39:17 +020081 /* [-o OFS|-f] with no params */
82 n = 0;
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000083 while (1) {
Denys Vlasenko92510142010-05-19 00:39:17 +020084 char *s;
Lauri Kasanen7c1b2b52010-07-04 00:04:03 +020085 char dev[LOOP_NAMESIZE];
Denys Vlasenko92510142010-05-19 00:39:17 +020086
Lauri Kasanen7c1b2b52010-07-04 00:04:03 +020087 sprintf(dev, LOOP_FORMAT, n);
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000088 s = query_loop(dev);
Denys Vlasenko92510142010-05-19 00:39:17 +020089 n++;
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000090 if (!s) {
Denys Vlasenko92510142010-05-19 00:39:17 +020091 if (n > 9 && errno && errno != ENXIO)
92 return EXIT_SUCCESS;
93 if (opt == OPT_f) {
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +000094 puts(dev);
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000095 return EXIT_SUCCESS;
Denis Vlasenko956a5692006-09-27 14:51:27 +000096 }
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000097 } else {
Denys Vlasenko92510142010-05-19 00:39:17 +020098 if (opt != OPT_f)
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000099 printf("%s: %s\n", dev, s);
100 if (ENABLE_FEATURE_CLEAN_UP)
101 free(s);
Denis Vlasenko956a5692006-09-27 14:51:27 +0000102 }
103 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104 return EXIT_SUCCESS;
Matt Kraai83788da2002-03-20 17:38:37 +0000105}