Skip to content

Commit

Permalink
ignore devices with device metadata missing
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Jun 11, 2023
1 parent c0ae169 commit bf9e1d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
12 changes: 12 additions & 0 deletions pkg/device/probe_linux.go
Expand Up @@ -106,6 +106,10 @@ func probe() (devices []Device, err error) {
for name, udevData := range udevDataMap {
device, err := newDevice(deviceMountMap, majorMinorMap, cdroms, swaps, name, deviceMap[name], udevData)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}

return nil, err
}
devices = append(devices, *device)
Expand Down Expand Up @@ -142,11 +146,19 @@ func probeDevices(majorMinor ...string) (devices []Device, err error) {

name, err := getDeviceName(majorMinor[i])
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}

return nil, err
}

device, err := newDevice(deviceMountMap, majorMinorMap, cdroms, swaps, name, majorMinor[i], udevData)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}

return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/device/sysfs_linux.go
Expand Up @@ -57,22 +57,22 @@ func getHidden(name string) bool {
// errors ignored since real devices do not have <sys>/hidden
// borrow idea from 'lsblk'
// https://github.com/util-linux/util-linux/commit/c8487d854ba5cf5bfcae78d8e5af5587e7622351
v, _ := readFirstLine("/sys/class/block/"+name+"/hidden", false)
v, _ := readFirstLine("/sys/class/block/" + name + "/hidden")
return v == "1"
}

func getRemovable(name string) (bool, error) {
s, err := readFirstLine("/sys/class/block/"+name+"/removable", false)
s, err := readFirstLine("/sys/class/block/" + name + "/removable")
return s != "" && s != "0", err
}

func getReadOnly(name string) (bool, error) {
s, err := readFirstLine("/sys/class/block/"+name+"/ro", false)
s, err := readFirstLine("/sys/class/block/" + name + "/ro")
return s != "" && s != "0", err
}

func getSize(name string) (uint64, error) {
s, err := readFirstLine("/sys/class/block/"+name+"/size", true)
s, err := readFirstLine("/sys/class/block/" + name + "/size")
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -104,5 +104,5 @@ func getHolders(name string) ([]string, error) {
}

func getDMName(name string) (string, error) {
return readFirstLine("/sys/class/block/"+name+"/dm/name", false)
return readFirstLine("/sys/class/block/" + name + "/dm/name")
}
5 changes: 1 addition & 4 deletions pkg/device/utils.go
Expand Up @@ -37,11 +37,8 @@ func readdirnames(dirname string, errorIfNotExist bool) ([]string, error) {
return dir.Readdirnames(-1)
}

func readFirstLine(filename string, errorIfNotExist bool) (string, error) {
func readFirstLine(filename string) (string, error) {
getError := func(err error) error {
if errorIfNotExist {
return err
}
switch {
case errors.Is(err, os.ErrNotExist), errors.Is(err, os.ErrInvalid):
return nil
Expand Down

0 comments on commit bf9e1d9

Please sign in to comment.