diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 91e25a1..c1c3345 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -211,7 +211,10 @@ var fsTypeMap = map[int64]string{ ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */ } -// Get disk partitions. +// Partitions returns disk partitions. If all is false, returns +// physical devices only (e.g. hard disks, cd-rom drives, USB keys) +// and ignore all others (e.g. memory partitions such as /dev/shm) +// // should use setmntent(3) but this implement use /etc/mtab file func Partitions(all bool) ([]PartitionStat, error) { filename := common.HostEtc("mtab") @@ -220,6 +223,11 @@ func Partitions(all bool) ([]PartitionStat, error) { return nil, err } + fs, err := getFileSystems() + if err != nil { + return nil, err + } + ret := make([]PartitionStat, 0, len(lines)) for _, line := range lines { @@ -230,12 +238,40 @@ func Partitions(all bool) ([]PartitionStat, error) { Fstype: fields[2], Opts: fields[3], } + if all == false { + if d.Device == "none" || !common.StringsHas(fs, d.Fstype) { + continue + } + } ret = append(ret, d) } return ret, nil } +// getFileSystems returns supported filesystems from /proc/filesystems +func getFileSystems() ([]string, error) { + filename := common.HostProc("filesystems") + lines, err := common.ReadLines(filename) + if err != nil { + return nil, err + } + var ret []string + for _, line := range lines { + if !strings.HasPrefix(line, "nodev") { + ret = append(ret, strings.TrimSpace(line)) + continue + } + t := strings.Split(line, "\t") + if len(t) != 2 || t[1] != "zfs" { + continue + } + ret = append(ret, strings.TrimSpace(t[1])) + } + + return ret, nil +} + func IOCounters() (map[string]IOCountersStat, error) { filename := common.HostProc("diskstats") lines, err := common.ReadLines(filename)