osbuild2: add new SgdiskStage for org.osbuild.sgdisk

New partitioning stage that uses `sgdisk(8)` via `org.osbuild.sgdisk`.
This commit is contained in:
Christian Kellner 2022-05-27 18:09:35 +02:00
parent 074973e03d
commit 4b4e8ca810
2 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,46 @@
package osbuild2
// Partition a target using sgdisk(8)
import (
"github.com/google/uuid"
)
type SgdiskStageOptions struct {
// UUID for the disk image's partition table
UUID uuid.UUID `json:"uuid"`
// Partition layout
Partitions []SgdiskPartition `json:"partitions,omitempty"`
}
func (SgdiskStageOptions) isStageOptions() {}
// Description of a partition
type SgdiskPartition struct {
// Mark the partition as bootable (dos)
Bootable bool `json:"bootable,omitempty"`
// The partition name
Name string `json:"name,omitempty"`
// The size of the partition (sectors)
Size uint64 `json:"size,omitempty"`
// The start offset of the partition (sectors)
Start uint64 `json:"start,omitempty"`
// The partition type
Type string `json:"type,omitempty"`
// UUID of the partition
UUID *uuid.UUID `json:"uuid,omitempty"`
}
func NewSgdiskStage(options *SgdiskStageOptions, device *Device) *Stage {
return &Stage{
Type: "org.osbuild.sgdisk",
Options: options,
Devices: Devices{"device": *device},
}
}

View file

@ -0,0 +1,38 @@
package osbuild2
import (
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestNewSgdiskStage(t *testing.T) {
uid := uuid.MustParse("68B2905B-DF3E-4FB3-80FA-49D1E773AA33")
partition := SgdiskPartition{
Bootable: true,
Name: "root",
Size: 2097152,
Start: 0,
Type: "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
UUID: &uid,
}
options := SgdiskStageOptions{
UUID: uuid.MustParse("D209C89E-EA5E-4FBD-B161-B461CCE297E0"),
Partitions: []SgdiskPartition{partition},
}
device := NewLoopbackDevice(&LoopbackDeviceOptions{Filename: "disk.raw"})
devices := Devices{"device": *device}
expectedStage := &Stage{
Type: "org.osbuild.sgdisk",
Options: &options,
Devices: devices,
}
actualStage := NewSgdiskStage(&options, device)
assert.Equal(t, expectedStage, actualStage)
}