Skip to content

Commit

Permalink
osbuild: validate sfdisk stage options
Browse files Browse the repository at this point in the history
DOS partition tables only support up to 4 partitions.  Don't create the
sfdisk stage options if this rule is violated.  Stage validation
failures panic, so we panic on the stage creation.  This might change in
the future (to return errors) but for now we use panics and put the
responsibility on the callers to validate their inputs if they want to
have better error control.

Test sfdisk stage validation failure
  • Loading branch information
achilleas-k authored and supakeen committed Dec 10, 2024
1 parent 855d6f3 commit 8e2b2b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/osbuild/sfdisk_stage.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package osbuild

import (
"fmt"

"github.com/osbuild/images/pkg/disk"
)

// Partition a target using sfdisk(8)

type SfdiskStageOptions struct {
Expand Down Expand Up @@ -36,7 +42,18 @@ type SfdiskPartition struct {
UUID string `json:"uuid,omitempty"`
}

func (o SfdiskStageOptions) validate() error {
if o.Label == disk.PT_DOS.String() && len(o.Partitions) > 4 {
return fmt.Errorf("sfdisk stage creation failed: \"dos\" partition table only supports up to 4 partitions: got %d", len(o.Partitions))
}
return nil
}

func NewSfdiskStage(options *SfdiskStageOptions, device *Device) *Stage {
if err := options.validate(); err != nil {
panic(err)
}

return &Stage{
Type: "org.osbuild.sfdisk",
Options: options,
Expand Down
19 changes: 19 additions & 0 deletions pkg/osbuild/sfdisk_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ func TestNewSfdiskStage(t *testing.T) {
actualStageDOS := NewSfdiskStage(&options, device)
assert.Equal(t, expectedStage, actualStageDOS)
}

func TestNewSfdiskStageInvalid(t *testing.T) {

partition := SfdiskPartition{
// doesn't really matter
}

options := SfdiskStageOptions{
Label: "dos",
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Partitions: []SfdiskPartition{partition, partition, partition, partition, partition}, // 5 partitions
}

device := NewLoopbackDevice(&LoopbackDeviceOptions{Filename: "disk.raw"})

assert.Panics(t, func() {
NewSfdiskStage(&options, device)
})
}

0 comments on commit 8e2b2b9

Please sign in to comment.