Skip to content

Commit

Permalink
[ISSUE-1145]: use a serial number to compare drives (#1146) (#1155)
Browse files Browse the repository at this point in the history
* [ISSUE-1145]: use a serial number to compare drives



* [ISSUE-1145]: loopbacks fix



* [ISSUE-1145]: cr notes



* [ISSUE-1145]: fixed error msg



---------

Signed-off-by: Dawid Korzepa <[email protected]>
  • Loading branch information
korzepadawid authored May 17, 2024
1 parent 9467946 commit 0b49c0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
33 changes: 13 additions & 20 deletions pkg/base/linuxutils/lsblk/lsblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ type WrapLsblk interface {

// LSBLK is a wrap for system lsblk util
type LSBLK struct {
e command.CmdExecutor
e command.CmdExecutor
log *logrus.Logger
}

// NewLSBLK is a constructor for LSBLK struct
func NewLSBLK(log *logrus.Logger) *LSBLK {
e := command.NewExecutor(log)
e.SetLevel(logrus.TraceLevel)
return &LSBLK{e: e}
return &LSBLK{e: e, log: log}
}

// CustomInt64 to handle Size lsblk output - 8001563222016 or "8001563222016"
Expand Down Expand Up @@ -157,16 +158,10 @@ func (l *LSBLK) GetBlockDevices(device string) ([]BlockDevice, error) {
return res, nil
}

// SearchDrivePath if not defined returns drive path based on drive S/N, VID and PID.
// SearchDrivePath if not defined returns drive path based on drive S/N.
// Receives an instance of drivecrd.Drive struct
// Returns drive's path based on provided drivecrd.Drive or error if something went wrong
func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) {
// device path might be already set by hwmgr
device := drive.Path
if device != "" {
return device, nil
}

// try to find it with lsblk
lsblkOut, err := l.GetBlockDevices("")
if err != nil {
Expand All @@ -175,20 +170,18 @@ func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) {

// get drive serial number
sn := drive.SerialNumber
vid := drive.VID
pid := drive.PID
for _, l := range lsblkOut {
if strings.EqualFold(l.Serial, sn) && strings.EqualFold(l.Vendor, vid) &&
strings.EqualFold(l.Model, pid) {
device = l.Name
break
for _, blockDevice := range lsblkOut {
if strings.EqualFold(blockDevice.Serial, sn) {
return blockDevice.Name, nil
}
}

if device == "" {
errMsg := fmt.Errorf("unable to find drive path by SN %s, VID %s, PID %s", sn, vid, pid)
return "", errMsg
// device path might be already set by hwmgr
if drive.Path != "" {
l.log.Warnf("unable to find a drive path by a given SN: %s, using a drive path: %s", sn, drive.Path)
return drive.Path, nil
}

return device, nil
errMsg := fmt.Errorf("unable to find drive path by a SN %s or in DriveCR", sn)
return "", errMsg
}
11 changes: 1 addition & 10 deletions pkg/base/linuxutils/lsblk/lsblk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestLSBLK_SearchDrivePath_Success(t *testing.T) {
l.e = e

// path is in drive spec
e.On("RunCmd", allDevicesCmd).Return(mocks.LsblkTwoDevicesStr, "", nil)
dCR := testDriveCR
path := "/dev/sda"
dCR.Spec.Path = path
Expand Down Expand Up @@ -126,16 +127,6 @@ func TestLSBLK_SearchDrivePath(t *testing.T) {
res, err = l.SearchDrivePath(&dCR.Spec)
assert.Equal(t, "", res)
assert.NotNil(t, err)

//different VID and PID
e.On("RunCmd", allDevicesCmd).Return(mocks.LsblkTwoDevicesStr, "", nil)
sn = "hdd1" // from mocks.LsblkTwoDevicesStr
dCR.Spec.SerialNumber = sn
dCR.Spec.VID = "vendor"
dCR.Spec.PID = "pid"

res, err = l.SearchDrivePath(&dCR.Spec)
assert.NotNil(t, err)
}

func TestLSBLK_GetBlockDevicesV2_Success(t *testing.T) {
Expand Down

0 comments on commit 0b49c0d

Please sign in to comment.