Skip to content

Commit

Permalink
disk: tweak ensure{Btrfs,LVM}() to be more linear
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 committed Jan 7, 2025
1 parent 5b6aa48 commit 7faaf55
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,35 +731,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 = LVMPartitionDOSID
}
// 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 = LVMPartitionDOSID
}

return nil
Expand Down Expand Up @@ -789,43 +789,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
Expand Down

0 comments on commit 7faaf55

Please sign in to comment.