diff --git a/fs/fs.go b/fs/fs.go index cb45c33c93..fd2772f998 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -527,22 +527,7 @@ func (i *RealFsInfo) GetDeviceInfoByFsUUID(uuid string) (*DeviceInfo, error) { return &DeviceInfo{deviceName, p.major, p.minor}, nil } -func (i *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) { - buf := new(syscall.Stat_t) - err := syscall.Stat(dir, buf) - if err != nil { - return nil, fmt.Errorf("stat failed on %s with error: %s", dir, err) - } - - // The type Dev in Stat_t is 32bit on mips. - major := major(uint64(buf.Dev)) // nolint: unconvert - minor := minor(uint64(buf.Dev)) // nolint: unconvert - for device, partition := range i.partitions { - if partition.major == major && partition.minor == minor { - return &DeviceInfo{device, major, minor}, nil - } - } - +func (i *RealFsInfo) mountInfoFromDir(dir string) (*mount.MountInfo, bool) { mount, found := i.mounts[dir] // try the parent dir if not found until we reach the root dir // this is an issue on btrfs systems where the directory is not @@ -551,6 +536,7 @@ func (i *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) { pathdir, _ := filepath.Split(dir) // break when we reach root if pathdir == "/" { + mount, found = i.mounts["/"] break } // trim "/" from the new parent path otherwise the next possible @@ -558,9 +544,28 @@ func (i *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) { dir = strings.TrimSuffix(pathdir, "/") mount, found = i.mounts[dir] } + return &mount, found +} + +func (i *RealFsInfo) GetDirFsDevice(dir string) (*DeviceInfo, error) { + buf := new(syscall.Stat_t) + err := syscall.Stat(dir, buf) + if err != nil { + return nil, fmt.Errorf("stat failed on %s with error: %s", dir, err) + } + + // The type Dev in Stat_t is 32bit on mips. + major := major(uint64(buf.Dev)) // nolint: unconvert + minor := minor(uint64(buf.Dev)) // nolint: unconvert + for device, partition := range i.partitions { + if partition.major == major && partition.minor == minor { + return &DeviceInfo{device, major, minor}, nil + } + } + mount, found := i.mountInfoFromDir(dir) if found && mount.FsType == "btrfs" && mount.Major == 0 && strings.HasPrefix(mount.Source, "/dev/") { - major, minor, err := getBtrfsMajorMinorIds(&mount) + major, minor, err := getBtrfsMajorMinorIds(mount) if err != nil { klog.Warningf("%s", err) } else { diff --git a/fs/fs_test.go b/fs/fs_test.go index dae8e4d577..699daffa55 100644 --- a/fs/fs_test.go +++ b/fs/fs_test.go @@ -27,6 +27,20 @@ import ( "k8s.io/utils/mount" ) +func TestMountInfoFromDir(t *testing.T) { + as := assert.New(t) + fsInfo := &RealFsInfo{ + mounts: map[string]mount.MountInfo{ + "/": {}, + }, + } + testDirs := []string{"/var/lib/kubelet", "/var/lib/rancher"} + for _, testDir := range testDirs { + _, found := fsInfo.mountInfoFromDir(testDir) + as.True(found, "failed to find MountInfo %s from FsInfo %s", testDir, fsInfo) + } +} + func TestGetDiskStatsMap(t *testing.T) { diskStatsMap, err := getDiskStatsMap("test_resources/diskstats") if err != nil {