osbuild2: properly calculate grub2 core location

The previous code assumed that the first partition is the location to
write the grub2 core image, implying it needs to always be a PReP or
BIOS-Boot partition. This is not an assumption we can easily make;
instead we now try to detect the correct partition based on its type.
This commit is contained in:
Christian Kellner 2022-02-25 19:29:53 +01:00 committed by Achilleas Koutsou
parent ca61baf03b
commit a26103312a

View file

@ -98,12 +98,16 @@ func (options Grub2InstStageOptions) MarshalJSON() ([]byte, error) {
func NewGrub2InstStageOption(filename string, pt *disk.PartitionTable, platform string) *Grub2InstStageOptions {
bootIdx := -1
rootIdx := -1
coreIdx := -1 // where to put grub2 core image
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.IsBIOSBoot() || partition.IsPReP() {
coreIdx = idx
}
if partition.Payload == nil {
continue
}
@ -126,6 +130,11 @@ func NewGrub2InstStageOption(filename string, pt *disk.PartitionTable, platform
bootIdx = rootIdx
}
if coreIdx == -1 {
panic("failed to find partition for the grub2 core in the grub2.inst stage")
}
coreLocation := pt.BytesToSectors(pt.Partitions[coreIdx].Start)
bootPart := pt.Partitions[bootIdx]
bootPayload := bootPart.Payload.(disk.Mountable) // this is guaranteed by the search loop above
prefixPath := "/boot/grub2"
@ -148,7 +157,7 @@ func NewGrub2InstStageOption(filename string, pt *disk.PartitionTable, platform
return &Grub2InstStageOptions{
Filename: filename,
Platform: platform,
Location: pt.BytesToSectors(pt.Partitions[0].Start),
Location: coreLocation,
Core: core,
Prefix: prefix,
}