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