losetup.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini losetup implementation for busybox
  4. *
  5. * Copyright (C) 2002 Matt Kraai.
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config LOSETUP
  10. //config: bool "losetup (5.5 kb)"
  11. //config: default y
  12. //config: help
  13. //config: losetup is used to associate or detach a loop device with a regular
  14. //config: file or block device, and to query the status of a loop device. This
  15. //config: version does not currently support enabling data encryption.
  16. //applet:IF_LOSETUP(APPLET_NOEXEC(losetup, losetup, BB_DIR_SBIN, BB_SUID_DROP, losetup))
  17. //kbuild:lib-$(CONFIG_LOSETUP) += losetup.o
  18. //usage:#define losetup_trivial_usage
  19. //usage: "[-rP] [-o OFS] {-f|LOOPDEV} FILE: associate loop devices\n"
  20. //usage: " losetup -c LOOPDEV: reread file size\n"
  21. //usage: " losetup -d LOOPDEV: disassociate\n"
  22. //usage: " losetup -a: show status\n"
  23. //usage: " losetup -f: show next free loop device"
  24. //usage:#define losetup_full_usage "\n\n"
  25. //usage: " -o OFS Start OFS bytes into FILE"
  26. //usage: "\n -P Scan for partitions"
  27. //usage: "\n -r Read-only"
  28. //usage: "\n -f Show/use next free loop device"
  29. //usage:
  30. //usage:#define losetup_notes_usage
  31. //usage: "One argument (losetup /dev/loop1) will display the current association\n"
  32. //usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
  33. //usage: "and filename of the file the loop device is currently bound to.\n\n"
  34. //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
  35. //usage: "with optional partition scanning (creates /dev/loop1p1, /dev/loop1p2\n"
  36. //usage: "etc. with -P) and with an optional offset (-o 12345). Encryption is\n"
  37. //usage: "not yet supported. losetup -f will show the first free loop device\n\n"
  38. #include "libbb.h"
  39. /* 1048575 is a max possible minor number in Linux circa 2010 */
  40. /* for now use something less extreme */
  41. #define MAX_LOOP_NUM 1023
  42. int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  43. int losetup_main(int argc UNUSED_PARAM, char **argv)
  44. {
  45. unsigned opt;
  46. char *opt_o;
  47. char dev[LOOP_NAMESIZE];
  48. enum {
  49. OPT_c = (1 << 0),
  50. OPT_d = (1 << 1),
  51. OPT_P = (1 << 2),
  52. OPT_o = (1 << 3),
  53. OPT_f = (1 << 4),
  54. OPT_a = (1 << 5),
  55. OPT_r = (1 << 6),
  56. };
  57. opt = getopt32(argv, "^" "cdPo:far" "\0" "?2:d--Pofar:a--Pofr", &opt_o);
  58. argv += optind;
  59. /* LOOPDEV */
  60. if (!opt && argv[0] && !argv[1]) {
  61. char *s;
  62. s = query_loop(argv[0]);
  63. if (!s)
  64. bb_simple_perror_msg_and_die(argv[0]);
  65. printf("%s: %s\n", argv[0], s);
  66. if (ENABLE_FEATURE_CLEAN_UP)
  67. free(s);
  68. return EXIT_SUCCESS;
  69. }
  70. /* -c LOOPDEV */
  71. if (opt == OPT_c && argv[0]) {
  72. int fd = xopen(argv[0], O_RDONLY);
  73. #ifndef LOOP_SET_CAPACITY
  74. # define LOOP_SET_CAPACITY 0x4C07
  75. #endif
  76. xioctl(fd, LOOP_SET_CAPACITY, /*ignored:*/0);
  77. return EXIT_SUCCESS;
  78. }
  79. /* -d LOOPDEV */
  80. if (opt == OPT_d && argv[0]) {
  81. if (del_loop(argv[0]))
  82. bb_simple_perror_msg_and_die(argv[0]);
  83. return EXIT_SUCCESS;
  84. }
  85. /* -a */
  86. if (opt == OPT_a) {
  87. int n;
  88. for (n = 0; n < MAX_LOOP_NUM; n++) {
  89. char *s;
  90. sprintf(dev, LOOP_FORMAT, n);
  91. s = query_loop(dev);
  92. if (s) {
  93. printf("%s: %s\n", dev, s);
  94. free(s);
  95. }
  96. }
  97. return EXIT_SUCCESS;
  98. }
  99. /* contains -f */
  100. if (opt & OPT_f) {
  101. char *s;
  102. int n;
  103. n = get_free_loop();
  104. if (n == -1)
  105. bb_simple_error_msg_and_die("no free loop devices");
  106. if (n < 0) /* n == -2: no /dev/loop-control, use legacy method */
  107. n = 0;
  108. /* or: n >= 0: the number of next free loopdev, just verify it */
  109. do {
  110. if (n > MAX_LOOP_NUM)
  111. bb_simple_error_msg_and_die("no free loop devices");
  112. sprintf(dev, LOOP_FORMAT, n++);
  113. s = query_loop(dev);
  114. free(s);
  115. } while (s);
  116. /* now: dev is next free "/dev/loopN" */
  117. if ((opt == OPT_f) && !argv[0]) {
  118. puts(dev);
  119. return EXIT_SUCCESS;
  120. }
  121. }
  122. /* [-rP] [-o OFS] {-f|LOOPDEV} FILE */
  123. if (argv[0] && ((opt & OPT_f) || argv[1])) {
  124. unsigned long long offset = 0;
  125. char *d = dev;
  126. if (opt & OPT_o)
  127. offset = xatoull(opt_o);
  128. if (!(opt & OPT_f))
  129. d = *argv++;
  130. if (argv[0]) {
  131. unsigned flags = (opt & OPT_r) ? BB_LO_FLAGS_READ_ONLY : 0;
  132. if (opt & OPT_P) {
  133. flags |= BB_LO_FLAGS_PARTSCAN;
  134. }
  135. if (set_loop(&d, argv[0], offset, 0, flags) < 0)
  136. bb_simple_perror_msg_and_die(argv[0]);
  137. return EXIT_SUCCESS;
  138. }
  139. }
  140. /* TODO: util-linux 2.28 shows this when run w/o params:
  141. * NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO
  142. * /dev/loop0 0 0 1 0 /PATH/TO/FILE 0
  143. *
  144. * implemented by reading /sys:
  145. *
  146. * open("/sys/block", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
  147. * newfstatat(3, "loop0/loop/backing_file", {st_mode=S_IFREG|0444, st_size=4096, ...}, 0) = 0
  148. * stat("/dev/loop0", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
  149. * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
  150. * read(5, "0\n", 4096) = 2
  151. * open("/sys/dev/block/7:0/loop/sizelimit", O_RDONLY|O_CLOEXEC) = 5
  152. * read(5, "0\n", 4096) = 2
  153. * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
  154. * read(5, "0\n", 4096) = 2
  155. * open("/sys/dev/block/7:0/loop/autoclear", O_RDONLY|O_CLOEXEC) = 5
  156. * read(5, "1\n", 4096) = 2
  157. * open("/sys/dev/block/7:0/ro", O_RDONLY|O_CLOEXEC) = 5
  158. * read(5, "0\n", 4096) = 2
  159. * open("/sys/dev/block/7:0/loop/backing_file", O_RDONLY|O_CLOEXEC) = 5
  160. * read(5, "/PATH/TO/FILE", 4096) = 37
  161. * open("/sys/dev/block/7:0/loop/dio", O_RDONLY|O_CLOEXEC) = 5
  162. * read(5, "0\n", 4096) = 2
  163. */
  164. bb_show_usage(); /* does not return */
  165. /*return EXIT_FAILURE;*/
  166. }