osbuild2: add new GenImage{Prepare,Finish}Stages

Add new helper functions that will return the necessary stages to
prepare a disk image, i.e. take care of creating the raw image
file, partitioning it, creating the devices on it (LUKS2, LVM2),
and formatting all the file systems: `GenImageStagesPrepare`.
Additionally, some partition layouts require some post-processing,
e.g. LVM2 where the volume group needs to be renamed "offline".
For this the `GenImageFinishStages` was added.

Co-Authored-By: Achilleas Koutsou <achilleas@koutsou.net>
This commit is contained in:
Christian Kellner 2022-02-11 12:12:35 +00:00 committed by Tom Gundersen
parent 670b936e6b
commit 85e2a53606

65
internal/osbuild2/disk.go Normal file
View file

@ -0,0 +1,65 @@
package osbuild2
import (
"fmt"
"github.com/osbuild/osbuild-composer/internal/disk"
)
// sfdiskStageOptions creates the options and devices properties for an
// org.osbuild.sfdisk stage based on a partition table description
func sfdiskStageOptions(pt *disk.PartitionTable) *SfdiskStageOptions {
partitions := make([]Partition, len(pt.Partitions))
for idx, p := range pt.Partitions {
partitions[idx] = Partition{
Bootable: p.Bootable,
Start: pt.BytesToSectors(p.Start),
Size: pt.BytesToSectors(p.Size),
Type: p.Type,
UUID: p.UUID,
}
}
stageOptions := &SfdiskStageOptions{
Label: pt.Type,
UUID: pt.UUID,
Partitions: partitions,
}
return stageOptions
}
func GenImagePrepareStages(pt *disk.PartitionTable, filename string) []*Stage {
stages := make([]*Stage, 0)
// create an empty file of the given size via `org.osbuild.truncate`
stage := NewTruncateStage(
&TruncateStageOptions{
Filename: filename,
Size: fmt.Sprintf("%d", pt.Size),
})
stages = append(stages, stage)
// create the partition layout in the empty file
sfOptions := sfdiskStageOptions(pt)
loopback := NewLoopbackDevice(
&LoopbackDeviceOptions{Filename: filename},
)
sfdisk := NewSfdiskStage(sfOptions, loopback)
stages = append(stages, sfdisk)
// Generate all the needed "devices", like LUKS2 and LVM2
s := GenDeviceCreationStages(pt, filename)
stages = append(stages, s...)
// Generate all the filesystems on partitons and devices
s = GenMkfsStages(pt, loopback)
stages = append(stages, s...)
return stages
}
func GenImageFinishStages(pt *disk.PartitionTable, filename string) []*Stage {
return GenDeviceFinishStages(pt, filename)
}