@@ -55,6 +55,11 @@ const (
55
55
errNotMounted = "not mounted"
56
56
)
57
57
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
+
58
63
// Mounter provides the default implementation of mount.Interface
59
64
// for the linux platform. This implementation assumes that the
60
65
// kubelet is running in the host's root mount namespace.
@@ -385,15 +390,11 @@ func (*Mounter) List() ([]MountPoint, error) {
385
390
return ListProcMounts (procMountsPath )
386
391
}
387
392
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
-
392
393
func statx (file string ) (unix.Statx_t , error ) {
393
394
var stat unix.Statx_t
394
395
if err := unix .Statx (0 , file , unix .AT_STATX_DONT_SYNC , 0 , & stat ); err != nil {
395
396
if err == unix .ENOSYS {
396
- return stat , errNotSupport
397
+ return stat , errStatxNotSupport
397
398
}
398
399
399
400
return stat , err
@@ -408,14 +409,12 @@ func statx(file string) (unix.Statx_t, error) {
408
409
// It also can distinguish between mountpoints and symbolic links.
409
410
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
410
411
// will return false. When in fact /tmp/b is a mount point.
411
-
412
- // TODO(j4ckstraw) add test
413
412
func (mounter * Mounter ) IsLikelyNotMountPoint (file string ) (bool , error ) {
414
413
var stat , rootStat unix.Statx_t
415
414
var err error
416
415
417
416
if stat , err = statx (file ); err != nil {
418
- if errors .Is (err , errNotSupport ) {
417
+ if errors .Is (err , errStatxNotSupport ) {
419
418
// not support statx, go slow path
420
419
mnt , mntErr := mounter .IsMountPoint (file )
421
420
return ! mnt , mntErr
@@ -424,15 +423,23 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
424
423
return false , err
425
424
}
426
425
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
+
427
438
root := filepath .Dir (strings .TrimSuffix (file , "/" ))
428
439
if rootStat , err = statx (root ); err != nil {
429
440
return false , err
430
441
}
431
442
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;
436
443
return ! (stat .Dev_major == rootStat .Dev_major && stat .Dev_minor == rootStat .Dev_minor ), nil
437
444
}
438
445
0 commit comments