From 15a2171d9e14a9d5be46d1a29e20afb85f2cd0fa Mon Sep 17 00:00:00 2001 From: Derek Su Date: Fri, 29 Mar 2024 01:12:22 +0000 Subject: [PATCH] Replace GetBlockDiskSubsystems with GetBlockDevice Longhorn 7672 Signed-off-by: Derek Su --- pkg/spdk/disk/types.go | 8 +++--- pkg/util/block.go | 61 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/pkg/spdk/disk/types.go b/pkg/spdk/disk/types.go index 80f3b8b9..5835b3d5 100644 --- a/pkg/spdk/disk/types.go +++ b/pkg/spdk/disk/types.go @@ -82,14 +82,14 @@ func getDriverForAuto(diskStatus *helpertypes.DiskStatus, diskPath string) (comm case string(commonTypes.DiskDriverNvme): return commonTypes.DiskDriverNvme, nil case string(commonTypes.DiskDriverVirtioPci): - subsystems, err := util.GetBlockDiskSubsystems(diskPath) + blockdevice, err := util.GetBlockDevice(diskPath) if err != nil { - return commonTypes.DiskDriverNone, errors.Wrapf(err, "failed to get disk subsystems for %s", diskPath) + return commonTypes.DiskDriverNone, errors.Wrapf(err, "failed to get blockdevice info for %s", diskPath) } - if slices.Contains(subsystems, string(BlockDiskSubsystemVirtio)) && slices.Contains(subsystems, string(BlockDiskSubsystemPci)) { + if slices.Contains(blockdevice.Subsystems, string(BlockDiskSubsystemVirtio)) && slices.Contains(blockdevice.Subsystems, string(BlockDiskSubsystemPci)) { diskDriver := commonTypes.DiskDriverVirtioBlk - if slices.Contains(subsystems, string(BlockDiskSubsystemScsi)) { + if slices.Contains(blockdevice.Subsystems, string(BlockDiskSubsystemScsi)) { diskDriver = commonTypes.DiskDriverVirtioScsi } return diskDriver, nil diff --git a/pkg/util/block.go b/pkg/util/block.go index e15926a3..49a3874d 100644 --- a/pkg/util/block.go +++ b/pkg/util/block.go @@ -1,8 +1,10 @@ package util import ( + "encoding/json" "fmt" "os" + "strconv" "strings" "github.com/pkg/errors" @@ -41,17 +43,66 @@ func GetDevNameFromBDF(bdf string) (string, error) { return "", fmt.Errorf("failed to find device for BDF %s", bdf) } -func GetBlockDiskSubsystems(devPath string) ([]string, error) { +type BlockDevice struct { + Name string `json:"name"` + Path string `json:"path"` + Subsystems []string `json:"subsystems"` + Maj int `json:"maj"` + Min int `json:"min"` +} + +type BlockDevices struct { + BlockDevices []struct { + Name string `json:"name"` + Path string `json:"path"` + MajMin string `json:"maj:min"` + Subsystems string `json:"subsystems"` + } `json:"blockdevices"` +} + +func GetBlockDevice(devPath string) (BlockDevice, error) { ne, err := helperutil.NewExecutor(commonTypes.ProcDirectory) if err != nil { - return nil, errors.Wrap(err, "failed to create executor") + return BlockDevice{}, errors.Wrap(err, "failed to create executor") } - cmdArgs := []string{"-n", "-d", "-o", "subsystems", devPath} + cmdArgs := []string{"-O", "-J", devPath} output, err := ne.Execute(nil, "lsblk", cmdArgs, types.ExecuteTimeout) if err != nil { - return nil, errors.Wrap(err, "failed to get disk subsystems") + return BlockDevice{}, errors.Wrap(err, "failed to get disk subsystems") + } + + var blockDevices BlockDevices + err = json.Unmarshal([]byte(output), &blockDevices) + if err != nil { + return BlockDevice{}, err + } + + if len(blockDevices.BlockDevices) == 0 { + return BlockDevice{}, fmt.Errorf("no blockdevices found") + } + + bd := blockDevices.BlockDevices[0] + majMinParts := strings.Split(bd.MajMin, ":") + if len(majMinParts) != 2 { + return BlockDevice{}, fmt.Errorf("invalid maj:min format") + } + maj, err := strconv.Atoi(majMinParts[0]) + if err != nil { + return BlockDevice{}, err + } + min, err := strconv.Atoi(majMinParts[1]) + if err != nil { + return BlockDevice{}, err } - return strings.Split(strings.TrimSpace(string(output)), ":"), nil + subsystems := strings.Split(bd.Subsystems, ":") + + return BlockDevice{ + Name: bd.Name, + Path: bd.Path, + Subsystems: subsystems, + Maj: maj, + Min: min, + }, nil }