Skip to content

Commit

Permalink
Replace GetBlockDiskSubsystems with GetBlockDevice
Browse files Browse the repository at this point in the history
Longhorn 7672

Signed-off-by: Derek Su <[email protected]>
  • Loading branch information
derekbit committed Mar 29, 2024
1 parent 452b716 commit 15a2171
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pkg/spdk/disk/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 56 additions & 5 deletions pkg/util/block.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package util

import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
}

0 comments on commit 15a2171

Please sign in to comment.