"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini losetup implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2002 Matt Kraai. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 10 | //usage:#define losetup_trivial_usage |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 11 | //usage: "[-r] [-o OFS] LOOPDEV FILE - associate loop devices\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 12 | //usage: " losetup -d LOOPDEV - disassociate\n" |
| 13 | //usage: " losetup [-f] - show" |
| 14 | //usage:#define losetup_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 15 | //usage: " -o OFS Start OFS bytes into FILE" |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 16 | //usage: "\n -r Read-only" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 30 | int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 31 | int losetup_main(int argc UNUSED_PARAM, char **argv) |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 32 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 33 | unsigned opt; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 34 | int n; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 35 | char *opt_o; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 36 | unsigned long long offset = 0; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 37 | enum { |
| 38 | OPT_d = (1 << 0), |
| 39 | OPT_o = (1 << 1), |
| 40 | OPT_f = (1 << 2), |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 41 | OPT_r = (1 << 3), /* must be last */ |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 42 | }; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 44 | /* max 2 args, -d,-o,-f opts are mutually exclusive */ |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 45 | opt_complementary = "?2:d--of:o--df:f--do"; |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 46 | opt = getopt32(argv, "do:fr", &opt_o); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 47 | argv += optind; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 49 | if (opt == OPT_o) |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 50 | offset = xatoull(opt_o); |
| 51 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 52 | if (opt == OPT_d) { |
| 53 | /* -d BLOCKDEV */ |
| 54 | if (!argv[0] || argv[1]) |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 55 | bb_show_usage(); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 56 | if (del_loop(argv[0])) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 57 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 58 | return EXIT_SUCCESS; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 61 | if (argv[0]) { |
| 62 | char *s; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 63 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 64 | if (opt == OPT_f) /* -f should not have arguments */ |
| 65 | bb_show_usage(); |
| 66 | |
| 67 | if (argv[1]) { |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 68 | /* [-r] [-o OFS] BLOCKDEV FILE */ |
| 69 | if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0) |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 70 | bb_simple_perror_msg_and_die(argv[0]); |
| 71 | return EXIT_SUCCESS; |
| 72 | } |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 73 | /* [-r] [-o OFS] BLOCKDEV */ |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 74 | s = query_loop(argv[0]); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 75 | if (!s) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 76 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 77 | printf("%s: %s\n", argv[0], s); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 78 | if (ENABLE_FEATURE_CLEAN_UP) |
| 79 | free(s); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 80 | return EXIT_SUCCESS; |
| 81 | } |
| 82 | |
Denys Vlasenko | 13e709c | 2011-09-12 02:13:47 +0200 | [diff] [blame] | 83 | /* [-r] [-o OFS|-f] with no params */ |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 84 | n = 0; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 85 | while (1) { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 86 | char *s; |
Lauri Kasanen | 7c1b2b5 | 2010-07-04 00:04:03 +0200 | [diff] [blame] | 87 | char dev[LOOP_NAMESIZE]; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 88 | |
Lauri Kasanen | 7c1b2b5 | 2010-07-04 00:04:03 +0200 | [diff] [blame] | 89 | sprintf(dev, LOOP_FORMAT, n); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 90 | s = query_loop(dev); |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 91 | n++; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 92 | if (!s) { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 93 | if (n > 9 && errno && errno != ENXIO) |
| 94 | return EXIT_SUCCESS; |
| 95 | if (opt == OPT_f) { |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 96 | puts(dev); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 97 | return EXIT_SUCCESS; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 99 | } else { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 100 | if (opt != OPT_f) |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 101 | printf("%s: %s\n", dev, s); |
| 102 | if (ENABLE_FEATURE_CLEAN_UP) |
| 103 | free(s); |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 106 | return EXIT_SUCCESS; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 107 | } |