losetup.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\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 -r Read-only"
  27. //usage: "\n -f Show/use next free loop device"
  28. //usage:
  29. //usage:#define losetup_notes_usage
  30. //usage: "One argument (losetup /dev/loop1) will display the current association\n"
  31. //usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
  32. //usage: "and filename of the file the loop device is currently bound to.\n\n"
  33. //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
  34. //usage: "with an optional offset (-o 12345). Encryption is not yet supported.\n"
  35. //usage: "losetup -f will show the first loop free loop device\n\n"
  36. #include "libbb.h"
  37. /* 1048575 is a max possible minor number in Linux circa 2010 */
  38. /* for now use something less extreme */
  39. #define MAX_LOOP_NUM 1023
  40. int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41. int losetup_main(int argc UNUSED_PARAM, char **argv)
  42. {
  43. unsigned opt;
  44. char *opt_o;
  45. char dev[LOOP_NAMESIZE];
  46. enum {
  47. OPT_d = (1 << 0),
  48. OPT_o = (1 << 1),
  49. OPT_f = (1 << 2),
  50. OPT_a = (1 << 3),
  51. OPT_r = (1 << 4), /* must be last */
  52. };
  53. opt = getopt32(argv, "^" "do:far" "\0" "?2:d--ofar:a--ofr", &opt_o);
  54. argv += optind;
  55. /* LOOPDEV */
  56. if (!opt && argv[0] && !argv[1]) {
  57. char *s;
  58. s = query_loop(argv[0]);
  59. if (!s)
  60. bb_simple_perror_msg_and_die(argv[0]);
  61. printf("%s: %s\n", argv[0], s);
  62. if (ENABLE_FEATURE_CLEAN_UP)
  63. free(s);
  64. return EXIT_SUCCESS;
  65. }
  66. /* -d LOOPDEV */
  67. if (opt == OPT_d && argv[0]) {
  68. if (del_loop(argv[0]))
  69. bb_simple_perror_msg_and_die(argv[0]);
  70. return EXIT_SUCCESS;
  71. }
  72. /* -a */
  73. if (opt == OPT_a) {
  74. int n;
  75. for (n = 0; n < MAX_LOOP_NUM; n++) {
  76. char *s;
  77. sprintf(dev, LOOP_FORMAT, n);
  78. s = query_loop(dev);
  79. if (s) {
  80. printf("%s: %s\n", dev, s);
  81. free(s);
  82. }
  83. }
  84. return EXIT_SUCCESS;
  85. }
  86. /* contains -f */
  87. if (opt & OPT_f) {
  88. char *s;
  89. int n = 0;
  90. do {
  91. if (n > MAX_LOOP_NUM)
  92. bb_error_msg_and_die("no free loop devices");
  93. sprintf(dev, LOOP_FORMAT, n++);
  94. s = query_loop(dev);
  95. free(s);
  96. } while (s);
  97. /* now: dev is next free "/dev/loopN" */
  98. if ((opt == OPT_f) && !argv[0]) {
  99. puts(dev);
  100. return EXIT_SUCCESS;
  101. }
  102. }
  103. /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
  104. if (argv[0] && ((opt & OPT_f) || argv[1])) {
  105. unsigned long long offset = 0;
  106. char *d = dev;
  107. if (opt & OPT_o)
  108. offset = xatoull(opt_o);
  109. if (!(opt & OPT_f))
  110. d = *argv++;
  111. if (argv[0]) {
  112. if (set_loop(&d, argv[0], offset, (opt & OPT_r) ? BB_LO_FLAGS_READ_ONLY : 0) < 0)
  113. bb_simple_perror_msg_and_die(argv[0]);
  114. return EXIT_SUCCESS;
  115. }
  116. }
  117. /* TODO: util-linux 2.28 shows this when run w/o params:
  118. * NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO
  119. * /dev/loop0 0 0 1 0 /PATH/TO/FILE 0
  120. *
  121. * implemented by reading /sys:
  122. *
  123. * open("/sys/block", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
  124. * newfstatat(3, "loop0/loop/backing_file", {st_mode=S_IFREG|0444, st_size=4096, ...}, 0) = 0
  125. * stat("/dev/loop0", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
  126. * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
  127. * read(5, "0\n", 4096) = 2
  128. * open("/sys/dev/block/7:0/loop/sizelimit", O_RDONLY|O_CLOEXEC) = 5
  129. * read(5, "0\n", 4096) = 2
  130. * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
  131. * read(5, "0\n", 4096) = 2
  132. * open("/sys/dev/block/7:0/loop/autoclear", O_RDONLY|O_CLOEXEC) = 5
  133. * read(5, "1\n", 4096) = 2
  134. * open("/sys/dev/block/7:0/ro", O_RDONLY|O_CLOEXEC) = 5
  135. * read(5, "0\n", 4096) = 2
  136. * open("/sys/dev/block/7:0/loop/backing_file", O_RDONLY|O_CLOEXEC) = 5
  137. * read(5, "/PATH/TO/FILE", 4096) = 37
  138. * open("/sys/dev/block/7:0/loop/dio", O_RDONLY|O_CLOEXEC) = 5
  139. * read(5, "0\n", 4096) = 2
  140. */
  141. bb_show_usage(); /* does not return */
  142. /*return EXIT_FAILURE;*/
  143. }