2018-07-11 23:43:49 +00:00
|
|
|
package diskio
|
2016-07-04 22:48:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-12-13 19:49:19 +00:00
|
|
|
"bytes"
|
2016-07-04 22:48:37 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-10-24 23:22:31 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-07-04 22:48:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type diskInfoCache struct {
|
2017-10-24 23:22:31 +00:00
|
|
|
udevDataPath string
|
|
|
|
values map[string]string
|
2016-07-04 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var udevPath = "/run/udev/data"
|
|
|
|
|
2018-01-17 23:12:05 +00:00
|
|
|
func (s *DiskIO) diskInfo(devName string) (map[string]string, error) {
|
2017-10-24 23:22:31 +00:00
|
|
|
var err error
|
|
|
|
var stat unix.Stat_t
|
|
|
|
|
|
|
|
path := "/dev/" + devName
|
|
|
|
err = unix.Stat(path, &stat)
|
2016-07-04 22:48:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.infoCache == nil {
|
|
|
|
s.infoCache = map[string]diskInfoCache{}
|
|
|
|
}
|
|
|
|
ic, ok := s.infoCache[devName]
|
|
|
|
if ok {
|
|
|
|
return ic.values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
major := stat.Rdev >> 8 & 0xff
|
|
|
|
minor := stat.Rdev & 0xff
|
2017-10-24 23:22:31 +00:00
|
|
|
udevDataPath := fmt.Sprintf("%s/b%d:%d", udevPath, major, minor)
|
|
|
|
|
|
|
|
di := map[string]string{}
|
2016-07-04 22:48:37 +00:00
|
|
|
|
2017-10-24 23:22:31 +00:00
|
|
|
s.infoCache[devName] = diskInfoCache{
|
|
|
|
udevDataPath: udevDataPath,
|
|
|
|
values: di,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(udevDataPath)
|
2016-07-04 22:48:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2017-10-24 23:22:31 +00:00
|
|
|
scnr := bufio.NewScanner(f)
|
2018-12-13 19:49:19 +00:00
|
|
|
var devlinks bytes.Buffer
|
2016-07-04 22:48:37 +00:00
|
|
|
for scnr.Scan() {
|
|
|
|
l := scnr.Text()
|
2018-12-13 19:49:19 +00:00
|
|
|
if len(l) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if l[:2] == "S:" {
|
|
|
|
if devlinks.Len() > 0 {
|
|
|
|
devlinks.WriteString(" ")
|
|
|
|
}
|
|
|
|
devlinks.WriteString("/dev/")
|
|
|
|
devlinks.WriteString(l[2:])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if l[:2] != "E:" {
|
2016-07-04 22:48:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
kv := strings.SplitN(l[2:], "=", 2)
|
|
|
|
if len(kv) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
di[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:49:19 +00:00
|
|
|
if devlinks.Len() > 0 {
|
|
|
|
di["DEVLINKS"] = devlinks.String()
|
|
|
|
}
|
|
|
|
|
2016-07-04 22:48:37 +00:00
|
|
|
return di, nil
|
|
|
|
}
|