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

disk: tweak ensure{Btrfs,LVM}() to be more linear #1073

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
108 changes: 54 additions & 54 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a package constant alongside the others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can push it here:

diff --git a/pkg/disk/disk.go b/pkg/disk/disk.go
index fdfd027cb..25da6d657 100644
--- a/pkg/disk/disk.go
+++ b/pkg/disk/disk.go
@@ -70,6 +70,9 @@ const (
 
        // Partition type ID for ESP on dos
        DosESPID = "ef00"
+
+       // Partition type ID for LVM partitions on dos
+       DosLvmID = "8e"
 )
 
 // pt type -> type -> ID mapping for convenience
diff --git a/pkg/disk/partition_table.go b/pkg/disk/partition_table.go
index d91df09bd..947631b37 100644
--- a/pkg/disk/partition_table.go
+++ b/pkg/disk/partition_table.go
@@ -733,7 +733,7 @@ func (pt *PartitionTable) ensureLVM() error {
        if pt.Type == PT_GPT {
                part.Type = LVMPartitionGUID
        } else {
-               part.Type = "8e"
+               part.Type = DosLvmID
        }
 
        return nil

but what is curious is that https://github.com/osbuild/images/blob/main/pkg/disk/disk.go#L82 uses a different DOS id for lvm it seems?

Copy link
Member

@achilleas-k achilleas-k Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 83 is still valid as it covers any generic Linux partition type but according to https://tldp.org/HOWTO/Partition-Mass-Storage-Definitions-Naming-HOWTO/x190.html you're right, 8e is specific for LVM, so let's add 8e and add it to the ID map.

}

return nil
Expand Down Expand Up @@ -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
Expand Down
Loading