[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
|
---|
[3621] | 11 | //usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
|
---|
[3232] | 12 | //usage: " losetup -d LOOPDEV - disassociate\n"
|
---|
[3621] | 13 | //usage: " losetup -a - show status\n"
|
---|
| 14 | //usage: " losetup -f - show next free loop device"
|
---|
[3232] | 15 | //usage:#define losetup_full_usage "\n\n"
|
---|
| 16 | //usage: " -o OFS Start OFS bytes into FILE"
|
---|
| 17 | //usage: "\n -r Read-only"
|
---|
[3621] | 18 | //usage: "\n -f Show/use next free loop device"
|
---|
[3232] | 19 | //usage:
|
---|
| 20 | //usage:#define losetup_notes_usage
|
---|
| 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 |
|
---|
[3621] | 30 | /* 1048575 is a max possible minor number in Linux circa 2010 */
|
---|
| 31 | /* for now use something less extreme */
|
---|
| 32 | #define MAX_LOOP_NUM 1023
|
---|
| 33 |
|
---|
[2725] | 34 | int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 35 | int losetup_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 36 | {
|
---|
[1765] | 37 | unsigned opt;
|
---|
| 38 | char *opt_o;
|
---|
[3621] | 39 | char dev[LOOP_NAMESIZE];
|
---|
[2725] | 40 | enum {
|
---|
| 41 | OPT_d = (1 << 0),
|
---|
| 42 | OPT_o = (1 << 1),
|
---|
| 43 | OPT_f = (1 << 2),
|
---|
[3621] | 44 | OPT_a = (1 << 3),
|
---|
| 45 | OPT_r = (1 << 4), /* must be last */
|
---|
[2725] | 46 | };
|
---|
[821] | 47 |
|
---|
[3621] | 48 | opt_complementary = "?2:d--ofar:a--ofr";
|
---|
| 49 | opt = getopt32(argv, "do:far", &opt_o);
|
---|
[1765] | 50 | argv += optind;
|
---|
[821] | 51 |
|
---|
[3621] | 52 | /* LOOPDEV */
|
---|
| 53 | if (!opt && argv[0] && !argv[1]) {
|
---|
[2725] | 54 | char *s;
|
---|
[1765] | 55 |
|
---|
[2725] | 56 | s = query_loop(argv[0]);
|
---|
[1765] | 57 | if (!s)
|
---|
[2725] | 58 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
[1765] | 59 | printf("%s: %s\n", argv[0], s);
|
---|
| 60 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 61 | free(s);
|
---|
[2725] | 62 | return EXIT_SUCCESS;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[3621] | 65 | /* -d LOOPDEV */
|
---|
| 66 | if (opt == OPT_d && argv[0]) {
|
---|
| 67 | if (del_loop(argv[0]))
|
---|
| 68 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 69 | return EXIT_SUCCESS;
|
---|
| 70 | }
|
---|
[2725] | 71 |
|
---|
[3621] | 72 | /* -a */
|
---|
| 73 | if (opt == OPT_a) {
|
---|
| 74 | int n;
|
---|
| 75 | for (n = 0; n < MAX_LOOP_NUM; n++) {
|
---|
| 76 | char *s;
|
---|
| 77 |
|
---|
| 78 | sprintf(dev, LOOP_FORMAT, n);
|
---|
| 79 | s = query_loop(dev);
|
---|
| 80 | if (s) {
|
---|
[1765] | 81 | printf("%s: %s\n", dev, s);
|
---|
[2725] | 82 | free(s);
|
---|
[3621] | 83 | }
|
---|
[1765] | 84 | }
|
---|
[3621] | 85 | return EXIT_SUCCESS;
|
---|
[1765] | 86 | }
|
---|
[3621] | 87 |
|
---|
| 88 | /* contains -f */
|
---|
| 89 | if (opt & OPT_f) {
|
---|
| 90 | char *s;
|
---|
| 91 | int n = 0;
|
---|
| 92 |
|
---|
| 93 | do {
|
---|
| 94 | if (n > MAX_LOOP_NUM)
|
---|
| 95 | bb_error_msg_and_die("no free loop devices");
|
---|
| 96 | sprintf(dev, LOOP_FORMAT, n++);
|
---|
| 97 | s = query_loop(dev);
|
---|
| 98 | free(s);
|
---|
| 99 | } while (s);
|
---|
| 100 | /* now: dev is next free "/dev/loopN" */
|
---|
| 101 | if ((opt == OPT_f) && !argv[0]) {
|
---|
| 102 | puts(dev);
|
---|
| 103 | return EXIT_SUCCESS;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
|
---|
| 108 | if (argv[0] && ((opt & OPT_f) || argv[1])) {
|
---|
| 109 | unsigned long long offset = 0;
|
---|
| 110 | char *d = dev;
|
---|
| 111 |
|
---|
| 112 | if (opt & OPT_o)
|
---|
| 113 | offset = xatoull(opt_o);
|
---|
| 114 | if (!(opt & OPT_f))
|
---|
| 115 | d = *argv++;
|
---|
| 116 |
|
---|
| 117 | if (argv[0]) {
|
---|
| 118 | if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
|
---|
| 119 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 120 | return EXIT_SUCCESS;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | bb_show_usage(); /* does not return */
|
---|
| 125 | /*return EXIT_FAILURE;*/
|
---|
[821] | 126 | }
|
---|