From 0b49c0df2e97db755e1b22a13c0bdd0c108b3ff5 Mon Sep 17 00:00:00 2001 From: Dawid Korzepa <56738926+korzepadawid@users.noreply.github.com> Date: Fri, 17 May 2024 09:01:01 +0200 Subject: [PATCH] [ISSUE-1145]: use a serial number to compare drives (#1146) (#1155) * [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 --- pkg/base/linuxutils/lsblk/lsblk.go | 33 ++++++++++--------------- pkg/base/linuxutils/lsblk/lsblk_test.go | 11 +-------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/pkg/base/linuxutils/lsblk/lsblk.go b/pkg/base/linuxutils/lsblk/lsblk.go index f703ca8d9..16c96afd4 100644 --- a/pkg/base/linuxutils/lsblk/lsblk.go +++ b/pkg/base/linuxutils/lsblk/lsblk.go @@ -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" @@ -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 { @@ -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 } diff --git a/pkg/base/linuxutils/lsblk/lsblk_test.go b/pkg/base/linuxutils/lsblk/lsblk_test.go index f3ac59b94..5ccb60bab 100644 --- a/pkg/base/linuxutils/lsblk/lsblk_test.go +++ b/pkg/base/linuxutils/lsblk/lsblk_test.go @@ -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 @@ -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) {