Use HOST_* environment variables for getting disk serial number in Linux

This commit is contained in:
Jaime Soriano Pastor 2018-07-02 10:04:57 +02:00
parent 4a180b209f
commit 61902bc2a5
2 changed files with 6 additions and 2 deletions

View File

@ -395,7 +395,7 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
minor := unix.Minor(uint64(stat.Rdev)) minor := unix.Minor(uint64(stat.Rdev))
// Try to get the serial from udev data // Try to get the serial from udev data
udevDataPath := fmt.Sprintf("/run/udev/data/b%d:%d", major, minor) udevDataPath := common.HostRun(fmt.Sprintf("udev/data/b%d:%d", major, minor))
if udevdata, err := ioutil.ReadFile(udevDataPath); err == nil { if udevdata, err := ioutil.ReadFile(udevDataPath); err == nil {
scanner := bufio.NewScanner(bytes.NewReader(udevdata)) scanner := bufio.NewScanner(bytes.NewReader(udevdata))
for scanner.Scan() { for scanner.Scan() {
@ -408,7 +408,7 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
// Try to get the serial from sysfs, look at the disk device (minor 0) directly // Try to get the serial from sysfs, look at the disk device (minor 0) directly
// because if it is a partition it is not going to contain any device information // because if it is a partition it is not going to contain any device information
devicePath := fmt.Sprintf("/sys/dev/block/%d:0/device", major) devicePath := common.HostSys(fmt.Sprintf("dev/block/%d:0/device", major))
model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model")) model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model"))
serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial")) serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial"))
if len(model) > 0 && len(serial) > 0 { if len(model) > 0 && len(serial) > 0 {

View File

@ -328,6 +328,10 @@ func HostVar(combineWith ...string) string {
return GetEnv("HOST_VAR", "/var", combineWith...) return GetEnv("HOST_VAR", "/var", combineWith...)
} }
func HostRun(combineWith ...string) string {
return GetEnv("HOST_RUN", "/run", combineWith...)
}
// https://gist.github.com/kylelemons/1525278 // https://gist.github.com/kylelemons/1525278
func Pipeline(cmds ...*exec.Cmd) ([]byte, []byte, error) { func Pipeline(cmds ...*exec.Cmd) ([]byte, []byte, error) {
// Require at least one command // Require at least one command