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: AddPartitionsForBootMode() helper function (COMPOSER-2355) #1024

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 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
90 changes: 90 additions & 0 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/datasizes"
"github.com/osbuild/images/pkg/platform"
)

type PartitionTable struct {
Expand Down Expand Up @@ -1054,3 +1055,92 @@ func EnsureBootPartition(pt *PartitionTable, bootFsType FSType) error {
pt.Partitions = append(pt.Partitions, bootPart)
return nil
}

// 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 {
switch bootMode {
case platform.BOOT_LEGACY:
// add BIOS boot partition
part, err := mkBIOSBoot(pt.Type)
if err != nil {
return err
}
pt.Partitions = append(pt.Partitions, part)
return nil
case platform.BOOT_UEFI:
// add ESP
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
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
default:
return fmt.Errorf("invalid boot mode specified: %s", bootMode)
achilleas-k marked this conversation as resolved.
Show resolved Hide resolved
}
}

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: partType,
UUID: BIOSBootPartitionUUID,
}, nil
}

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: partType,
UUID: EFISystemPartitionUUID,
Payload: &Filesystem{
Type: "vfat",
UUID: EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
}, nil
}
193 changes: 193 additions & 0 deletions pkg/disk/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/osbuild/images/internal/testdisk"
"github.com/osbuild/images/pkg/datasizes"
"github.com/osbuild/images/pkg/disk"
"github.com/osbuild/images/pkg/platform"
)

func TestPartitionTable_GetMountpointSize(t *testing.T) {
Expand Down Expand Up @@ -1023,3 +1024,195 @@ func TestEnsureBootPartition(t *testing.T) {
})
}
}

func TestAddPartitionsForBootMode(t *testing.T) {
type testCase struct {
pt disk.PartitionTable
bootMode platform.BootMode
expected disk.PartitionTable
errmsg string
}

testCases := map[string]testCase{
// the partition table type shouldn't matter when the boot mode is
// none, but let's test with both anyway
"none-gpt": {
pt: disk.PartitionTable{Type: "gpt"},
bootMode: platform.BOOT_NONE,
expected: disk.PartitionTable{Type: "gpt"},
},
"none-dos": {
pt: disk.PartitionTable{Type: "dos"},
bootMode: platform.BOOT_NONE,
expected: disk.PartitionTable{Type: "dos"},
},
"bios-gpt": {
pt: disk.PartitionTable{Type: "gpt"},
bootMode: platform.BOOT_LEGACY,
expected: disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Bootable: true,
Start: 0,
Size: 1 * datasizes.MiB,
Type: disk.BIOSBootPartitionGUID,
UUID: disk.BIOSBootPartitionUUID,
},
},
},
},
"bios-dos": {
pt: disk.PartitionTable{Type: "dos"},
bootMode: platform.BOOT_LEGACY,
expected: disk.PartitionTable{
Type: "dos",
Partitions: []disk.Partition{
{
Bootable: true,
Start: 0,
Size: 1 * datasizes.MiB,
Type: disk.DosBIOSBootID,
UUID: disk.BIOSBootPartitionUUID,
},
},
},
},
"uefi-gpt": {
pt: disk.PartitionTable{Type: "gpt"},
bootMode: platform.BOOT_UEFI,
expected: disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Start: 0 * datasizes.MiB,
Size: 200 * datasizes.MiB,
Type: disk.EFISystemPartitionGUID,
UUID: disk.EFISystemPartitionUUID,
Payload: &disk.Filesystem{
Type: "vfat",
UUID: disk.EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
},
},
},
},
"uefi-dos": {
pt: disk.PartitionTable{Type: "dos"},
bootMode: platform.BOOT_UEFI,
expected: disk.PartitionTable{
Type: "dos",
Partitions: []disk.Partition{
{
Start: 0 * datasizes.MiB,
Size: 200 * datasizes.MiB,
Type: disk.DosESPID,
UUID: disk.EFISystemPartitionUUID,
Payload: &disk.Filesystem{
Type: "vfat",
UUID: disk.EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
},
},
},
},
"hybrid-gpt": {
pt: disk.PartitionTable{Type: "gpt"},
bootMode: platform.BOOT_HYBRID,
expected: disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Size: 1 * datasizes.MiB,
Bootable: true,
Type: disk.BIOSBootPartitionGUID,
UUID: disk.BIOSBootPartitionUUID,
},
{
Size: 200 * datasizes.MiB,
Type: disk.EFISystemPartitionGUID,
UUID: disk.EFISystemPartitionUUID,
Payload: &disk.Filesystem{
Type: "vfat",
UUID: disk.EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
},
},
},
},
"hybrid-dos": {
pt: disk.PartitionTable{Type: "dos"},
bootMode: platform.BOOT_HYBRID,
expected: disk.PartitionTable{
Type: "dos",
Partitions: []disk.Partition{
{
Size: 1 * datasizes.MiB,
Bootable: true,
Type: disk.DosBIOSBootID,
UUID: disk.BIOSBootPartitionUUID,
},
{
Size: 200 * datasizes.MiB,
Type: disk.DosESPID,
UUID: disk.EFISystemPartitionUUID,
Payload: &disk.Filesystem{
Type: "vfat",
UUID: disk.EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
},
},
},
},
"bad-pttype-bios": {
pt: disk.PartitionTable{Type: "super-gpt"},
bootMode: platform.BOOT_LEGACY,
errmsg: "error creating BIOS boot partition: unknown or unsupported partition table type: super-gpt",
},
"bad-pttype-uefi": {
pt: disk.PartitionTable{Type: "super-gpt"},
bootMode: platform.BOOT_UEFI,
errmsg: "error creating EFI system partition: unknown or unsupported partition table type: super-gpt",
},
"bad-pttype-hybrid": {
pt: disk.PartitionTable{Type: "super-gpt"},
bootMode: platform.BOOT_HYBRID,
errmsg: "error creating BIOS boot partition: unknown or unsupported partition table type: super-gpt",
},
}

for name := range testCases {
tc := testCases[name]
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
pt := tc.pt
err := disk.AddPartitionsForBootMode(&pt, tc.bootMode)
if tc.errmsg == "" {
assert.NoError(err)
assert.Equal(tc.expected, pt)
} else {
assert.EqualError(err, tc.errmsg)
}
})
}
}
Loading