disk: boot partition finder as partitiontable method

Moved the function that searches for the boot partition index to the
PartitionTable struct as a method. The method returns -1 if not found
and it's now the responsibility of the caller to handle the case where
it is not found.
This commit is contained in:
Achilleas Koutsou 2021-07-13 16:14:25 +02:00 committed by Ondřej Budai
parent bc1b45f7fa
commit 5909ca202d
2 changed files with 25 additions and 21 deletions

View file

@ -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

View file

@ -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 {