diff options
Diffstat (limited to 'util-linux/losetup.c')
-rw-r--r-- | util-linux/losetup.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/util-linux/losetup.c b/util-linux/losetup.c index 2248f2cba..cc6c2b1d5 100644 --- a/util-linux/losetup.c +++ b/util-linux/losetup.c | |||
@@ -20,13 +20,14 @@ | |||
20 | //kbuild:lib-$(CONFIG_LOSETUP) += losetup.o | 20 | //kbuild:lib-$(CONFIG_LOSETUP) += losetup.o |
21 | 21 | ||
22 | //usage:#define losetup_trivial_usage | 22 | //usage:#define losetup_trivial_usage |
23 | //usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE: associate loop devices\n" | 23 | //usage: "[-rP] [-o OFS] {-f|LOOPDEV} FILE: associate loop devices\n" |
24 | //usage: " losetup -c LOOPDEV: reread file size\n" | 24 | //usage: " losetup -c LOOPDEV: reread file size\n" |
25 | //usage: " losetup -d LOOPDEV: disassociate\n" | 25 | //usage: " losetup -d LOOPDEV: disassociate\n" |
26 | //usage: " losetup -a: show status\n" | 26 | //usage: " losetup -a: show status\n" |
27 | //usage: " losetup -f: show next free loop device" | 27 | //usage: " losetup -f: show next free loop device" |
28 | //usage:#define losetup_full_usage "\n\n" | 28 | //usage:#define losetup_full_usage "\n\n" |
29 | //usage: " -o OFS Start OFS bytes into FILE" | 29 | //usage: " -o OFS Start OFS bytes into FILE" |
30 | //usage: "\n -P Scan for partitions" | ||
30 | //usage: "\n -r Read-only" | 31 | //usage: "\n -r Read-only" |
31 | //usage: "\n -f Show/use next free loop device" | 32 | //usage: "\n -f Show/use next free loop device" |
32 | //usage: | 33 | //usage: |
@@ -35,8 +36,9 @@ | |||
35 | //usage: "(if any), or disassociate it (with -d). The display shows the offset\n" | 36 | //usage: "(if any), or disassociate it (with -d). The display shows the offset\n" |
36 | //usage: "and filename of the file the loop device is currently bound to.\n\n" | 37 | //usage: "and filename of the file the loop device is currently bound to.\n\n" |
37 | //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" | 38 | //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" |
38 | //usage: "with an optional offset (-o 12345). Encryption is not yet supported.\n" | 39 | //usage: "with optional partition scanning (creates /dev/loop1p1, /dev/loop1p2\n" |
39 | //usage: "losetup -f will show the first loop free loop device\n\n" | 40 | //usage: "etc. with -P) and with an optional offset (-o 12345). Encryption is\n" |
41 | //usage: "not yet supported. losetup -f will show the first free loop device\n\n" | ||
40 | 42 | ||
41 | #include "libbb.h" | 43 | #include "libbb.h" |
42 | 44 | ||
@@ -53,13 +55,14 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
53 | enum { | 55 | enum { |
54 | OPT_c = (1 << 0), | 56 | OPT_c = (1 << 0), |
55 | OPT_d = (1 << 1), | 57 | OPT_d = (1 << 1), |
56 | OPT_o = (1 << 2), | 58 | OPT_P = (1 << 2), |
57 | OPT_f = (1 << 3), | 59 | OPT_o = (1 << 3), |
58 | OPT_a = (1 << 4), | 60 | OPT_f = (1 << 4), |
59 | OPT_r = (1 << 5), | 61 | OPT_a = (1 << 5), |
62 | OPT_r = (1 << 6), | ||
60 | }; | 63 | }; |
61 | 64 | ||
62 | opt = getopt32(argv, "^" "cdo:far" "\0" "?2:d--ofar:a--ofr", &opt_o); | 65 | opt = getopt32(argv, "^" "cdPo:far" "\0" "?2:d--Pofar:a--Pofr", &opt_o); |
63 | argv += optind; | 66 | argv += optind; |
64 | 67 | ||
65 | /* LOOPDEV */ | 68 | /* LOOPDEV */ |
@@ -111,11 +114,17 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
111 | /* contains -f */ | 114 | /* contains -f */ |
112 | if (opt & OPT_f) { | 115 | if (opt & OPT_f) { |
113 | char *s; | 116 | char *s; |
114 | int n = 0; | 117 | int n; |
115 | 118 | ||
119 | n = get_free_loop(); | ||
120 | if (n == -1) | ||
121 | bb_simple_error_msg_and_die("no free loop devices"); | ||
122 | if (n < 0) /* n == -2: no /dev/loop-control, use legacy method */ | ||
123 | n = 0; | ||
124 | /* or: n >= 0: the number of next free loopdev, just verify it */ | ||
116 | do { | 125 | do { |
117 | if (n > MAX_LOOP_NUM) | 126 | if (n > MAX_LOOP_NUM) |
118 | bb_error_msg_and_die("no free loop devices"); | 127 | bb_simple_error_msg_and_die("no free loop devices"); |
119 | sprintf(dev, LOOP_FORMAT, n++); | 128 | sprintf(dev, LOOP_FORMAT, n++); |
120 | s = query_loop(dev); | 129 | s = query_loop(dev); |
121 | free(s); | 130 | free(s); |
@@ -127,7 +136,7 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
127 | } | 136 | } |
128 | } | 137 | } |
129 | 138 | ||
130 | /* [-r] [-o OFS] {-f|LOOPDEV} FILE */ | 139 | /* [-rP] [-o OFS] {-f|LOOPDEV} FILE */ |
131 | if (argv[0] && ((opt & OPT_f) || argv[1])) { | 140 | if (argv[0] && ((opt & OPT_f) || argv[1])) { |
132 | unsigned long long offset = 0; | 141 | unsigned long long offset = 0; |
133 | char *d = dev; | 142 | char *d = dev; |
@@ -138,7 +147,11 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
138 | d = *argv++; | 147 | d = *argv++; |
139 | 148 | ||
140 | if (argv[0]) { | 149 | if (argv[0]) { |
141 | if (set_loop(&d, argv[0], offset, (opt & OPT_r) ? BB_LO_FLAGS_READ_ONLY : 0) < 0) | 150 | unsigned flags = (opt & OPT_r) ? BB_LO_FLAGS_READ_ONLY : 0; |
151 | if (opt & OPT_P) { | ||
152 | flags |= BB_LO_FLAGS_PARTSCAN; | ||
153 | } | ||
154 | if (set_loop(&d, argv[0], offset, flags) < 0) | ||
142 | bb_simple_perror_msg_and_die(argv[0]); | 155 | bb_simple_perror_msg_and_die(argv[0]); |
143 | return EXIT_SUCCESS; | 156 | return EXIT_SUCCESS; |
144 | } | 157 | } |