blob: 21e228ecd5bc2123513d8a0065e4586198846ee8 [file] [log] [blame]
Lorenz Brun6211e4d2023-11-14 19:09:40 +01001From d56a2d05e536534730660813c182055bb705b22a Mon Sep 17 00:00:00 2001
Lorenz Brun878f5f92020-05-12 16:15:39 +02002From: Lorenz Brun <lorenz@brun.one>
Lorenz Brun6211e4d2023-11-14 19:09:40 +01003Date: Tue, 17 Mar 2020 21:41:08 +0100
4Subject: [PATCH] Provide native mounter implementation for Linux
Lorenz Brun878f5f92020-05-12 16:15:39 +02005
6---
Lorenz Brun6211e4d2023-11-14 19:09:40 +01007 mount_linux.go | 141 +++++++++++++++++++++++++++++++++++++++++++++++++
8 1 file changed, 141 insertions(+)
Lorenz Brun878f5f92020-05-12 16:15:39 +02009
Lorenz Brund13c1c62022-03-30 19:58:58 +020010diff --git a/mount_linux.go b/mount_linux.go
Tim Windelschmidtddc5e6a2024-04-23 23:44:34 +020011index 07ce76d..e925185 100644
Lorenz Brund13c1c62022-03-30 19:58:58 +020012--- a/mount_linux.go
13+++ b/mount_linux.go
Tim Windelschmidtddc5e6a2024-04-23 23:44:34 +020014@@ -69,6 +70,8 @@ type Mounter struct {
Lorenz Brun6211e4d2023-11-14 19:09:40 +010015 withSystemd *bool
16 trySystemd bool
17 withSafeNotMountedBehavior bool
18+ withLinuxUtils bool
19+ nativeSupportedFstypes map[string]struct{}
Lorenz Brun878f5f92020-05-12 16:15:39 +020020 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +010021
Lorenz Brund13c1c62022-03-30 19:58:58 +020022 var _ MounterForceUnmounter = &Mounter{}
Tim Windelschmidtddc5e6a2024-04-23 23:44:34 +020023@@ -81,6 +84,8 @@ func New(mounterPath string) Interface {
Lorenz Brun6211e4d2023-11-14 19:09:40 +010024 mounterPath: mounterPath,
25 trySystemd: true,
26 withSafeNotMountedBehavior: detectSafeNotMountedBehavior(),
27+ withLinuxUtils: detectLinuxUtils(),
28+ nativeSupportedFstypes: detectNativeSupportedFstypes(),
Lorenz Brun878f5f92020-05-12 16:15:39 +020029 }
30 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +010031
Tim Windelschmidtddc5e6a2024-04-23 23:44:34 +020032@@ -93,6 +98,8 @@ func NewWithoutSystemd(mounterPath string) Interface {
Lorenz Brun6211e4d2023-11-14 19:09:40 +010033 mounterPath: mounterPath,
34 trySystemd: false,
35 withSafeNotMountedBehavior: detectSafeNotMountedBehavior(),
36+ withLinuxUtils: detectLinuxUtils(),
37+ nativeSupportedFstypes: detectNativeSupportedFstypes(),
38 }
39 }
40
Tim Windelschmidtddc5e6a2024-04-23 23:44:34 +020041@@ -111,6 +118,29 @@ func (mounter *Mounter) hasSystemd() bool {
Lorenz Brun6211e4d2023-11-14 19:09:40 +010042 return *mounter.withSystemd
43 }
44
Lorenz Brund13c1c62022-03-30 19:58:58 +020045+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 Windelschmidtddc5e6a2024-04-23 23:44:34 +020071@@ -126,6 +156,10 @@ func (mounter *Mounter) Mount(source string, target string, fstype string, optio
Lorenz Brun878f5f92020-05-12 16:15:39 +020072 // 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 Brund13c1c62022-03-30 19:58:58 +020076+ return mounter.mountNative(source, target, fstype, options, sensitiveOptions)
Lorenz Brun878f5f92020-05-12 16:15:39 +020077+ }
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 Windelschmidtddc5e6a2024-04-23 23:44:34 +020082@@ -157,6 +191,9 @@ func (mounter *Mounter) MountSensitiveWithoutSystemd(source string, target strin
Lorenz Brun6211e4d2023-11-14 19:09:40 +010083
Lorenz Brund13c1c62022-03-30 19:58:58 +020084 // 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 Windelschmidtddc5e6a2024-04-23 23:44:34 +020092@@ -179,6 +216,80 @@ func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string
Lorenz Brund13c1c62022-03-30 19:58:58 +020093 return mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, options, sensitiveOptions, mountFlags, false)
Lorenz Brun878f5f92020-05-12 16:15:39 +020094 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +010095
Lorenz Brun878f5f92020-05-12 16:15:39 +020096+// 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 Brund13c1c62022-03-30 19:58:58 +0200171 // 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 Windelschmidtddc5e6a2024-04-23 23:44:34 +0200173@@ -294,6 +405,30 @@ func detectSafeNotMountedBehaviorWithExec(exec utilexec.Interface) bool {
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100174 return false
Lorenz Brun878f5f92020-05-12 16:15:39 +0200175 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100176
Lorenz Brun878f5f92020-05-12 16:15:39 +0200177+// 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 Brun6211e4d2023-11-14 19:09:40 +0100185+ filesystemsRaw, err := os.ReadFile("/proc/filesystems")
Lorenz Brun878f5f92020-05-12 16:15:39 +0200186+ 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 Windelschmidtddc5e6a2024-04-23 23:44:34 +0200204@@ -364,6 +499,12 @@ func AddSystemdScopeSensitive(systemdRunPath, mountName, command string, args []
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100205 // If the mounter has safe "not mounted" behavior, no error will be returned when the target is not a mount point.
Lorenz Brun878f5f92020-05-12 16:15:39 +0200206 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 Brun6211e4d2023-11-14 19:09:40 +0100217--
2182.41.0
Lorenz Brun878f5f92020-05-12 16:15:39 +0200219