Merge pull request #634 from qaz52897/support_old_kernel

Support Linux kernels without /proc/self/mountinfo (<2.6.26)
pull/577/merge
Lomanic 6 years ago committed by GitHub
commit b67304da73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -224,10 +224,21 @@ func Partitions(all bool) ([]PartitionStat, error) {
} }
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
useMounts := false
filename := common.HostProc("self/mountinfo") filename := common.HostProc("self/mountinfo")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err if err != err.(*os.PathError) {
return nil, err
}
//if kernel not support self/mountinfo
useMounts = true
filename = common.HostProc("self/mounts")
lines, err = common.ReadLines(filename)
if err != nil {
return nil, err
}
} }
fs, err := getFileSystems() fs, err := getFileSystems()
@ -238,44 +249,64 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
ret := make([]PartitionStat, 0, len(lines)) ret := make([]PartitionStat, 0, len(lines))
for _, line := range lines { for _, line := range lines {
var d PartitionStat
// a line of self/mountinfo has the following structure: // 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 // 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) // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
// split the mountinfo line by the separator hyphen // split the mountinfo line by the separator hyphen
parts := strings.Split(line, " - ") if useMounts {
if len(parts) != 2 { fields := strings.Fields(line)
return nil, fmt.Errorf("found invalid mountinfo line in file %s: %s ", filename, line)
} d = PartitionStat{
Device: fields[0],
Mountpoint: unescapeFstab(fields[1]),
Fstype: fields[2],
Opts: fields[3],
}
fields := strings.Fields(parts[0]) if !all {
blockDeviceID := fields[2] if d.Device == "none" || !common.StringsHas(fs, d.Fstype) {
mountPoint := fields[4] continue
mountOpts := fields[5] }
}
} 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[1]) fields := strings.Fields(parts[0])
fstype := fields[0] blockDeviceID := fields[2]
device := fields[1] mountPoint := fields[4]
mountOpts := fields[5]
d := PartitionStat{ fields = strings.Fields(parts[1])
Device: device, fstype := fields[0]
Mountpoint: mountPoint, device := fields[1]
Fstype: fstype,
Opts: mountOpts, d = PartitionStat{
} Device: device,
if all == false { Mountpoint: mountPoint,
if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { Fstype: fstype,
continue Opts: mountOpts,
} }
}
// /dev/root is not the real device name if !all {
// so we get the real device name from its major/minor number if d.Device == "none" || !common.StringsHas(fs, d.Fstype) {
if d.Device == "/dev/root" { continue
devpath, err := os.Readlink(common.HostSys("/dev/block/" + blockDeviceID)) }
if err != nil { }
return nil, err
// /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) ret = append(ret, d)
} }

Loading…
Cancel
Save