Skip to content

Commit

Permalink
Fix nvme path filtering logic for udevadm trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
pwschuurman committed Mar 7, 2024
1 parent d6b1856 commit a78bcfb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
20 changes: 18 additions & 2 deletions pkg/deviceutils/device-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
diskSDPattern = "/dev/sd*"
diskNvmePath = "/dev/nvme"
diskNvmePattern = "^/dev/nvme[0-9]+n[0-9]+$"
devNvmePattern = "/dev/nvme*"
// How many times to retry for a consistent read of /proc/mounts.
maxListTries = 3
// Number of fields per line in /proc/mounts as per the fstab man page.
Expand Down Expand Up @@ -69,6 +70,8 @@ var (
scsiRegex = regexp.MustCompile(scsiPattern)
// regex to parse google_nvme_id output and extract the serial
nvmeRegex = regexp.MustCompile(nvmePattern)
// regex to filter only disk drive paths from filepath.Glob output of devNvmePattern
diskNvmeRegex = regexp.MustCompile(diskNvmePattern)
)

// DeviceUtils are a collection of methods that act on the devices attached
Expand Down Expand Up @@ -306,15 +309,28 @@ func getDevFsSerial(devFsPath string) (string, error) {
}
}

func filterAvailableNvmeDevFsPaths(devNvmePaths []string) []string {
// Devices under /dev/nvme need to be filtered for disk drive paths only.
diskNvmePaths := []string{}
for _, devNvmePath := range devNvmePaths {
if diskNvmeRegex.MatchString(devNvmePath) {
diskNvmePaths = append(diskNvmePaths, devNvmePath)
}
}
return diskNvmePaths
}

func findAvailableDevFsPaths() ([]string, error) {
diskSDPaths, err := filepath.Glob(diskSDPattern)
if err != nil {
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskSDPattern, err)
}
diskNvmePaths, err := filepath.Glob(diskNvmePattern)
devNvmePaths, err := filepath.Glob(devNvmePattern)
if err != nil {
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskNvmePattern, err)
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", devNvmePattern, err)
}
// Devices under /dev/nvme need to be filtered for disk drive paths only.
diskNvmePaths := filterAvailableNvmeDevFsPaths(devNvmePaths)
return append(diskSDPaths, diskNvmePaths...), nil
}

Expand Down
17 changes: 11 additions & 6 deletions pkg/deviceutils/device-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package deviceutils

import (
"fmt"
"regexp"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -64,8 +64,6 @@ func less(a, b fmt.Stringer) bool {
// and may prevent our tests from running on all platforms. See the following test for an example:
// https://github.com/golang/go/blob/d33548d178016122726342911f8e15016a691472/src/syscall/exec_linux_test.go#L250
func TestDiskNvmePattern(t *testing.T) {
nvmeDiskRegex := regexp.MustCompile(diskNvmePattern)

testCases := []struct {
paths []string
wantPaths []string
Expand Down Expand Up @@ -97,12 +95,19 @@ func TestDiskNvmePattern(t *testing.T) {
}

for _, tc := range testCases {
gotPaths := []string{}
devPaths := []string{}

for _, path := range tc.paths {
if nvmeDiskRegex.MatchString(path) {
gotPaths = append(gotPaths, path)
ok, err := filepath.Match(devNvmePattern, path)
if err != nil {
t.Errorf("Error encountered matching path: %v", path)
}

if ok {
devPaths = append(devPaths, path)
}
}
gotPaths := filterAvailableNvmeDevFsPaths(devPaths)
if diff := cmp.Diff(gotPaths, tc.wantPaths, cmpopts.SortSlices(less)); diff != "" {
t.Errorf("Unexpected NVMe device paths (-got, +want):\n%s", diff)
}
Expand Down

0 comments on commit a78bcfb

Please sign in to comment.