distro: rhel85 streamline to single function

This commit is contained in:
Gianluca Zuccarelli 2021-08-18 12:37:45 +01:00 committed by Tom Gundersen
parent 49540ee91a
commit 87c176503a
4 changed files with 100 additions and 121 deletions

View file

@ -14,6 +14,15 @@ import (
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
const (
// architecture names
X86_64ArchName = "x86_64"
Aarch64ArchName = "aarch64"
Ppc64leArchName = "ppc64le"
S390xArchName = "s390x"
)
// A Distro represents composer's notion of what a given distribution is.
type Distro interface {
// Returns the name of the distro.

View file

@ -9,6 +9,7 @@ import (
"sort"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/disk"
"github.com/osbuild/osbuild-composer/internal/distro"
osbuild "github.com/osbuild/osbuild-composer/internal/osbuild2"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
@ -52,17 +53,10 @@ const (
blueprintPkgsKey = "blueprint"
)
const (
// architecture names
x86_64ArchName = "x86_64"
aarch64ArchName = "aarch64"
ppc64leArchName = "ppc64le"
s390xArchName = "s390x"
)
type BootType string
type ValidArches map[string]disk.PartitionTable
const (
UnsetBootType BootType = ""
LegacyBootType BootType = "legacy"
@ -213,6 +207,8 @@ type imageType struct {
bootable bool
// If set to a value, it is preferred over the architecture value
bootType BootType
// List of valid arches for the image type
validArches ValidArches
}
func (t *imageType) Name() string {
@ -500,7 +496,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
// Architecture definitions
x86_64 := architecture{
name: x86_64ArchName,
name: distro.X86_64ArchName,
distro: rd,
packageSets: map[string]rpmmd.PackageSet{
buildPkgsKey: x8664BuildPackageSet(),
@ -513,7 +509,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
}
aarch64 := architecture{
name: aarch64ArchName,
name: distro.Aarch64ArchName,
distro: rd,
packageSets: map[string]rpmmd.PackageSet{
bootUEFIPkgsKey: aarch64UEFIBootPackageSet(),
@ -524,7 +520,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
ppc64le := architecture{
distro: rd,
name: ppc64leArchName,
name: distro.Ppc64leArchName,
packageSets: map[string]rpmmd.PackageSet{
bootLegacyPkgsKey: ppc64leLegacyBootPackageSet(),
buildPkgsKey: ppc64leBuildPackageSet(),
@ -534,7 +530,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
}
s390x := architecture{
distro: rd,
name: s390xArchName,
name: distro.S390xArchName,
packageSets: map[string]rpmmd.PackageSet{
bootLegacyPkgsKey: s390xLegacyBootPackageSet(),
},
@ -614,6 +610,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: qcow2Pipelines,
exports: []string{"qcow2"},
validArches: defaultArches,
}
vhdImgType := imageType{
@ -633,6 +630,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 4 * GigaByte,
pipelines: vhdPipelines,
exports: []string{"vpc"},
validArches: defaultArches,
}
vmdkImgType := imageType{
@ -647,6 +645,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 4 * GigaByte,
pipelines: vmdkPipelines,
exports: []string{"vmdk"},
validArches: defaultArches,
}
openstackImgType := imageType{
@ -661,6 +660,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 4 * GigaByte,
pipelines: openstackPipelines,
exports: []string{"qcow2"},
validArches: defaultArches,
}
// EC2 services
@ -692,6 +692,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: ec2Pipelines,
exports: []string{"image"},
validArches: ec2ValidArches,
}
amiImgTypeAarch64 := imageType{
@ -709,6 +710,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: ec2Pipelines,
exports: []string{"image"},
validArches: ec2ValidArches,
}
ec2ImgTypeX86_64 := imageType{
@ -727,6 +729,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: rhelEc2Pipelines,
exports: []string{"archive"},
validArches: ec2ValidArches,
}
ec2ImgTypeAarch64 := imageType{
@ -744,6 +747,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: rhelEc2Pipelines,
exports: []string{"archive"},
validArches: ec2ValidArches,
}
ec2HaImgTypeX86_64 := imageType{
@ -762,6 +766,7 @@ func newDistro(name, modulePlatformID, ostreeRef string) distro.Distro {
defaultSize: 10 * GigaByte,
pipelines: rhelEc2Pipelines,
exports: []string{"archive"},
validArches: ec2ValidArches,
}
tarImgType := imageType{

View file

@ -27,50 +27,39 @@ const (
rootPartitionUUID = "6264D520-3FB9-423F-8AB8-7A0A8E3D3562"
)
var x86PartitionTable = disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Partitions: []disk.Partition{},
}
var aarch64PartitionTable = disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Partitions: []disk.Partition{},
}
var ppc64lePartitionTable = disk.PartitionTable{
UUID: "0x14fc63d2",
Type: "dos",
Partitions: []disk.Partition{},
}
var s390xPartitionTable = disk.PartitionTable{
UUID: "0x14fc63d2",
Type: "dos",
Partitions: []disk.Partition{},
}
var validArches = map[string]disk.PartitionTable{
x86_64ArchName: x86PartitionTable,
aarch64ArchName: aarch64PartitionTable,
ppc64leArchName: ppc64lePartitionTable,
s390xArchName: s390xPartitionTable,
var defaultArches = map[string]disk.PartitionTable{
distro.X86_64ArchName: gptPartitionTable,
distro.Aarch64ArchName: gptPartitionTable,
distro.Ppc64leArchName: dosPartitionTable,
distro.S390xArchName: dosPartitionTable,
}
var ec2ValidArches = map[string]disk.PartitionTable{
x86_64ArchName: x86PartitionTable,
aarch64ArchName: aarch64PartitionTable,
distro.X86_64ArchName: gptPartitionTable,
distro.Aarch64ArchName: gptPartitionTable,
}
var gptPartitionTable = disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Partitions: []disk.Partition{},
}
var dosPartitionTable = disk.PartitionTable{
UUID: "0x14fc63d2",
Type: "dos",
Partitions: []disk.Partition{},
}
func createPartitionTable(
mountpoints []blueprint.FilesystemCustomization,
imageOptions distro.ImageOptions,
arch distro.Arch,
t *imageType,
ec2 bool,
rng *rand.Rand,
) disk.PartitionTable {
archName := arch.Name()
partitionTable, ok := validArches[archName]
archName := t.arch.Name()
partitionTable, ok := t.validArches[archName]
if !ok {
panic("unknown arch: " + archName)
@ -78,35 +67,34 @@ func createPartitionTable(
partitionTable.Size = imageOptions.Size
partitions := []disk.Partition{}
var start uint64
var start uint64 = 2048
if archName == x86_64ArchName {
biosBootPartition := disk.Partition{
Bootable: true,
Size: 2048,
Start: 2048,
Type: biosBootPartitionType,
UUID: biosBootPartitionUUID,
}
if archName == distro.X86_64ArchName {
biosBootPartition := createPartition("bios", 2048, start, archName, rng)
partitions = append(partitions, biosBootPartition)
bootEFIPartition := createPartition("/boot/efi", 204800, 4096, archName, rng)
partitions = append(partitions, bootEFIPartition)
start = uint64(4096 + 204800)
} else if archName == aarch64ArchName {
bootEFIPartition := createPartition("/boot/efi", 204800, 2048, archName, rng)
partitions = append(partitions, bootEFIPartition)
start = uint64(2048 + 204800)
} else if archName == ppc64leArchName {
biosBootPartition := disk.Partition{
Start: 2048,
Size: 8192,
Type: "41",
Bootable: true,
start += biosBootPartition.Size
if t.bootType != LegacyBootType {
bootEFIPartition := createPartition("/boot/efi", 204800, start, archName, rng)
partitions = append(partitions, bootEFIPartition)
start += bootEFIPartition.Size
}
} else if archName == distro.Aarch64ArchName {
if ec2 {
bootEFIParition := createPartition("/boot/efi", 409600, start, archName, rng)
partitions = append(partitions, bootEFIParition)
start += bootEFIParition.Size
bootPartition := createPartition("/boot", 1048576, 411648, archName, rng)
partitions = append(partitions, bootPartition)
start += bootPartition.Size
} else {
bootEFIPartition := createPartition("/boot/efi", 204800, start, archName, rng)
partitions = append(partitions, bootEFIPartition)
start += bootEFIPartition.Size
}
} else if archName == distro.Ppc64leArchName {
biosBootPartition := createPartition("bios", 8192, start, archName, rng)
partitions = append(partitions, biosBootPartition)
start = uint64(10240)
} else if archName == s390xArchName {
start = uint64(2048)
start += biosBootPartition.Size
}
for _, m := range mountpoints {
@ -130,44 +118,21 @@ func createPartitionTable(
return partitionTable
}
func ec2PartitionTable(imageOptions distro.ImageOptions, arch distro.Arch, rng *rand.Rand) disk.PartitionTable {
archName := arch.Name()
partitionTable, ok := ec2ValidArches[archName]
if !ok {
panic("unsupported EC2 arch: " + archName)
}
partitionTable.Size = imageOptions.Size
partitions := []disk.Partition{}
var start uint64
if archName == x86_64ArchName {
biosBootPartition := disk.Partition{
Bootable: true,
Size: 2048,
Start: 2048,
Type: biosBootPartitionType,
UUID: biosBootPartitionUUID,
}
partitions = append(partitions, biosBootPartition)
start = 4096
} else if archName == aarch64ArchName {
bootEFIParition := createPartition("/boot/efi", 409600, 2048, archName, rng)
partitions = append(partitions, bootEFIParition)
bootPartition := createPartition("/boot", 1048576, 411648, archName, rng)
partitions = append(partitions, bootPartition)
start = 1460224
}
rootSize := (imageOptions.Size / sectorSize) - start - 100
rootPartition := createPartition("/", rootSize, start, archName, rng)
partitions = append(partitions, rootPartition)
partitionTable.Partitions = append(partitionTable.Partitions, partitions...)
return partitionTable
}
func createPartition(mountpoint string, size uint64, start uint64, archName string, rng *rand.Rand) disk.Partition {
if mountpoint == "bios" {
diskPartition := disk.Partition{
Start: start,
Size: size,
Bootable: true,
}
if archName == distro.X86_64ArchName {
diskPartition.Type = biosBootPartitionType
diskPartition.UUID = biosBootPartitionUUID
return diskPartition
}
diskPartition.Type = "41"
return diskPartition
}
var filesystem disk.Filesystem
// /boot/efi mountpoint is a special case
// return early
@ -193,7 +158,7 @@ func createPartition(mountpoint string, size uint64, start uint64, archName stri
partition.UUID = bootPartitionUUID
return partition
}
if archName == x86_64ArchName || archName == aarch64ArchName {
if archName == distro.X86_64ArchName || archName == distro.Aarch64ArchName {
if mountpoint == "/" {
// set Label for root mountpoint
filesystem.Label = "root"
@ -205,7 +170,7 @@ func createPartition(mountpoint string, size uint64, start uint64, archName stri
partition.UUID = uuid.Must(newRandomUUIDFromReader(rng)).String()
return partition
}
if mountpoint == "/" && archName == s390xArchName {
if mountpoint == "/" && archName == distro.S390xArchName {
partition.Bootable = true
}
return partition

View file

@ -23,7 +23,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
return nil, err
}
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t.arch, rng)
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t, false, rng)
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
if options.Subscription == nil {
@ -57,7 +57,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
}
func prependKernelCmdlineStage(pipeline *osbuild.Pipeline, t *imageType, pt *disk.PartitionTable) *osbuild.Pipeline {
if t.arch.name == s390xArchName {
if t.arch.name == distro.S390xArchName {
rootPartition := pt.RootPartition()
if rootPartition == nil {
panic("s390x image must have a root partition, this is a programming error")
@ -76,7 +76,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
return nil, err
}
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t.arch, rng)
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t, false, rng)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
kernelVer := kernelVerStr(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name, t.Arch().Name())
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
@ -103,7 +103,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
return nil, err
}
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t.arch, rng)
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t, false, rng)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
kernelVer := kernelVerStr(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name, t.Arch().Name())
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
@ -130,7 +130,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
return nil, err
}
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t.arch, rng)
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t, false, rng)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
kernelVer := kernelVerStr(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name, t.Arch().Name())
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
@ -392,15 +392,15 @@ func ec2CommonPipelines(t *imageType, customizations *blueprint.Customizations,
pipelines := make([]osbuild.Pipeline, 0)
pipelines = append(pipelines, *buildPipeline(repos, packageSetSpecs[buildPkgsKey]))
partitionTable := ec2PartitionTable(options, t.arch, rng)
partitionTable := createPartitionTable(customizations.GetFilesystems(), options, t, true, rng)
var treePipeline *osbuild.Pipeline
var err error
switch arch := t.arch.Name(); arch {
// rhel-ec2-x86_64, rhel-ha-ec2
case x86_64ArchName:
case distro.X86_64ArchName:
treePipeline, err = ec2X86_64BaseTreePipeline(repos, packageSetSpecs[osPkgsKey], packageSetSpecs[blueprintPkgsKey], customizations, options, t.enabledServices, t.disabledServices, t.defaultTarget, withRHUI, &partitionTable)
// rhel-ec2-aarch64
case aarch64ArchName:
case distro.Aarch64ArchName:
treePipeline, err = ec2BaseTreePipeline(repos, packageSetSpecs[osPkgsKey], packageSetSpecs[blueprintPkgsKey], customizations, options, t.enabledServices, t.disabledServices, t.defaultTarget, withRHUI, &partitionTable)
default:
return nil, fmt.Errorf("ec2CommonPipelines: unsupported image architecture: %q", arch)
@ -977,7 +977,7 @@ func qemuPipeline(inputPipelineName, inputFilename, outputFilename, format, qcow
}
func bootloaderConfigStage(t *imageType, partitionTable disk.PartitionTable, kernel *blueprint.KernelCustomization, kernelVer string) *osbuild.Stage {
if t.arch.name == s390xArchName {
if t.arch.name == distro.S390xArchName {
return osbuild.NewZiplStage(new(osbuild.ZiplStageOptions))
}
@ -993,7 +993,7 @@ func bootloaderInstStage(filename string, pt *disk.PartitionTable, arch *archite
return osbuild.NewGrub2InstStage(grub2InstStageOptions(filename, pt, platform))
}
if arch.name == s390xArchName {
if arch.name == distro.S390xArchName {
devmap := map[string]osbuild.Device(*devices)
devmap["disk"] = *disk
ziplDevices := osbuild.CopyStageDevices(devmap)