From d1b1e32b6e0e60278a4123a7e2b159a5a658f793 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Sat, 19 Feb 2022 13:29:27 +0100 Subject: [PATCH] disk: Partition.Payload is now an Entity Partition.Payload now supports every type of disk.Entity which enables creating PartitionTables with LUKS, LVM, and Btrfs. \o/ Co-Authored-By: Achilleas Koutsou --- internal/disk/partition.go | 21 ++++++++------------- internal/osbuild1/qemu_assembler.go | 3 ++- internal/osbuild2/grub2_inst_stage.go | 10 +++++++--- internal/osbuild2/grub2_stage.go | 19 ++----------------- internal/osbuild2/zipl_inst_stage.go | 8 ++++++-- 5 files changed, 25 insertions(+), 36 deletions(-) diff --git a/internal/disk/partition.go b/internal/disk/partition.go index 9a83d857a..91ca52ce2 100644 --- a/internal/disk/partition.go +++ b/internal/disk/partition.go @@ -15,7 +15,7 @@ type Partition struct { UUID string // If nil, the partition is raw; It doesn't contain a payload. - Payload *Filesystem + Payload Entity } func (p *Partition) IsContainer() bool { @@ -27,24 +27,19 @@ func (p *Partition) Clone() Entity { return nil } - ent := p.Payload.Clone() - var fs *Filesystem - if ent != nil { - fsEnt, cloneOk := ent.(*Filesystem) - if !cloneOk { - panic("Filesystem.Clone() returned an Entity that cannot be converted to *Filesystem; this is a programming error") - } - fs = fsEnt - } - - return &Partition{ + partition := &Partition{ Start: p.Start, Size: p.Size, Type: p.Type, Bootable: p.Bootable, UUID: p.UUID, - Payload: fs, } + + if p.Payload != nil { + partition.Payload = p.Payload.Clone() + } + + return partition } func (pt *Partition) GetItemCount() uint { diff --git a/internal/osbuild1/qemu_assembler.go b/internal/osbuild1/qemu_assembler.go index 076252540..90eeb01bf 100644 --- a/internal/osbuild1/qemu_assembler.go +++ b/internal/osbuild1/qemu_assembler.go @@ -71,7 +71,8 @@ func NewQEMUAssemblerOptions(pt *disk.PartitionTable) QEMUAssemblerOptions { func NewQEMUPartition(p *disk.Partition) QEMUPartition { var fs *QEMUFilesystem if p.Payload != nil { - f := NewQEMUFilesystem(p.Payload) + // NOTE: Partition Payload for QEMU assembler should always be a Filesystem + f := NewQEMUFilesystem(p.Payload.(*disk.Filesystem)) fs = &f } return QEMUPartition{ diff --git a/internal/osbuild2/grub2_inst_stage.go b/internal/osbuild2/grub2_inst_stage.go index c7a65dd38..354863a67 100644 --- a/internal/osbuild2/grub2_inst_stage.go +++ b/internal/osbuild2/grub2_inst_stage.go @@ -107,9 +107,13 @@ func NewGrub2InstStageOption(filename string, pt *disk.PartitionTable, platform if partition.Payload == nil { continue } - if partition.Payload.GetMountpoint() == "/boot" { + mnt, isMountable := partition.Payload.(disk.Mountable) + if !isMountable { + continue + } + if mnt.GetMountpoint() == "/boot" { bootIdx = idx - } else if partition.Payload.GetMountpoint() == "/" { + } else if mnt.GetMountpoint() == "/" { rootIdx = idx } } @@ -123,7 +127,7 @@ func NewGrub2InstStageOption(filename string, pt *disk.PartitionTable, platform } bootPart := pt.Partitions[bootIdx] - bootPayload := bootPart.Payload + bootPayload := bootPart.Payload.(disk.Mountable) // this is guaranteed by the search loop above prefixPath := "/boot/grub2" if bootPayload.GetMountpoint() == "/boot" { prefixPath = "/grub2" diff --git a/internal/osbuild2/grub2_stage.go b/internal/osbuild2/grub2_stage.go index df49a255a..c59d28fc8 100644 --- a/internal/osbuild2/grub2_stage.go +++ b/internal/osbuild2/grub2_stage.go @@ -49,23 +49,7 @@ func NewGrub2StageOptions(pt *disk.PartitionTable, vendor string, install bool) *GRUB2StageOptions { - var bootFs, rootFs disk.Mountable - for idx := range pt.Partitions { - // NOTE: we only support having /boot at the top level of the partition - // table (e.g., not in LUKS or LVM), so we don't need to descend into - // VolumeContainer types. If /boot is on the root partition, then the - // root partition needs to be at the top level. - partition := &pt.Partitions[idx] - if partition.Payload == nil { - continue - } - if partition.Payload.GetMountpoint() == "/boot" { - bootFs = partition.Payload - } else if partition.Payload.GetMountpoint() == "/" { - rootFs = partition.Payload - } - } - + rootFs := pt.FindMountable("/") if rootFs == nil { panic("root filesystem must be defined for grub2 stage, this is a programming error") } @@ -76,6 +60,7 @@ func NewGrub2StageOptions(pt *disk.PartitionTable, Legacy: legacy, } + bootFs := pt.FindMountable("/boot") if bootFs != nil { bootFsUUID := uuid.MustParse(bootFs.GetFSSpec().UUID) stageOptions.BootFilesystemUUID = &bootFsUUID diff --git a/internal/osbuild2/zipl_inst_stage.go b/internal/osbuild2/zipl_inst_stage.go index 40e68cec3..e897ed4a6 100644 --- a/internal/osbuild2/zipl_inst_stage.go +++ b/internal/osbuild2/zipl_inst_stage.go @@ -42,9 +42,13 @@ func NewZiplInstStageOptions(kernel string, pt *disk.PartitionTable) *ZiplInstSt if partition.Payload == nil { continue } - if partition.Payload.GetMountpoint() == "/boot" { + mnt, isMountable := partition.Payload.(disk.Mountable) + if !isMountable { + continue + } + if mnt.GetMountpoint() == "/boot" { bootIdx = idx - } else if partition.Payload.GetMountpoint() == "/" { + } else if mnt.GetMountpoint() == "/" { rootIdx = idx } }