Skip to content
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

Feat partitionless disks #308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/block/block_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down