From 9bae4aed460bfcc121a46eb6c38f9708e08c7e72 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Thu, 7 Nov 2024 15:11:47 +0100 Subject: [PATCH] disk: set the BIOS boot and ESP partition type correctly for dos Define two new const strings: DosBIOSBootID and DosESPID, which are used for the BIOS boot and the EFI system partitions respectively on dos/mbr partition tables. In the new helper functions, use the partition table type to determine which ID to use. This means that the functions will return an error when the partition table type is not set (or is set to an unknown value). --- pkg/disk/disk.go | 6 +++++ pkg/disk/partition_table.go | 53 ++++++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/pkg/disk/disk.go b/pkg/disk/disk.go index 5c26d18aac..2484295ba2 100644 --- a/pkg/disk/disk.go +++ b/pkg/disk/disk.go @@ -63,6 +63,12 @@ const ( // Partition type ID for any native Linux filesystem on dos DosLinuxTypeID = "83" + + // Partition type ID for BIOS boot partition on dos + DosBIOSBootID = "ef02" + + // Partition type ID for ESP on dos + DosESPID = "ef00" ) // FSType is the filesystem type enum. diff --git a/pkg/disk/partition_table.go b/pkg/disk/partition_table.go index 4edb202f47..4ea45c3ef3 100644 --- a/pkg/disk/partition_table.go +++ b/pkg/disk/partition_table.go @@ -1068,16 +1068,31 @@ func AddPartitionsForBootMode(pt *PartitionTable, bootMode platform.BootMode) er switch bootMode { case platform.BOOT_LEGACY: // add BIOS boot partition - pt.Partitions = append(pt.Partitions, mkBIOSBoot()) + part, err := mkBIOSBoot(pt.Type) + if err != nil { + return err + } + pt.Partitions = append(pt.Partitions, part) return nil case platform.BOOT_UEFI: // add ESP - pt.Partitions = append(pt.Partitions, mkESP(200*datasizes.MiB)) + part, err := mkESP(200*datasizes.MiB, pt.Type) + if err != nil { + return err + } + pt.Partitions = append(pt.Partitions, part) return nil case platform.BOOT_HYBRID: // add both - pt.Partitions = append(pt.Partitions, mkBIOSBoot()) - pt.Partitions = append(pt.Partitions, mkESP(200*datasizes.MiB)) + bios, err := mkBIOSBoot(pt.Type) + if err != nil { + return err + } + esp, err := mkESP(200*datasizes.MiB, pt.Type) + if err != nil { + return err + } + pt.Partitions = append(pt.Partitions, bios, esp) return nil case platform.BOOT_NONE: return nil @@ -1086,19 +1101,37 @@ func AddPartitionsForBootMode(pt *PartitionTable, bootMode platform.BootMode) er } } -func mkBIOSBoot() Partition { +func mkBIOSBoot(ptType string) (Partition, error) { + var partType string + switch ptType { + case "dos": + partType = DosBIOSBootID + case "gpt": + partType = BIOSBootPartitionGUID + default: + return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table type: %s", ptType) + } return Partition{ Size: 1 * datasizes.MiB, Bootable: true, - Type: BIOSBootPartitionGUID, + Type: partType, UUID: BIOSBootPartitionUUID, - } + }, nil } -func mkESP(size uint64) Partition { +func mkESP(size uint64, ptType string) (Partition, error) { + var partType string + switch ptType { + case "dos": + partType = DosESPID + case "gpt": + partType = EFISystemPartitionGUID + default: + return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table type: %s", ptType) + } return Partition{ Size: size, - Type: EFISystemPartitionGUID, + Type: partType, UUID: EFISystemPartitionUUID, Payload: &Filesystem{ Type: "vfat", @@ -1109,5 +1142,5 @@ func mkESP(size uint64) Partition { FSTabFreq: 0, FSTabPassNo: 2, }, - } + }, nil }