diff --git a/internal/disk/disk.go b/internal/disk/disk.go index 320580d02..a088b0024 100644 --- a/internal/disk/disk.go +++ b/internal/disk/disk.go @@ -120,6 +120,26 @@ func (pt PartitionTable) RootPartition() *Partition { return nil } +// Returns the index of the boot partition: the partition whose filesystem has +// /boot as a mountpoint. If there is no explicit boot partition, the root +// partition is returned. +// If neither boot nor root partitions are found, returns -1. +func (pt PartitionTable) BootPartitionIndex() int { + // find partition with '/boot' mountpoint and fallback to '/' + rootIdx := -1 + for idx, part := range pt.Partitions { + if part.Filesystem == nil { + continue + } + if part.Filesystem.Mountpoint == "/boot" { + return idx + } else if part.Filesystem.Mountpoint == "/" { + rootIdx = idx + } + } + return rootIdx +} + // Converts Partition to osbuild.QEMUPartition that encodes the same partition. func (p Partition) QEMUPartition() osbuild.QEMUPartition { var fs *osbuild.QEMUFilesystem diff --git a/internal/distro/rhel85/stage_options.go b/internal/distro/rhel85/stage_options.go index aec6819fb..39c8970b0 100644 --- a/internal/distro/rhel85/stage_options.go +++ b/internal/distro/rhel85/stage_options.go @@ -442,7 +442,10 @@ func copyFSTreeOptions(inputName, inputPipeline string, pt *disk.PartitionTable, } func grub2InstStageOptions(filename string, pt *disk.PartitionTable, platform string) *osbuild.Grub2InstStageOptions { - bootPartIndex := findBootPartition(pt) + bootPartIndex := pt.BootPartitionIndex() + if bootPartIndex == -1 { + panic("failed to find boot or root partition for grub2.inst stage") + } core := osbuild.CoreMkImage{ Type: "mkimage", PartLabel: pt.Type, @@ -452,7 +455,7 @@ func grub2InstStageOptions(filename string, pt *disk.PartitionTable, platform st prefix := osbuild.PrefixPartition{ Type: "partition", PartLabel: pt.Type, - Number: bootPartIndex, + Number: uint(bootPartIndex), Path: "/boot/grub2", } @@ -465,25 +468,6 @@ func grub2InstStageOptions(filename string, pt *disk.PartitionTable, platform st } } -func findBootPartition(pt *disk.PartitionTable) uint { - // find partition with '/boot' mountpoint and fallback to '/' - rootIdx := -1 - for idx, part := range pt.Partitions { - if part.Filesystem == nil { - continue - } - if part.Filesystem.Mountpoint == "/boot" { - return uint(idx) - } else if part.Filesystem.Mountpoint == "/" { - rootIdx = idx - } - } - if rootIdx == -1 { - panic("failed to find boot or root partition for grub2.inst stage") - } - return uint(rootIdx) -} - func qemuStageOptions(filename, format, compat string) *osbuild.QEMUStageOptions { var options osbuild.QEMUFormatOptions switch format {