From c96153d1014f2950e8fc0a4ac9a8766b7207c726 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 28 Nov 2024 09:52:52 +0100 Subject: [PATCH] disk: tweak ensure{Btrfs,LVM}() to be more linear Tiny drive-by commit to make `ensure{Btrfs,LVM}()` more linear to read. I.e. avoid nested if/else when returning and do error checks early. --- pkg/disk/partition_table.go | 108 ++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/pkg/disk/partition_table.go b/pkg/disk/partition_table.go index 978b34d7fb..d91df09bdf 100644 --- a/pkg/disk/partition_table.go +++ b/pkg/disk/partition_table.go @@ -705,35 +705,35 @@ func (pt *PartitionTable) ensureLVM() error { if _, ok := parent.(*LVMLogicalVolume); ok { return nil - } else if part, ok := parent.(*Partition); ok { - filesystem := part.Payload - - vg := &LVMVolumeGroup{ - Name: "rootvg", - Description: "created via lvm2 and osbuild", - } + } + part, ok := parent.(*Partition) + if !ok { + return fmt.Errorf("Unsupported parent for LVM") + } + filesystem := part.Payload - // create root logical volume on the new volume group with the same - // size and filesystem as the previous root partition - _, err := vg.CreateLogicalVolume("rootlv", part.Size, filesystem) - if err != nil { - panic(fmt.Sprintf("Could not create LV: %v", err)) - } + vg := &LVMVolumeGroup{ + Name: "rootvg", + Description: "created via lvm2 and osbuild", + } - // replace the top-level partition payload with the new volume group - part.Payload = vg + // create root logical volume on the new volume group with the same + // size and filesystem as the previous root partition + _, err := vg.CreateLogicalVolume("rootlv", part.Size, filesystem) + if err != nil { + panic(fmt.Sprintf("Could not create LV: %v", err)) + } - // reset the vg partition size - it will be grown later - part.Size = 0 + // replace the top-level partition payload with the new volume group + part.Payload = vg - if pt.Type == PT_GPT { - part.Type = LVMPartitionGUID - } else { - part.Type = "8e" - } + // reset the vg partition size - it will be grown later + part.Size = 0 + if pt.Type == PT_GPT { + part.Type = LVMPartitionGUID } else { - return fmt.Errorf("Unsupported parent for LVM") + part.Type = "8e" } return nil @@ -763,43 +763,43 @@ func (pt *PartitionTable) ensureBtrfs() error { if _, ok := parent.(*Btrfs); ok { return nil - } else if part, ok := parent.(*Partition); ok { - rootMountable, ok := rootPath[0].(Mountable) - if !ok { - return fmt.Errorf("root entity is not mountable: %T, this is a violation of entityPath() contract", rootPath[0]) - } + } + part, ok := parent.(*Partition) + if !ok { + return fmt.Errorf("unsupported parent for btrfs: %T", parent) + } + rootMountable, ok := rootPath[0].(Mountable) + if !ok { + return fmt.Errorf("root entity is not mountable: %T, this is a violation of entityPath() contract", rootPath[0]) + } - opts, err := rootMountable.GetFSTabOptions() - if err != nil { - return err - } + opts, err := rootMountable.GetFSTabOptions() + if err != nil { + return err + } - btrfs := &Btrfs{ - Label: "root", - Subvolumes: []BtrfsSubvolume{ - { - Name: "root", - Mountpoint: "/", - Compress: DefaultBtrfsCompression, - ReadOnly: opts.ReadOnly(), - Size: part.Size, - }, + btrfs := &Btrfs{ + Label: "root", + Subvolumes: []BtrfsSubvolume{ + { + Name: "root", + Mountpoint: "/", + Compress: DefaultBtrfsCompression, + ReadOnly: opts.ReadOnly(), + Size: part.Size, }, - } + }, + } - // replace the top-level partition payload with a new btrfs filesystem - part.Payload = btrfs + // replace the top-level partition payload with a new btrfs filesystem + part.Payload = btrfs - // reset the btrfs partition size - it will be grown later - part.Size = 0 + // reset the btrfs partition size - it will be grown later + part.Size = 0 - part.Type, err = getPartitionTypeIDfor(pt.Type, "data") - if err != nil { - return fmt.Errorf("error converting partition table to btrfs: %w", err) - } - - } else { - return fmt.Errorf("unsupported parent for btrfs: %T", parent) + part.Type, err = getPartitionTypeIDfor(pt.Type, "data") + if err != nil { + return fmt.Errorf("error converting partition table to btrfs: %w", err) } return nil