Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otk-gen-partiton-table: generate strictly internal representation for now #2

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions cmd/partition-table/main.go → cmd/otk-gen-partition-table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/osbuild/images/internal/cmdutil"
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/disk"
"github.com/osbuild/images/pkg/osbuild"
)

var basePt = disk.PartitionTable{
Expand Down Expand Up @@ -65,46 +66,52 @@ var basePt = disk.PartitionTable{
},
}

// Serializable version of the partition table that is used for formatting the
// output of this command.
type partitionTable struct {
Size uint64 `json:"size"`
UUID string `json:"uuid"`
Type string `json:"type"`
SectorSize uint64 `json:"sector_size"`

Partitions []partition
type otkGenPartitionInput struct {
TotalSize uint64 `json:"total_size"`
}

type partition struct {
Start uint64 `json:"start"`
Size uint64 `json:"size"`
Type string `json:"type"`
Bootable bool `json:"bootable"`

UUID string `json:"uuid"`
type otkGenPartitionsOutput struct {
PartitionTable *disk.PartitionTable `json:"internal-partition-table"`
KernelOptsList []string `json:"kernel_opts_list"`
}

func main() {
func run() error {
var genPartInput otkGenPartitionInput
if err := json.NewDecoder(os.Stdin).Decode(&genPartInput); err != nil {
return err
}

rngSeed, err := cmdutil.SeedArgFor(&buildconfig.BuildConfig{}, "", "", "")
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
return err
}
source := rand.NewSource(rngSeed)
// math/rand is good enough in this case
/* #nosec G404 */
rng := rand.New(source)

pt, err := disk.NewPartitionTable(&basePt, nil, 0, disk.DefaultPartitioningMode, nil, rng)
pt, err := disk.NewPartitionTable(&basePt, nil, genPartInput.TotalSize, disk.DefaultPartitioningMode, nil, rng)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
return err
}

ptJson, err := json.Marshal(pt)
kernelOptions := osbuild.GenImageKernelOptions(pt)
otkPart := otkGenPartitionsOutput{
PartitionTable: pt,
KernelOptsList: kernelOptions,
}
ptJson, err := json.Marshal(otkPart)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to martial partition table: %s\n", err.Error())
return fmt.Errorf("failed to martial partition table: %w\n", err)
}

fmt.Printf("%s\n", ptJson)
return nil
}

func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err.Error())
os.Exit(1)
}
fmt.Println(string(ptJson))
}