disk: move stage option generation functions to osbuild1,2

Co-Authored-By: Christian Kellner <christian@kellner.me>
This commit is contained in:
Achilleas Koutsou 2022-02-08 20:33:25 +01:00 committed by Tom Gundersen
parent 05a2f97549
commit 3110ae4629
11 changed files with 111 additions and 108 deletions

View file

@ -1,7 +1,5 @@
package disk
import osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
// Filesystem related functions
type Filesystem struct {
Type string
@ -39,16 +37,6 @@ func (fs *Filesystem) Clone() Entity {
}
}
// Converts Filesystem to osbuild.QEMUFilesystem that encodes the same fs.
func (fs *Filesystem) QEMUFilesystem() osbuild.QEMUFilesystem {
return osbuild.QEMUFilesystem{
Type: fs.Type,
UUID: fs.UUID,
Label: fs.Label,
Mountpoint: fs.Mountpoint,
}
}
func (fs *Filesystem) GetMountpoint() string {
return fs.Mountpoint
}

View file

@ -2,8 +2,6 @@ package disk
import (
"fmt"
osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
)
type Partition struct {
@ -49,23 +47,6 @@ func (p *Partition) Clone() Entity {
}
}
// Converts Partition to osbuild.QEMUPartition that encodes the same partition.
func (p *Partition) QEMUPartition() osbuild.QEMUPartition {
var fs *osbuild.QEMUFilesystem
if p.Payload != nil {
f := p.Payload.QEMUFilesystem()
fs = &f
}
return osbuild.QEMUPartition{
Start: p.Start,
Size: p.Size,
Type: p.Type,
Bootable: p.Bootable,
UUID: p.UUID,
Filesystem: fs,
}
}
func (pt *Partition) GetItemCount() uint {
if pt.Payload == nil {
return 0

View file

@ -4,11 +4,8 @@ import (
"errors"
"fmt"
"math/rand"
"sort"
"github.com/google/uuid"
osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
"github.com/osbuild/osbuild-composer/internal/osbuild2"
)
type PartitionTable struct {
@ -84,62 +81,6 @@ func (pt *PartitionTable) SectorsToBytes(size uint64) uint64 {
return size * sectorSize
}
// Converts PartitionTable to osbuild.QEMUAssemblerOptions that encode
// the same partition table.
func (pt *PartitionTable) QEMUAssemblerOptions() osbuild.QEMUAssemblerOptions {
var partitions []osbuild.QEMUPartition
for _, p := range pt.Partitions {
partitions = append(partitions, p.QEMUPartition())
}
return osbuild.QEMUAssemblerOptions{
Size: pt.Size,
PTUUID: pt.UUID,
PTType: pt.Type,
Partitions: partitions,
}
}
// Generates org.osbuild.fstab stage options from this partition table.
func (pt *PartitionTable) FSTabStageOptions() *osbuild.FSTabStageOptions {
var options osbuild.FSTabStageOptions
for _, p := range pt.Partitions {
fs := p.Payload
if fs == nil {
continue
}
options.AddFilesystem(fs.UUID, fs.Type, fs.Mountpoint, fs.FSTabOptions, fs.FSTabFreq, fs.FSTabPassNo)
}
// sort the entries by PassNo to maintain backward compatibility
sort.Slice(options.FileSystems, func(i, j int) bool {
return options.FileSystems[i].PassNo < options.FileSystems[j].PassNo
})
return &options
}
// Generates org.osbuild.fstab stage options from this partition table.
func (pt *PartitionTable) FSTabStageOptionsV2() *osbuild2.FSTabStageOptions {
var options osbuild2.FSTabStageOptions
for _, p := range pt.Partitions {
fs := p.Payload
if fs == nil {
continue
}
options.AddFilesystem(fs.UUID, fs.Type, fs.Mountpoint, fs.FSTabOptions, fs.FSTabFreq, fs.FSTabPassNo)
}
// sort the entries by PassNo to maintain backward compatibility
sort.Slice(options.FileSystems, func(i, j int) bool {
return options.FileSystems[i].PassNo < options.FileSystems[j].PassNo
})
return &options
}
func (pt *PartitionTable) FindPartitionForMountpoint(mountpoint string) *Partition {
for idx, p := range pt.Partitions {
if p.Payload == nil {

View file

@ -417,7 +417,7 @@ func (t *imageType) pipeline(c *blueprint.Customizations, options distro.ImageOp
p.AddStage(osbuild.NewFixBLSStage())
if pt != nil {
p.AddStage(osbuild.NewFSTabStage(pt.FSTabStageOptions()))
p.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(pt)))
}
if t.bootable {
@ -824,7 +824,7 @@ func defaultPartitionTable(imageSize uint64, arch distro.Arch, rng *rand.Rand) d
}
func qemuAssembler(pt *disk.PartitionTable, format string, filename string, imageOptions distro.ImageOptions, arch distro.Arch, qcow2Compat string) *osbuild.Assembler {
options := pt.QEMUAssemblerOptions()
options := osbuild.NewQEMUAssemblerOptions(pt)
options.Format = format
options.Filename = filename

View file

@ -46,7 +46,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
}))
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer, false, false))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -87,7 +87,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
return nil, err
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer, false, false))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -118,7 +118,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
return nil, err
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer, false, false))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -149,7 +149,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
return nil, err
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer, false, false))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -431,7 +431,7 @@ func ec2CommonPipelines(t *imageType, customizations *blueprint.Customizations,
return nil, err
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer, false, false))
// The last stage must be the SELinux stage
@ -1126,7 +1126,7 @@ func ostreeDeployPipeline(
},
))
fstabOptions := pt.FSTabStageOptionsV2()
fstabOptions := osbuild.NewFSTabStageOptions(pt)
fstabOptions.OSTree = &osbuild.OSTreeFstab{
Deployment: osbuild.OSTreeDeployment{
OSName: osname,

View file

@ -520,7 +520,7 @@ func osPipeline(t *imageType,
if pt != nil {
p = prependKernelCmdlineStage(p, t, pt)
p.AddStage(osbuild.NewFSTabStage(pt.FSTabStageOptionsV2()))
p.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(pt)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(bpPackages, c.GetKernel().Name)
p.AddStage(bootloaderConfigStage(t, *pt, c.GetKernel(), kernelVer, false, false))
}
@ -842,7 +842,7 @@ func ostreeDeployPipeline(
},
))
fstabOptions := pt.FSTabStageOptionsV2()
fstabOptions := osbuild.NewFSTabStageOptions(pt)
fstabOptions.OSTree = &osbuild.OSTreeFstab{
Deployment: osbuild.OSTreeDeployment{
OSName: osname,

View file

@ -507,7 +507,7 @@ func osPipeline(t *imageType,
if pt != nil {
p = prependKernelCmdlineStage(p, t, pt)
p.AddStage(osbuild.NewFSTabStage(pt.FSTabStageOptionsV2()))
p.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(pt)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(bpPackages, c.GetKernel().Name)
p.AddStage(bootloaderConfigStage(t, *pt, c.GetKernel(), kernelVer, false, false))
}
@ -838,7 +838,7 @@ func ostreeDeployPipeline(
},
))
fstabOptions := pt.FSTabStageOptionsV2()
fstabOptions := osbuild.NewFSTabStageOptions(pt)
fstabOptions.OSTree = &osbuild.OSTreeFstab{
Deployment: osbuild.OSTreeDeployment{
OSName: osname,

View file

@ -42,7 +42,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
},
}))
}
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -79,7 +79,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
}
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -108,7 +108,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
}
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -136,7 +136,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
return nil, err
}
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
treePipeline.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false)))
@ -410,7 +410,7 @@ func ec2CommonPipelines(t *imageType, customizations *blueprint.Customizations,
}
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
// The last stage must be the SELinux stage
@ -551,7 +551,7 @@ func ec2SapPipelines(t *imageType, customizations *blueprint.Customizations, opt
)))
treePipeline = prependKernelCmdlineStage(treePipeline, t, &partitionTable)
treePipeline.AddStage(osbuild.NewFSTabStage(partitionTable.FSTabStageOptionsV2()))
treePipeline.AddStage(osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(&partitionTable)))
kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(packageSetSpecs[blueprintPkgsKey], customizations.GetKernel().Name)
treePipeline.AddStage(bootloaderConfigStage(t, partitionTable, customizations.GetKernel(), kernelVer))
// The last stage must be the SELinux stage

