Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the device revision support for linux and fixed NUMANodeID read process #273

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 7 additions & 1 deletion pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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,
Expand All @@ -203,6 +208,7 @@ func (d *Disk) String() string {
serial,
wwn,
removable,
revision,
)
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, Leonardo!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! but this would deserve a separate PR

return nodeInt
}
}
Expand All @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok to add support for linux first and add a TODO for windows/mac

// 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"))
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down