Add bus name & bus number to disk name, example: bus_0_megaraid_disk_01

Signed-off-by: Denys <zxzharmlesszxz@gmail.com>
This commit is contained in:
Denys 2024-03-16 00:37:09 +01:00
parent 3a012b5bb1
commit 7f7c652b0f

View file

@ -44,11 +44,25 @@ type SMARTctl struct {
}
func extractDiskName(input string) string {
re := regexp.MustCompile(`^(?:/dev/\S+/\S+\s\[|/dev/|\[)(?:\s\[|)(?P<disk>[a-z0-9_]+)(?:\].*|)$`)
re := regexp.MustCompile(`^(?:/dev/(?P<bus_name>\S+)/(?P<bus_num>\S+)\s\[|/dev/|\[)(?:\s\[|)(?P<disk>[a-z0-9_]+)(?:\].*|)$`)
match := re.FindStringSubmatch(input)
if len(match) > 0 {
return match[re.SubexpIndex("disk")]
busNameIndex := re.SubexpIndex("bus_name")
busNumIndex := re.SubexpIndex("bus_num")
diskIndex := re.SubexpIndex("disk")
var name []string
if busNameIndex != -1 && match[busNameIndex] != "" {
name = append(name, match[busNameIndex])
}
if busNumIndex != -1 && match[busNumIndex] != "" {
name = append(name, match[busNumIndex])
}
if diskIndex != -1 && match[diskIndex] != "" {
name = append(name, match[diskIndex])
}
return strings.Join(name, "_")
}
return ""
}