From 5a1770d6d8d3edb07469d9657fe254468fa554e4 Mon Sep 17 00:00:00 2001 From: Leonardo Witt Date: Tue, 31 Aug 2021 17:57:54 -0300 Subject: [PATCH] Added the device revision support for linux and fixed NUMANodeID read process --- README.md | 1 + pkg/block/block.go | 8 +++++++- pkg/block/block_linux.go | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a3b4630f..88bdf754 100644 --- a/README.md +++ b/README.md @@ -511,6 +511,7 @@ Each `ghw.Disk` struct contains the following fields: [World Wide Name](https://en.wikipedia.org/wiki/World_Wide_Name) * `ghw.Disk.Partitions` contains an array of pointers to `ghw.Partition` structs, one for each partition on the disk + `ghw.Disk.Revision` contains a string with the disk's device revision Each `ghw.Partition` struct contains these fields: diff --git a/pkg/block/block.go b/pkg/block/block.go index 7a2e4e38..4c5232a6 100644 --- a/pkg/block/block.go +++ b/pkg/block/block.go @@ -106,6 +106,7 @@ type Disk struct { SerialNumber string `json:"serial_number"` WWN string `json:"wwn"` Partitions []*Partition `json:"partitions"` + Revision string `json:"revision"` // TODO(jaypipes): Add PCI field for accessing PCI device information // PCI *PCIDevice `json:"pci"` } @@ -190,8 +191,12 @@ func (d *Disk) String() string { if d.IsRemovable { removable = " removable=true" } + revision := "" + if d.Revision != "" { + revision = " revision=" + d.Revision + } return fmt.Sprintf( - "%s %s (%s) %s [@%s%s]%s%s%s%s%s", + "%s %s (%s) %s [@%s%s]%s%s%s%s%s%s", d.Name, d.DriveType.String(), sizeStr, @@ -203,6 +208,7 @@ func (d *Disk) String() string { serial, wwn, removable, + revision, ) } diff --git a/pkg/block/block_linux.go b/pkg/block/block_linux.go index 07db1067..e7fc737c 100644 --- a/pkg/block/block_linux.go +++ b/pkg/block/block_linux.go @@ -71,8 +71,8 @@ func diskNUMANodeID(paths *linuxpath.Paths, disk string) int { return -1 } for partial := link; strings.HasPrefix(partial, "../devices/"); partial = filepath.Base(partial) { - if nodeContents, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, partial, "numa_node")); err != nil { - if nodeInt, err := strconv.Atoi(string(nodeContents)); err != nil { + if nodeContents, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, partial, "numa_node")); err == nil { + if nodeInt, err := strconv.Atoi(string(nodeContents)); err == nil { return nodeInt } } @@ -91,6 +91,17 @@ func diskVendor(paths *linuxpath.Paths, disk string) string { return strings.TrimSpace(string(contents)) } +func diskRevision(paths *linuxpath.Paths, disk string) string { + // In Linux, the disk revision is found in the + // /sys/block/$DEVICE/device/rev file in sysfs + path := filepath.Join(paths.SysBlock, disk, "device", "rev") + contents, err := ioutil.ReadFile(path) + if err != nil { + return util.UNKNOWN + } + return strings.TrimSpace(string(contents)) +} + func udevInfo(paths *linuxpath.Paths, disk string) (map[string]string, error) { // Get device major:minor numbers devNo, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, disk, "dev")) @@ -286,6 +297,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk { serialNo := diskSerialNumber(paths, dname) wwn := diskWWN(paths, dname) removable := diskIsRemovable(paths, dname) + revision := diskRevision(paths, dname) d := &Disk{ Name: dname, @@ -300,6 +312,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk { Model: model, SerialNumber: serialNo, WWN: wwn, + Revision: revision, } parts := diskPartitions(ctx, paths, dname)