Skip to content

Commit a93a697

Browse files
j4ckstrawk8s-publishing-bot
authored andcommitted
feat: support statx STATX_ATTR_MOUNT_ROOT attribute
Signed-off-by: j4ckstraw <[email protected]> fix: not use glibc Signed-off-by: j4ckstraw <[email protected]> fix linter error Kubernetes-commit: 81ef146e2feb70645059c1d9cd9df43829f65ff7
1 parent 3971fb4 commit a93a697

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

mount_linux.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ const (
5555
errNotMounted = "not mounted"
5656
)
5757

58+
var (
59+
// Error statx support since Linux 4.11, https://man7.org/linux/man-pages/man2/statx.2.html
60+
errStatxNotSupport = errors.New("the statx syscall is not supported. At least Linux kernel 4.11 is needed")
61+
)
62+
5863
// Mounter provides the default implementation of mount.Interface
5964
// for the linux platform. This implementation assumes that the
6065
// kubelet is running in the host's root mount namespace.
@@ -385,15 +390,11 @@ func (*Mounter) List() ([]MountPoint, error) {
385390
return ListProcMounts(procMountsPath)
386391
}
387392

388-
// statx support since Linux 4.11, glibc 2.28
389-
// refer: https://man7.org/linux/man-pages/man2/statx.2.html
390-
var errNotSupport = errors.New("The statx syscall is not supported. At least Linux kernel 4.11 is needed")
391-
392393
func statx(file string) (unix.Statx_t, error) {
393394
var stat unix.Statx_t
394395
if err := unix.Statx(0, file, unix.AT_STATX_DONT_SYNC, 0, &stat); err != nil {
395396
if err == unix.ENOSYS {
396-
return stat, errNotSupport
397+
return stat, errStatxNotSupport
397398
}
398399

399400
return stat, err
@@ -408,14 +409,12 @@ func statx(file string) (unix.Statx_t, error) {
408409
// It also can distinguish between mountpoints and symbolic links.
409410
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
410411
// will return false. When in fact /tmp/b is a mount point.
411-
412-
// TODO(j4ckstraw) add test
413412
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
414413
var stat, rootStat unix.Statx_t
415414
var err error
416415

417416
if stat, err = statx(file); err != nil {
418-
if errors.Is(err, errNotSupport) {
417+
if errors.Is(err, errStatxNotSupport) {
419418
// not support statx, go slow path
420419
mnt, mntErr := mounter.IsMountPoint(file)
421420
return !mnt, mntErr
@@ -424,15 +423,23 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
424423
return false, err
425424
}
426425

426+
if stat.Attributes_mask != 0 {
427+
if stat.Attributes_mask&unix.STATX_ATTR_MOUNT_ROOT != 0 {
428+
if stat.Attributes&unix.STATX_ATTR_MOUNT_ROOT != 0 {
429+
// file is a mountpoint
430+
return false, nil
431+
} else {
432+
// no need to check rootStat if unix.STATX_ATTR_MOUNT_ROOT supported
433+
return true, nil
434+
}
435+
}
436+
}
437+
427438
root := filepath.Dir(strings.TrimSuffix(file, "/"))
428439
if rootStat, err = statx(root); err != nil {
429440
return false, err
430441
}
431442

432-
// TODO add STATX_ATTR_MOUNT_ROOT support, which can check mountpoint correctly.
433-
// Linux 5.8 commit 80340fe3605c0e78cfe496c3b3878be828cfdbfe
434-
// stat->attributes |= STATX_ATTR_MOUNT_ROOT;
435-
// stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
436443
return !(stat.Dev_major == rootStat.Dev_major && stat.Dev_minor == rootStat.Dev_minor), nil
437444
}
438445

0 commit comments

Comments
 (0)