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 {