From b4386f37510bc25e39b231fa587288ad0abf0b68 Mon Sep 17 00:00:00 2001 From: Artem Chernyshev Date: Thu, 30 Mar 2023 17:38:40 +0300 Subject: [PATCH] feat: make disk utils read subsystem information from the `/sys/block` This way it will be possible to tell what kind of block device is that. Signed-off-by: Artem Chernyshev --- blockdevice/util/disk/disks.go | 11 ++++++++++- blockdevice/util/disk/disks_test.go | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/blockdevice/util/disk/disks.go b/blockdevice/util/disk/disks.go index 032d1c5..f17b48c 100644 --- a/blockdevice/util/disk/disks.go +++ b/blockdevice/util/disk/disks.go @@ -90,6 +90,8 @@ type Disk struct { Type Type // BusPath PCI bus path. BusPath string + // SubSystem is the dest path of symlink /sys/block//subsystem. + SubSystem string // ReadOnly indicates that the kernel has marked this disk as read-only. ReadOnly bool } @@ -209,7 +211,7 @@ func Get(dev string) *Disk { readOnlyBool = true } - return &Disk{ + disk := &Disk{ DeviceName: fmt.Sprintf("/dev/%s", dev), Size: size, Model: readFile(sysblock, dev, "device/model"), @@ -222,6 +224,13 @@ func Get(dev string) *Disk { BusPath: busPath, ReadOnly: readOnlyBool, } + + path, err := filepath.EvalSymlinks(filepath.Join(sysblock, dev, "subsystem")) + if err == nil { + disk.SubSystem = path + } + + return disk } // Find disk matching provided spec. diff --git a/blockdevice/util/disk/disks_test.go b/blockdevice/util/disk/disks_test.go index 4b39b72..374b899 100644 --- a/blockdevice/util/disk/disks_test.go +++ b/blockdevice/util/disk/disks_test.go @@ -26,6 +26,7 @@ func (suite *DisksSuite) TestDisks() { for _, d := range disks { suite.Require().NotEmpty(d.DeviceName) suite.Require().NotEmpty(d.Model) + suite.Require().NotEmpty(d.SubSystem) } } }