Skip to content

Commit

Permalink
Detect iSCSI devices
Browse files Browse the repository at this point in the history
  • Loading branch information
avishayt committed Mar 15, 2022
1 parent fb0598c commit 5fa13c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
DRIVE_TYPE_FDD // Floppy disk drive
DRIVE_TYPE_ODD // Optical disk drive
DRIVE_TYPE_SSD // Solid-state drive
DRIVE_TYPE_ISCSI // iSCSI drive
)

var (
Expand All @@ -38,6 +39,7 @@ var (
DRIVE_TYPE_FDD: "FDD",
DRIVE_TYPE_ODD: "ODD",
DRIVE_TYPE_SSD: "SSD",
DRIVE_TYPE_ISCSI: "ISCSI",
}

// NOTE(fromani): the keys are all lowercase and do not match
Expand All @@ -51,6 +53,7 @@ var (
"fdd": DRIVE_TYPE_FDD,
"odd": DRIVE_TYPE_ODD,
"ssd": DRIVE_TYPE_SSD,
"iscsi": DRIVE_TYPE_ISCSI,
}
)

Expand Down
3 changes: 3 additions & 0 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
size := diskSizeBytes(paths, dname)
pbs := diskPhysicalBlockSizeBytes(paths, dname)
busPath := diskBusPath(paths, dname)
if strings.Contains(busPath, "-iscsi-") {
driveType = DRIVE_TYPE_ISCSI
}
node := diskNUMANodeID(paths, dname)
vendor := diskVendor(paths, dname)
model := diskModel(paths, dname)
Expand Down
38 changes: 38 additions & 0 deletions pkg/block/block_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
package block

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/jaypipes/ghw/pkg/context"
"github.com/jaypipes/ghw/pkg/linuxpath"
)

func TestParseMountEntry(t *testing.T) {
Expand Down Expand Up @@ -179,3 +184,36 @@ func TestDiskTypes(t *testing.T) {
}
}
}

func TestISCSI(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}

baseDir, _ := ioutil.TempDir("", "test")
ctx := context.New()
ctx.Chroot = baseDir
paths := linuxpath.New(ctx)

_ = os.MkdirAll(paths.SysBlock, 0755)
_ = os.MkdirAll(paths.RunUdevData, 0755)

// Emulate an iSCSI device
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "size"), []byte("500118192\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "dev"), []byte("259:0\n"), 0644)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "queue"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "queue", "rotational"), []byte("0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "queue", "physical_block_size"), []byte("512\n"), 0644)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "device"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "device", "vendor"), []byte("LIO-ORG\n"), 0644)
udevData := "E:ID_MODEL=disk0\nE:ID_SERIAL=6001405961d8b6f55cf48beb0de296b2\n" +
"E:ID_PATH=ip-192.168.130.10:3260-iscsi-iqn.2022-01.com.redhat.foo:disk0-lun-0\n" +
"E:ID_WWN=0x6001405961d8b6f55cf48beb0de296b2\n"
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(udevData), 0644)

diskInventory := disks(ctx, paths)
if diskInventory[0].DriveType != DRIVE_TYPE_ISCSI {
t.Fatalf("Got drive type %s, but expected ISCSI", diskInventory[0].DriveType)
}
}

0 comments on commit 5fa13c6

Please sign in to comment.