Merge pull request #1 from nvinzens/replace_self_mounts_with_self_mountinfo

replace self/mounts with self/mountinfo
pull/593/head
Nico Vinzens 7 years ago committed by GitHub
commit 453688f8c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,13 +8,13 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
) )
const ( const (
@ -224,7 +224,7 @@ func Partitions(all bool) ([]PartitionStat, error) {
} }
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
filename := common.HostProc("self/mounts") filename := common.HostProc("self/mountinfo")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -238,18 +238,45 @@ 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 {
fields := strings.Fields(line) // 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)
// 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)
}
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]
d := PartitionStat{ d := PartitionStat{
Device: fields[0], Device: device,
Mountpoint: fields[1], Mountpoint: mountPoint,
Fstype: fields[2], Fstype: fstype,
Opts: fields[3], Opts: mountOpts,
} }
if all == false { if all == false {
if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { if d.Device == "none" || !common.StringsHas(fs, d.Fstype) {
continue 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)
}
ret = append(ret, d) ret = append(ret, d)
} }

Loading…
Cancel
Save