Skip to content

Commit

Permalink
Add method to return nvme controller device
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshaySainiDell committed Oct 25, 2024
1 parent c918563 commit 033c071
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion gofsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type FSinterface interface {
findFSType(ctx context.Context, mountpoint string) (fsType string, err error)
getMpathNameFromDevice(ctx context.Context, device string) (string, error)
fsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error)
getNVMeController(device string) (string, error)

// Architecture agnostic implementations, generally just wrappers
GetDiskFormat(ctx context.Context, disk string) (string, error)
Expand All @@ -73,6 +74,7 @@ type FSinterface interface {
FindFSType(ctx context.Context, mountpoint string) (fsType string, err error)
GetMpathNameFromDevice(ctx context.Context, device string) (string, error)
FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error)
GetNVMeController(device string) (string, error)
}

// MultipathDevDiskByIDPrefix is a pathname prefix for items located in /dev/disk/by-id
Expand Down Expand Up @@ -300,4 +302,9 @@ func GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN string) ([]st
// FsInfo given the path of the filesystem will return its stats
func FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
return fs.fsInfo(ctx, path)
}
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func GetNVMeController(device string) (string, error) {
return fs.getNVMeController(device)
}
5 changes: 5 additions & 0 deletions gofsutil_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,9 @@ func (fs *FS) GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN stri
// FsInfo given the path of the filesystem will return its stats
func (fs *FS) FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
return fs.fsInfo(ctx, path)
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func (fs *FS) GetNVMeController(device string) (string, error) {
return fs.getNVMeController(device)
}
37 changes: 37 additions & 0 deletions gofsutil_mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,40 @@ func wwnMatches(nguid, wwn string) bool {

return false
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func (fs *FS) getNVMeController(device string) (string, error) {
devicePath := filepath.Join(fs.SysBlockDir, device)

// Check if the device path exists
if _, err := os.Stat(devicePath); os.IsNotExist(err) {
return "", fmt.Errorf("device %s does not exist", device)
}

// Resolve the symlink to find the actual path in /sys/device e.g. /sys/devices/virtual/nvme-fabrics/ctl/nvme0/nvme0n1
realPath, err := filepath.EvalSymlinks(devicePath)
if err != nil {
return "", fmt.Errorf("error resolving symlink for %s: %v", device, err)
}

isNvmeController := false
// Split the path and look for the controller in /sys/class/nvme
pathParts := strings.Split(realPath, "/")
for i, part := range pathParts {
if strings.Contains(part, "ctl") {
isNvmeController = true
} else if isNvmeController && part == device {
// The controller is the part right before the device name
if i > 0 && strings.HasPrefix(pathParts[i-1], "nvme") {
return pathParts[i-1], nil
}
}
}

if !isNvmeController {
log.Infof("Not a valid nvme controller device: %s ", device)
return "", nil
}

return "", fmt.Errorf("controller not found for device %s", device)
}

0 comments on commit 033c071

Please sign in to comment.