losetup.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. //usage:#define losetup_trivial_usage
  10. //usage: "[-r] [-o OFS] LOOPDEV FILE - associate loop devices\n"
  11. //usage: " losetup -d LOOPDEV - disassociate\n"
  12. //usage: " losetup [-f] - show"
  13. //usage:#define losetup_full_usage "\n\n"
  14. //usage: " -o OFS Start OFS bytes into FILE"
  15. //usage: "\n -r Read-only"
  16. //usage: "\n -f Show first free loop device"
  17. //usage:
  18. //usage:#define losetup_notes_usage
  19. //usage: "No arguments will display all current associations.\n"
  20. //usage: "One argument (losetup /dev/loop1) will display the current association\n"
  21. //usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
  22. //usage: "and filename of the file the loop device is currently bound to.\n\n"
  23. //usage: "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
  24. //usage: "with an optional offset (-o 12345). Encryption is not yet supported.\n"
  25. //usage: "losetup -f will show the first loop free loop device\n\n"
  26. #include "libbb.h"
  27. int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  28. int losetup_main(int argc UNUSED_PARAM, char **argv)
  29. {
  30. unsigned opt;
  31. int n;
  32. char *opt_o;
  33. unsigned long long offset = 0;
  34. enum {
  35. OPT_d = (1 << 0),
  36. OPT_o = (1 << 1),
  37. OPT_f = (1 << 2),
  38. OPT_r = (1 << 3), /* must be last */
  39. };
  40. /* max 2 args, -d,-o,-f opts are mutually exclusive */
  41. opt_complementary = "?2:d--of:o--df:f--do";
  42. opt = getopt32(argv, "do:fr", &opt_o);
  43. argv += optind;
  44. if (opt == OPT_o)
  45. offset = xatoull(opt_o);
  46. if (opt == OPT_d) {
  47. /* -d BLOCKDEV */
  48. if (!argv[0] || argv[1])
  49. bb_show_usage();
  50. if (del_loop(argv[0]))
  51. bb_simple_perror_msg_and_die(argv[0]);
  52. return EXIT_SUCCESS;
  53. }
  54. if (argv[0]) {
  55. char *s;
  56. if (opt == OPT_f) /* -f should not have arguments */
  57. bb_show_usage();
  58. if (argv[1]) {
  59. /* [-r] [-o OFS] BLOCKDEV FILE */
  60. if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0)
  61. bb_simple_perror_msg_and_die(argv[0]);
  62. return EXIT_SUCCESS;
  63. }
  64. /* [-r] [-o OFS] BLOCKDEV */
  65. s = query_loop(argv[0]);
  66. if (!s)
  67. bb_simple_perror_msg_and_die(argv[0]);
  68. printf("%s: %s\n", argv[0], s);
  69. if (ENABLE_FEATURE_CLEAN_UP)
  70. free(s);
  71. return EXIT_SUCCESS;
  72. }
  73. /* [-r] [-o OFS|-f] with no params */
  74. n = 0;
  75. while (1) {
  76. char *s;
  77. char dev[LOOP_NAMESIZE];
  78. sprintf(dev, LOOP_FORMAT, n);
  79. s = query_loop(dev);
  80. n++;
  81. if (!s) {
  82. if (n > 9 && errno && errno != ENXIO)
  83. return EXIT_SUCCESS;
  84. if (opt == OPT_f) {
  85. puts(dev);
  86. return EXIT_SUCCESS;
  87. }
  88. } else {
  89. if (opt != OPT_f)
  90. printf("%s: %s\n", dev, s);
  91. if (ENABLE_FEATURE_CLEAN_UP)
  92. free(s);
  93. }
  94. }
  95. return EXIT_SUCCESS;
  96. }