losetup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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"
  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. //kbuild:lib-$(CONFIG_LOSETUP) += losetup.o
  18. //applet:IF_LOSETUP(APPLET(losetup, BB_DIR_SBIN, BB_SUID_DROP))
  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_complementary = "?2:d--ofar:a--ofr";
  54. opt = getopt32(argv, "do:far", &opt_o);
  55. argv += optind;
  56. /* LOOPDEV */
  57. if (!opt && argv[0] && !argv[1]) {
  58. char *s;
  59. s = query_loop(argv[0]);
  60. if (!s)
  61. bb_simple_perror_msg_and_die(argv[0]);
  62. printf("%s: %s\n", argv[0], s);
  63. if (ENABLE_FEATURE_CLEAN_UP)
  64. free(s);
  65. return EXIT_SUCCESS;
  66. }
  67. /* -d LOOPDEV */
  68. if (opt == OPT_d && argv[0]) {
  69. if (del_loop(argv[0]))
  70. bb_simple_perror_msg_and_die(argv[0]);
  71. return EXIT_SUCCESS;
  72. }
  73. /* -a */
  74. if (opt == OPT_a) {
  75. int n;
  76. for (n = 0; n < MAX_LOOP_NUM; n++) {
  77. char *s;
  78. sprintf(dev, LOOP_FORMAT, n);
  79. s = query_loop(dev);
  80. if (s) {
  81. printf("%s: %s\n", dev, s);
  82. free(s);
  83. }
  84. }
  85. return EXIT_SUCCESS;
  86. }
  87. /* contains -f */
  88. if (opt & OPT_f) {
  89. char *s;
  90. int n = 0;
  91. do {
  92. if (n > MAX_LOOP_NUM)
  93. bb_error_msg_and_die("no free loop devices");
  94. sprintf(dev, LOOP_FORMAT, n++);
  95. s = query_loop(dev);
  96. free(s);
  97. } while (s);
  98. /* now: dev is next free "/dev/loopN" */
  99. if ((opt == OPT_f) && !argv[0]) {
  100. puts(dev);
  101. return EXIT_SUCCESS;
  102. }
  103. }
  104. /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
  105. if (argv[0] && ((opt & OPT_f) || argv[1])) {
  106. unsigned long long offset = 0;
  107. char *d = dev;
  108. if (opt & OPT_o)
  109. offset = xatoull(opt_o);
  110. if (!(opt & OPT_f))
  111. d = *argv++;
  112. if (argv[0]) {
  113. if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
  114. bb_simple_perror_msg_and_die(argv[0]);
  115. return EXIT_SUCCESS;
  116. }
  117. }
  118. bb_show_usage(); /* does not return */
  119. /*return EXIT_FAILURE;*/
  120. }