From a45f6d8a03ae92b06e8a53d0711d629fdc46a9b9 Mon Sep 17 00:00:00 2001 From: JHE Date: Tue, 5 Feb 2019 21:06:23 +0800 Subject: [PATCH 1/5] if mountinfo not found, use mounts --- disk/disk_linux.go | 79 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index a6e5ee3..1713536 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -227,7 +227,16 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro filename := common.HostProc("self/mountinfo") lines, err := common.ReadLines(filename) if err != nil { - return nil, err + UseMounts := true + } + + //if kernel not support self/mountinfo + if UseMounts { + filename := common.HostProc("self/mounts") + lines, err := common.ReadLines(filename) + if err != nil { + return nil, err + } } fs, err := getFileSystems() @@ -243,39 +252,53 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) // split the mountinfo line by the separator hyphen - parts := strings.Split(line, " - ") - if len(parts) != 2 { - return nil, fmt.Errorf("found invalid mountinfo line in file %s: %s ", filename, line) - } + if UseMounts { + fields := strings.Fields(line) + + d := PartitionStat{ + Device: fields[0], + Mountpoint: unescapeFstab(fields[1]), + Fstype: fields[2], + Opts: fields[3], + } + } else { + parts := strings.Split(line, " - ") + if len(parts) != 2 { + return nil, fmt.Errorf("found invalid mountinfo line in file %s: %s ", filename, line) + } - fields := strings.Fields(parts[0]) - blockDeviceID := fields[2] - mountPoint := fields[4] - mountOpts := fields[5] + fields := strings.Fields(parts[0]) + blockDeviceID := fields[2] + mountPoint := fields[4] + mountOpts := fields[5] - fields = strings.Fields(parts[1]) - fstype := fields[0] - device := fields[1] + fields = strings.Fields(parts[1]) + fstype := fields[0] + device := fields[1] - d := PartitionStat{ - Device: device, - Mountpoint: mountPoint, - Fstype: fstype, - Opts: mountOpts, - } - if all == false { - if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { - continue + d := PartitionStat{ + Device: device, + Mountpoint: mountPoint, + Fstype: fstype, + Opts: mountOpts, } } - // /dev/root is not the real device name - // so we get the real device name from its major/minor number - if d.Device == "/dev/root" { - devpath, err := os.Readlink(common.HostSys("/dev/block/" + blockDeviceID)) - if err != nil { - return nil, err + + if UseMounts { + if all == false { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } + } + // /dev/root is not the real device name + // so we get the real device name from its major/minor number + if d.Device == "/dev/root" { + devpath, err := os.Readlink(common.HostSys("/dev/block/" + blockDeviceID)) + if err != nil { + return nil, err + } + d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1) } - d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1) } ret = append(ret, d) } From d1413496199dce7179f808ab8808f1d9db0c3ebc Mon Sep 17 00:00:00 2001 From: JHE Date: Tue, 5 Feb 2019 21:23:46 +0800 Subject: [PATCH 2/5] if mountinfo not found, use mounts --- disk/disk_linux.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 1713536..7dbb5a7 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -226,12 +226,13 @@ func Partitions(all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { filename := common.HostProc("self/mountinfo") lines, err := common.ReadLines(filename) + UseMounts := false if err != nil { - UseMounts := true + UseMounts = true } //if kernel not support self/mountinfo - if UseMounts { + if UseMounts == true { filename := common.HostProc("self/mounts") lines, err := common.ReadLines(filename) if err != nil { @@ -252,7 +253,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) // split the mountinfo line by the separator hyphen - if UseMounts { + if UseMounts == true { fields := strings.Fields(line) d := PartitionStat{ @@ -284,12 +285,13 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro } } - if UseMounts { - if all == false { - if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { - continue - } + if all == false { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue } + } + + if UseMounts == true { // /dev/root is not the real device name // so we get the real device name from its major/minor number if d.Device == "/dev/root" { From a5cb715e6407fc8b47b717c92a4cec6805a4cb99 Mon Sep 17 00:00:00 2001 From: JHE Date: Tue, 5 Feb 2019 23:30:19 +0800 Subject: [PATCH 3/5] if mountinfo not found, use mounts --- disk/disk_linux.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 7dbb5a7..c272c2c 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -224,9 +224,10 @@ func Partitions(all bool) ([]PartitionStat, error) { } func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + UseMounts := false + filename := common.HostProc("self/mountinfo") lines, err := common.ReadLines(filename) - UseMounts := false if err != nil { UseMounts = true } @@ -234,7 +235,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro //if kernel not support self/mountinfo if UseMounts == true { filename := common.HostProc("self/mounts") - lines, err := common.ReadLines(filename) + lines, err = common.ReadLines(filename) if err != nil { return nil, err } @@ -247,6 +248,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro ret := make([]PartitionStat, 0, len(lines)) + var d PartitionStat + for _, line := range lines { // a line of self/mountinfo has the following structure: // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue @@ -256,12 +259,18 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro if UseMounts == true { fields := strings.Fields(line) - d := PartitionStat{ + d = PartitionStat{ Device: fields[0], Mountpoint: unescapeFstab(fields[1]), Fstype: fields[2], Opts: fields[3], } + + if all == false { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } + } } else { parts := strings.Split(line, " - ") if len(parts) != 2 { @@ -277,21 +286,19 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro fstype := fields[0] device := fields[1] - d := PartitionStat{ + d = PartitionStat{ Device: device, Mountpoint: mountPoint, Fstype: fstype, Opts: mountOpts, } - } - if all == false { - if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { - continue + if all == false { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } } - } - if UseMounts == true { // /dev/root is not the real device name // so we get the real device name from its major/minor number if d.Device == "/dev/root" { From 06a21ae66ae6f1d676537a82e9af6c4ef8f6bd19 Mon Sep 17 00:00:00 2001 From: JHE Date: Thu, 7 Feb 2019 00:01:51 +0800 Subject: [PATCH 4/5] Fix some issue. --- disk/disk_linux.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index c272c2c..0069af0 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -224,16 +224,16 @@ func Partitions(all bool) ([]PartitionStat, error) { } func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { - UseMounts := false + useMounts := false filename := common.HostProc("self/mountinfo") lines, err := common.ReadLines(filename) if err != nil { - UseMounts = true + useMounts = true } //if kernel not support self/mountinfo - if UseMounts == true { + if useMounts { filename := common.HostProc("self/mounts") lines, err = common.ReadLines(filename) if err != nil { @@ -256,7 +256,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) // split the mountinfo line by the separator hyphen - if UseMounts == true { + if useMounts { fields := strings.Fields(line) d = PartitionStat{ @@ -266,7 +266,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro Opts: fields[3], } - if all == false { + if !all { if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { continue } @@ -293,7 +293,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro Opts: mountOpts, } - if all == false { + if !all { if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { continue } From a08b926ce632e7bffd46d6a2e950ef7ab45dcfc0 Mon Sep 17 00:00:00 2001 From: JHE Date: Sun, 10 Feb 2019 00:01:25 +0800 Subject: [PATCH 5/5] It could be another error than the "self/mountinfo" missing --- disk/disk_linux.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 0069af0..0546491 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -229,12 +229,12 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro filename := common.HostProc("self/mountinfo") lines, err := common.ReadLines(filename) if err != nil { + if err != err.(*os.PathError) { + return nil, err + } + //if kernel not support self/mountinfo useMounts = true - } - - //if kernel not support self/mountinfo - if useMounts { - filename := common.HostProc("self/mounts") + filename = common.HostProc("self/mounts") lines, err = common.ReadLines(filename) if err != nil { return nil, err @@ -248,9 +248,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro ret := make([]PartitionStat, 0, len(lines)) - var d PartitionStat - for _, line := range lines { + var d PartitionStat // a line of self/mountinfo has the following structure: // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)