blob: 2965f5edfda5471da3eb045080912c0d9426d3fb [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
Lorenz Brun6211e4d2023-11-14 19:09:40 +010011index 7d18072..5e4a79e 100644
Lorenz Brund13c1c62022-03-30 19:58:58 +020012--- a/mount_linux.go
13+++ b/mount_linux.go
Lorenz Brun6211e4d2023-11-14 19:09:40 +010014@@ -34,6 +34,7 @@ import (
15
16 "github.com/moby/sys/mountinfo"
17
Lorenz Brun878f5f92020-05-12 16:15:39 +020018+ "golang.org/x/sys/unix"
Lorenz Brunb876fc32020-07-14 13:54:01 +020019 "k8s.io/klog/v2"
Lorenz Brun878f5f92020-05-12 16:15:39 +020020 utilexec "k8s.io/utils/exec"
Lorenz Brun6211e4d2023-11-14 19:09:40 +010021 )
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 Brun878f5f92020-05-12 16:15:39 +020028 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +010029
Lorenz Brund13c1c62022-03-30 19:58:58 +020030 var _ MounterForceUnmounter = &Mounter{}
Lorenz Brun6211e4d2023-11-14 19:09:40 +010031@@ -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 Brun878f5f92020-05-12 16:15:39 +020037 }
38 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +010039
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 Brund13c1c62022-03-30 19:58:58 +020053+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 Brun6211e4d2023-11-14 19:09:40 +010079@@ -120,6 +150,10 @@ func (mounter *Mounter) Mount(source string, target string, fstype string, optio
Lorenz Brun878f5f92020-05-12 16:15:39 +020080 // 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 Brund13c1c62022-03-30 19:58:58 +020084+ return mounter.mountNative(source, target, fstype, options, sensitiveOptions)
Lorenz Brun878f5f92020-05-12 16:15:39 +020085+ }
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 Brun6211e4d2023-11-14 19:09:40 +010090@@ -151,6 +185,9 @@ func (mounter *Mounter) MountSensitiveWithoutSystemd(source string, target strin
91
Lorenz Brund13c1c62022-03-30 19:58:58 +020092 // 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 Brun6211e4d2023-11-14 19:09:40 +0100100@@ -173,6 +210,80 @@ func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string
Lorenz Brund13c1c62022-03-30 19:58:58 +0200101 return mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, options, sensitiveOptions, mountFlags, false)
Lorenz Brun878f5f92020-05-12 16:15:39 +0200102 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100103
Lorenz Brun878f5f92020-05-12 16:15:39 +0200104+// 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 Brund13c1c62022-03-30 19:58:58 +0200179 // 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 Brun6211e4d2023-11-14 19:09:40 +0100181@@ -288,6 +399,30 @@ func detectSafeNotMountedBehaviorWithExec(exec utilexec.Interface) bool {
182 return false
Lorenz Brun878f5f92020-05-12 16:15:39 +0200183 }
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100184
Lorenz Brun878f5f92020-05-12 16:15:39 +0200185+// 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 Brun6211e4d2023-11-14 19:09:40 +0100193+ filesystemsRaw, err := os.ReadFile("/proc/filesystems")
Lorenz Brun878f5f92020-05-12 16:15:39 +0200194+ 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 Brun6211e4d2023-11-14 19:09:40 +0100212@@ -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 Brun878f5f92020-05-12 16:15:39 +0200214 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 Brun6211e4d2023-11-14 19:09:40 +0100225--
2262.41.0
Lorenz Brun878f5f92020-05-12 16:15:39 +0200227