diff --git a/staging/src/k8s.io/mount-utils/mount_linux.go b/staging/src/k8s.io/mount-utils/mount_linux.go index 89e7d0675ff..c2d1ab72bcf 100644 --- a/staging/src/k8s.io/mount-utils/mount_linux.go +++ b/staging/src/k8s.io/mount-utils/mount_linux.go @@ -29,6 +29,7 @@ import ( "path/filepath" "strconv" "strings" + "syscall" "time" "github.com/moby/sys/mountinfo" @@ -403,12 +404,30 @@ func statx(file string) (unix.Statx_t, error) { return stat, nil } +func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) { + stat, err := os.Stat(file) + if err != nil { + return true, err + } + rootStat, err := os.Stat(filepath.Dir(strings.TrimSuffix(file, "/"))) + if err != nil { + return true, err + } + // If the directory has a different device as parent, then it is a mountpoint. + if stat.Sys().(*syscall.Stat_t).Dev != rootStat.Sys().(*syscall.Stat_t).Dev { + return false, nil + } + + return true, nil +} + // IsLikelyNotMountPoint determines if a directory is not a mountpoint. -// If fast check failed, fall back to slow path. If the path is in fact -// a bind mount from one part of a mount to another it will be detected. -// It also can distinguish between mountpoints and symbolic links. +// It is fast but not necessarily ALWAYS correct. If the path is in fact +// a bind mount from one part of a mount to another it will not be detected. +// It also can not distinguish between mountpoints and symbolic links. // mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b") -// will return false. When in fact /tmp/b is a mount point. +// will return true. When in fact /tmp/b is a mount point. If this situation +// is of interest to you, don't use this function... func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) { var stat, rootStat unix.Statx_t var err error @@ -416,8 +435,11 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) { if stat, err = statx(file); err != nil { if errors.Is(err, errStatxNotSupport) { // not support statx, go slow path - mnt, mntErr := mounter.IsMountPoint(file) - return !mnt, mntErr + // mnt, mntErr := mounter.IsMountPoint(file) + // return !mnt, mntErr + + // fall back to isLikelyNotMountPoint + return mounter.isLikelyNotMountPoint(file) } return false, err