Skip to content

Commit

Permalink
linux/udev: fix udevadm info parser to not be fatal
Browse files Browse the repository at this point in the history
The current parser returns an error when it encounters newer prefix
values in the output of udevadm info.  Anytime a system upgrades to
a newer release, there is a change new fields will be present preventing
disko from producing any output.

- Update and document prefixes defined in systemd v255
- Replace error with an warning output
- make sure we have two tokens when parsing udevadm info output

Signed-off-by: Ryan Harper <[email protected]>
  • Loading branch information
raharper committed Feb 16, 2024
1 parent 82804d0 commit b0f7b9a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
41 changes: 37 additions & 4 deletions linux/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,50 @@ func parseUdevInfo(out []byte, info *disko.UdevInfo) error {
}

toks = bytes.SplitN(line, []byte(": "), 2)
payload = string(toks[1])

if len(toks) != 2 {
log.Printf("parseUdevInfo: ignoring unparsable line %q\n", line)
continue

Check warning on line 53 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}

payload = string(toks[1])
switch toks[0][0] {

Check failure on line 57 in linux/util.go

View workflow job for this annotation

GitHub Actions / lint

switch statements should only be cuddled with variables switched (wsl)
case 'P':
// Device path in /sys/
info.SysPath = payload
case 'M':
// Device name in /sys/ (i.e. the last component of "P:")
continue
case 'R':

Check warning on line 64 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L64

Added line #L64 was not covered by tests
// Device number in /sys/ (i.e. the numeric suffix of the last component of "P:")
continue
case 'U':

Check warning on line 67 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L66-L67

Added lines #L66 - L67 were not covered by tests
// Kernel subsystem
continue
case 'T':

Check warning on line 70 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L69-L70

Added lines #L69 - L70 were not covered by tests
// Kernel device type with subsystem
continue
case 'D':

Check warning on line 73 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L72-L73

Added lines #L72 - L73 were not covered by tests
// Kernel device node major/minor
continue
case 'I':

Check warning on line 76 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L75-L76

Added lines #L75 - L76 were not covered by tests
// Network interface index
continue

Check warning on line 78 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L78

Added line #L78 was not covered by tests
case 'N':
// Kernel device node name
info.Name = payload
case 'L':
// Device node symlink priority
continue
case 'S':
// Device node symlink
info.Symlinks = append(info.Symlinks, strings.Split(payload, " ")...)
case 'Q':

Check warning on line 88 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L88

Added line #L88 was not covered by tests
// Block device sequence number (DISKSEQ)
continue
case 'V':

Check warning on line 91 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L90-L91

Added lines #L90 - L91 were not covered by tests
// Attached driver
continue

Check warning on line 93 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L93

Added line #L93 was not covered by tests
case 'E':
kv := strings.SplitN(payload, "=", 2)
// use of Unquote is to decode \x20, \x2f and friends.
Expand All @@ -67,10 +102,8 @@ func parseUdevInfo(out []byte, info *disko.UdevInfo) error {
}

info.Properties[kv[0]] = strings.TrimSpace(s)
case 'L':
// a 'devlink priority'. skip for now.
default:
return fmt.Errorf("error parsing line: (%s)", line)
log.Printf("parseUdevInfo: ignoring unknown udevadm info prefix %q in %q\n", toks[0][0], line)

Check warning on line 106 in linux/util.go

View check run for this annotation

Codecov / codecov/patch

linux/util.go#L106

Added line #L106 was not covered by tests
}
}

Expand Down
2 changes: 2 additions & 0 deletions linux/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func TestParseUdevInfo(t *testing.T) {
data := []byte(`P: /devices/virtual/block/dm-0
N: dm-0
M: dm-0
S: disk/by-id/dm-name-nvme0n1p6_crypt
S: disk/by-id/dm-uuid-CRYPT-LUKS1-b174c64e7a714359a8b56b79fb66e92b-nvme0n1p6_crypt
S: disk/by-uuid/25df9069-80c7-46f4-a47c-305613c2cb6b
Expand Down Expand Up @@ -49,6 +50,7 @@ E: DEVNAME=/dev/dm-0
func TestParseUdevInfo2(t *testing.T) {
data := []byte(`P: /devices/pci0000:00/..../block/sda
N: sda
M: sda
S: disk/by-id/scsi-35000c500a0d8963f
S: disk/by-id/wwn-0x5000c500a0d8963f
S: disk/by-path/pci-0000:05:00.0-scsi-0:0:8:0
Expand Down

0 comments on commit b0f7b9a

Please sign in to comment.