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 dd464f8
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 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,34 @@ 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)
data, err := ioutil.ReadFile(devPath)
if err != nil {
return model, err
}

devModelPath := path.Join(devPath, "device", "model")
majorMinor := strings.TrimSpace(string(data))
driveInfoPath := runDevDataPfx + majorMinor

_, err = os.Stat(devModelPath)
f, err := os.Open(driveInfoPath)
if err != nil {
// check parent dir
devModelPath = path.Join(devPath, "..", "device", "model")
_, err = os.Stat(devModelPath)
if err != nil {
return model, err
}
return model, err
}
defer f.Close()

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
}
}

return model, err
Expand Down

0 comments on commit dd464f8

Please sign in to comment.