Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disk: extract maybeAddBootPartition() helper #1062

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/disk/export_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package disk

var (
PayloadEntityMap = payloadEntityMap
EntityPath = entityPath
PayloadEntityMap = payloadEntityMap
EntityPath = entityPath
AddBootPartition = addBootPartition
AddPartitionsForBootMode = addPartitionsForBootMode
)

func FindDirectoryEntityPath(pt *PartitionTable, path string) []Entity {
Expand Down
58 changes: 34 additions & 24 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,10 +996,10 @@ func EnsureRootFilesystem(pt *PartitionTable, defaultFsType FSType) error {
return nil
}

// AddBootPartition creates a boot partition. The function will append the boot
// addBootPartition creates a boot partition. The function will append the boot
// partition to the end of the existing partition table therefore it is best to
// call this function early to put boot near the front (as is conventional).
func AddBootPartition(pt *PartitionTable, bootFsType FSType) error {
func addBootPartition(pt *PartitionTable, bootFsType FSType) error {
if bootFsType == FS_NONE {
return fmt.Errorf("error creating boot partition: no filesystem type")
}
Expand Down Expand Up @@ -1034,15 +1034,15 @@ func AddBootPartition(pt *PartitionTable, bootFsType FSType) error {
return nil
}

// AddPartitionsForBootMode creates partitions to satisfy the boot mode requirements:
// addPartitionsForBootMode creates partitions to satisfy the boot mode requirements:
// - BIOS/legacy: adds a 1 MiB BIOS boot partition.
// - UEFI: adds a 200 MiB EFI system partition.
// - Hybrid: adds both.
//
// The function will append the new partitions to the end of the existing
// partition table therefore it is best to call this function early to put them
// near the front (as is conventional).
func AddPartitionsForBootMode(pt *PartitionTable, bootMode platform.BootMode) error {
func addPartitionsForBootMode(pt *PartitionTable, bootMode platform.BootMode) error {
switch bootMode {
case platform.BOOT_LEGACY:
// add BIOS boot partition
Expand Down Expand Up @@ -1155,6 +1155,28 @@ func (options *CustomPartitionTableOptions) getfstype(fstype string) (string, er
return options.DefaultFSType.String(), nil
}

func maybeAddBootPartition(pt *PartitionTable, disk *blueprint.DiskCustomization, defaultFSType FSType) error {
// The boot type will be the default only if it's a supported filesystem
// type for /boot (ext4 or xfs). Otherwise, we default to xfs.
// FS_NONE also falls back to xfs.
var bootFsType FSType
switch defaultFSType {
case FS_EXT4, FS_XFS:
bootFsType = defaultFSType
default:
bootFsType = FS_XFS
}

if needsBoot(disk) {
// we need a /boot partition to boot LVM or Btrfs, create boot
// partition if it does not already exist
if err := addBootPartition(pt, bootFsType); err != nil {
return err
}
}
return nil
}

// NewCustomPartitionTable creates a partition table based almost entirely on the disk customizations from a blueprint.
func NewCustomPartitionTable(customizations *blueprint.DiskCustomization, options *CustomPartitionTableOptions, rng *rand.Rand) (*PartitionTable, error) {
if options == nil {
Expand Down Expand Up @@ -1184,30 +1206,18 @@ func NewCustomPartitionTable(customizations *blueprint.DiskCustomization, option
return nil, fmt.Errorf("%s invalid partition table type enum value: %d", errPrefix, options.PartitionTableType)
}

// add any partition(s) that are needed for booting (like /boot/efi)
// if needed
//
// TODO: switch to ensure ESP in case customizations already include it
if err := AddPartitionsForBootMode(pt, options.BootMode); err != nil {
if err := addPartitionsForBootMode(pt, options.BootMode); err != nil {
return nil, fmt.Errorf("%s %w", errPrefix, err)
}

// The boot type will be the default only if it's a supported filesystem
// type for /boot (ext4 or xfs). Otherwise, we default to xfs.
// FS_NONE also falls back to xfs.
var bootFsType FSType
switch options.DefaultFSType {
case FS_EXT4, FS_XFS:
bootFsType = options.DefaultFSType
default:
bootFsType = FS_XFS
}

if needsBoot(customizations) {
// we need a /boot partition to boot LVM or Btrfs, create boot
// partition if it does not already exist
if err := AddBootPartition(pt, bootFsType); err != nil {
return nil, fmt.Errorf("%s %w", errPrefix, err)
}
// add the /boot partition (if it is needed)
if err := maybeAddBootPartition(pt, customizations, options.DefaultFSType); err != nil {
return nil, fmt.Errorf("%s %w", errPrefix, err)
}

// add user customized partitions
for _, part := range customizations.Partitions {
switch part.Type {
case "plain", "":
Expand Down
Loading