gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/mount_util.h (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GVISOR_TEST_UTIL_MOUNT_UTIL_H_ 16 #define GVISOR_TEST_UTIL_MOUNT_UTIL_H_ 17 18 #include <errno.h> 19 #include <sys/mount.h> 20 21 #include <functional> 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 26 #include "gmock/gmock.h" 27 #include "absl/container/flat_hash_map.h" 28 #include "test/util/cleanup.h" 29 #include "test/util/posix_error.h" 30 #include "test/util/test_util.h" 31 32 namespace gvisor { 33 namespace testing { 34 35 // Mount mounts the filesystem, and unmounts when the returned reference is 36 // destroyed. 37 inline PosixErrorOr<Cleanup> Mount(const std::string& source, 38 const std::string& target, 39 const std::string& fstype, 40 uint64_t mountflags, const std::string& data, 41 uint64_t umountflags) { 42 if (mount(source.c_str(), target.c_str(), fstype.c_str(), mountflags, 43 data.c_str()) == -1) { 44 return PosixError(errno, "mount failed"); 45 } 46 return Cleanup([target, umountflags]() { 47 EXPECT_THAT(umount2(target.c_str(), umountflags), SyscallSucceeds()); 48 }); 49 } 50 51 struct ProcMountsEntry { 52 std::string spec; 53 std::string mount_point; 54 std::string fstype; 55 std::string mount_opts; 56 uint32_t dump; 57 uint32_t fsck; 58 }; 59 60 // ProcSelfMountsEntries returns a parsed representation of /proc/self/mounts. 61 PosixErrorOr<std::vector<ProcMountsEntry>> ProcSelfMountsEntries(); 62 63 // ProcSelfMountsEntries returns a parsed representation of mounts from the 64 // provided content. 65 PosixErrorOr<std::vector<ProcMountsEntry>> ProcSelfMountsEntriesFrom( 66 const std::string& content); 67 68 struct ProcMountInfoEntry { 69 uint64_t id; 70 uint64_t parent_id; 71 dev_t major; 72 dev_t minor; 73 std::string root; 74 std::string mount_point; 75 std::string mount_opts; 76 std::string optional; 77 std::string fstype; 78 std::string mount_source; 79 std::string super_opts; 80 }; 81 82 // ProcSelfMountInfoEntries returns a parsed representation of 83 // /proc/self/mountinfo. 84 PosixErrorOr<std::vector<ProcMountInfoEntry>> ProcSelfMountInfoEntries(); 85 86 // ProcSelfMountInfoEntriesFrom returns a parsed representation of 87 // mountinfo from the provided content. 88 PosixErrorOr<std::vector<ProcMountInfoEntry>> ProcSelfMountInfoEntriesFrom( 89 const std::string&); 90 91 // Interprets the input string mopts as a comma separated list of mount 92 // options. A mount option can either be just a value, or a key=value pair. For 93 // example, the string "rw,relatime,fd=7" will be parsed into a map like { "rw": 94 // "", "relatime": "", "fd": "7" }. 95 absl::flat_hash_map<std::string, std::string> ParseMountOptions( 96 std::string mopts); 97 98 struct MountOptional { 99 int shared; 100 int master; 101 int propagate_from; 102 }; 103 104 // MountOptionals returns a map of mount points to their optional fields as 105 // found in mountinfo. Duplicate mount points with different mount points will 106 // map to a vector of optionals. The order of the optionals is determined by 107 // their order in mountinfo (the order they were mounted in). 108 PosixErrorOr<absl::flat_hash_map<std::string, std::vector<MountOptional>>> 109 MountOptionals(); 110 111 // ParseOptionalTag is a helper that parses a single entry in a mount's 112 // set of optional tags. 113 PosixError ParseOptionalTag(std::string_view tag, MountOptional* opt); 114 115 } // namespace testing 116 } // namespace gvisor 117 118 #endif // GVISOR_TEST_UTIL_MOUNT_UTIL_H_