From 49037dd0d8c08aef773911c36eb77b5b8990429d Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 5 Mar 2022 03:15:26 +0000 Subject: [PATCH] [disk][linux] add fallback to /proc/self --- disk/disk_linux.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 5a0cf46..18c96c1 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -11,6 +11,7 @@ import ( "fmt" "io/ioutil" "os" + "path" "path/filepath" "strconv" "strings" @@ -221,20 +222,32 @@ var fsTypeMap = map[int64]string{ ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */ } -func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { - useMounts := false - - filename := common.HostProc("1/mountinfo") - lines, err := common.ReadLines(filename) +// readMountFile reads mountinfo or mounts file under /proc/1 or /proc/self +func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) { + filename = common.HostProc(path.Join(root, "mountinfo")) + lines, err = common.ReadLines(filename) if err != nil { var pathErr *os.PathError if !errors.As(err, &pathErr) { - return nil, err + return } // if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26) useMounts = true - filename = common.HostProc("1/mounts") + filename = common.HostProc(path.Join(root, "mounts")) lines, err = common.ReadLines(filename) + if err != nil { + return + } + return + } + return +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + lines, useMounts, filename, err := readMountFile("1") + if err != nil { + // fallback to "/proc/self/mountinfo" #1159 + lines, useMounts, filename, err = readMountFile("self") if err != nil { return nil, err }