Skip to content

Commit a813376

Browse files
fix mount options after remount
Signed-off-by: yaroslavborbat <[email protected]>
1 parent d99d3f7 commit a813376

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

staging/src/k8s.io/mount-utils/mount_linux.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/moby/sys/mountinfo"
3636
"golang.org/x/sys/unix"
3737

38-
libcontaineruserns "github.com/opencontainers/runc/libcontainer/userns"
3938
"k8s.io/klog/v2"
4039
utilexec "k8s.io/utils/exec"
4140
)
@@ -114,7 +113,7 @@ func (mounter *Mounter) hasSystemd() bool {
114113

115114
// Map unix.Statfs mount flags ro, nodev, noexec, nosuid, noatime, relatime,
116115
// nodiratime to mount option flag strings.
117-
func getUserNSBindMountOptions(path string, statfs func(path string, buf *unix.Statfs_t) (err error)) ([]string, error) {
116+
func getBindMountOptions(path string, statfs func(path string, buf *unix.Statfs_t) (err error)) ([]string, error) {
118117
var s unix.Statfs_t
119118
var mountOpts []string
120119
if err := statfs(path, &s); err != nil {
@@ -137,32 +136,23 @@ func getUserNSBindMountOptions(path string, statfs func(path string, buf *unix.S
137136
return mountOpts, nil
138137
}
139138

140-
// Do a bind mount including the needed remount for applying the bind opts.
141-
// If the remount fails and we are running in a user namespace
142-
// figure out if the source filesystem has the ro, nodev, noexec, nosuid,
143-
// noatime, relatime or nodiratime flag set and try another remount with the found flags.
139+
// Performs a bind mount with the specified options, and then remounts
140+
// the mount point with the same `nodev`, `nosuid`, `noexec`, `nosuid`, `noatime`,
141+
// `relatime`, `nodiratime` options as the original mount point.
144142
func (mounter *Mounter) bindMountSensitive(mounterPath string, mountCmd string, source string, target string, fstype string, bindOpts []string, bindRemountOpts []string, bindRemountOptsSensitive []string, mountFlags []string, systemdMountRequired bool) error {
145-
err := mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, bindOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired)
143+
err := mounter.doMount(mounterPath, mountCmd, source, target, fstype, bindOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired)
146144
if err != nil {
147145
return err
148146
}
149-
err = mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, bindRemountOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired)
150-
if libcontaineruserns.RunningInUserNS() {
151-
if err == nil {
152-
return nil
153-
}
154-
// Check if the source has ro, nodev, noexec, nosuid, noatime, relatime,
155-
// nodiratime flag...
156-
fixMountOpts, err := getUserNSBindMountOptions(source, unix.Statfs)
157-
if err != nil {
158-
return &os.PathError{Op: "statfs", Path: source, Err: err}
159-
}
160-
// ... and retry the mount with flags found above.
161-
bindRemountOpts = append(bindRemountOpts, fixMountOpts...)
162-
return mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, bindRemountOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired)
163-
} else {
164-
return err
147+
// Check if the source has ro, nodev, noexec, nosuid, noatime, relatime,
148+
// nodiratime flag...
149+
fixMountOpts, err := getBindMountOptions(source, unix.Statfs)
150+
if err != nil {
151+
return &os.PathError{Op: "statfs", Path: source, Err: err}
165152
}
153+
// ... and retry the mount with flags found above.
154+
bindRemountOpts = append(bindRemountOpts, fixMountOpts...)
155+
return mounter.doMount(mounterPath, mountCmd, source, target, fstype, bindRemountOpts, bindRemountOptsSensitive, mountFlags, systemdMountRequired)
166156
}
167157

168158
// Mount mounts source to target as fstype with given options. 'source' and 'fstype' must

staging/src/k8s.io/mount-utils/mount_linux_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ func mkStatfsFlags[T1 constraints.Integer, T2 constraints.Integer](orig T1, add
821821
return orig | T1(add)
822822
}
823823

824-
func TestGetUserNSBindMountOptions(t *testing.T) {
824+
func TestGetBindMountOptions(t *testing.T) {
825825
var testCases = map[string]struct {
826826
flags int32 // smallest size used by any platform we care about
827827
mountoptions string
@@ -843,9 +843,9 @@ func TestGetUserNSBindMountOptions(t *testing.T) {
843843
return nil
844844
}
845845

846-
testGetUserNSBindMountOptionsSingleCase := func(t *testing.T) {
846+
testGetBindMountOptionsSingleCase := func(t *testing.T) {
847847
path := strings.Split(t.Name(), "/")[1]
848-
options, _ := getUserNSBindMountOptions(path, statfsMock)
848+
options, _ := getBindMountOptions(path, statfsMock)
849849
sort.Strings(options)
850850
optionString := strings.Join(options, ",")
851851
mountOptions := testCases[path].mountoptions
@@ -855,7 +855,7 @@ func TestGetUserNSBindMountOptions(t *testing.T) {
855855
}
856856

857857
for k := range testCases {
858-
t.Run(k, testGetUserNSBindMountOptionsSingleCase)
858+
t.Run(k, testGetBindMountOptionsSingleCase)
859859
}
860860
}
861861

0 commit comments

Comments
 (0)