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

many: use new disk.PartitionTableType instead of string #1025

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/osbuild-playground/partitiontables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/osbuild/images/pkg/disk"

var basePT = disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Type: disk.PT_GPT,
Partitions: []disk.Partition{
{
Size: 1048576, // 1MB
Expand Down
6 changes: 5 additions & 1 deletion cmd/otk/osbuild-gen-partition-table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ func makePartitionTableFromOtkInput(input *Input) (*disk.PartitionTable, error)
}
}

partType, err := disk.NewPartitionTableType(string(input.Properties.Type))
if err != nil {
return nil, err
}
pt := &disk.PartitionTable{
UUID: input.Properties.UUID,
Type: string(input.Properties.Type),
Type: partType,
SectorSize: input.Properties.SectorSize,
StartOffset: startOffset,
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/otk/osbuild-gen-partition-table/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestGenPartitionTableIntegrationPPC(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 10742661120,
UUID: "0x14fc63d2",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Bootable: true,
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestGenPartitionTableMinimal(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestGenPartitionTableCustomizationExtraMp(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 15893266432,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down Expand Up @@ -572,7 +572,7 @@ func TestGenPartitionTableCustomizationExtraMpPlusModificationPartitionMode(t *t
PartitionTable: &disk.PartitionTable{
Size: 13739491328,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 3002073088,
Expand Down Expand Up @@ -630,7 +630,7 @@ func TestGenPartitionTablePropertiesDefaultSize(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 16106127360,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down Expand Up @@ -681,7 +681,7 @@ func TestGenPartitionTableModificationMinDiskSize(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 21474836480,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestGenPartitionTableModificationFilename(t *testing.T) {
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestGenPartitionCreateESPDos(t *testing.T) {
Internal: otkdisk.Internal{
PartitionTable: &disk.PartitionTable{
Size: 12885950464,
Type: "dos",
Type: disk.PT_DOS,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Partitions: []disk.Partition{
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/otk/osbuild-make-grub2-inst-stage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var fakePt = &disk.PartitionTable{
Type: "gpt",
Type: disk.PT_GPT,
Partitions: []disk.Partition{
{
Size: 1 * datasizes.MiB,
Expand Down
2 changes: 1 addition & 1 deletion cmd/otk/osbuild-make-partition-stages/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var minimalInputBase = makestages.Input{
PartitionTable: &disk.PartitionTable{
Size: 10738466816,
UUID: "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
Type: "dos",
Type: disk.PT_DOS,
Partitions: []disk.Partition{
{
Start: 1048576,
Expand Down
6 changes: 3 additions & 3 deletions internal/testdisk/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func MakeFakePartitionTable(mntPoints ...string) *disk.PartitionTable {

}
return &disk.PartitionTable{
Type: "gpt",
Type: disk.PT_GPT,
Partitions: partitions,
}
}
Expand All @@ -42,7 +42,7 @@ func MakeFakePartitionTable(mntPoints ...string) *disk.PartitionTable {
func MakeFakeBtrfsPartitionTable(mntPoints ...string) *disk.PartitionTable {
var subvolumes []disk.BtrfsSubvolume
pt := &disk.PartitionTable{
Type: "gpt",
Type: disk.PT_GPT,
Size: 10 * datasizes.GiB,
Partitions: []disk.Partition{},
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func MakeFakeBtrfsPartitionTable(mntPoints ...string) *disk.PartitionTable {
func MakeFakeLVMPartitionTable(mntPoints ...string) *disk.PartitionTable {
var lvs []disk.LVMLogicalVolume
pt := &disk.PartitionTable{
Type: "gpt",
Type: disk.PT_GPT,
Size: 10 * datasizes.GiB,
Partitions: []disk.Partition{},
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package disk

import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -141,6 +142,24 @@ func (t PartitionTableType) String() string {
}
}

func (t PartitionTableType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}

func (t *PartitionTableType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}

new, err := NewPartitionTableType(s)
if err != nil {
return err
}
*t = new
return nil
}

func NewPartitionTableType(s string) (PartitionTableType, error) {
switch s {
case "":
Expand Down
2 changes: 1 addition & 1 deletion pkg/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDynamicallyResizePartitionTable(t *testing.T) {
}
pt := disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Type: disk.PT_GPT,
Partitions: []disk.Partition{
{
Size: 2048,
Expand Down
37 changes: 29 additions & 8 deletions pkg/disk/enums_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package disk_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/osbuild/images/pkg/disk"
"github.com/stretchr/testify/assert"

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

func TestEnumPartitionTableType(t *testing.T) {
enumMap := map[string]disk.PartitionTableType{
"": disk.PT_NONE,
"dos": disk.PT_DOS,
"gpt": disk.PT_GPT,
}
var partitionTypeEnumMap = map[string]disk.PartitionTableType{
"": disk.PT_NONE,
"dos": disk.PT_DOS,
"gpt": disk.PT_GPT,
}

func TestEnumPartitionTableType(t *testing.T) {
assert := assert.New(t)
for name, num := range enumMap {
for name, num := range partitionTypeEnumMap {
ptt, err := disk.NewPartitionTableType(name)
expected := disk.PartitionTableType(num)

Expand All @@ -34,6 +37,24 @@ func TestEnumPartitionTableType(t *testing.T) {
assert.EqualError(err, "unknown or unsupported partition table type name: not-a-type")
}

func TestEnumPartitionTableTypeJSON(t *testing.T) {
for name, num := range partitionTypeEnumMap {
jsData, err := json.Marshal(num)
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf(`"%s"`, name), string(jsData))

var unmarshaledNum disk.PartitionTableType
err = json.Unmarshal(jsData, &unmarshaledNum)
assert.NoError(t, err)
assert.Equal(t, unmarshaledNum, num)
}

// bad unmarshal
var unmarshaledNum disk.PartitionTableType
err := json.Unmarshal([]byte(`"bad"`), &unmarshaledNum)
assert.EqualError(t, err, `unknown or unsupported partition table type name: bad`)
}

func TestEnumFSType(t *testing.T) {
enumMap := map[string]disk.FSType{
"": disk.FS_NONE,
Expand Down
42 changes: 21 additions & 21 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

type PartitionTable struct {
Size uint64 // Size of the disk (in bytes).
UUID string // Unique identifier of the partition table (GPT only).
Type string // Partition table type, e.g. dos, gpt.
Size uint64 // Size of the disk (in bytes).
UUID string // Unique identifier of the partition table (GPT only).
Type PartitionTableType // Partition table type, e.g. dos, gpt.
Partitions []Partition

SectorSize uint64 // Sector size in bytes
Expand Down Expand Up @@ -244,7 +244,7 @@ func (pt *PartitionTable) GenerateUUIDs(rng *rand.Rand) {

// if this is a MBR partition table, there is no need to generate
// uuids for the partitions themselves
if pt.Type != "gpt" {
if pt.Type != PT_GPT {
return
}

Expand Down Expand Up @@ -340,7 +340,7 @@ func (pt *PartitionTable) CreateMountpoint(mountpoint string, size uint64) (Enti
n := len(pt.Partitions)
var maxNo int

if pt.Type == "gpt" {
if pt.Type == PT_GPT {
switch mountpoint {
case "/boot":
partition.Type = XBootLDRPartitionGUID
Expand Down Expand Up @@ -399,7 +399,7 @@ func (pt *PartitionTable) HeaderSize() uint64 {
// this also ensure we have enough space for the MBR
header := pt.SectorsToBytes(1)

if pt.Type == "dos" {
if pt.Type == PT_DOS {
return header
}

Expand Down Expand Up @@ -455,7 +455,7 @@ func (pt *PartitionTable) relayout(size uint64) uint64 {
footer := uint64(0)

// The GPT header is also at the end of the partition table
if pt.Type == "gpt" {
if pt.Type == PT_GPT {
footer = header
}

Expand Down Expand Up @@ -727,7 +727,7 @@ func (pt *PartitionTable) ensureLVM() error {
// reset the vg partition size - it will be grown later
part.Size = 0

if pt.Type == "gpt" {
if pt.Type == PT_GPT {
part.Type = LVMPartitionGUID
} else {
part.Type = "8e"
Expand Down Expand Up @@ -793,7 +793,7 @@ func (pt *PartitionTable) ensureBtrfs() error {
// reset the btrfs partition size - it will be grown later
part.Size = 0

if pt.Type == "gpt" {
if pt.Type == PT_GPT {
part.Type = FilesystemDataGUID
} else {
part.Type = DosLinuxTypeID
Expand Down Expand Up @@ -981,9 +981,9 @@ func EnsureRootFilesystem(pt *PartitionTable, defaultFsType FSType) error {

var partType string
switch pt.Type {
case "dos":
case PT_DOS:
partType = DosLinuxTypeID
case "gpt":
case PT_GPT:
partType = FilesystemDataGUID
default:
return fmt.Errorf("error creating root partition: unknown or unsupported partition table type: %s", pt.Type)
Expand Down Expand Up @@ -1035,9 +1035,9 @@ func EnsureBootPartition(pt *PartitionTable, bootFsType FSType) error {

var partType string
switch pt.Type {
case "dos":
case PT_DOS:
partType = DosLinuxTypeID
case "gpt":
case PT_GPT:
partType = XBootLDRPartitionGUID
default:
return fmt.Errorf("error creating boot partition: unknown or unsupported partition table type: %s", pt.Type)
Expand Down Expand Up @@ -1101,15 +1101,15 @@ func AddPartitionsForBootMode(pt *PartitionTable, bootMode platform.BootMode) er
}
}

func mkBIOSBoot(ptType string) (Partition, error) {
func mkBIOSBoot(ptType PartitionTableType) (Partition, error) {
var partType string
switch ptType {
case "dos":
case PT_DOS:
partType = DosBIOSBootID
case "gpt":
case PT_GPT:
partType = BIOSBootPartitionGUID
default:
return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table type: %s", ptType)
return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table enum: %d", ptType)
}
return Partition{
Size: 1 * datasizes.MiB,
Expand All @@ -1119,15 +1119,15 @@ func mkBIOSBoot(ptType string) (Partition, error) {
}, nil
}

func mkESP(size uint64, ptType string) (Partition, error) {
func mkESP(size uint64, ptType PartitionTableType) (Partition, error) {
var partType string
switch ptType {
case "dos":
case PT_DOS:
partType = DosESPID
case "gpt":
case PT_GPT:
partType = EFISystemPartitionGUID
default:
return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table type: %s", ptType)
return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table enum: %d", ptType)
}
return Partition{
Size: size,
Expand Down
Loading
Loading