Skip to content

Commit

Permalink
Fix size probe for invalid devices (#931)
Browse files Browse the repository at this point in the history
for any invalid (or) detached drives, size probe will fail with the
following error

```
strconv.ParseUint: parsing "": invalid syntax
```

which prevents the directpv containers to start

bonus: improve probe error messages
  • Loading branch information
Praveenrajmani authored Aug 22, 2024
1 parent 6643fbd commit a0b8eed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions pkg/device/probe_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package device

import (
"errors"
"fmt"
"os"

"github.com/minio/directpv/pkg/sys"
Expand Down Expand Up @@ -52,31 +53,31 @@ func newDevice(
}

if device.Size, err = getSize(name); err != nil {
return nil, err
return nil, fmt.Errorf("unable to get size; device=%v; err=%w", name, err)
}

device.Hidden = getHidden(name)

if device.Removable, err = getRemovable(name); err != nil {
return nil, err
return nil, fmt.Errorf("unable to get removable flag; device=%v; err=%w", name, err)
}

if device.ReadOnly, err = getReadOnly(name); err != nil {
return nil, err
return nil, fmt.Errorf("unable to get read-only flag; device=%v; err=%w", name, err)
}

if device.Holders, err = getHolders(name); err != nil {
return nil, err
return nil, fmt.Errorf("unable to get holders; device=%v; %w", name, err)
}

partitions, err := getPartitions(name)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get partition info; device=%v; err=%w", name, err)
}
device.Partitioned = len(partitions) != 0

if device.DMName, err = getDMName(name); err != nil {
return nil, err
return nil, fmt.Errorf("unable to get DM name; device=%v; err=%w", name, err)
}

return device, nil
Expand All @@ -85,22 +86,22 @@ func newDevice(
func probe() (devices []Device, err error) {
deviceMap, udevDataMap, err := probeFromUdev()
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to probe from udev; %w", err)
}

_, deviceMountMap, majorMinorMap, _, err := sys.GetMounts(true)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get mounts; %w", err)
}

cdroms, err := getCDROMs()
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get CDROM information; %w", err)
}

swaps, err := getSwaps()
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get swap information; %w", err)
}

for name, udevData := range udevDataMap {
Expand All @@ -117,17 +118,17 @@ func probe() (devices []Device, err error) {
func probeDevices(majorMinor ...string) (devices []Device, err error) {
_, deviceMountMap, majorMinorMap, _, err := sys.GetMounts(true)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get mounts; %w", err)
}

cdroms, err := getCDROMs()
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get CDROM information; %w", err)
}

swaps, err := getSwaps()
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get swap information; %w", err)
}

for i := range majorMinor {
Expand All @@ -137,12 +138,12 @@ func probeDevices(majorMinor ...string) (devices []Device, err error) {
continue
}

return nil, err
return nil, fmt.Errorf("unable to read udev data; majorminor=%v; err=%w", majorMinor[i], err)
}

name, err := getDeviceName(majorMinor[i])
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to get device name; majorminor=%v; err=%w", majorMinor[i], err)
}

device, err := newDevice(deviceMountMap, majorMinorMap, cdroms, swaps, name, majorMinor[i], udevData)
Expand Down
2 changes: 1 addition & 1 deletion pkg/device/sysfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func getReadOnly(name string) (bool, error) {

func getSize(name string) (uint64, error) {
s, err := readFirstLine("/sys/class/block/" + name + "/size")
if err != nil {
if err != nil || s == "" {
return 0, err
}
ui64, err := strconv.ParseUint(s, 10, 64)
Expand Down

0 comments on commit a0b8eed

Please sign in to comment.