Skip to content

Commit

Permalink
otk-gen-partition-table: move the "filename" customization here
Browse files Browse the repository at this point in the history
The otk-make-partition-{stages,mount-devices} externals both
needed the filename modification. It seems easiest to just
define it in one place in `otk-gen-partition-table` and
then just to use the outputs from that.
  • Loading branch information
mvo5 authored and achilleas-k committed Jun 6, 2024
1 parent 8322391 commit b048f89
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 48 deletions.
6 changes: 6 additions & 0 deletions cmd/otk-gen-partition-table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type InputModifications struct {
PartitionMode disk.PartitioningMode `json:"partition_mode"`
Filesystems []blueprint.FilesystemCustomization `json:"filesystems"`
MinDiskSize string `json:"min_disk_size"`
Filename string `json:"filename"`
}

type Output = otkpart.PartitionTable
Expand Down Expand Up @@ -181,6 +182,10 @@ func genPartitionTable(genPartInput *Input, rng *rand.Rand) (*Output, error) {
if err != nil {
return nil, err
}
fname := "disk.img"
if genPartInput.Modifications.Filename != "" {
fname = genPartInput.Modifications.Filename
}

kernelOptions := osbuild.GenImageKernelOptions(pt)
otkPart := &Output{
Expand All @@ -190,6 +195,7 @@ func genPartitionTable(genPartInput *Input, rng *rand.Rand) (*Output, error) {
},
KernelOptsList: kernelOptions,
PartitionMap: makePartMap(pt),
Filename: fname,
},
}

