]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
9abd5e4b KZ |
2 | * SPDX-License-Identifier: GPL-2.0-or-later |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * Original implementation from Ted Ts'o; losetup was part of mount. | |
10 | * | |
11 | * Copyright (C) 2011-2023 Karel Zak <kzak@redhat.com> | |
39fde137 | 12 | * |
6dbe3af9 KZ |
13 | * losetup.c - setup and control loop devices |
14 | */ | |
896352d3 | 15 | #include <assert.h> |
6dbe3af9 KZ |
16 | #include <stdio.h> |
17 | #include <string.h> | |
fd6b7a7f | 18 | #include <errno.h> |
6dbe3af9 KZ |
19 | #include <stdlib.h> |
20 | #include <unistd.h> | |
21 | #include <sys/ioctl.h> | |
fd6b7a7f | 22 | #include <sys/stat.h> |
7711bc17 | 23 | #include <inttypes.h> |
65329058 | 24 | #include <getopt.h> |
6dbe3af9 | 25 | |
7e86cd54 OO |
26 | #include <libsmartcols.h> |
27 | ||
934df30d | 28 | #include "c.h" |
107293a6 | 29 | #include "cctype.h" |
7eda085c | 30 | #include "nls.h" |
934df30d | 31 | #include "strutils.h" |
de4acb05 | 32 | #include "loopdev.h" |
efb8854f | 33 | #include "closestream.h" |
96801c48 | 34 | #include "optutils.h" |
896352d3 | 35 | #include "xalloc.h" |
114ade3d | 36 | #include "canonicalize.h" |
362f5d20 | 37 | #include "pathnames.h" |
96801c48 | 38 | |
39fde137 | 39 | enum { |
c7e0925d | 40 | A_CREATE = 1, /* setup a new device */ |
c654c4f0 KZ |
41 | A_DELETE, /* delete given device(s) */ |
42 | A_DELETE_ALL, /* delete all devices */ | |
39fde137 | 43 | A_SHOW, /* list devices */ |
09ec0c0a | 44 | A_SHOW_ONE, /* print info about one device */ |
39fde137 KZ |
45 | A_FIND_FREE, /* find first unused */ |
46 | A_SET_CAPACITY, /* set device capacity */ | |
64c3bb3c | 47 | A_SET_DIRECT_IO, /* set accessing backing file by direct io */ |
a1a41597 | 48 | A_SET_BLOCKSIZE, /* set logical block size of the loop device */ |
39fde137 KZ |
49 | }; |
50 | ||
896352d3 OO |
51 | enum { |
52 | COL_NAME = 0, | |
53 | COL_AUTOCLR, | |
54 | COL_BACK_FILE, | |
55 | COL_BACK_INO, | |
56 | COL_BACK_MAJMIN, | |
422f54c0 KZ |
57 | COL_BACK_MAJ, |
58 | COL_BACK_MIN, | |
896352d3 | 59 | COL_MAJMIN, |
422f54c0 KZ |
60 | COL_MAJ, |
61 | COL_MIN, | |
896352d3 OO |
62 | COL_OFFSET, |
63 | COL_PARTSCAN, | |
db8f7815 | 64 | COL_REF, |
896352d3 | 65 | COL_RO, |
4ad996d7 | 66 | COL_SIZELIMIT, |
faeef4d2 | 67 | COL_DIO, |
76493ceb | 68 | COL_LOGSEC, |
896352d3 OO |
69 | }; |
70 | ||
7e86cd54 OO |
71 | /* basic output flags */ |
72 | static int no_headings; | |
73 | static int raw; | |
4827093d | 74 | static int json; |
896352d3 OO |
75 | |
76 | struct colinfo { | |
37b2b3fa | 77 | const char * const name; |
896352d3 OO |
78 | double whint; |
79 | int flags; | |
80 | const char *help; | |
46de0fe6 KZ |
81 | |
82 | int json_type; /* default is string */ | |
896352d3 OO |
83 | }; |
84 | ||
37b2b3fa | 85 | static const struct colinfo infos[] = { |
46de0fe6 | 86 | [COL_AUTOCLR] = { "AUTOCLEAR", 1, SCOLS_FL_RIGHT, N_("autoclear flag set"), SCOLS_JSON_BOOLEAN}, |
9e85d591 | 87 | [COL_BACK_FILE] = { "BACK-FILE", 0.3, SCOLS_FL_NOEXTREMES, N_("device backing file")}, |
46de0fe6 | 88 | [COL_BACK_INO] = { "BACK-INO", 4, SCOLS_FL_RIGHT, N_("backing file inode number"), SCOLS_JSON_NUMBER}, |
896352d3 | 89 | [COL_BACK_MAJMIN] = { "BACK-MAJ:MIN", 6, 0, N_("backing file major:minor device number")}, |
422f54c0 KZ |
90 | [COL_BACK_MAJ] = { "BACK-MAJ", 6, 0, N_("backing file major device number")}, |
91 | [COL_BACK_MIN] = { "BACK-MIN", 6, 0, N_("backing file minor device number")}, | |
896352d3 | 92 | [COL_NAME] = { "NAME", 0.25, 0, N_("loop device name")}, |
46de0fe6 KZ |
93 | [COL_OFFSET] = { "OFFSET", 5, SCOLS_FL_RIGHT, N_("offset from the beginning"), SCOLS_JSON_NUMBER}, |
94 | [COL_PARTSCAN] = { "PARTSCAN", 1, SCOLS_FL_RIGHT, N_("partscan flag set"), SCOLS_JSON_BOOLEAN}, | |
db8f7815 | 95 | [COL_REF] = { "REF", 0.1, 0, N_("loop device reference string")}, |
46de0fe6 KZ |
96 | [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device"), SCOLS_JSON_BOOLEAN}, |
97 | [COL_SIZELIMIT] = { "SIZELIMIT", 5, SCOLS_FL_RIGHT, N_("size limit of the file in bytes"), SCOLS_JSON_NUMBER}, | |
896352d3 | 98 | [COL_MAJMIN] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")}, |
422f54c0 KZ |
99 | [COL_MAJ] = { "MAJ", 1, SCOLS_FL_RIGHT, N_("loop device major number"), SCOLS_JSON_NUMBER}, |
100 | [COL_MIN] = { "MIN", 1, SCOLS_FL_RIGHT, N_("loop device minor number"), SCOLS_JSON_NUMBER}, | |
46de0fe6 KZ |
101 | [COL_DIO] = { "DIO", 1, SCOLS_FL_RIGHT, N_("access backing file with direct-io"), SCOLS_JSON_BOOLEAN}, |
102 | [COL_LOGSEC] = { "LOG-SEC", 4, SCOLS_FL_RIGHT, N_("logical sector size in bytes"), SCOLS_JSON_NUMBER}, | |
896352d3 OO |
103 | }; |
104 | ||
d78deddc | 105 | static int columns[ARRAY_SIZE(infos) * 2] = {-1}; |
40b17508 | 106 | static size_t ncolumns; |
6dbe3af9 | 107 | |
896352d3 OO |
108 | static int get_column_id(int num) |
109 | { | |
40b17508 KZ |
110 | assert(num >= 0); |
111 | assert((size_t) num < ncolumns); | |
d78deddc | 112 | assert(columns[num] < (int) ARRAY_SIZE(infos)); |
896352d3 OO |
113 | return columns[num]; |
114 | } | |
115 | ||
37b2b3fa | 116 | static const struct colinfo *get_column_info(int num) |
896352d3 OO |
117 | { |
118 | return &infos[ get_column_id(num) ]; | |
119 | } | |
120 | ||
121 | static int column_name_to_id(const char *name, size_t namesz) | |
122 | { | |
123 | size_t i; | |
124 | ||
d78deddc | 125 | for (i = 0; i < ARRAY_SIZE(infos); i++) { |
896352d3 OO |
126 | const char *cn = infos[i].name; |
127 | ||
107293a6 | 128 | if (!c_strncasecmp(name, cn, namesz) && !*(cn + namesz)) |
896352d3 OO |
129 | return i; |
130 | } | |
131 | warnx(_("unknown column: %s"), name); | |
132 | return -1; | |
133 | } | |
d03dd608 | 134 | |
39fde137 KZ |
135 | static int printf_loopdev(struct loopdev_cxt *lc) |
136 | { | |
137 | uint64_t x; | |
138 | dev_t dev = 0; | |
139 | ino_t ino = 0; | |
1b504263 | 140 | char *fname; |
a3b421df | 141 | uint32_t type; |
39fde137 KZ |
142 | |
143 | fname = loopcxt_get_backing_file(lc); | |
144 | if (!fname) | |
145 | return -EINVAL; | |
146 | ||
147 | if (loopcxt_get_backing_devno(lc, &dev) == 0) | |
148 | loopcxt_get_backing_inode(lc, &ino); | |
149 | ||
150 | if (!dev && !ino) { | |
151 | /* | |
152 | * Probably non-root user (no permissions to | |
153 | * call LOOP_GET_STATUS ioctls). | |
154 | */ | |
a6ca0456 JB |
155 | printf("%s%s: []: (%s)", |
156 | loopcxt_get_device(lc), | |
157 | loopcxt_is_lost(lc) ? " (lost)" : "", | |
158 | fname); | |
39fde137 KZ |
159 | |
160 | if (loopcxt_get_offset(lc, &x) == 0 && x) | |
161 | printf(_(", offset %ju"), x); | |
39fde137 KZ |
162 | if (loopcxt_get_sizelimit(lc, &x) == 0 && x) |
163 | printf(_(", sizelimit %ju"), x); | |
a6ca0456 | 164 | |
9c6af25c | 165 | goto done; |
39fde137 KZ |
166 | } |
167 | ||
a6ca0456 JB |
168 | printf("%s%s: [%04jd]:%ju (%s)", |
169 | loopcxt_get_device(lc), | |
170 | loopcxt_is_lost(lc) ? " (lost)" : "", | |
171 | (intmax_t) dev, (uintmax_t) ino, fname); | |
39fde137 KZ |
172 | |
173 | if (loopcxt_get_offset(lc, &x) == 0 && x) | |
174 | printf(_(", offset %ju"), x); | |
39fde137 KZ |
175 | if (loopcxt_get_sizelimit(lc, &x) == 0 && x) |
176 | printf(_(", sizelimit %ju"), x); | |
177 | ||
178 | if (loopcxt_get_encrypt_type(lc, &type) == 0) { | |
179 | const char *e = loopcxt_get_crypt_name(lc); | |
180 | ||
181 | if ((!e || !*e) && type == 1) | |
182 | e = "XOR"; | |
183 | if (e && *e) | |
a3b421df | 184 | printf(_(", encryption %s (type %u)"), e, type); |
39fde137 | 185 | } |
9c6af25c KZ |
186 | |
187 | done: | |
188 | free(fname); | |
39fde137 KZ |
189 | printf("\n"); |
190 | return 0; | |
191 | } | |
192 | ||
bc0ac075 KZ |
193 | static int show_all_loops(struct loopdev_cxt *lc, const char *file, |
194 | uint64_t offset, int flags) | |
39fde137 | 195 | { |
bc0ac075 | 196 | struct stat sbuf, *st = &sbuf; |
6d62bc0f | 197 | char *cn_file = NULL; |
bc0ac075 | 198 | |
39fde137 | 199 | if (loopcxt_init_iterator(lc, LOOPITER_FL_USED)) |
6e90a44c | 200 | return -1; |
39fde137 | 201 | |
bc0ac075 KZ |
202 | if (!file || stat(file, st)) |
203 | st = NULL; | |
204 | ||
205 | while (loopcxt_next(lc) == 0) { | |
6d62bc0f KZ |
206 | if (file) { |
207 | int used; | |
208 | const char *bf = cn_file ? cn_file : file; | |
209 | ||
74a4705a | 210 | used = loopcxt_is_used(lc, st, bf, offset, 0, flags); |
6d62bc0f KZ |
211 | if (!used && !cn_file) { |
212 | bf = cn_file = canonicalize_path(file); | |
74a4705a | 213 | used = loopcxt_is_used(lc, st, bf, offset, 0, flags); |
6d62bc0f KZ |
214 | } |
215 | if (!used) | |
114ade3d SK |
216 | continue; |
217 | } | |
bc0ac075 KZ |
218 | printf_loopdev(lc); |
219 | } | |
c654c4f0 | 220 | loopcxt_deinit_iterator(lc); |
f614b73c | 221 | free(cn_file); |
6e90a44c KZ |
222 | return 0; |
223 | } | |
224 | ||
a6ca0456 JB |
225 | static void warn_lost(struct loopdev_cxt *lc) |
226 | { | |
227 | dev_t devno = loopcxt_get_devno(lc); | |
228 | ||
229 | if (devno <= 0) | |
230 | return; | |
231 | ||
232 | warnx(("device node %s (%u:%u) is lost. You may use mknod(1) to recover it."), | |
233 | loopcxt_get_device(lc), major(devno), minor(devno)); | |
234 | } | |
235 | ||
c654c4f0 KZ |
236 | static int delete_loop(struct loopdev_cxt *lc) |
237 | { | |
a6ca0456 | 238 | if (loopcxt_delete_device(lc)) { |
c654c4f0 | 239 | warn(_("%s: detach failed"), loopcxt_get_device(lc)); |
a6ca0456 JB |
240 | if (loopcxt_is_lost(lc)) |
241 | warn_lost(lc); | |
242 | } else | |
c654c4f0 KZ |
243 | return 0; |
244 | ||
245 | return -1; | |
246 | } | |
247 | ||
248 | static int delete_all_loops(struct loopdev_cxt *lc) | |
249 | { | |
250 | int res = 0; | |
251 | ||
252 | if (loopcxt_init_iterator(lc, LOOPITER_FL_USED)) | |
253 | return -1; | |
254 | ||
255 | while (loopcxt_next(lc) == 0) | |
256 | res += delete_loop(lc); | |
257 | ||
258 | loopcxt_deinit_iterator(lc); | |
259 | return res; | |
39fde137 KZ |
260 | } |
261 | ||
7e86cd54 | 262 | static int set_scols_data(struct loopdev_cxt *lc, struct libscols_line *ln) |
896352d3 | 263 | { |
40b17508 | 264 | size_t i; |
896352d3 OO |
265 | |
266 | for (i = 0; i < ncolumns; i++) { | |
48f1f38b KZ |
267 | const char *p = NULL; /* external data */ |
268 | char *np = NULL; /* allocated here */ | |
896352d3 | 269 | uint64_t x = 0; |
a9e3ec19 | 270 | int rc = 0; |
896352d3 OO |
271 | |
272 | switch(get_column_id(i)) { | |
273 | case COL_NAME: | |
274 | p = loopcxt_get_device(lc); | |
a6ca0456 JB |
275 | if (loopcxt_is_lost(lc)) { |
276 | xasprintf(&np, "%s (lost)", p); | |
277 | p = NULL; | |
278 | } | |
896352d3 OO |
279 | break; |
280 | case COL_BACK_FILE: | |
01dd2838 | 281 | np = loopcxt_get_backing_file(lc); |
896352d3 OO |
282 | break; |
283 | case COL_OFFSET: | |
284 | if (loopcxt_get_offset(lc, &x) == 0) | |
285 | xasprintf(&np, "%jd", x); | |
896352d3 | 286 | break; |
4ad996d7 | 287 | case COL_SIZELIMIT: |
896352d3 OO |
288 | if (loopcxt_get_sizelimit(lc, &x) == 0) |
289 | xasprintf(&np, "%jd", x); | |
896352d3 OO |
290 | break; |
291 | case COL_BACK_MAJMIN: | |
292 | { | |
293 | dev_t dev = 0; | |
294 | if (loopcxt_get_backing_devno(lc, &dev) == 0 && dev) | |
7d5caac4 KZ |
295 | xasprintf(&np, raw || json ? "%u:%u" : "%8u:%-3u", |
296 | major(dev), minor(dev)); | |
896352d3 OO |
297 | break; |
298 | } | |
422f54c0 KZ |
299 | case COL_BACK_MAJ: |
300 | { | |
301 | dev_t dev = 0; | |
302 | if (loopcxt_get_backing_devno(lc, &dev) == 0 && dev) | |
303 | xasprintf(&np, "%u", major(dev)); | |
304 | break; | |
305 | } | |
306 | case COL_BACK_MIN: | |
307 | { | |
308 | dev_t dev = 0; | |
309 | if (loopcxt_get_backing_devno(lc, &dev) == 0 && dev) | |
310 | xasprintf(&np, "%u", minor(dev)); | |
311 | break; | |
312 | } | |
896352d3 OO |
313 | case COL_MAJMIN: |
314 | { | |
a6ca0456 | 315 | dev_t dev = loopcxt_get_devno(lc); |
422f54c0 | 316 | if (dev) |
7d5caac4 | 317 | xasprintf(&np, raw || json ? "%u:%u" :"%3u:%-3u", |
422f54c0 KZ |
318 | major(dev), minor(dev)); |
319 | break; | |
320 | } | |
321 | case COL_MAJ: { | |
a6ca0456 | 322 | dev_t dev = loopcxt_get_devno(lc); |
422f54c0 KZ |
323 | if (dev) |
324 | xasprintf(&np, "%u", major(dev)); | |
325 | break; | |
326 | } | |
327 | case COL_MIN: { | |
a6ca0456 | 328 | dev_t dev = loopcxt_get_devno(lc); |
422f54c0 KZ |
329 | if (dev) |
330 | xasprintf(&np, "%u", minor(dev)); | |
896352d3 OO |
331 | break; |
332 | } | |
333 | case COL_BACK_INO: | |
334 | { | |
335 | ino_t ino = 0; | |
336 | if (loopcxt_get_backing_inode(lc, &ino) == 0 && ino) | |
337 | xasprintf(&np, "%ju", ino); | |
896352d3 OO |
338 | break; |
339 | } | |
340 | case COL_AUTOCLR: | |
48f1f38b | 341 | p = loopcxt_is_autoclear(lc) ? "1" : "0"; |
896352d3 OO |
342 | break; |
343 | case COL_RO: | |
48f1f38b | 344 | p = loopcxt_is_readonly(lc) ? "1" : "0"; |
896352d3 | 345 | break; |
faeef4d2 ML |
346 | case COL_DIO: |
347 | p = loopcxt_is_dio(lc) ? "1" : "0"; | |
348 | break; | |
896352d3 | 349 | case COL_PARTSCAN: |
48f1f38b | 350 | p = loopcxt_is_partscan(lc) ? "1" : "0"; |
896352d3 | 351 | break; |
76493ceb | 352 | case COL_LOGSEC: |
a1a41597 SB |
353 | if (loopcxt_get_blocksize(lc, &x) == 0) |
354 | xasprintf(&np, "%jd", x); | |
355 | break; | |
db8f7815 KZ |
356 | case COL_REF: |
357 | np = loopcxt_get_refname(lc); | |
358 | break; | |
896352d3 OO |
359 | default: |
360 | return -EINVAL; | |
361 | } | |
48f1f38b KZ |
362 | |
363 | ||
364 | if (p) | |
780ce22c | 365 | rc = scols_line_set_data(ln, i, p); /* calls strdup() */ |
48f1f38b | 366 | else if (np) |
780ce22c KZ |
367 | rc = scols_line_refer_data(ln, i, np); /* only refers */ |
368 | ||
369 | if (rc) | |
370 | err(EXIT_FAILURE, _("failed to add output data")); | |
896352d3 | 371 | } |
48f1f38b | 372 | |
896352d3 OO |
373 | return 0; |
374 | } | |
375 | ||
48f1f38b | 376 | static int show_table(struct loopdev_cxt *lc, |
9f56106d KZ |
377 | const char *file, |
378 | uint64_t offset, | |
7e86cd54 | 379 | int flags) |
896352d3 OO |
380 | { |
381 | struct stat sbuf, *st = &sbuf; | |
48f1f38b | 382 | struct libscols_table *tb; |
7e86cd54 | 383 | struct libscols_line *ln; |
40b17508 KZ |
384 | int rc = 0; |
385 | size_t i; | |
896352d3 | 386 | |
710ed55d KZ |
387 | scols_init_debug(0); |
388 | ||
0925a9dd | 389 | if (!(tb = scols_new_table())) |
780ce22c | 390 | err(EXIT_FAILURE, _("failed to allocate output table")); |
0925a9dd | 391 | scols_table_enable_raw(tb, raw); |
4827093d | 392 | scols_table_enable_json(tb, json); |
0925a9dd | 393 | scols_table_enable_noheadings(tb, no_headings); |
896352d3 | 394 | |
4827093d KZ |
395 | if (json) |
396 | scols_table_set_name(tb, "loopdevices"); | |
397 | ||
e0b06769 | 398 | for (i = 0; i < ncolumns; i++) { |
37b2b3fa | 399 | const struct colinfo *ci = get_column_info(i); |
46de0fe6 | 400 | struct libscols_column *cl; |
896352d3 | 401 | |
46de0fe6 KZ |
402 | cl = scols_table_new_column(tb, ci->name, ci->whint, ci->flags); |
403 | if (!cl) | |
780ce22c | 404 | err(EXIT_FAILURE, _("failed to allocate output column")); |
46de0fe6 KZ |
405 | if (json) |
406 | scols_column_set_json_type(cl, ci->json_type); | |
896352d3 OO |
407 | } |
408 | ||
9f56106d | 409 | /* only one loopdev requested (already assigned to loopdev_cxt) */ |
896352d3 | 410 | if (loopcxt_get_device(lc)) { |
48f1f38b | 411 | ln = scols_table_new_line(tb, NULL); |
7e86cd54 | 412 | if (!ln) |
780ce22c | 413 | err(EXIT_FAILURE, _("failed to allocate output line")); |
48f1f38b | 414 | rc = set_scols_data(lc, ln); |
896352d3 | 415 | |
9f56106d | 416 | /* list all loopdevs */ |
48f1f38b KZ |
417 | } else { |
418 | char *cn_file = NULL; | |
896352d3 | 419 | |
48f1f38b KZ |
420 | rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED); |
421 | if (rc) | |
422 | goto done; | |
423 | if (!file || stat(file, st)) | |
424 | st = NULL; | |
425 | ||
426 | while (loopcxt_next(lc) == 0) { | |
427 | if (file) { | |
428 | int used; | |
429 | const char *bf = cn_file ? cn_file : file; | |
6d62bc0f | 430 | |
74a4705a | 431 | used = loopcxt_is_used(lc, st, bf, offset, 0, flags); |
48f1f38b KZ |
432 | if (!used && !cn_file) { |
433 | bf = cn_file = canonicalize_path(file); | |
74a4705a | 434 | used = loopcxt_is_used(lc, st, bf, offset, 0, flags); |
48f1f38b KZ |
435 | } |
436 | if (!used) | |
437 | continue; | |
6d62bc0f | 438 | } |
48f1f38b KZ |
439 | |
440 | ln = scols_table_new_line(tb, NULL); | |
441 | if (!ln) | |
780ce22c | 442 | err(EXIT_FAILURE, _("failed to allocate output line")); |
48f1f38b KZ |
443 | rc = set_scols_data(lc, ln); |
444 | if (rc) | |
445 | break; | |
6d62bc0f | 446 | } |
896352d3 | 447 | |
48f1f38b KZ |
448 | loopcxt_deinit_iterator(lc); |
449 | free(cn_file); | |
896352d3 | 450 | } |
48f1f38b KZ |
451 | done: |
452 | if (rc == 0) | |
453 | rc = scols_print_table(tb); | |
454 | scols_unref_table(tb); | |
455 | return rc; | |
896352d3 OO |
456 | } |
457 | ||
6e1eda6f | 458 | static void __attribute__((__noreturn__)) usage(void) |
aadb9303 | 459 | { |
6e1eda6f | 460 | FILE *out = stdout; |
e0b06769 SK |
461 | size_t i; |
462 | ||
aadb9303 KZ |
463 | fputs(USAGE_HEADER, out); |
464 | ||
465 | fprintf(out, | |
466 | _(" %1$s [options] [<loopdev>]\n" | |
467 | " %1$s [options] -f | <loopdev> <file>\n"), | |
468 | program_invocation_short_name); | |
469 | ||
451dbcfa BS |
470 | fputs(USAGE_SEPARATOR, out); |
471 | fputs(_("Set up and control loop devices.\n"), out); | |
472 | ||
ec3624aa | 473 | /* commands */ |
aadb9303 | 474 | fputs(USAGE_OPTIONS, out); |
9c47f25e | 475 | fputs(_(" -a, --all list all used devices\n"), out); |
aa06617f | 476 | fputs(_(" -d, --detach <loopdev>... detach one or more devices\n"), out); |
9c47f25e BS |
477 | fputs(_(" -D, --detach-all detach all used devices\n"), out); |
478 | fputs(_(" -f, --find find first unused device\n"), out); | |
aa06617f | 479 | fputs(_(" -c, --set-capacity <loopdev> resize the device\n"), out); |
9c47f25e | 480 | fputs(_(" -j, --associated <file> list all devices associated with <file>\n"), out); |
9a94b634 | 481 | fputs(_(" -L, --nooverlap avoid possible conflict between devices\n"), out); |
9c47f25e | 482 | |
ec3624aa | 483 | /* commands options */ |
aadb9303 | 484 | fputs(USAGE_SEPARATOR, out); |
59a4ed11 | 485 | fputs(_(" -o, --offset <num> start at offset <num> into file\n"), out); |
aa06617f | 486 | fputs(_(" --sizelimit <num> device is limited to <num> bytes of the file\n"), out); |
ec06412c | 487 | fputs(_(" -b, --sector-size <num> set the logical sector size to <num>\n"), out); |
aa06617f BS |
488 | fputs(_(" -P, --partscan create a partitioned loop device\n"), out); |
489 | fputs(_(" -r, --read-only set up a read-only loop device\n"), out); | |
f98d9641 | 490 | fputs(_(" --direct-io[=<on|off>] open backing file with O_DIRECT\n"), out); |
db8f7815 | 491 | fputs(_(" --loop-ref <string> loop device reference\n"), out); |
59a4ed11 SK |
492 | fputs(_(" --show print device name after setup (with -f)\n"), out); |
493 | fputs(_(" -v, --verbose verbose mode\n"), out); | |
aadb9303 | 494 | |
ec3624aa | 495 | /* output options */ |
9f56106d | 496 | fputs(USAGE_SEPARATOR, out); |
ec3624aa | 497 | fputs(_(" -J, --json use JSON --list output format\n"), out); |
14576644 | 498 | fputs(_(" -l, --list list info about all or specified (default)\n"), out); |
0d0d12ad | 499 | fputs(_(" -n, --noheadings don't print headings for --list output\n"), out); |
ec3624aa | 500 | fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out); |
289673b1 | 501 | fputs(_(" --output-all output all columns\n"), out); |
9f56106d KZ |
502 | fputs(_(" --raw use raw --list output format\n"), out); |
503 | ||
aadb9303 | 504 | fputs(USAGE_SEPARATOR, out); |
bad4c729 | 505 | fprintf(out, USAGE_HELP_OPTIONS(31)); |
aadb9303 | 506 | |
c3a4cfc5 | 507 | fputs(USAGE_COLUMNS, out); |
d78deddc | 508 | for (i = 0; i < ARRAY_SIZE(infos); i++) |
896352d3 OO |
509 | fprintf(out, " %12s %s\n", infos[i].name, _(infos[i].help)); |
510 | ||
bad4c729 | 511 | fprintf(out, USAGE_MAN_TAIL("losetup(8)")); |
6997468e | 512 | |
6e1eda6f | 513 | exit(EXIT_SUCCESS); |
aadb9303 | 514 | } |
22853e4a | 515 | |
6daf185c | 516 | static void warn_size(const char *filename, uint64_t size, uint64_t offset, int flags) |
35545456 KZ |
517 | { |
518 | struct stat st; | |
519 | ||
520 | if (!size) { | |
b048b8af | 521 | if (stat(filename, &st) || S_ISBLK(st.st_mode)) |
35545456 KZ |
522 | return; |
523 | size = st.st_size; | |
6daf185c KZ |
524 | |
525 | if (flags & LOOPDEV_FL_OFFSET) | |
526 | size -= offset; | |
35545456 KZ |
527 | } |
528 | ||
529 | if (size < 512) | |
97b820bf BS |
530 | warnx(_("%s: Warning: file is smaller than 512 bytes; the loop device " |
531 | "may be useless or invisible for system tools."), | |
35545456 KZ |
532 | filename); |
533 | else if (size % 512) | |
6f3f2243 KZ |
534 | warnx(_("%s: Warning: file does not end on a 512-byte sector boundary; " |
535 | "the remaining end of the file will be ignored."), | |
35545456 KZ |
536 | filename); |
537 | } | |
538 | ||
af9fc22d TW |
539 | static int find_unused(struct loopdev_cxt *lc) |
540 | { | |
541 | int rc; | |
542 | ||
543 | rc = loopcxt_find_unused(lc); | |
544 | if (!rc) | |
545 | return 0; | |
546 | ||
547 | if (access(_PATH_DEV_LOOPCTL, F_OK) == 0 && | |
548 | access(_PATH_DEV_LOOPCTL, W_OK) != 0) | |
549 | ; | |
550 | else | |
551 | errno = -rc; | |
552 | ||
553 | warn(_("cannot find an unused loop device")); | |
554 | ||
555 | return rc; | |
556 | } | |
557 | ||
9a94b634 KZ |
558 | static int create_loop(struct loopdev_cxt *lc, |
559 | int nooverlap, int lo_flags, int flags, | |
db8f7815 KZ |
560 | const char *file, const char *refname, |
561 | uint64_t offset, uint64_t sizelimit, | |
422f0e9f | 562 | uint64_t blocksize) |
9a94b634 KZ |
563 | { |
564 | int hasdev = loopcxt_has_device(lc); | |
3ff6fb80 | 565 | int rc = 0, ntries = 0; |
9a94b634 | 566 | |
d8ba61fc | 567 | /* losetup --find --noverlap file.img */ |
9a94b634 KZ |
568 | if (!hasdev && nooverlap) { |
569 | rc = loopcxt_find_overlap(lc, file, offset, sizelimit); | |
570 | switch (rc) { | |
571 | case 0: /* not found */ | |
572 | break; | |
573 | ||
574 | case 1: /* overlap */ | |
575 | loopcxt_deinit(lc); | |
576 | errx(EXIT_FAILURE, _("%s: overlapping loop device exists"), file); | |
577 | ||
578 | case 2: /* overlap -- full size and offset match (reuse) */ | |
579 | { | |
580 | uint32_t lc_encrypt_type; | |
581 | ||
582 | /* Once a loop is initialized RO, there is no | |
583 | * way to change its parameters. */ | |
584 | if (loopcxt_is_readonly(lc) | |
585 | && !(lo_flags & LO_FLAGS_READ_ONLY)) { | |
586 | loopcxt_deinit(lc); | |
587 | errx(EXIT_FAILURE, _("%s: overlapping read-only loop device exists"), file); | |
588 | } | |
589 | ||
590 | /* This is no more supported, but check to be safe. */ | |
591 | if (loopcxt_get_encrypt_type(lc, &lc_encrypt_type) == 0 | |
592 | && lc_encrypt_type != LO_CRYPT_NONE) { | |
593 | loopcxt_deinit(lc); | |
594 | errx(EXIT_FAILURE, _("%s: overlapping encrypted loop device exists"), file); | |
595 | } | |
f27d989c | 596 | |
d5fd456c | 597 | lc->config.info.lo_flags &= ~LO_FLAGS_AUTOCLEAR; |
9fcc8936 | 598 | if (loopcxt_ioctl_status(lc)) { |
f27d989c SB |
599 | loopcxt_deinit(lc); |
600 | errx(EXIT_FAILURE, _("%s: failed to re-use loop device"), file); | |
601 | } | |
9a94b634 KZ |
602 | return 0; /* success, re-use */ |
603 | } | |
604 | default: /* error */ | |
605 | loopcxt_deinit(lc); | |
606 | errx(EXIT_FAILURE, _("failed to inspect loop devices")); | |
607 | return -errno; | |
608 | } | |
609 | } | |
610 | ||
dfa92ef1 | 611 | if (hasdev) |
9a94b634 KZ |
612 | loopcxt_add_device(lc); |
613 | ||
d8ba61fc KZ |
614 | /* losetup --noverlap /dev/loopN file.img */ |
615 | if (hasdev && nooverlap) { | |
616 | struct loopdev_cxt lc2; | |
617 | ||
618 | if (loopcxt_init(&lc2, 0)) { | |
619 | loopcxt_deinit(lc); | |
620 | err(EXIT_FAILURE, _("failed to initialize loopcxt")); | |
621 | } | |
622 | rc = loopcxt_find_overlap(&lc2, file, offset, sizelimit); | |
623 | loopcxt_deinit(&lc2); | |
624 | ||
625 | if (rc) { | |
626 | loopcxt_deinit(lc); | |
627 | if (rc > 0) | |
628 | errx(EXIT_FAILURE, _("%s: overlapping loop device exists"), file); | |
629 | err(EXIT_FAILURE, _("%s: failed to check for conflicting loop devices"), file); | |
630 | } | |
631 | } | |
632 | ||
9a94b634 KZ |
633 | /* Create a new device */ |
634 | do { | |
635 | const char *errpre; | |
636 | ||
637 | /* Note that loopcxt_{find_unused,set_device}() resets | |
638 | * loopcxt struct. | |
639 | */ | |
af9fc22d | 640 | if (!hasdev && (rc = find_unused(lc))) |
9a94b634 | 641 | break; |
9a94b634 KZ |
642 | if (flags & LOOPDEV_FL_OFFSET) |
643 | loopcxt_set_offset(lc, offset); | |
644 | if (flags & LOOPDEV_FL_SIZELIMIT) | |
645 | loopcxt_set_sizelimit(lc, sizelimit); | |
646 | if (lo_flags) | |
647 | loopcxt_set_flags(lc, lo_flags); | |
422f0e9f KZ |
648 | if (blocksize > 0) |
649 | loopcxt_set_blocksize(lc, blocksize); | |
db8f7815 KZ |
650 | if (refname && (rc = loopcxt_set_refname(lc, refname))) { |
651 | warnx(_("cannot set loop reference string")); | |
652 | break; | |
653 | } | |
9a94b634 KZ |
654 | if ((rc = loopcxt_set_backing_file(lc, file))) { |
655 | warn(_("%s: failed to use backing file"), file); | |
656 | break; | |
657 | } | |
658 | errno = 0; | |
659 | rc = loopcxt_setup_device(lc); | |
660 | if (rc == 0) | |
661 | break; /* success */ | |
3ff6fb80 | 662 | |
3e03cb68 | 663 | if ((errno == EBUSY || errno == EAGAIN) && !hasdev && ntries < 64) { |
3ff6fb80 KZ |
664 | xusleep(200000); |
665 | ntries++; | |
9a94b634 | 666 | continue; |
3ff6fb80 | 667 | } |
9a94b634 KZ |
668 | |
669 | /* errors */ | |
a6ca0456 | 670 | errpre = hasdev && lc->fd < 0 ? |
9a94b634 KZ |
671 | loopcxt_get_device(lc) : file; |
672 | warn(_("%s: failed to set up loop device"), errpre); | |
673 | break; | |
674 | } while (hasdev == 0); | |
675 | ||
676 | return rc; | |
677 | } | |
678 | ||
39fde137 KZ |
679 | int main(int argc, char **argv) |
680 | { | |
681 | struct loopdev_cxt lc; | |
9a94b634 | 682 | int act = 0, flags = 0, no_overlap = 0, c; |
db8f7815 | 683 | char *file = NULL, *refname = NULL; |
a1a41597 | 684 | uint64_t offset = 0, sizelimit = 0, blocksize = 0; |
7e86cd54 | 685 | int res = 0, showdev = 0, lo_flags = 0; |
896352d3 OO |
686 | char *outarg = NULL; |
687 | int list = 0; | |
a1a41597 | 688 | unsigned long use_dio = 0, set_dio = 0, set_blocksize = 0; |
6c7d5ae9 | 689 | |
aadb9303 KZ |
690 | enum { |
691 | OPT_SIZELIMIT = CHAR_MAX + 1, | |
9f56106d | 692 | OPT_SHOW, |
64c3bb3c | 693 | OPT_RAW, |
db8f7815 | 694 | OPT_REF, |
289673b1 SK |
695 | OPT_DIO, |
696 | OPT_OUTPUT_ALL | |
aadb9303 KZ |
697 | }; |
698 | static const struct option longopts[] = { | |
87918040 SK |
699 | { "all", no_argument, NULL, 'a' }, |
700 | { "set-capacity", required_argument, NULL, 'c' }, | |
701 | { "detach", required_argument, NULL, 'd' }, | |
702 | { "detach-all", no_argument, NULL, 'D' }, | |
703 | { "find", no_argument, NULL, 'f' }, | |
704 | { "nooverlap", no_argument, NULL, 'L' }, | |
705 | { "help", no_argument, NULL, 'h' }, | |
706 | { "associated", required_argument, NULL, 'j' }, | |
707 | { "json", no_argument, NULL, 'J' }, | |
708 | { "list", no_argument, NULL, 'l' }, | |
76493ceb | 709 | { "sector-size", required_argument, NULL, 'b' }, |
87918040 SK |
710 | { "noheadings", no_argument, NULL, 'n' }, |
711 | { "offset", required_argument, NULL, 'o' }, | |
712 | { "output", required_argument, NULL, 'O' }, | |
289673b1 | 713 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, |
87918040 SK |
714 | { "sizelimit", required_argument, NULL, OPT_SIZELIMIT }, |
715 | { "partscan", no_argument, NULL, 'P' }, | |
716 | { "read-only", no_argument, NULL, 'r' }, | |
717 | { "direct-io", optional_argument, NULL, OPT_DIO }, | |
718 | { "raw", no_argument, NULL, OPT_RAW }, | |
db8f7815 | 719 | { "loop-ref", required_argument, NULL, OPT_REF, }, |
87918040 SK |
720 | { "show", no_argument, NULL, OPT_SHOW }, |
721 | { "verbose", no_argument, NULL, 'v' }, | |
722 | { "version", no_argument, NULL, 'V' }, | |
723 | { NULL, 0, NULL, 0 } | |
d99f0140 | 724 | }; |
22853e4a | 725 | |
c1ac3144 KZ |
726 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
727 | { 'D','a','c','d','f','j' }, | |
896352d3 | 728 | { 'D','c','d','f','l' }, |
65178cb3 | 729 | { 'D','c','d','f','O' }, |
4827093d | 730 | { 'J',OPT_RAW }, |
c1ac3144 KZ |
731 | { 0 } |
732 | }; | |
733 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
734 | ||
22853e4a KZ |
735 | setlocale(LC_ALL, ""); |
736 | bindtextdomain(PACKAGE, LOCALEDIR); | |
737 | textdomain(PACKAGE); | |
25b7045e | 738 | close_stdout_atexit(); |
22853e4a | 739 | |
defa0710 KZ |
740 | if (loopcxt_init(&lc, 0)) |
741 | err(EXIT_FAILURE, _("failed to initialize loopcxt")); | |
742 | ||
a1a41597 | 743 | while ((c = getopt_long(argc, argv, "ab:c:d:Dfhj:JlLno:O:PrvV", |
d99f0140 | 744 | longopts, NULL)) != -1) { |
c1ac3144 KZ |
745 | |
746 | err_exclusive_options(c, longopts, excl, excl_st); | |
747 | ||
22853e4a | 748 | switch (c) { |
8b125fae | 749 | case 'a': |
39fde137 | 750 | act = A_SHOW; |
8b125fae | 751 | break; |
a1a41597 SB |
752 | case 'b': |
753 | set_blocksize = 1; | |
754 | blocksize = strtosize_or_err(optarg, _("failed to parse logical block size")); | |
755 | break; | |
d34ac93a | 756 | case 'c': |
6e90a44c | 757 | act = A_SET_CAPACITY; |
a6ca0456 | 758 | if (loopcxt_set_device(&lc, optarg)) |
defa0710 KZ |
759 | err(EXIT_FAILURE, _("%s: failed to use device"), |
760 | optarg); | |
d34ac93a | 761 | break; |
faf142b6 | 762 | case 'r': |
c7e0925d | 763 | lo_flags |= LO_FLAGS_READ_ONLY; |
faf142b6 | 764 | break; |
db8f7815 KZ |
765 | case OPT_REF: |
766 | refname = optarg; | |
767 | break; | |
22853e4a | 768 | case 'd': |
c654c4f0 | 769 | act = A_DELETE; |
a6ca0456 | 770 | if (loopcxt_set_device(&lc, optarg)) |
defa0710 KZ |
771 | err(EXIT_FAILURE, _("%s: failed to use device"), |
772 | optarg); | |
22853e4a | 773 | break; |
34f9b684 | 774 | case 'D': |
c654c4f0 | 775 | act = A_DELETE_ALL; |
34f9b684 | 776 | break; |
d162fcb5 | 777 | case 'f': |
bcdbdc72 | 778 | act = A_FIND_FREE; |
d162fcb5 | 779 | break; |
4827093d KZ |
780 | case 'J': |
781 | json = 1; | |
782 | break; | |
259fcc57 | 783 | case 'j': |
bc0ac075 KZ |
784 | act = A_SHOW; |
785 | file = optarg; | |
259fcc57 | 786 | break; |
896352d3 OO |
787 | case 'l': |
788 | list = 1; | |
789 | break; | |
9a94b634 KZ |
790 | case 'L': |
791 | no_overlap = 1; | |
792 | break; | |
9f56106d | 793 | case 'n': |
7e86cd54 | 794 | no_headings = 1; |
9f56106d KZ |
795 | break; |
796 | case OPT_RAW: | |
7e86cd54 | 797 | raw = 1; |
9f56106d | 798 | break; |
22853e4a | 799 | case 'o': |
e9e426eb | 800 | offset = strtosize_or_err(optarg, _("failed to parse offset")); |
bc0ac075 | 801 | flags |= LOOPDEV_FL_OFFSET; |
22853e4a | 802 | break; |
896352d3 OO |
803 | case 'O': |
804 | outarg = optarg; | |
65178cb3 | 805 | list = 1; |
896352d3 | 806 | break; |
289673b1 | 807 | case OPT_OUTPUT_ALL: |
cdc69063 | 808 | list = 1; |
289673b1 SK |
809 | for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++) |
810 | columns[ncolumns] = ncolumns; | |
811 | break; | |
916bf85e KZ |
812 | case 'P': |
813 | lo_flags |= LO_FLAGS_PARTSCAN; | |
814 | break; | |
aadb9303 | 815 | case OPT_SHOW: |
ba3809b0 KZ |
816 | showdev = 1; |
817 | break; | |
64c3bb3c | 818 | case OPT_DIO: |
f98d9641 KZ |
819 | use_dio = set_dio = 1; |
820 | if (optarg) | |
818341be | 821 | use_dio = ul_parse_switch(optarg, "on", "off", NULL); |
d53346ed AXH |
822 | if (use_dio) |
823 | lo_flags |= LO_FLAGS_DIRECT_IO; | |
64c3bb3c | 824 | break; |
22853e4a | 825 | case 'v': |
22853e4a | 826 | break; |
aadb9303 | 827 | case OPT_SIZELIMIT: /* --sizelimit */ |
e9e426eb | 828 | sizelimit = strtosize_or_err(optarg, _("failed to parse size")); |
c7e0925d | 829 | flags |= LOOPDEV_FL_SIZELIMIT; |
7bcefc7f | 830 | break; |
2c308875 KZ |
831 | |
832 | case 'h': | |
833 | usage(); | |
834 | case 'V': | |
835 | print_version(EXIT_SUCCESS); | |
22853e4a | 836 | default: |
677ec86c | 837 | errtryhelp(EXIT_FAILURE); |
22853e4a KZ |
838 | } |
839 | } | |
d162fcb5 | 840 | |
bcf445fd KZ |
841 | ul_path_init_debug(); |
842 | ul_sysfs_init_debug(); | |
843 | ||
896352d3 OO |
844 | /* default is --list --all */ |
845 | if (argc == 1) { | |
846 | act = A_SHOW; | |
847 | list = 1; | |
848 | } | |
849 | ||
4827093d KZ |
850 | if (!act && argc == 2 && (raw || json)) { |
851 | act = A_SHOW; | |
852 | list = 1; | |
853 | } | |
854 | ||
896352d3 OO |
855 | /* default --list output columns */ |
856 | if (list && !ncolumns) { | |
857 | columns[ncolumns++] = COL_NAME; | |
4ad996d7 | 858 | columns[ncolumns++] = COL_SIZELIMIT; |
896352d3 OO |
859 | columns[ncolumns++] = COL_OFFSET; |
860 | columns[ncolumns++] = COL_AUTOCLR; | |
861 | columns[ncolumns++] = COL_RO; | |
862 | columns[ncolumns++] = COL_BACK_FILE; | |
faeef4d2 | 863 | columns[ncolumns++] = COL_DIO; |
76493ceb | 864 | columns[ncolumns++] = COL_LOGSEC; |
896352d3 | 865 | } |
39fde137 | 866 | |
c7e0925d KZ |
867 | if (act == A_FIND_FREE && optind < argc) { |
868 | /* | |
869 | * losetup -f <backing_file> | |
870 | */ | |
871 | act = A_CREATE; | |
872 | file = argv[optind++]; | |
c3f5a0f1 KZ |
873 | |
874 | if (optind < argc) | |
875 | errx(EXIT_FAILURE, _("unexpected arguments")); | |
c7e0925d | 876 | } |
896352d3 OO |
877 | |
878 | if (list && !act && optind == argc) | |
879 | /* | |
880 | * losetup --list defaults to --all | |
881 | */ | |
882 | act = A_SHOW; | |
883 | ||
09ec0c0a KZ |
884 | if (!act && optind + 1 == argc) { |
885 | /* | |
896352d3 | 886 | * losetup [--list] <device> |
64c3bb3c | 887 | * OR |
a1a41597 | 888 | * losetup {--direct-io[=off]|--logical-blocksize=size}... <device> |
09ec0c0a | 889 | */ |
5b3bb7da | 890 | if (set_dio) { |
64c3bb3c | 891 | act = A_SET_DIRECT_IO; |
5b3bb7da KZ |
892 | lo_flags &= ~LO_FLAGS_DIRECT_IO; |
893 | } else if (set_blocksize) | |
a1a41597 | 894 | act = A_SET_BLOCKSIZE; |
5b3bb7da KZ |
895 | else |
896 | act = A_SHOW_ONE; | |
897 | ||
a6ca0456 | 898 | if (loopcxt_set_device(&lc, argv[optind])) |
defa0710 KZ |
899 | err(EXIT_FAILURE, _("%s: failed to use device"), |
900 | argv[optind]); | |
901 | optind++; | |
09ec0c0a | 902 | } |
c7e0925d KZ |
903 | if (!act) { |
904 | /* | |
905 | * losetup <loopdev> <backing_file> | |
906 | */ | |
907 | act = A_CREATE; | |
908 | ||
909 | if (optind >= argc) | |
910 | errx(EXIT_FAILURE, _("no loop device specified")); | |
21ce3f3a | 911 | /* don't use is_loopdev() here, the device does not have exist yet */ |
defa0710 | 912 | if (loopcxt_set_device(&lc, argv[optind])) |
e9e7698e | 913 | err(EXIT_FAILURE, _("%s: failed to use device"), |
defa0710 KZ |
914 | argv[optind]); |
915 | optind++; | |
c7e0925d KZ |
916 | |
917 | if (optind >= argc) | |
918 | errx(EXIT_FAILURE, _("no file specified")); | |
919 | file = argv[optind++]; | |
920 | } | |
921 | ||
922 | if (act != A_CREATE && | |
5cf05c71 | 923 | (sizelimit || lo_flags || showdev)) |
c7e0925d | 924 | errx(EXIT_FAILURE, |
136b23ef | 925 | _("the options %s are allowed during loop device setup only"), |
014e7eb3 | 926 | "--{sizelimit,partscan,read-only,show}"); |
c7e0925d | 927 | |
934df30d KZ |
928 | if ((flags & LOOPDEV_FL_OFFSET) && |
929 | act != A_CREATE && (act != A_SHOW || !file)) | |
136b23ef | 930 | errx(EXIT_FAILURE, _("the option --offset is not allowed in this context")); |
c654c4f0 | 931 | |
896352d3 OO |
932 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
933 | &ncolumns, column_name_to_id) < 0) | |
934 | return EXIT_FAILURE; | |
935 | ||
39fde137 | 936 | switch (act) { |
c7e0925d | 937 | case A_CREATE: |
db8f7815 | 938 | res = create_loop(&lc, no_overlap, lo_flags, flags, file, refname, |
422f0e9f | 939 | offset, sizelimit, blocksize); |
35545456 KZ |
940 | if (res == 0) { |
941 | if (showdev) | |
942 | printf("%s\n", loopcxt_get_device(&lc)); | |
6daf185c | 943 | warn_size(file, sizelimit, offset, flags); |
35545456 | 944 | } |
c7e0925d | 945 | break; |
c654c4f0 KZ |
946 | case A_DELETE: |
947 | res = delete_loop(&lc); | |
948 | while (optind < argc) { | |
a6ca0456 | 949 | if (loopcxt_set_device(&lc, argv[optind])) |
defa0710 KZ |
950 | warn(_("%s: failed to use device"), |
951 | argv[optind]); | |
952 | optind++; | |
c654c4f0 KZ |
953 | res += delete_loop(&lc); |
954 | } | |
955 | break; | |
956 | case A_DELETE_ALL: | |
957 | res = delete_all_loops(&lc); | |
958 | break; | |
bcdbdc72 | 959 | case A_FIND_FREE: |
af9fc22d TW |
960 | res = find_unused(&lc); |
961 | if (!res) | |
556dac80 JB |
962 | printf("%s%s\n", loopcxt_get_device(&lc), |
963 | loopcxt_is_lost(&lc) ? " (lost)" : ""); | |
bcdbdc72 | 964 | break; |
39fde137 | 965 | case A_SHOW: |
896352d3 | 966 | if (list) |
48f1f38b | 967 | res = show_table(&lc, file, offset, flags); |
896352d3 OO |
968 | else |
969 | res = show_all_loops(&lc, file, offset, flags); | |
bc0ac075 | 970 | break; |
09ec0c0a | 971 | case A_SHOW_ONE: |
896352d3 | 972 | if (list) |
48f1f38b | 973 | res = show_table(&lc, NULL, 0, 0); |
896352d3 OO |
974 | else |
975 | res = printf_loopdev(&lc); | |
09ec0c0a | 976 | if (res) |
136b23ef | 977 | warn("%s", loopcxt_get_device(&lc)); |
09ec0c0a | 978 | break; |
6e90a44c | 979 | case A_SET_CAPACITY: |
9fcc8936 | 980 | res = loopcxt_ioctl_capacity(&lc); |
293714c0 JM |
981 | if (res) |
982 | warn(_("%s: set capacity failed"), | |
983 | loopcxt_get_device(&lc)); | |
6e90a44c | 984 | break; |
64c3bb3c | 985 | case A_SET_DIRECT_IO: |
422f0e9f KZ |
986 | res = loopcxt_ioctl_dio(&lc, use_dio); |
987 | if (res) | |
988 | warn(_("%s: set direct io failed"), | |
989 | loopcxt_get_device(&lc)); | |
990 | break; | |
a1a41597 | 991 | case A_SET_BLOCKSIZE: |
422f0e9f KZ |
992 | res = loopcxt_ioctl_blocksize(&lc, blocksize); |
993 | if (res) | |
994 | warn(_("%s: set logical block size failed"), | |
995 | loopcxt_get_device(&lc)); | |
64c3bb3c | 996 | break; |
39fde137 | 997 | default: |
6e1eda6f RM |
998 | warnx(_("bad usage")); |
999 | errtryhelp(EXIT_FAILURE); | |
39fde137 KZ |
1000 | break; |
1001 | } | |
1002 | ||
a6ca0456 | 1003 | if (res && (act == A_SET_CAPACITY |
556dac80 | 1004 | || act == A_CREATE |
a6ca0456 JB |
1005 | || act == A_SET_DIRECT_IO |
1006 | || act == A_SET_BLOCKSIZE) | |
1007 | && loopcxt_is_lost(&lc)) | |
1008 | warn_lost(&lc); | |
1009 | ||
39fde137 | 1010 | loopcxt_deinit(&lc); |
c7e0925d | 1011 | return res ? EXIT_FAILURE : EXIT_SUCCESS; |
22853e4a | 1012 | } |