-
Notifications
You must be signed in to change notification settings - Fork 10
fix: Handle JBOD/passthrough disks behind RAID controllers gracefully #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be7c4c8
fix: Handle JBOD/passthrough disks behind RAID controllers gracefully
andaaron d342650
feat: Classify JBOD disks using per-controller identity correlation
andaaron 4b46bbc
refactor: Consolidate Linux sysfs helpers into linux/sysfs subpackage
andaaron 7952c56
megaraid: classify JBOD drives by serial number
andaaron 1b598cf
disko: add internal-only disko.Unknown for RAID driver error paths
andaaron 79132f0
disko: implement review feedback
andaaron c5f63db
fix: H:C:T:L is actually Host:Channel:Target:LUN
andaaron 40025f6
disko: tighten ReadSCSITarget contract and handle NVMe JBOD medium
andaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package sysfs | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ReadSCSITarget returns Target from /sys/block/<kname>/device -> | ||
| // Host:Channel:Target:LUN (H:C:T:L). For SCSI-attached JBOD/passthrough disks | ||
| // behind a RAID HBA, Target matches the controller-reported drive ID | ||
| // (megaraid Drive.DID, mpi3mr PhysicalDrive.PID), which lets callers | ||
| // correlate a Linux block device with an entry in the controller's PD list. | ||
| // | ||
| // The "device" entry under a SCSI block device is a symlink into | ||
| // /sys/class/scsi_device/, e.g.: | ||
| // | ||
| // % ls -al /sys/block/sda/device | ||
| // lrwxrwxrwx 1 root root 0 Jan 31 16:11 /sys/block/sda/device -> ../../../2:0:0:0 | ||
| // | ||
| // Callers must only invoke ReadSCSITarget for devices that udev reports as | ||
| // SCSI (ID_SCSI=1); virtio-blk, NVMe, ATA/SATA, etc. do not expose this | ||
| // symlink and should be filtered upstream. ok=false with a nil error is | ||
| // reserved for benign cases (empty kname, missing "device" symlink); a | ||
| // malformed Host:Channel:Target:LUN link target is reported as an | ||
| // error. sysRoot is injectable for tests; production passes "/sys". | ||
| func ReadSCSITarget(sysRoot, kname string) (target int, ok bool, err error) { | ||
| if kname == "" { | ||
| return 0, false, nil | ||
| } | ||
|
|
||
| link := filepath.Join(sysRoot, "block", kname, "device") | ||
|
|
||
| dest, err := os.Readlink(link) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return 0, false, nil | ||
| } | ||
| return 0, false, fmt.Errorf("readlink %q: %w", link, err) | ||
| } | ||
|
|
||
| // Parse out the SCSI device id from the symlink. e.g. | ||
| // ../../../0:3:110:0 -> 0:3:110:0 | ||
| // matching an entry under /sys/class/scsi_device/. | ||
| scsiDeviceID := filepath.Base(dest) | ||
| hctlFields := strings.Split(scsiDeviceID, ":") | ||
|
|
||
| const requiredHCTLFields = 4 | ||
| if len(hctlFields) != requiredHCTLFields { | ||
| return 0, false, fmt.Errorf( | ||
| "invalid SCSI Host:Channel:Target:LUN value %q from %q: expected %d fields, got %d", | ||
| scsiDeviceID, link, requiredHCTLFields, len(hctlFields)) | ||
| } | ||
|
|
||
| t, cerr := strconv.Atoi(hctlFields[2]) | ||
| if cerr != nil { | ||
| return 0, false, fmt.Errorf("parse SCSI target from %q: %w", scsiDeviceID, cerr) | ||
| } | ||
|
|
||
| return t, true, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package sysfs | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // makeBlockDeviceSymlink wires up a fake sysfs entry at | ||
| // <root>/block/<kname>/device pointing at | ||
| // ../../scsi_device/<host:channel:target:lun>. | ||
| func makeBlockDeviceSymlink(t *testing.T, root, kname, hctl string) { | ||
| t.Helper() | ||
|
|
||
| blockDir := filepath.Join(root, "block", kname) | ||
| require.NoError(t, os.MkdirAll(blockDir, 0o755), "mkdir %q", blockDir) | ||
|
|
||
| scsiDir := filepath.Join(root, "scsi_device", hctl) | ||
| require.NoError(t, os.MkdirAll(scsiDir, 0o755), "mkdir %q", scsiDir) | ||
|
|
||
| link := filepath.Join(blockDir, "device") | ||
| target := filepath.Join("..", "..", "scsi_device", hctl) | ||
| require.NoError(t, os.Symlink(target, link), "symlink") | ||
| } | ||
|
|
||
| func TestReadSCSITargetJBOD(t *testing.T) { | ||
| root := t.TempDir() | ||
| makeBlockDeviceSymlink(t, root, "sdb", "0:2:3:0") | ||
|
|
||
| target, ok, err := ReadSCSITarget(root, "sdb") | ||
| require.NoError(t, err) | ||
| require.True(t, ok, "expected ok=true for a SCSI-backed block device") | ||
| assert.Equal(t, 3, target, "target") | ||
| } | ||
|
|
||
| // An NVMe/virtio-style block device has no Host:Channel:Target:LUN | ||
| // "device" symlink. | ||
| // ReadSCSITarget must report ok=false (not an error) so the caller can | ||
| // fall through to generic udev detection. | ||
| func TestReadSCSITargetNoDevice(t *testing.T) { | ||
|
andaaron marked this conversation as resolved.
|
||
| root := t.TempDir() | ||
| require.NoError(t, os.MkdirAll(filepath.Join(root, "block", "nvme0n1"), 0o755)) | ||
|
|
||
| _, ok, err := ReadSCSITarget(root, "nvme0n1") | ||
| require.NoError(t, err) | ||
| assert.False(t, ok, "expected ok=false when device symlink is absent") | ||
| } | ||
|
|
||
| // A "device" symlink whose last segment isn't Host:Channel:Target:LUN | ||
| // (e.g. points at a PCI node) is malformed for a SCSI block device and | ||
| // should be reported as an error so the caller can log/diagnose. Callers | ||
| // are expected to filter non-SCSI devices upstream via udev (ID_SCSI=1). | ||
| func TestReadSCSITargetNonHCTL(t *testing.T) { | ||
| root := t.TempDir() | ||
| blockDir := filepath.Join(root, "block", "vda") | ||
| require.NoError(t, os.MkdirAll(blockDir, 0o755)) | ||
| other := filepath.Join(root, "devices", "virtio0") | ||
| require.NoError(t, os.MkdirAll(other, 0o755)) | ||
| require.NoError(t, os.Symlink(filepath.Join("..", "..", "devices", "virtio0"), | ||
| filepath.Join(blockDir, "device"))) | ||
|
|
||
| _, ok, err := ReadSCSITarget(root, "vda") | ||
| require.Error(t, err, "expected error for malformed Host:Channel:Target:LUN link target") | ||
| assert.False(t, ok, "expected ok=false when link target is not Host:Channel:Target:LUN") | ||
| } | ||
|
|
||
| func TestReadSCSITargetBadTarget(t *testing.T) { | ||
| root := t.TempDir() | ||
| makeBlockDeviceSymlink(t, root, "sdc", "0:2:notanum:0") | ||
|
|
||
| _, ok, err := ReadSCSITarget(root, "sdc") | ||
| require.Error(t, err, "expected parse error") | ||
| assert.False(t, ok, "expected ok=false on parse error") | ||
| } | ||
|
|
||
| func TestReadSCSITargetEmptyKname(t *testing.T) { | ||
| _, ok, err := ReadSCSITarget("/sys", "") | ||
| require.NoError(t, err) | ||
| assert.False(t, ok, "expected ok=false for empty kname") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Package sysfs contains Linux-specific helpers for inspecting the sysfs | ||
| // hierarchy. The helpers are driver-agnostic so they can be shared by the | ||
| // top-level linux package and by individual RAID driver packages without | ||
| // creating an import cycle back into linux. | ||
| package sysfs | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // IsSysPathRAID checks whether syspath (udevadm DEVPATH) belongs to a RAID | ||
| // controller whose PCI driver is registered at driverSysPath. | ||
| // | ||
| // syspath will look something like | ||
| // /devices/pci0000:3a/0000:3a:02.0/0000:3c:00.0/host0/target0:2:2/0:2:2:0/block/sdc | ||
| func IsSysPathRAID(syspath string, driverSysPath string) bool { | ||
| if !strings.HasPrefix(syspath, "/sys") { | ||
| syspath = "/sys" + syspath | ||
| } | ||
|
|
||
| if !strings.Contains(syspath, "/host") { | ||
| return false | ||
| } | ||
|
|
||
| fp, err := filepath.EvalSymlinks(syspath) | ||
| if err != nil { | ||
| fmt.Printf("seriously? %s\n", err) | ||
| return false | ||
| } | ||
|
|
||
| for _, path := range GetSysPaths(driverSysPath) { | ||
| if strings.HasPrefix(fp, path) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // GetSysPaths returns the resolved PCI device paths for a RAID driver. | ||
| func GetSysPaths(driverSysPath string) []string { | ||
| paths := []string{} | ||
| // a raid driver has directory entries for each of the scsi hosts on that controller. | ||
| // $cd /sys/bus/pci/drivers/<driver name> | ||
| // $ for d in *; do [ -d "$d" ] || continue; echo "$d -> $( cd "$d" && pwd -P )"; done | ||
| // 0000:3c:00.0 -> /sys/devices/pci0000:3a/0000:3a:02.0/0000:3c:00.0 | ||
| // module -> /sys/module/<driver module name> | ||
|
|
||
| // We take a hack path and consider anything with a ":" in that dir as a host path. | ||
| matches, err := filepath.Glob(driverSysPath + "/*:*") | ||
|
|
||
| if err != nil { | ||
| fmt.Printf("errors: %s\n", err) | ||
| return paths | ||
| } | ||
|
|
||
| for _, p := range matches { | ||
| if fp, err := filepath.EvalSymlinks(p); err == nil { | ||
| paths = append(paths, fp) | ||
| } | ||
| } | ||
|
|
||
| return paths | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we'd call this if kname is empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't. I made the change.