diff --git a/internal/osbuild2/sgdisk_stage.go b/internal/osbuild2/sgdisk_stage.go new file mode 100644 index 000000000..6b21695e0 --- /dev/null +++ b/internal/osbuild2/sgdisk_stage.go @@ -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}, + } +} diff --git a/internal/osbuild2/sgdisk_stage_test.go b/internal/osbuild2/sgdisk_stage_test.go new file mode 100644 index 000000000..fb42f21a7 --- /dev/null +++ b/internal/osbuild2/sgdisk_stage_test.go @@ -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) +}