blob: d450b5a7825c3e8568093c3becd129c6be41b29b [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Matt Kraai83788da2002-03-20 17:38:37 +00002/*
3 * Mini losetup implementation for busybox
4 *
5 * Copyright (C) 2002 Matt Kraai.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Matt Kraai83788da2002-03-20 17:38:37 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define losetup_trivial_usage
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080011//usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage: " losetup -d LOOPDEV - disassociate\n"
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020013//usage: " losetup -a - show status\n"
14//usage: " losetup -f - show next free loop device"
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage:#define losetup_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020016//usage: " -o OFS Start OFS bytes into FILE"
Denys Vlasenko13e709c2011-09-12 02:13:47 +020017//usage: "\n -r Read-only"
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020018//usage: "\n -f Show/use next free loop device"
Pere Orga5bc8c002011-04-11 03:29:49 +020019//usage:
20//usage:#define losetup_notes_usage
Pere Orga5bc8c002011-04-11 03:29:49 +020021//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 Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Matt Kraai83788da2002-03-20 17:38:37 +000029
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020030/* 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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000034int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko92510142010-05-19 00:39:17 +020035int losetup_main(int argc UNUSED_PARAM, char **argv)
Matt Kraai83788da2002-03-20 17:38:37 +000036{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000037 unsigned opt;
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000038 char *opt_o;
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080039 char dev[LOOP_NAMESIZE];
Denys Vlasenko92510142010-05-19 00:39:17 +020040 enum {
41 OPT_d = (1 << 0),
42 OPT_o = (1 << 1),
43 OPT_f = (1 << 2),
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080044 OPT_a = (1 << 3),
45 OPT_r = (1 << 4), /* must be last */
Denys Vlasenko92510142010-05-19 00:39:17 +020046 };
Matt Kraai83788da2002-03-20 17:38:37 +000047
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080048 opt_complementary = "?2:d--ofar:a--ofr";
49 opt = getopt32(argv, "do:far", &opt_o);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000050 argv += optind;
Matt Kraai83788da2002-03-20 17:38:37 +000051
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080052 /* LOOPDEV */
53 if (!opt && argv[0] && !argv[1]) {
Denys Vlasenko92510142010-05-19 00:39:17 +020054 char *s;
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000055
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000056 s = query_loop(argv[0]);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000057 if (!s)
Denis Vlasenko94e33652007-12-22 15:44:23 +000058 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000059 printf("%s: %s\n", argv[0], s);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000060 if (ENABLE_FEATURE_CLEAN_UP)
61 free(s);
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000062 return EXIT_SUCCESS;
63 }
64
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080065 /* -d LOOPDEV */
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020066 if (opt == OPT_d && argv[0]) {
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080067 if (del_loop(argv[0]))
68 bb_simple_perror_msg_and_die(argv[0]);
69 return EXIT_SUCCESS;
70 }
Denys Vlasenko92510142010-05-19 00:39:17 +020071
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080072 /* -a */
73 if (opt == OPT_a) {
74 int n;
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020075 for (n = 0; n < MAX_LOOP_NUM; n++) {
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080076 char *s;
77
78 sprintf(dev, LOOP_FORMAT, n);
79 s = query_loop(dev);
80 if (s) {
Denis Vlasenko7ae209c2007-09-26 17:54:18 +000081 printf("%s: %s\n", dev, s);
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020082 free(s);
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080083 }
84 }
85 return EXIT_SUCCESS;
86 }
87
88 /* contains -f */
89 if (opt & OPT_f) {
90 char *s;
91 int n = 0;
92
93 do {
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020094 if (n > MAX_LOOP_NUM)
95 bb_error_msg_and_die("no free loop devices");
96 sprintf(dev, LOOP_FORMAT, n++);
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080097 s = query_loop(dev);
Denys Vlasenko4928e9f2013-06-27 03:45:16 +020098 free(s);
Mandeep Singh Baines7991d452013-03-04 16:33:12 -080099 } while (s);
Denys Vlasenko4928e9f2013-06-27 03:45:16 +0200100 /* now: dev is next free "/dev/loopN" */
Mandeep Singh Baines7991d452013-03-04 16:33:12 -0800101 if ((opt == OPT_f) && !argv[0]) {
102 puts(dev);
103 return EXIT_SUCCESS;
Denis Vlasenko956a5692006-09-27 14:51:27 +0000104 }
105 }
Mandeep Singh Baines7991d452013-03-04 16:33:12 -0800106
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
Denys Vlasenko4928e9f2013-06-27 03:45:16 +0200112 if (opt & OPT_o)
Mandeep Singh Baines7991d452013-03-04 16:33:12 -0800113 offset = xatoull(opt_o);
Denys Vlasenko4928e9f2013-06-27 03:45:16 +0200114 if (!(opt & OPT_f))
115 d = *argv++;
Mandeep Singh Baines7991d452013-03-04 16:33:12 -0800116
117 if (argv[0]) {
Denys Vlasenko4928e9f2013-06-27 03:45:16 +0200118 if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
Mandeep Singh Baines7991d452013-03-04 16:33:12 -0800119 bb_simple_perror_msg_and_die(argv[0]);
120 return EXIT_SUCCESS;
121 }
122 }
123
Denys Vlasenko4928e9f2013-06-27 03:45:16 +0200124 bb_show_usage(); /* does not return */
125 /*return EXIT_FAILURE;*/
Matt Kraai83788da2002-03-20 17:38:37 +0000126}