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