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

bib: extract common platformFor() helper and add tests #693

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions bib/cmd/bootc-image-builder/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var (
GenPartitionTable = genPartitionTable
CreateRand = createRand
BuildCobraCmdline = buildCobraCmdline
PlatformFor = platformFor
)

func MockOsGetuid(new func() int) (restore func()) {
Expand Down
100 changes: 52 additions & 48 deletions bib/cmd/bootc-image-builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,49 @@ func genPartitionTable(c *ManifestConfig, customizations *blueprint.Customizatio
return pt, nil
}

func platformFor(archi arch.Arch, imgFormat platform.ImageFormat, UEFIVendor string) (platform.Platform, error) {
// note that this is not setting `QCOW2Compat: "1.1"` because
// this is the default in qemu since 2013 (upstream commit
// 8ad1898c, version qemu 1.7)
switch archi {
case arch.ARCH_X86_64:
return &platform.X86{
BasePlatform: platform.BasePlatform{
ImageFormat: imgFormat,
},
BIOS: true,
UEFIVendor: UEFIVendor,
}, nil
case arch.ARCH_AARCH64:
// aarch64 always uses UEFI, so let's enforce the vendor
if imgFormat == platform.FORMAT_ISO && UEFIVendor == "" {
return nil, fmt.Errorf("UEFI vendor must be set for aarch64 ISO")
}
return &platform.Aarch64{
BasePlatform: platform.BasePlatform{
ImageFormat: imgFormat,
},
UEFIVendor: UEFIVendor,
}, nil
case arch.ARCH_S390X:
return &platform.S390X{
Zipl: true,
BasePlatform: platform.BasePlatform{
ImageFormat: imgFormat,
},
}, nil
case arch.ARCH_PPC64LE:
return &platform.PPC64LE{
BIOS: true,
BasePlatform: platform.BasePlatform{
ImageFormat: imgFormat,
},
}, nil
default:
return nil, fmt.Errorf("unsupported architecture %v", archi)
}
}

func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest, error) {
if c.Imgref == "" {
return nil, fmt.Errorf("pipeline: no base image defined")
Expand Down Expand Up @@ -260,34 +303,12 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
"console=ttyS0",
}

switch c.Architecture {
case arch.ARCH_X86_64:
img.Platform = &platform.X86{
BasePlatform: platform.BasePlatform{},
BIOS: true,
}
case arch.ARCH_AARCH64:
img.Platform = &platform.Aarch64{
UEFIVendor: "fedora",
BasePlatform: platform.BasePlatform{
QCOW2Compat: "1.1",
},
}
case arch.ARCH_S390X:
img.Platform = &platform.S390X{
BasePlatform: platform.BasePlatform{
QCOW2Compat: "1.1",
},
Zipl: true,
}
case arch.ARCH_PPC64LE:
img.Platform = &platform.PPC64LE{
BasePlatform: platform.BasePlatform{
QCOW2Compat: "1.1",
},
BIOS: true,
}
// not setting uefi vendor here as it is irrelevant for disk-images
platform, err := platformFor(c.Architecture, platform.FORMAT_UNSET, "")
if err != nil {
return nil, err
}
img.Platform = platform

if kopts := customizations.GetKernel(); kopts != nil && kopts.Append != "" {
img.KernelOptionsAppend = append(img.KernelOptionsAppend, kopts.Append)
Expand Down Expand Up @@ -408,28 +429,11 @@ func manifestForISO(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest, erro
img.UseRHELLoraxTemplates =
c.SourceInfo.OSRelease.ID != "fedora" || c.SourceInfo.OSRelease.VersionID == "eln"

switch c.Architecture {
case arch.ARCH_X86_64:
img.Platform = &platform.X86{
BasePlatform: platform.BasePlatform{
ImageFormat: platform.FORMAT_ISO,
},
BIOS: true,
UEFIVendor: c.SourceInfo.UEFIVendor,
}
case arch.ARCH_AARCH64:
// aarch64 always uses UEFI, so let's enforce the vendor
if c.SourceInfo.UEFIVendor == "" {
return nil, fmt.Errorf("UEFI vendor must be set for aarch64 ISO")
}
img.Platform = &platform.Aarch64{
BasePlatform: platform.BasePlatform{
ImageFormat: platform.FORMAT_ISO,
},
UEFIVendor: c.SourceInfo.UEFIVendor,
}
platform, err := platformFor(c.Architecture, platform.FORMAT_ISO, c.SourceInfo.UEFIVendor)
if err != nil {
return nil, err
}

img.Platform = platform
img.Filename = "install.iso"

mf := manifest.New()
Expand Down
30 changes: 30 additions & 0 deletions bib/cmd/bootc-image-builder/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/disk"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/platform"
"github.com/osbuild/images/pkg/runner"

bib "github.com/osbuild/bootc-image-builder/bib/cmd/bootc-image-builder"
Expand Down Expand Up @@ -423,3 +424,32 @@ func TestGenPartitionTableSetsRootfsForAllFilesystemsBtrfs(t *testing.T) {
mnt, _ = findMountableSizeableFor(pt, "/boot/efi")
assert.Equal(t, "vfat", mnt.GetFSType())
}

func TestPlatformForSmoke(t *testing.T) {
for _, tc := range []struct {
arch arch.Arch
uefi string
}{
{arch.ARCH_X86_64, "foodora"},
{arch.ARCH_AARCH64, "dubian"},
{arch.ARCH_S390X, ""},
{arch.ARCH_PPC64LE, ""},
} {
platf, err := bib.PlatformFor(tc.arch, platform.FORMAT_ISO, tc.uefi)
assert.NoError(t, err)
assert.Equal(t, tc.uefi, platf.GetUEFIVendor())
assert.Equal(t, platform.FORMAT_ISO, platf.GetImageFormat())
}
}

func TestPlatformForUnhappy(t *testing.T) {
_, err := bib.PlatformFor(arch.Arch(911), platform.FORMAT_ISO, "")
assert.ErrorContains(t, err, "unsupported architecture ")

_, err = bib.PlatformFor(arch.ARCH_AARCH64, platform.FORMAT_ISO, "")
assert.ErrorContains(t, err, "UEFI vendor must be set for aarch64 ISO")

// for non ISO it is okay to have uset uefi vendor
_, err = bib.PlatformFor(arch.ARCH_AARCH64, platform.FORMAT_QCOW2, "")
assert.NoError(t, err)
}
Loading