diff --git a/v3/disk/disk.go b/v3/disk/disk.go index ae63c394..dd4cc1d5 100644 --- a/v3/disk/disk.go +++ b/v3/disk/disk.go @@ -80,3 +80,17 @@ func Partitions(all bool) ([]PartitionStat, error) { func IOCounters(names ...string) (map[string]IOCountersStat, error) { return IOCountersWithContext(context.Background(), names...) } + +// SerialNumber returns Serial Number of given device or empty string +// on error. Name of device is expected, eg. /dev/sda +func SerialNumber(name string) (string, error) { + return SerialNumberWithContext(context.Background(), name) +} + +// Label returns label of given device or empty string on error. +// Name of device is expected, eg. /dev/sda +// Supports label based on devicemapper name +// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm +func Label(name string) (string, error) { + return LabelWithContext(context.Background(), name) +} diff --git a/v3/disk/disk_darwin.go b/v3/disk/disk_darwin.go index 2c87d7fc..985e9f35 100644 --- a/v3/disk/disk_darwin.go +++ b/v3/disk/disk_darwin.go @@ -76,3 +76,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro func getFsType(stat unix.Statfs_t) string { return common.ByteToString(stat.Fstypename[:]) } + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_fallback.go b/v3/disk/disk_fallback.go index 9cfccbb7..f50ebf19 100644 --- a/v3/disk/disk_fallback.go +++ b/v3/disk/disk_fallback.go @@ -19,3 +19,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { return nil, common.ErrNotImplementedError } + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_freebsd.go b/v3/disk/disk_freebsd.go index a24cba73..767a28b9 100644 --- a/v3/disk/disk_freebsd.go +++ b/v3/disk/disk_freebsd.go @@ -160,3 +160,11 @@ func parsedevstat(buf []byte) (devstat, error) { func getFsType(stat unix.Statfs_t) string { return common.ByteToString(stat.Fstypename[:]) } + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_linux.go b/v3/disk/disk_linux.go index b302cf5a..459da5ef 100644 --- a/v3/disk/disk_linux.go +++ b/v3/disk/disk_linux.go @@ -431,25 +431,19 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC } d.Name = name - d.SerialNumber = GetDiskSerialNumber(name) - d.Label = GetLabel(name) + d.SerialNumber, _ = SerialNumberWithContext(ctx, name) + d.Label, _ = LabelWithContext(ctx, name) ret[name] = d } return ret, nil } -// GetDiskSerialNumber returns Serial Number of given device or empty string -// on error. Name of device is expected, eg. /dev/sda -func GetDiskSerialNumber(name string) string { - return GetDiskSerialNumberWithContext(context.Background(), name) -} - -func GetDiskSerialNumberWithContext(ctx context.Context, name string) string { +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { var stat unix.Stat_t err := unix.Stat(name, &stat) if err != nil { - return "" + return "", err } major := unix.Major(uint64(stat.Rdev)) minor := unix.Minor(uint64(stat.Rdev)) @@ -461,7 +455,7 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string { for scanner.Scan() { values := strings.Split(scanner.Text(), "=") if len(values) == 2 && values[0] == "E:ID_SERIAL" { - return values[1] + return values[1], nil } } } @@ -472,28 +466,24 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string { model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model")) serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial")) if len(model) > 0 && len(serial) > 0 { - return fmt.Sprintf("%s_%s", string(model), string(serial)) + return fmt.Sprintf("%s_%s", string(model), string(serial)), nil } - return "" + return "", nil } -// GetLabel returns label of given device or empty string on error. -// Name of device is expected, eg. /dev/sda -// Supports label based on devicemapper name -// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm -func GetLabel(name string) string { +func LabelWithContext(ctx context.Context, name string) (string, error) { // Try label based on devicemapper name dmname_filename := common.HostSys(fmt.Sprintf("block/%s/dm/name", name)) if !common.PathExists(dmname_filename) { - return "" + return "", nil } dmname, err := ioutil.ReadFile(dmname_filename) if err != nil { - return "" + return "", err } else { - return strings.TrimSpace(string(dmname)) + return strings.TrimSpace(string(dmname)), nil } } diff --git a/v3/disk/disk_openbsd.go b/v3/disk/disk_openbsd.go index 5c53420d..24324a4f 100644 --- a/v3/disk/disk_openbsd.go +++ b/v3/disk/disk_openbsd.go @@ -148,3 +148,11 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { func getFsType(stat unix.Statfs_t) string { return common.IntToString(stat.F_fstypename[:]) } + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_solaris.go b/v3/disk/disk_solaris.go index 7d48444e..8601458e 100644 --- a/v3/disk/disk_solaris.go +++ b/v3/disk/disk_solaris.go @@ -113,3 +113,10 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { return usageStat, nil } +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_windows.go b/v3/disk/disk_windows.go index 248ed1f5..1293586c 100644 --- a/v3/disk/disk_windows.go +++ b/v3/disk/disk_windows.go @@ -181,3 +181,11 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC } return drivemap, nil } + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +}