From 0697c463bc9fb9113ff05db37617dc029bad50cb Mon Sep 17 00:00:00 2001 From: Somanchi Poorna Sobhita Date: Tue, 4 Aug 2026 09:46:18 +0000 Subject: [PATCH] core: extract the /dev prefix handling into a helper The same 'split on / and prepend /dev when there is no separator' block appeared five times in this file: GetDevicePartitions, GetDeviceProperties, GetUdevInfo, GetDeviceFilesystems and GetDiskUUID. GetUdevInfo was the one that had it wrong, which is what the previous commit fixed. Move it into resolveDevicePath so there is a single definition to reason about, and document why the check exists: a caller may pass a bare kernel name, a device node, or the mount point of a PVC-backed block device, and only the first needs the prefix. No behaviour change. GetDevicePartitions still compares lsblk output against the unmodified device name, so only the path passed to lsblk is resolved. Signed-off-by: Somanchi Poorna Sobhita --- pkg/util/sys/device.go | 49 ++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/pkg/util/sys/device.go b/pkg/util/sys/device.go index 66455dc97..6b917497c 100644 --- a/pkg/util/sys/device.go +++ b/pkg/util/sys/device.go @@ -130,15 +130,22 @@ func ListDevices(executor exec.Executor) ([]string, error) { return strings.Split(devices, "\n"), nil } +// resolveDevicePath returns an absolute path for a device. Callers pass either +// a bare kernel name ("sdb") or an absolute path, which may be a device node +// ("/dev/sdb") or a mount point for a PVC-backed block device +// ("/mnt/"). Only a bare kernel name needs the /dev prefix; adding it +// unconditionally would produce "/dev//dev/sdb". +func resolveDevicePath(device string) string { + if len(strings.Split(device, "/")) == 1 { + return fmt.Sprintf("/dev/%s", device) + } + + return device +} + // GetDevicePartitions gets partitions on a given device func GetDevicePartitions(device string, executor exec.Executor) (partitions []Partition, unusedSpace uint64, err error) { - var devicePath string - splitDevicePath := strings.Split(device, "/") - if len(splitDevicePath) == 1 { - devicePath = fmt.Sprintf("/dev/%s", device) // device path for OSD on devices. - } else { - devicePath = device // use the exact device path (like /mnt/) in case of PVC block device - } + devicePath := resolveDevicePath(device) output, err := executor.ExecuteCommandWithOutput("lsblk", devicePath, "--bytes", "--pairs", "--output", "NAME,SIZE,TYPE,PKNAME") @@ -197,14 +204,7 @@ func GetDevicePartitions(device string, executor exec.Executor) (partitions []Pa // GetDeviceProperties gets device properties func GetDeviceProperties(device string, executor exec.Executor) (map[string]string, error) { - // As we are mounting the block mode PVs on /mnt we use the entire path, - // e.g., if the device path is /mnt/example-pvc then it's taken completely - // else if it's just vdb then the following is used - devicePath := strings.Split(device, "/") - if len(devicePath) == 1 { - device = fmt.Sprintf("/dev/%s", device) - } - return GetDevicePropertiesFromPath(device, executor) + return GetDevicePropertiesFromPath(resolveDevicePath(device), executor) } // GetDevicePropertiesFromPath gets a device property from a path @@ -235,14 +235,7 @@ func IsLV(devicePath string, executor exec.Executor) (bool, error) { // GetUdevInfo gets udev information func GetUdevInfo(device string, executor exec.Executor) (map[string]string, error) { - // The caller may pass either a kernel name ("sdb") or a full device path - // ("/dev/sdb"), so only add the /dev prefix when it is missing. Adding it - // unconditionally produces "/dev//dev/sdb", which udevadm rejects. - devicePath := strings.Split(device, "/") - if len(devicePath) == 1 { - device = fmt.Sprintf("/dev/%s", device) - } - output, err := executor.ExecuteCommandWithOutput("udevadm", "info", "--query=property", device) + output, err := executor.ExecuteCommandWithOutput("udevadm", "info", "--query=property", resolveDevicePath(device)) if err != nil { return nil, err } @@ -253,10 +246,7 @@ func GetUdevInfo(device string, executor exec.Executor) (map[string]string, erro // GetDeviceFilesystems get the file systems available func GetDeviceFilesystems(device string, executor exec.Executor) (string, error) { - devicePath := strings.Split(device, "/") - if len(devicePath) == 1 { - device = fmt.Sprintf("/dev/%s", device) - } + device = resolveDevicePath(device) output, err := executor.ExecuteCommandWithOutput("udevadm", "info", "--query=property", device) if err != nil { return "", err @@ -271,10 +261,7 @@ func GetDiskUUID(device string, executor exec.Executor) (string, error) { return "", errors.Wrap(err, "sgdisk not found") } - devicePath := strings.Split(device, "/") - if len(devicePath) == 1 { - device = fmt.Sprintf("/dev/%s", device) - } + device = resolveDevicePath(device) output, err := executor.ExecuteCommandWithOutput(sgdiskCmd, "--print", device) if err != nil {