Skip to content
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
69 changes: 69 additions & 0 deletions pkg/domain/entities/physicaldrive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
package physicaldrive

import (
"fmt"
"path/filepath"
"strings"

"github.com/pkg/errors"

"github.com/scality/raidmgmt/pkg/domain/entities/raidcontroller"
"github.com/scality/raidmgmt/pkg/utils"
)

const (
Expand All @@ -23,6 +26,7 @@ type (
Vendor string `json:"vendor,omitempty"` // Vendor
Model string `json:"model,omitempty"` // Model
Serial string `json:"serial,omitempty"` // Serial number
WWN string `json:"wwn,omitempty"` // World Wide Name
Size uint64 `json:"size,omitempty"` // Size in bytes
Type DiskType `json:"type,omitempty"` // Type (e.g.: HDD, SSD)
JBOD bool `json:"jbod,omitempty"` // Is the disk in JBOD mode
Expand Down Expand Up @@ -110,6 +114,71 @@ func (pd *PhysicalDrive) IsAvailable() bool {
return pd.Status == PDStatusUnassignedGood
}

// ComputePermanentPath computes the permanent path of the physical drive.
// NOTE: For physical drives backed by hardware RAID controllers this might not
// be available.
// nolint: funlen,nestif // This function is pretty simple to follow.
func (pd *PhysicalDrive) ComputePaths() error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about writing this as 2 methods (permanent and device) that returns the computed values instead of modifying implicitly ?
Better error handling, readability and testability IMO

Copy link
Copy Markdown
Contributor Author

@TeddyAndrieux TeddyAndrieux May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a method of physicalDrive struct to me it's cleaner to update in place
For separating permanent and device I could but one depends on the other and in all case we want to compute boths so... I don't know if it brings a lot of value proper error message are handled by this function and not the upper ones

if pd.PermanentPath == "" {
if pd.Type == DiskTypeNVMe {
if pd.Model != "" && pd.Serial != "" {
permanentPath := fmt.Sprintf(
"/dev/disk/by-id/nvme-%s_%s",
strings.ReplaceAll(pd.Model, " ", "_"),
strings.ReplaceAll(pd.Serial, " ", "_"),
)
if utils.FileExists(permanentPath) {
pd.PermanentPath = permanentPath
}
}
} else {
if pd.WWN != "" {
permanentPath := fmt.Sprintf("/dev/disk/by-id/wwn-%s", pd.WWN)
if utils.FileExists(permanentPath) {
pd.PermanentPath = permanentPath
}
}

if pd.PermanentPath == "" && pd.Vendor != "" && pd.Model != "" && pd.Serial != "" {
permanentPath := fmt.Sprintf(
"/dev/disk/by-id/scsi-S%s_%s_%s",
strings.ReplaceAll(pd.Vendor, " ", "_"),
strings.ReplaceAll(pd.Model, " ", "_"),
strings.ReplaceAll(pd.Serial, " ", "_"),
)
if utils.FileExists(permanentPath) {
pd.PermanentPath = permanentPath
}
}
}

if pd.PermanentPath == "" {
return errors.New("failed to compute permanent path")
}
}

if pd.DevicePath == "" {
resolvedPath, err := filepath.EvalSymlinks(pd.PermanentPath)
if err != nil {
return errors.Wrap(err, "failed to resolve permanent path symlink")
}

pd.DevicePath = resolvedPath
} else {
// Verify that device path matches the resolved symlink of permanent path
resolvedPath, err := filepath.EvalSymlinks(pd.PermanentPath)
if err != nil {
return errors.Wrap(err, "failed to resolve permanent path symlink")
}

if resolvedPath != pd.DevicePath {
return errors.New("device path does not match resolved permanent path")
}
}

return nil
}

// IsEqualTo checks if the Slot instance is equal to another Slot instance.
func (s *Slot) IsEqualTo(other *Slot) bool {
if s == nil && other == nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/implementation/raidcontroller/megaraid/logicalvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,12 @@ func getPaths(vdp *VDProperties, pdrives []*physicaldrive.PhysicalDrive) (

pd := pdrives[0]

permanentPath = computePermanentPath(pd)
if !CustomFileExists(permanentPath) {
return devicePath, "", errors.New("failed to get permanent path from physical drive")
err = pd.ComputePaths()
if err != nil {
return devicePath, "", errors.Wrap(err, "failed to compute paths from physical drive")
}

return pd.DevicePath, pd.PermanentPath, nil
Comment on lines +576 to +581
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cf com above, would make this part less implicity and enable more precise error handling

}

// If the devicePath is empty let's retrieve it from the permanent path
Expand Down
18 changes: 3 additions & 15 deletions pkg/implementation/raidcontroller/megaraid/physicaldrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package megaraid

import (
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -119,6 +118,7 @@ func (a *Adapter) physicalDrive(
Vendor: strings.TrimSpace(ddAttributes.ManufacturerID),
Model: strings.TrimSpace(pd.Model),
Serial: strings.TrimSpace(ddAttributes.SerialNumber),
WWN: fmt.Sprintf("0x%s", strings.TrimSpace(ddAttributes.WWN)),
Size: size,
Type: pd.DiskType(),
Status: pd.PDStatus(),
Expand All @@ -128,14 +128,9 @@ func (a *Adapter) physicalDrive(
}

if physicalDrive.JBOD {
physicalDrive.PermanentPath = computePermanentPath(physicalDrive)
if !utils.FileExists((physicalDrive.PermanentPath)) {
return nil, errors.New("permanent path does not exist")
}

physicalDrive.DevicePath, err = filepath.EvalSymlinks(physicalDrive.PermanentPath)
err := physicalDrive.ComputePaths()
if err != nil {
return nil, errors.Wrap(err, "failed to evaluate symlinks")
return nil, errors.Wrap(err, "failed to compute paths")
}
}

Expand Down Expand Up @@ -258,10 +253,3 @@ func (a *Adapter) setJBOD(

return nil
}

// computePermanentPath computes the permanent path for a given physical drive.
// The permanent path is a string that represents the physical drive
// this may not exists if the drive is not in JBOD.
func computePermanentPath(pd *physicaldrive.PhysicalDrive) string {
return fmt.Sprintf("/dev/disk/by-id/scsi-S%s_%s_%s", pd.Vendor, pd.Model, pd.Serial)
}