[disk]linux: implements 'all' argument.

pull/185/head
Shirou WAKAYAMA 9 years ago
parent 5daf090513
commit ca99d7dc3f

@ -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)

Loading…
Cancel
Save