3
0

losetup.c 5.6 KB

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