From 9e418ac5ad769eb10cc2de7723af2f5ce374a17e Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Fri, 3 Dec 2021 00:31:57 -0800 Subject: [PATCH 1/3] consider partitionless disks to have one partition Signed-off-by: Tai Groot --- pkg/block/block_linux.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/block/block_linux.go b/pkg/block/block_linux.go index ce164132..431d67d7 100644 --- a/pkg/block/block_linux.go +++ b/pkg/block/block_linux.go @@ -226,6 +226,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(ctx, disk) + p := &Partition{ + Name: disk, + SizeBytes: size, + MountPoint: mp, + Type: pt, + IsReadOnly: ro, + UUID: du, + } + out = append(out, p) + + } return out } From 0e160d1030dac9916b32af3607acc9f1466499c3 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Fri, 3 Dec 2021 10:29:59 -0800 Subject: [PATCH 2/3] update docstring to reflect partitionless disk changes Signed-off-by: Tai Groot --- pkg/block/block_linux.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/block/block_linux.go b/pkg/block/block_linux.go index 431d67d7..f43ccfd7 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) From e52c6edec0eb64c50881c3fa81b2dca4631f9155 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 19 Mar 2022 16:30:07 -0700 Subject: [PATCH 3/3] Update unexported api calls for new functionality Signed-off-by: Tai Groot --- pkg/block/block_linux.go | 2 +- pkg/block/block_linux_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/block/block_linux.go b/pkg/block/block_linux.go index f43ccfd7..8be32595 100644 --- a/pkg/block/block_linux.go +++ b/pkg/block/block_linux.go @@ -233,7 +233,7 @@ func diskPartitions(ctx *context.Context, paths *linuxpath.Paths, disk string) [ if len(out) == 0 { size := partitionSizeBytes(paths, disk, "") mp, pt, ro := partitionInfo(paths, disk) - du := diskPartUUID(ctx, disk) + du := diskPartUUID(paths, disk, "") p := &Partition{ Name: disk, SizeBytes: size, 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.")