| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 1 | From 2c25c4cdf0a3526625bf38c0d16519d8b5c80132 Mon Sep 17 00:00:00 2001 |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 2 | From: Lorenz Brun <lorenz@brun.one> |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 3 | Date: Tue, 17 Mar 2020 21:41:08 +0100 |
| 4 | Subject: [PATCH] Provide native mounter implementation for Linux |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 5 | |
| 6 | --- |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 7 | mount_linux.go | 140 ++++++++++++++++++ |
| 8 | 1 file changed, 140 insertions(+) |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 9 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 10 | diff --git a/mount_linux.go b/mount_linux.go |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 11 | index 9c0b6d5..542d0e3 100644 |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 12 | --- a/mount_linux.go |
| 13 | +++ b/mount_linux.go |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 14 | @@ -69,6 +69,8 @@ type Mounter struct { |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 15 | withSystemd *bool |
| 16 | trySystemd bool |
| 17 | withSafeNotMountedBehavior bool |
| 18 | + withLinuxUtils bool |
| 19 | + nativeSupportedFstypes map[string]struct{} |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 20 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 21 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 22 | var _ MounterForceUnmounter = &Mounter{} |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 23 | @@ -81,6 +83,8 @@ func New(mounterPath string) Interface { |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 24 | mounterPath: mounterPath, |
| 25 | trySystemd: true, |
| 26 | withSafeNotMountedBehavior: detectSafeNotMountedBehavior(), |
| 27 | + withLinuxUtils: detectLinuxUtils(), |
| 28 | + nativeSupportedFstypes: detectNativeSupportedFstypes(), |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 29 | } |
| 30 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 31 | |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 32 | @@ -93,6 +97,8 @@ func NewWithoutSystemd(mounterPath string) Interface { |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 33 | mounterPath: mounterPath, |
| 34 | trySystemd: false, |
| 35 | withSafeNotMountedBehavior: detectSafeNotMountedBehavior(), |
| 36 | + withLinuxUtils: detectLinuxUtils(), |
| 37 | + nativeSupportedFstypes: detectNativeSupportedFstypes(), |
| 38 | } |
| 39 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 40 | |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 41 | @@ -155,6 +161,29 @@ func (mounter *Mounter) bindMountSensitive(mounterPath string, mountCmd string, |
| 42 | return mounter.doMount(mounterPath, mountCmd, source, target, fstype, bindRemountOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired) |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 43 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 44 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 45 | +func (mounter *Mounter) mountNative(source string, target string, fstype string, options []string, sensitiveOptions []string) error { |
| 46 | + flags, pflags, fsoptions := parseMountOptions(options) |
| 47 | + if len(pflags) > 0 { |
| 48 | + return fmt.Errorf("the native mounter is active and does not support mount propagation at the moment") |
| 49 | + } |
| 50 | + |
| 51 | + if !mounter.nativeSupportsFstype(fstype) && flags&unix.MS_BIND == 0 { |
| 52 | + return fmt.Errorf("the native mounter is active and cannot mount filesystems of type \"%v\"", fstype) |
| 53 | + } |
| 54 | + |
| 55 | + if flags&unix.MS_BIND != 0 && flags & ^uintptr(unix.MS_BIND) != 0 { |
| 56 | + if err := unix.Mount(source, target, "", unix.MS_BIND, ""); err != nil { |
| 57 | + return fmt.Errorf("bind pre-mount failed: %w", err) |
| 58 | + } |
| 59 | + flags |= unix.MS_REMOUNT |
| 60 | + } |
| 61 | + |
| 62 | + if err := unix.Mount(source, target, fstype, flags, fsoptions); err != nil { |
| 63 | + return fmt.Errorf("failed to mount filesystem: %w", err) |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | // Mount mounts source to target as fstype with given options. 'source' and 'fstype' must |
| 69 | // be an empty string in case it's not required, e.g. for remount, or for auto filesystem |
| 70 | // type, where kernel handles fstype for you. The mount 'options' is a list of options, |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 71 | @@ -170,6 +199,10 @@ func (mounter *Mounter) Mount(source string, target string, fstype string, optio |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 72 | // method should be used by callers that pass sensitive material (like |
| 73 | // passwords) as mount options. |
| 74 | func (mounter *Mounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { |
| 75 | + if !mounter.withLinuxUtils { |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 76 | + return mounter.mountNative(source, target, fstype, options, sensitiveOptions) |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 77 | + } |
| 78 | + |
| 79 | // Path to mounter binary if containerized mounter is needed. Otherwise, it is set to empty. |
| 80 | // All Linux distros are expected to be shipped with a mount utility that a support bind mounts. |
| 81 | mounterPath := "" |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 82 | @@ -197,6 +230,9 @@ func (mounter *Mounter) MountSensitiveWithoutSystemd(source string, target strin |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 83 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 84 | // MountSensitiveWithoutSystemdWithMountFlags is the same as MountSensitiveWithoutSystemd with additional mount flags. |
| 85 | func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string) error { |
| 86 | + if !mounter.withLinuxUtils { |
| 87 | + return mounter.mountNative(source, target, fstype, options, sensitiveOptions) |
| 88 | + } |
| 89 | mounterPath := "" |
| 90 | bind, bindOpts, bindRemountOpts, bindRemountOptsSensitive := MakeBindOptsSensitive(options, sensitiveOptions) |
| 91 | if bind { |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 92 | @@ -215,6 +251,80 @@ func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 93 | return mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, options, sensitiveOptions, mountFlags, false) |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 94 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 95 | |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 96 | +// nativeSupportsFstype checks if the native mounter can mount the given fstype |
| 97 | +func (mounter *Mounter) nativeSupportsFstype(fstype string) bool { |
| 98 | + _, ok := mounter.nativeSupportedFstypes[fstype] |
| 99 | + return ok |
| 100 | +} |
| 101 | + |
| 102 | +// parseMountOptions parses the string and returns the flags, propagation |
| 103 | +// flags and any mount data that it contains. |
| 104 | +// Taken from libcontainer/specconv/spec_linux.go (Apache 2.0) and modified |
| 105 | +func parseMountOptions(options []string) (uintptr, []uintptr, string) { |
| 106 | + var ( |
| 107 | + flag uintptr |
| 108 | + pgflag []uintptr |
| 109 | + data []string |
| 110 | + ) |
| 111 | + flags := map[string]struct { |
| 112 | + clear bool |
| 113 | + flag uintptr |
| 114 | + }{ |
| 115 | + "async": {true, syscall.MS_SYNCHRONOUS}, |
| 116 | + "atime": {true, syscall.MS_NOATIME}, |
| 117 | + "bind": {false, syscall.MS_BIND}, |
| 118 | + "defaults": {false, 0}, |
| 119 | + "dev": {true, syscall.MS_NODEV}, |
| 120 | + "diratime": {true, syscall.MS_NODIRATIME}, |
| 121 | + "dirsync": {false, syscall.MS_DIRSYNC}, |
| 122 | + "exec": {true, syscall.MS_NOEXEC}, |
| 123 | + "mand": {false, syscall.MS_MANDLOCK}, |
| 124 | + "noatime": {false, syscall.MS_NOATIME}, |
| 125 | + "nodev": {false, syscall.MS_NODEV}, |
| 126 | + "nodiratime": {false, syscall.MS_NODIRATIME}, |
| 127 | + "noexec": {false, syscall.MS_NOEXEC}, |
| 128 | + "nomand": {true, syscall.MS_MANDLOCK}, |
| 129 | + "norelatime": {true, syscall.MS_RELATIME}, |
| 130 | + "nostrictatime": {true, syscall.MS_STRICTATIME}, |
| 131 | + "nosuid": {false, syscall.MS_NOSUID}, |
| 132 | + "rbind": {false, syscall.MS_BIND | syscall.MS_REC}, |
| 133 | + "relatime": {false, syscall.MS_RELATIME}, |
| 134 | + "remount": {false, syscall.MS_REMOUNT}, |
| 135 | + "ro": {false, syscall.MS_RDONLY}, |
| 136 | + "rw": {true, syscall.MS_RDONLY}, |
| 137 | + "strictatime": {false, syscall.MS_STRICTATIME}, |
| 138 | + "suid": {true, syscall.MS_NOSUID}, |
| 139 | + "sync": {false, syscall.MS_SYNCHRONOUS}, |
| 140 | + } |
| 141 | + propagationFlags := map[string]uintptr{ |
| 142 | + "private": syscall.MS_PRIVATE, |
| 143 | + "shared": syscall.MS_SHARED, |
| 144 | + "slave": syscall.MS_SLAVE, |
| 145 | + "unbindable": syscall.MS_UNBINDABLE, |
| 146 | + "rprivate": syscall.MS_PRIVATE | syscall.MS_REC, |
| 147 | + "rshared": syscall.MS_SHARED | syscall.MS_REC, |
| 148 | + "rslave": syscall.MS_SLAVE | syscall.MS_REC, |
| 149 | + "runbindable": syscall.MS_UNBINDABLE | syscall.MS_REC, |
| 150 | + } |
| 151 | + for _, o := range options { |
| 152 | + // If the option does not exist in the flags table or the flag |
| 153 | + // is not supported on the platform, |
| 154 | + // then it is a data value for a specific fs type |
| 155 | + if f, exists := flags[o]; exists && f.flag != 0 { |
| 156 | + if f.clear { |
| 157 | + flag &= ^f.flag |
| 158 | + } else { |
| 159 | + flag |= f.flag |
| 160 | + } |
| 161 | + } else if f, exists := propagationFlags[o]; exists && f != 0 { |
| 162 | + pgflag = append(pgflag, f) |
| 163 | + } else { |
| 164 | + data = append(data, o) |
| 165 | + } |
| 166 | + } |
| 167 | + return flag, pgflag, strings.Join(data, ",") |
| 168 | +} |
| 169 | + |
| 170 | // doMount runs the mount command. mounterPath is the path to mounter binary if containerized mounter is used. |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 171 | // sensitiveOptions is an extension of options except they will not be logged (because they may contain sensitive material) |
| 172 | // systemdMountRequired is an extension of option to decide whether uses systemd mount. |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 173 | @@ -330,6 +440,30 @@ func detectSafeNotMountedBehaviorWithExec(exec utilexec.Interface) bool { |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 174 | return false |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 175 | } |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 176 | |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 177 | +// detectLinuxUtils detects if the host operating system has the mount and unmount commands present |
| 178 | +func detectLinuxUtils() bool { |
| 179 | + _, err := exec.LookPath("mount") |
| 180 | + return err == nil |
| 181 | +} |
| 182 | + |
| 183 | +func detectNativeSupportedFstypes() map[string]struct{} { |
| 184 | + nativeSupportedFstypes := make(map[string]struct{}) |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 185 | + filesystemsRaw, err := os.ReadFile("/proc/filesystems") |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 186 | + if err != nil { |
| 187 | + return nativeSupportedFstypes |
| 188 | + } |
| 189 | + filesystemLines := strings.Split(string(filesystemsRaw), "\n") |
| 190 | + for _, line := range filesystemLines { |
| 191 | + fields := strings.Fields(line) |
| 192 | + if len(fields) != 2 { |
| 193 | + continue |
| 194 | + } |
| 195 | + filesystem := fields[1] |
| 196 | + nativeSupportedFstypes[filesystem] = struct{}{} |
| 197 | + } |
| 198 | + return nativeSupportedFstypes |
| 199 | +} |
| 200 | + |
| 201 | // MakeMountArgs makes the arguments to the mount(8) command. |
| 202 | // options MUST not contain sensitive material (like passwords). |
| 203 | func MakeMountArgs(source, target, fstype string, options []string) (mountArgs []string) { |
| Tim Windelschmidt | cfbc903 | 2025-07-15 14:18:45 +0200 | [diff] [blame^] | 204 | @@ -400,6 +534,12 @@ func AddSystemdScopeSensitive(systemdRunPath, mountName, command string, args [] |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 205 | // If the mounter has safe "not mounted" behavior, no error will be returned when the target is not a mount point. |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 206 | func (mounter *Mounter) Unmount(target string) error { |
| 207 | klog.V(4).Infof("Unmounting %s", target) |
| 208 | + if !mounter.withLinuxUtils { |
| 209 | + if err := unix.Unmount(target, unix.UMOUNT_NOFOLLOW); err != nil { |
| 210 | + return fmt.Errorf("unmount failed: %v", err) |
| 211 | + } |
| 212 | + return nil |
| 213 | + } |
| 214 | command := exec.Command("umount", target) |
| 215 | output, err := command.CombinedOutput() |
| 216 | if err != nil { |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 217 | -- |
| 218 | 2.44.1 |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 219 | |