3
0

losetup.c 2.7 KB

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