diff --git a/pkg/block/block_linux.go b/pkg/block/block_linux.go index ce164132..8be32595 100644 --- a/pkg/block/block_linux.go +++ b/pkg/block/block_linux.go @@ -192,7 +192,9 @@ func diskWWN(paths *linuxpath.Paths, disk string) string { // diskPartitions takes the name of a disk (note: *not* the path of the disk, // but just the name. In other words, "sda", not "/dev/sda" and "nvme0n1" not // "/dev/nvme0n1") and returns a slice of pointers to Partition structs -// representing the partitions in that disk +// representing the partitions in that disk. If a disk has no partitions, +// it returns information on the disk instead, but the Partition UUID is set +// to the empty string (since there isn't one.) func diskPartitions(ctx *context.Context, paths *linuxpath.Paths, disk string) []*Partition { out := make([]*Partition, 0) path := filepath.Join(paths.SysBlock, disk) @@ -226,6 +228,23 @@ func diskPartitions(ctx *context.Context, paths *linuxpath.Paths, disk string) [ } out = append(out, p) } + // this is a disk with no partitions on it. + // Consider the filesystem on the disk to be a partition. + if len(out) == 0 { + size := partitionSizeBytes(paths, disk, "") + mp, pt, ro := partitionInfo(paths, disk) + du := diskPartUUID(paths, disk, "") + p := &Partition{ + Name: disk, + SizeBytes: size, + MountPoint: mp, + Type: pt, + IsReadOnly: ro, + UUID: du, + } + out = append(out, p) + + } return out } diff --git a/pkg/block/block_linux_test.go b/pkg/block/block_linux_test.go index 98290e45..14299722 100644 --- a/pkg/block/block_linux_test.go +++ b/pkg/block/block_linux_test.go @@ -195,6 +195,28 @@ func TestDiskTypes(t *testing.T) { } } +func TestDiskPartitionless(t *testing.T) { + if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok { + t.Skip("Skipping block tests.") + } + baseDir, _ := ioutil.TempDir("", "test") + defer os.RemoveAll(baseDir) + ctx := context.New() + ctx.Chroot = baseDir + paths := linuxpath.New(ctx) + + _ = os.MkdirAll(paths.SysBlock, 0755) + _ = os.MkdirAll(paths.RunUdevData, 0755) + + // Emulate a disk with no partitions + _ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755) + _ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "dev"), []byte("259:0\n"), 0644) + partitions := diskPartitions(ctx, paths, "sda") + if len(partitions) == 0 { + t.Fatalf("Got no partitions but expected sda") + } +} + func TestDiskPartLabel(t *testing.T) { if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok { t.Skip("Skipping block tests.")