Skip to content

Commit

Permalink
Read model from file under /run/udev/data/
Browse files Browse the repository at this point in the history
  • Loading branch information
anjalshireesh committed Sep 28, 2023
1 parent 17f1b27 commit f9a864e
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (

const (
sysClassBlock = "/sys/class/block"
runDevDataPfx = "/run/udev/data/b"
devDir = "/dev/"
devLoopDir = "/dev/loop"
)
Expand Down Expand Up @@ -252,31 +253,31 @@ func getDeviceModel(partDevice string) (string, error) {
var model string

partDevName := strings.ReplaceAll(partDevice, devDir, "")
partDevPath := path.Join(sysClassBlock, partDevName)
devPath, err := os.Readlink(partDevPath)
devPath := path.Join(sysClassBlock, partDevName, "dev")

_, err := os.Stat(devPath)
if err != nil {
return model, err
}

if !path.IsAbs(devPath) {
devPath = path.Join(sysClassBlock, devPath)
}

devModelPath := path.Join(devPath, "device", "model")

_, err = os.Stat(devModelPath)
if err != nil {
// check parent dir
devModelPath = path.Join(devPath, "..", "device", "model")
_, err = os.Stat(devModelPath)
data, err := ioutil.ReadFile(devPath)
if err == nil {
majorMinor := strings.TrimSpace(string(data))
driveInfoPath := runDevDataPfx + majorMinor
f, err := os.Open(driveInfoPath)
if err != nil {
return model, err
}
}

data, err := ioutil.ReadFile(devModelPath)
if err == nil {
model = strings.TrimSpace(string(data))
buf := bufio.NewScanner(f)
for buf.Scan() {
field := strings.SplitN(buf.Text(), "=", 2)
if len(field) == 2 && field[0] == "E:ID_MODEL" {
model = field[1]
break
}
}
f.Close()
}

return model, err
Expand Down

0 comments on commit f9a864e

Please sign in to comment.