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

[ISSUE-1145]: use a serial number to compare drives (rel/CY2310 fix) #1155

Merged
merged 1 commit into from
May 17, 2024
Merged
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
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