Skip to content

Commit

Permalink
disk: set the BIOS boot and ESP partition type correctly for dos
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
achilleas-k authored and mvo5 committed Nov 7, 2024
1 parent 796a482 commit 9bae4ae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 43 additions & 10 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -1109,5 +1142,5 @@ func mkESP(size uint64) Partition {
FSTabFreq: 0,
FSTabPassNo: 2,
},
}
}, nil
}

0 comments on commit 9bae4ae

Please sign in to comment.