mount.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/ByteBuffer.h>
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/JsonValue.h>
  30. #include <AK/Optional.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/File.h>
  33. #include <fcntl.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. static int parse_options(const StringView& options)
  38. {
  39. int flags = 0;
  40. Vector<StringView> parts = options.split_view(',');
  41. for (auto& part : parts) {
  42. if (part == "defaults")
  43. continue;
  44. else if (part == "nodev")
  45. flags |= MS_NODEV;
  46. else if (part == "noexec")
  47. flags |= MS_NOEXEC;
  48. else if (part == "nosuid")
  49. flags |= MS_NOSUID;
  50. else if (part == "bind")
  51. flags |= MS_BIND;
  52. else if (part == "ro")
  53. flags |= MS_RDONLY;
  54. else if (part == "remount")
  55. flags |= MS_REMOUNT;
  56. else
  57. fprintf(stderr, "Ignoring invalid option: %s\n", part.to_string().characters());
  58. }
  59. return flags;
  60. }
  61. static bool is_source_none(const char* source)
  62. {
  63. return !strcmp("none", source);
  64. }
  65. static int get_source_fd(const char* source)
  66. {
  67. if (is_source_none(source))
  68. return -1;
  69. int fd = open(source, O_RDWR);
  70. if (fd < 0)
  71. fd = open(source, O_RDONLY);
  72. if (fd < 0) {
  73. int saved_errno = errno;
  74. auto message = String::format("Failed to open: %s\n", source);
  75. errno = saved_errno;
  76. perror(message.characters());
  77. }
  78. return fd;
  79. }
  80. static bool mount_all()
  81. {
  82. // Mount all filesystems listed in /etc/fstab.
  83. dbg() << "Mounting all filesystems...";
  84. auto fstab = Core::File::construct("/etc/fstab");
  85. if (!fstab->open(Core::IODevice::OpenMode::ReadOnly)) {
  86. fprintf(stderr, "Failed to open /etc/fstab: %s\n", fstab->error_string());
  87. return false;
  88. }
  89. bool all_ok = true;
  90. while (fstab->can_read_line()) {
  91. ByteBuffer buffer = fstab->read_line(1024);
  92. StringView line_view = (const char*)buffer.data();
  93. // Trim the trailing newline, if any.
  94. if (line_view.length() > 0 && line_view[line_view.length() - 1] == '\n')
  95. line_view = line_view.substring_view(0, line_view.length() - 1);
  96. String line = line_view;
  97. // Skip comments and blank lines.
  98. if (line.is_empty() || line.starts_with("#"))
  99. continue;
  100. Vector<String> parts = line.split('\t');
  101. if (parts.size() < 3) {
  102. fprintf(stderr, "Invalid fstab entry: %s\n", line.characters());
  103. all_ok = false;
  104. continue;
  105. }
  106. const char* mountpoint = parts[1].characters();
  107. const char* fstype = parts[2].characters();
  108. int flags = parts.size() >= 4 ? parse_options(parts[3]) : 0;
  109. if (strcmp(mountpoint, "/") == 0) {
  110. dbg() << "Skipping mounting root";
  111. continue;
  112. }
  113. const char* filename = parts[0].characters();
  114. int fd = get_source_fd(filename);
  115. dbg() << "Mounting " << filename << "(" << fstype << ")"
  116. << " on " << mountpoint;
  117. int rc = mount(fd, mountpoint, fstype, flags);
  118. if (rc != 0) {
  119. fprintf(stderr, "Failed to mount %s (FD: %d) (%s) on %s: %s\n", filename, fd, fstype, mountpoint, strerror(errno));
  120. all_ok = false;
  121. continue;
  122. }
  123. }
  124. return all_ok;
  125. }
  126. static bool print_mounts()
  127. {
  128. // Output info about currently mounted filesystems.
  129. auto df = Core::File::construct("/proc/df");
  130. if (!df->open(Core::IODevice::ReadOnly)) {
  131. fprintf(stderr, "Failed to open /proc/df: %s\n", df->error_string());
  132. return false;
  133. }
  134. auto content = df->read_all();
  135. auto json = JsonValue::from_string(content);
  136. ASSERT(json.has_value());
  137. json.value().as_array().for_each([](auto& value) {
  138. auto fs_object = value.as_object();
  139. auto class_name = fs_object.get("class_name").to_string();
  140. auto mount_point = fs_object.get("mount_point").to_string();
  141. auto source = fs_object.get("source").as_string_or("none");
  142. auto readonly = fs_object.get("readonly").to_bool();
  143. auto mount_flags = fs_object.get("mount_flags").to_int();
  144. printf("%s on %s type %s (", source.characters(), mount_point.characters(), class_name.characters());
  145. if (readonly || mount_flags & MS_RDONLY)
  146. printf("ro");
  147. else
  148. printf("rw");
  149. if (mount_flags & MS_NODEV)
  150. printf(",nodev");
  151. if (mount_flags & MS_NOEXEC)
  152. printf(",noexec");
  153. if (mount_flags & MS_NOSUID)
  154. printf(",nosuid");
  155. if (mount_flags & MS_BIND)
  156. printf(",bind");
  157. printf(")\n");
  158. });
  159. return true;
  160. }
  161. int main(int argc, char** argv)
  162. {
  163. const char* source = nullptr;
  164. const char* mountpoint = nullptr;
  165. const char* fs_type = nullptr;
  166. const char* options = nullptr;
  167. bool should_mount_all = false;
  168. Core::ArgsParser args_parser;
  169. args_parser.add_positional_argument(source, "Source path", "source", Core::ArgsParser::Required::No);
  170. args_parser.add_positional_argument(mountpoint, "Mount point", "mountpoint", Core::ArgsParser::Required::No);
  171. args_parser.add_option(fs_type, "File system type", nullptr, 't', "fstype");
  172. args_parser.add_option(options, "Mount options", nullptr, 'o', "options");
  173. args_parser.add_option(should_mount_all, "Mount all file systems listed in /etc/fstab", nullptr, 'a');
  174. args_parser.parse(argc, argv);
  175. if (should_mount_all) {
  176. return mount_all() ? 0 : 1;
  177. }
  178. if (!source && !mountpoint)
  179. return print_mounts() ? 0 : 1;
  180. if (source && mountpoint) {
  181. if (!fs_type)
  182. fs_type = "ext2";
  183. int flags = options ? parse_options(options) : 0;
  184. int fd = get_source_fd(source);
  185. if (mount(fd, mountpoint, fs_type, flags) < 0) {
  186. perror("mount");
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. args_parser.print_usage(stderr, argv[0]);
  192. return 1;
  193. }