Expand Down
62 changes: 60 additions & 2 deletions cmd/otk-gen-partition-table/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestUnmarshalOutput(t *testing.T) {
UUID: "12345",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 911,
Expand Down Expand Up @@ -157,7 +158,8 @@ func TestUnmarshalOutput(t *testing.T) {
"ExtraPadding": 0,
"StartOffset": 0
}
}
},
"filename": "disk.img"
}
}`
output, err := json.MarshalIndent(fakeOtkOutput, "", " ")
Expand Down Expand Up @@ -270,7 +272,8 @@ var expectedSimplePartOutput = `{
"ExtraPadding": 0,
"StartOffset": 0
}
}
},
"filename": "disk.img"
}
}
`
Expand Down Expand Up @@ -309,6 +312,7 @@ func TestGenPartitionTableMinimal(t *testing.T) {
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
Expand Down Expand Up @@ -368,6 +372,7 @@ func TestGenPartitionTableCustomizationExtraMp(t *testing.T) {
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 15890120704,
Expand Down Expand Up @@ -456,6 +461,7 @@ func TestGenPartitionTableCustomizationExtraMpPlusModificationPartitionMode(t *t
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 13739491328,
Expand Down Expand Up @@ -513,6 +519,7 @@ func TestGenPartitionTablePropertiesDefaultSize(t *testing.T) {
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 16106127360,
Expand Down Expand Up @@ -563,6 +570,7 @@ func TestGenPartitionTableModificationMinDiskSize(t *testing.T) {
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 21474836480,
Expand All @@ -587,3 +595,53 @@ func TestGenPartitionTableModificationMinDiskSize(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedOutput, output)
}

func TestGenPartitionTableModificationFilename(t *testing.T) {
inp := &genpart.Input{
Properties: genpart.InputProperties{
Type: "dos",
},
Partitions: []*genpart.InputPartition{
{
Mountpoint: "/",
Size: "10 GiB",
Type: "ext4",
},
},
Modifications: genpart.InputModifications{
Filename: "custom-disk.img",
},
}
expectedOutput := &otkpart.PartitionTable{
Const: otkpart.Const{
KernelOptsList: []string{},
PartitionMap: map[string]otkpart.Partition{
"root": {
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
},
},
Filename: "custom-disk.img",
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Partitions: []disk.Partition{
{
Start: 1048576,
Size: 10737418240,
Payload: &disk.Filesystem{
Type: "ext4",
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
Mountpoint: "/",
},
},
},
},
},
},
}
output, err := genpart.GenPartitionTable(inp, rand.New(rand.NewSource(0)))

Check failure on line 644 in cmd/otk-gen-partition-table/main_test.go

View workflow job for this annotation

GitHub Actions / ⌨ Lint

G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
assert.NoError(t, err)
assert.Equal(t, expectedOutput, output)
}
9 changes: 2 additions & 7 deletions cmd/otk-make-partition-mounts-devices/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import (
"github.com/osbuild/images/pkg/osbuild"
)

type Input struct {
otkpart.PartitionTable

// XXX: move filename into gen-part-stages
Filename string `json:"filename"`
}
type Input = otkpart.PartitionTable

type Output struct {
RootMountName string `json:"root_mount_name"`
Expand All @@ -29,7 +24,7 @@ func run(r io.Reader, w io.Writer) error {
return err
}

rootMntName, mounts, devices, err := osbuild.GenMountsDevicesFromPT(inp.Filename, inp.PartitionTable.Const.Internal.PartitionTable)
rootMntName, mounts, devices, err := osbuild.GenMountsDevicesFromPT(inp.Const.Filename, inp.Const.Internal.PartitionTable)
if err != nil {
return err
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/otk-make-partition-mounts-devices/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ const expectedOutput = `{
func TestIntegration(t *testing.T) {
pt := testdisk.MakeFakePartitionTable("/", "/boot", "/boot/efi")
input := mkdevmnt.Input{
Filename: "test.disk",
PartitionTable: otkpart.PartitionTable{
Const: otkpart.Const{
Internal: otkpart.Internal{
PartitionTable: pt,
},
Const: otkpart.Const{
Filename: "test.disk",
Internal: otkpart.Internal{
PartitionTable: pt,
},
},
}
Expand Down
19 changes: 3 additions & 16 deletions cmd/otk-make-partition-stages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ import (
"github.com/osbuild/images/internal/otkpart"
)

type Input struct {
otkpart.PartitionTable

// XXX: move filename into gen-part-stages
Modifications InputModifications `json:"modifications"`
}

type InputModifications struct {
Filename string `json:"filename"`
}
type Input = otkpart.PartitionTable

func makeImagePrepareStages(inp Input, filename string) (stages []*osbuild.Stage, err error) {
defer func() {
Expand All @@ -32,7 +23,7 @@ func makeImagePrepareStages(inp Input, filename string) (stages []*osbuild.Stage
// rhel7 uses PTSgdisk, if we ever need to support this, make this
// configurable
partTool := osbuild.PTSfdisk
stages = osbuild.GenImagePrepareStages(inp.PartitionTable.Const.Internal.PartitionTable, filename, partTool)
stages = osbuild.GenImagePrepareStages(inp.Const.Internal.PartitionTable, inp.Const.Filename, partTool)
return stages, nil
}

Expand All @@ -42,11 +33,7 @@ func run(r io.Reader, w io.Writer) error {
return err
}

fname := "disk.img"
if inp.Modifications.Filename != "" {
fname = inp.Modifications.Filename
}
stages, err := makeImagePrepareStages(inp, fname)
stages, err := makeImagePrepareStages(inp, inp.Const.Filename)
if err != nil {
return fmt.Errorf("cannot make partition stages: %w", err)
}
Expand Down
33 changes: 16 additions & 17 deletions cmd/otk-make-partition-stages/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ import (
// disk.PartitionTable so encoding it in json here will not add
// a benefit for the test
var minimalInputBase = makestages.Input{
PartitionTable: otkpart.PartitionTable{
Const: otkpart.Const{
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Partitions: []disk.Partition{
{
Start: 1048576,
Size: 10737418240,
Payload: &disk.Filesystem{
Type: "ext4",
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
Mountpoint: "/",
},
Const: otkpart.Const{
Internal: otkpart.Internal{
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Partitions: []disk.Partition{
{
Start: 1048576,
Size: 10737418240,
Payload: &disk.Filesystem{
Type: "ext4",
UUID: "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
Mountpoint: "/",
},
},
},
Expand Down Expand Up @@ -94,6 +92,7 @@ var minimalExpectedStages = `[

func TestIntegration(t *testing.T) {
minimalInput := minimalInputBase
minimalInput.Const.Filename = "disk.img"
expectedStages := minimalExpectedStages

inpJSON, err := json.Marshal(&minimalInput)
Expand All @@ -109,7 +108,7 @@ func TestIntegration(t *testing.T) {

func TestModificationFname(t *testing.T) {
input := minimalInputBase
input.Modifications.Filename = "mydisk.img2"
input.Const.Filename = "mydisk.img2"
expectedStages := strings.Replace(minimalExpectedStages, `"disk.img"`, `"mydisk.img2"`, -1)

inpJSON, err := json.Marshal(&input)
Expand Down
3 changes: 3 additions & 0 deletions internal/otkpart/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Const struct {
// can write, e.g. "filesystem.partition_map.boot.uuid"
PartitionMap map[string]Partition `json:"partition_map"`
Internal Internal `json:"internal"`

// XXX: or diskname?
Filename string `json:"filename"`
}

// "exported" view of partitions, this is an API so only add things here
Expand Down

0 comments on commit b048f89

Please sign in to comment.