mount.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <AK/Optional.h>
  30. #include <LibCore/ArgsParser.h>
  31. #include <LibCore/File.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. static int parse_options(const StringView& options)
  37. {
  38. int flags = 0;
  39. Vector<StringView> parts = options.split_view(',');
  40. for (auto& part : parts) {
  41. if (part == "defaults")
  42. continue;
  43. else if (part == "nodev")
  44. flags |= MS_NODEV;
  45. else if (part == "noexec")
  46. flags |= MS_NOEXEC;
  47. else if (part == "nosuid")
  48. flags |= MS_NOSUID;
  49. else if (part == "bind")
  50. flags |= MS_BIND;
  51. else if (part == "ro")
  52. flags |= MS_RDONLY;
  53. else if (part == "remount")
  54. flags |= MS_REMOUNT;
  55. else
  56. fprintf(stderr, "Ignoring invalid option: %s\n", part.to_string().characters());
  57. }
  58. return flags;
  59. }
  60. static bool is_source_none(const char* source)
  61. {
  62. return !strcmp("none", source);
  63. }
  64. static int get_source_fd(const char* source)
  65. {
  66. if (is_source_none(source))
  67. return -1;
  68. int fd = open(source, O_RDWR);
  69. if (fd < 0)
  70. fd = open(source, O_RDONLY);
  71. if (fd < 0) {
  72. int saved_errno = errno;
  73. auto message = String::format("Failed to open: %s\n", source);
  74. errno = saved_errno;
  75. perror(message.characters());
  76. }
  77. return fd;
  78. }
  79. static bool mount_all()
  80. {
  81. // Mount all filesystems listed in /etc/fstab.
  82. dbg() << "Mounting all filesystems...";
  83. auto fstab = Core::File::construct("/etc/fstab");
  84. if (!fstab->open(Core::IODevice::OpenMode::ReadOnly)) {
  85. fprintf(stderr, "Failed to open /etc/fstab: %s\n", fstab->error_string());
  86. return false;
  87. }
  88. bool all_ok = true;
  89. while (fstab->can_read_line()) {
  90. ByteBuffer buffer = fstab->read_line(1024);
  91. StringView line_view = (const char*)buffer.data();
  92. // Trim the trailing newline, if any.
  93. if (line_view.length() > 0 && line_view[line_view.length() - 1] == '\n')
  94. line_view = line_view.substring_view(0, line_view.length() - 1);
  95. String line = line_view;
  96. // Skip comments and blank lines.
  97. if (line.is_empty() || line.starts_with("#"))
  98. continue;
  99. Vector<String> parts = line.split('\t');
  100. if (parts.size() < 3) {
  101. fprintf(stderr, "Invalid fstab entry: %s\n", line.characters());
  102. all_ok = false;
  103. continue;
  104. }
  105. const char* mountpoint = parts[1].characters();
  106. const char* fstype = parts[2].characters();
  107. int flags = parts.size() >= 4 ? parse_options(parts[3]) : 0;
  108. if (strcmp(mountpoint, "/") == 0) {
  109. dbg() << "Skipping mounting root";
  110. continue;
  111. }
  112. const char* filename = parts[0].characters();
  113. int fd = get_source_fd(filename);
  114. dbg() << "Mounting " << filename << "(" << fstype << ")"
  115. << " on " << mountpoint;
  116. int rc = mount(fd, mountpoint, fstype, flags);
  117. if (rc != 0) {
  118. fprintf(stderr, "Failed to mount %s (FD: %d) (%s) on %s: %s\n", filename, fd, fstype, mountpoint, strerror(errno));
  119. all_ok = false;
  120. continue;
  121. }
  122. }
  123. return all_ok;
  124. }
  125. static bool print_mounts()
  126. {
  127. // Output info about currently mounted filesystems.
  128. auto df = Core::File::construct("/proc/df");
  129. if (!df->open(Core::IODevice::ReadOnly)) {
  130. fprintf(stderr, "Failed to open /proc/df: %s\n", df->error_string());
  131. return false;
  132. }
  133. auto content = df->read_all();
  134. auto json = JsonValue::from_string(content);
  135. ASSERT(json.has_value());
  136. json.value().as_array().for_each([](auto& value) {
  137. auto fs_object = value.as_object();
  138. auto class_name = fs_object.get("class_name").to_string();
  139. auto mount_point = fs_object.get("mount_point").to_string();
  140. auto source = fs_object.get("source").as_string_or("none");
  141. auto readonly = fs_object.get("readonly").to_bool();
  142. auto mount_flags = fs_object.get("mount_flags").to_int();
  143. printf("%s on %s type %s (", source.characters(), mount_point.characters(), class_name.characters());
  144. if (readonly || mount_flags & MS_RDONLY)
  145. printf("ro");
  146. else
  147. printf("rw");
  148. if (mount_flags & MS_NODEV)
  149. printf(",nodev");
  150. if (mount_flags & MS_NOEXEC)
  151. printf(",noexec");
  152. if (mount_flags & MS_NOSUID)
  153. printf(",nosuid");
  154. if (mount_flags & MS_BIND)
  155. printf(",bind");
  156. printf(")\n");
  157. });
  158. return true;
  159. }
  160. int main(int argc, char** argv)
  161. {
  162. const char* source = nullptr;
  163. const char* mountpoint = nullptr;
  164. const char* fs_type = nullptr;
  165. const char* options = nullptr;
  166. bool should_mount_all = false;
  167. Core::ArgsParser args_parser;
  168. args_parser.add_positional_argument(source, "Source path", "source", Core::ArgsParser::Required::No);
  169. args_parser.add_positional_argument(mountpoint, "Mount point", "mountpoint", Core::ArgsParser::Required::No);
  170. args_parser.add_option(fs_type, "File system type", nullptr, 't', "fstype");
  171. args_parser.add_option(options, "Mount options", nullptr, 'o', "options");
  172. args_parser.add_option(should_mount_all, "Mount all file systems listed in /etc/fstab", nullptr, 'a');
  173. args_parser.parse(argc, argv);
  174. if (should_mount_all) {
  175. return mount_all() ? 0 : 1;
  176. }
  177. if (!source && !mountpoint)
  178. return print_mounts() ? 0 : 1;
  179. if (source && mountpoint) {
  180. if (!fs_type)
  181. fs_type = "ext2";
  182. int flags = options ? parse_options(options) : 0;
  183. int fd = get_source_fd(source);
  184. if (mount(fd, mountpoint, fs_type, flags) < 0) {
  185. perror("mount");
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. args_parser.print_usage(stderr, argv[0]);
  191. return 1;
  192. }