View file

@ -1,5 +1,11 @@
package osbuild1
import (
"sort"
"github.com/osbuild/osbuild-composer/internal/disk"
)
// The FSTabStageOptions describe the content of the /etc/fstab file.
//
// The structure of the options follows the format of /etc/fstab, except
@ -42,3 +48,20 @@ func (options *FSTabStageOptions) AddFilesystem(id string, vfsType string, path
PassNo: passNo,
})
}
func NewFSTabStageOptions(pt *disk.PartitionTable) *FSTabStageOptions {
var options FSTabStageOptions
genOption := func(mnt disk.Mountable, path []disk.Entity) error {
fsSpec := mnt.GetFSSpec()
fsOptions := mnt.GetFSTabOptions()
options.AddFilesystem(fsSpec.UUID, mnt.GetFSType(), mnt.GetMountpoint(), fsOptions.MntOps, fsOptions.Freq, fsOptions.PassNo)
return nil
}
_ = pt.ForEachMountable(genOption)
// sort the entries by PassNo to maintain backward compatibility
sort.Slice(options.FileSystems, func(i, j int) bool {
return options.FileSystems[i].PassNo < options.FileSystems[j].PassNo
})
return &options
}

View file

@ -1,5 +1,7 @@
package osbuild1
import "github.com/osbuild/osbuild-composer/internal/disk"
// QEMUAssemblerOptions desrcibe how to assemble a tree into an image using qemu.
//
// The assembler creates an image of the given size, adds a GRUB2 bootloader
@ -47,3 +49,48 @@ func NewQEMUAssembler(options *QEMUAssemblerOptions) *Assembler {
Options: options,
}
}
// NewQEMUAssemblerOptions creates and returns QEMUAssemblerOptions based on
// the given PartitionTable.
func NewQEMUAssemblerOptions(pt *disk.PartitionTable) QEMUAssemblerOptions {
var partitions []QEMUPartition
for idx := range pt.Partitions {
partitions = append(partitions, NewQEMUPartition(&pt.Partitions[idx]))
}
return QEMUAssemblerOptions{
Size: pt.Size,
PTUUID: pt.UUID,
PTType: pt.Type,
Partitions: partitions,
}
}
// NewQEMUPartition creates and returns a QEMUPartition based on the given
// Partition.
func NewQEMUPartition(p *disk.Partition) QEMUPartition {
var fs *QEMUFilesystem
if p.Payload != nil {
f := NewQEMUFilesystem(p.Payload)
fs = &f
}
return QEMUPartition{
Start: p.Start,
Size: p.Size,
Type: p.Type,
Bootable: p.Bootable,
UUID: p.UUID,
Filesystem: fs,
}
}
// NewQEMUFilesystem creates and returns a QEMUFilesystem based on the given
// Filesystem.
func NewQEMUFilesystem(fs *disk.Filesystem) QEMUFilesystem {
return QEMUFilesystem{
Type: fs.Type,
UUID: fs.UUID,
Label: fs.Label,
Mountpoint: fs.Mountpoint,
}
}

View file

@ -1,5 +1,11 @@
package osbuild2
import (
"sort"
"github.com/osbuild/osbuild-composer/internal/disk"
)
// The FSTabStageOptions describe the content of the /etc/fstab file.
//
// The structure of the options follows the format of /etc/fstab, except
@ -48,3 +54,20 @@ func (options *FSTabStageOptions) AddFilesystem(id string, vfsType string, path
PassNo: passNo,
})
}
func NewFSTabStageOptions(pt *disk.PartitionTable) *FSTabStageOptions {
var options FSTabStageOptions
genOption := func(mnt disk.Mountable, path []disk.Entity) error {
fsSpec := mnt.GetFSSpec()
fsOptions := mnt.GetFSTabOptions()
options.AddFilesystem(fsSpec.UUID, mnt.GetFSType(), mnt.GetMountpoint(), fsOptions.MntOps, fsOptions.Freq, fsOptions.PassNo)
return nil
}
_ = pt.ForEachMountable(genOption) // genOption always returns nil
// sort the entries by PassNo to maintain backward compatibility
sort.Slice(options.FileSystems, func(i, j int) bool {
return options.FileSystems[i].PassNo < options.FileSystems[j].PassNo
})
return &options
}