[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
| 3 | * Mini losetup implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2002 Matt Kraai.
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[3232] | 10 | //usage:#define losetup_trivial_usage
|
---|
| 11 | //usage: "[-r] [-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"
|
---|
| 15 | //usage: " -o OFS Start OFS bytes into FILE"
|
---|
| 16 | //usage: "\n -r Read-only"
|
---|
| 17 | //usage: "\n -f Show first free loop device"
|
---|
| 18 | //usage:
|
---|
| 19 | //usage:#define losetup_notes_usage
|
---|
| 20 | //usage: "No arguments will display all current associations.\n"
|
---|
| 21 | //usage: "One argument (losetup /dev/loop1) will display the current association\n"
|
---|
| 22 | //usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
|
---|
| 23 | //usage: "and filename of the file the loop device is currently bound to.\n\n"
|
---|
| 24 | //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
|
---|
| 25 | //usage: "with an optional offset (-o 12345). Encryption is not yet supported.\n"
|
---|
| 26 | //usage: "losetup -f will show the first loop free loop device\n\n"
|
---|
| 27 |
|
---|
[1765] | 28 | #include "libbb.h"
|
---|
[821] | 29 |
|
---|
[2725] | 30 | int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 31 | int losetup_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 32 | {
|
---|
[1765] | 33 | unsigned opt;
|
---|
[2725] | 34 | int n;
|
---|
[1765] | 35 | char *opt_o;
|
---|
| 36 | unsigned long long offset = 0;
|
---|
[2725] | 37 | enum {
|
---|
| 38 | OPT_d = (1 << 0),
|
---|
| 39 | OPT_o = (1 << 1),
|
---|
| 40 | OPT_f = (1 << 2),
|
---|
[3232] | 41 | OPT_r = (1 << 3), /* must be last */
|
---|
[2725] | 42 | };
|
---|
[821] | 43 |
|
---|
[3232] | 44 | /* max 2 args, -d,-o,-f opts are mutually exclusive */
|
---|
[2725] | 45 | opt_complementary = "?2:d--of:o--df:f--do";
|
---|
[3232] | 46 | opt = getopt32(argv, "do:fr", &opt_o);
|
---|
[1765] | 47 | argv += optind;
|
---|
[821] | 48 |
|
---|
[2725] | 49 | if (opt == OPT_o)
|
---|
| 50 | offset = xatoull(opt_o);
|
---|
[1765] | 51 |
|
---|
[2725] | 52 | if (opt == OPT_d) {
|
---|
| 53 | /* -d BLOCKDEV */
|
---|
| 54 | if (!argv[0] || argv[1])
|
---|
[1765] | 55 | bb_show_usage();
|
---|
[2725] | 56 | if (del_loop(argv[0]))
|
---|
| 57 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 58 | return EXIT_SUCCESS;
|
---|
[1765] | 59 | }
|
---|
| 60 |
|
---|
[2725] | 61 | if (argv[0]) {
|
---|
| 62 | char *s;
|
---|
[1765] | 63 |
|
---|
[2725] | 64 | if (opt == OPT_f) /* -f should not have arguments */
|
---|
| 65 | bb_show_usage();
|
---|
[1765] | 66 |
|
---|
[2725] | 67 | if (argv[1]) {
|
---|
[3232] | 68 | /* [-r] [-o OFS] BLOCKDEV FILE */
|
---|
| 69 | if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0)
|
---|
[2725] | 70 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 71 | return EXIT_SUCCESS;
|
---|
| 72 | }
|
---|
[3232] | 73 | /* [-r] [-o OFS] BLOCKDEV */
|
---|
[2725] | 74 | s = query_loop(argv[0]);
|
---|
[1765] | 75 | if (!s)
|
---|
[2725] | 76 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
[1765] | 77 | printf("%s: %s\n", argv[0], s);
|
---|
| 78 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 79 | free(s);
|
---|
[2725] | 80 | return EXIT_SUCCESS;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[3232] | 83 | /* [-r] [-o OFS|-f] with no params */
|
---|
[2725] | 84 | n = 0;
|
---|
| 85 | while (1) {
|
---|
| 86 | char *s;
|
---|
| 87 | char dev[LOOP_NAMESIZE];
|
---|
| 88 |
|
---|
| 89 | sprintf(dev, LOOP_FORMAT, n);
|
---|
| 90 | s = query_loop(dev);
|
---|
| 91 | n++;
|
---|
| 92 | if (!s) {
|
---|
| 93 | if (n > 9 && errno && errno != ENXIO)
|
---|
| 94 | return EXIT_SUCCESS;
|
---|
| 95 | if (opt == OPT_f) {
|
---|
| 96 | puts(dev);
|
---|
| 97 | return EXIT_SUCCESS;
|
---|
| 98 | }
|
---|
| 99 | } else {
|
---|
| 100 | if (opt != OPT_f)
|
---|
[1765] | 101 | printf("%s: %s\n", dev, s);
|
---|
[2725] | 102 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 103 | free(s);
|
---|
[1765] | 104 | }
|
---|
| 105 | }
|
---|
[821] | 106 | return EXIT_SUCCESS;
|
---|
| 107 | }
|
---|