Skip to content

Commit 2654d8c

Browse files
committed
many: use new disk.PartitionTableType instead of string
This commit makes use of the new `disk.PartitionTableType instead of just using an untyped string (thanks to Achilleas for adding the new type and to Tomáš for suggesting to use it more).
1 parent fb51dd3 commit 2654d8c

25 files changed

+131
-128
lines changed

Diff for: internal/testdisk/partition.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func MakeFakePartitionTable(mntPoints ...string) *disk.PartitionTable {
3232

3333
}
3434
return &disk.PartitionTable{
35-
Type: "gpt",
35+
Type: disk.PT_GPT,
3636
Partitions: partitions,
3737
}
3838
}
@@ -42,7 +42,7 @@ func MakeFakePartitionTable(mntPoints ...string) *disk.PartitionTable {
4242
func MakeFakeBtrfsPartitionTable(mntPoints ...string) *disk.PartitionTable {
4343
var subvolumes []disk.BtrfsSubvolume
4444
pt := &disk.PartitionTable{
45-
Type: "gpt",
45+
Type: disk.PT_GPT,
4646
Size: 10 * datasizes.GiB,
4747
Partitions: []disk.Partition{},
4848
}
@@ -107,7 +107,7 @@ func MakeFakeBtrfsPartitionTable(mntPoints ...string) *disk.PartitionTable {
107107
func MakeFakeLVMPartitionTable(mntPoints ...string) *disk.PartitionTable {
108108
var lvs []disk.LVMLogicalVolume
109109
pt := &disk.PartitionTable{
110-
Type: "gpt",
110+
Type: disk.PT_GPT,
111111
Size: 10 * datasizes.GiB,
112112
Partitions: []disk.Partition{},
113113
}

Diff for: pkg/disk/disk_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestDynamicallyResizePartitionTable(t *testing.T) {
5252
}
5353
pt := disk.PartitionTable{
5454
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
55-
Type: "gpt",
55+
Type: disk.PT_GPT,
5656
Partitions: []disk.Partition{
5757
{
5858
Size: 2048,

Diff for: pkg/disk/partition_table.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
)
1313

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

2020
SectorSize uint64 // Sector size in bytes
@@ -243,7 +243,7 @@ func (pt *PartitionTable) GenerateUUIDs(rng *rand.Rand) {
243243

244244
// if this is a MBR partition table, there is no need to generate
245245
// uuids for the partitions themselves
246-
if pt.Type != "gpt" {
246+
if pt.Type != PT_GPT {
247247
return
248248
}
249249

@@ -339,7 +339,7 @@ func (pt *PartitionTable) CreateMountpoint(mountpoint string, size uint64) (Enti
339339
n := len(pt.Partitions)
340340
var maxNo int
341341

342-
if pt.Type == "gpt" {
342+
if pt.Type == PT_GPT {
343343
switch mountpoint {
344344
case "/boot":
345345
partition.Type = XBootLDRPartitionGUID
@@ -398,7 +398,7 @@ func (pt *PartitionTable) HeaderSize() uint64 {
398398
// this also ensure we have enough space for the MBR
399399
header := pt.SectorsToBytes(1)
400400

401-
if pt.Type == "dos" {
401+
if pt.Type == PT_DOS {
402402
return header
403403
}
404404

@@ -454,7 +454,7 @@ func (pt *PartitionTable) relayout(size uint64) uint64 {
454454
footer := uint64(0)
455455

456456
// The GPT header is also at the end of the partition table
457-
if pt.Type == "gpt" {
457+
if pt.Type == PT_GPT {
458458
footer = header
459459
}
460460

@@ -726,7 +726,7 @@ func (pt *PartitionTable) ensureLVM() error {
726726
// reset the vg partition size - it will be grown later
727727
part.Size = 0
728728

729-
if pt.Type == "gpt" {
729+
if pt.Type == PT_GPT {
730730
part.Type = LVMPartitionGUID
731731
} else {
732732
part.Type = "8e"
@@ -792,7 +792,7 @@ func (pt *PartitionTable) ensureBtrfs() error {
792792
// reset the btrfs partition size - it will be grown later
793793
part.Size = 0
794794

795-
if pt.Type == "gpt" {
795+
if pt.Type == PT_GPT {
796796
part.Type = FilesystemDataGUID
797797
} else {
798798
part.Type = DosLinuxTypeID
@@ -980,9 +980,9 @@ func EnsureRootFilesystem(pt *PartitionTable, defaultFsType FSType) error {
980980

981981
var partType string
982982
switch pt.Type {
983-
case "dos":
983+
case PT_DOS:
984984
partType = DosLinuxTypeID
985-
case "gpt":
985+
case PT_GPT:
986986
partType = FilesystemDataGUID
987987
default:
988988
return fmt.Errorf("error creating root partition: unknown or unsupported partition table type: %s", pt.Type)
@@ -1034,9 +1034,9 @@ func EnsureBootPartition(pt *PartitionTable, bootFsType FSType) error {
10341034

10351035
var partType string
10361036
switch pt.Type {
1037-
case "dos":
1037+
case PT_DOS:
10381038
partType = DosLinuxTypeID
1039-
case "gpt":
1039+
case PT_GPT:
10401040
partType = XBootLDRPartitionGUID
10411041
default:
10421042
return fmt.Errorf("error creating boot partition: unknown or unsupported partition table type: %s", pt.Type)

Diff for: pkg/disk/partition_table_internal_test.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
var TestPartitionTables = map[string]PartitionTable{
1818
"plain": {
1919
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
20-
Type: "gpt",
20+
Type: PT_GPT,
2121
Partitions: []Partition{
2222
{
2323
Size: 1 * MiB,
@@ -69,7 +69,7 @@ var TestPartitionTables = map[string]PartitionTable{
6969

7070
"plain-noboot": {
7171
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
72-
Type: "gpt",
72+
Type: PT_GPT,
7373
Partitions: []Partition{
7474
{
7575
Size: 1 * MiB,
@@ -108,7 +108,7 @@ var TestPartitionTables = map[string]PartitionTable{
108108

109109
"luks": {
110110
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
111-
Type: "gpt",
111+
Type: PT_GPT,
112112
Partitions: []Partition{
113113
{
114114
Size: 1 * MiB,
@@ -163,7 +163,7 @@ var TestPartitionTables = map[string]PartitionTable{
163163
},
164164
"luks+lvm": {
165165
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
166-
Type: "gpt",
166+
Type: PT_GPT,
167167
Partitions: []Partition{
168168
{
169169
Size: 1 * MiB,
@@ -238,7 +238,7 @@ var TestPartitionTables = map[string]PartitionTable{
238238
},
239239
"btrfs": {
240240
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
241-
Type: "gpt",
241+
Type: PT_GPT,
242242
Partitions: []Partition{
243243
{
244244
Size: 1 * MiB,
@@ -544,7 +544,7 @@ func TestRelayout(t *testing.T) {
544544
testCases := map[string]testCase{
545545
"simple-dos": {
546546
pt: &PartitionTable{
547-
Type: "dos",
547+
Type: PT_DOS,
548548
Size: 100 * MiB,
549549
Partitions: []Partition{
550550
{
@@ -560,7 +560,7 @@ func TestRelayout(t *testing.T) {
560560
},
561561
size: 100 * MiB,
562562
expected: &PartitionTable{
563-
Type: "dos",
563+
Type: PT_DOS,
564564
Size: 100 * MiB,
565565
Partitions: []Partition{
566566
{
@@ -579,7 +579,7 @@ func TestRelayout(t *testing.T) {
579579
},
580580
"simple-gpt": {
581581
pt: &PartitionTable{
582-
Type: "gpt",
582+
Type: PT_GPT,
583583
Size: 100 * MiB,
584584
Partitions: []Partition{
585585
{
@@ -595,7 +595,7 @@ func TestRelayout(t *testing.T) {
595595
},
596596
size: 100 * MiB,
597597
expected: &PartitionTable{
598-
Type: "gpt",
598+
Type: PT_GPT,
599599
Size: 100 * MiB,
600600
Partitions: []Partition{
601601
{
@@ -614,7 +614,7 @@ func TestRelayout(t *testing.T) {
614614
},
615615
"simple-gpt-root-first": {
616616
pt: &PartitionTable{
617-
Type: "gpt",
617+
Type: PT_GPT,
618618
Size: 100 * MiB,
619619
Partitions: []Partition{
620620
{
@@ -633,7 +633,7 @@ func TestRelayout(t *testing.T) {
633633
},
634634
size: 100 * MiB,
635635
expected: &PartitionTable{
636-
Type: "gpt",
636+
Type: PT_GPT,
637637
Size: 100 * MiB,
638638
Partitions: []Partition{
639639
{
@@ -656,7 +656,7 @@ func TestRelayout(t *testing.T) {
656656
},
657657
"lvm-dos": {
658658
pt: &PartitionTable{
659-
Type: "dos",
659+
Type: PT_DOS,
660660
Size: 100 * MiB,
661661
Partitions: []Partition{
662662
{
@@ -678,7 +678,7 @@ func TestRelayout(t *testing.T) {
678678
},
679679
size: 100 * MiB,
680680
expected: &PartitionTable{
681-
Type: "dos",
681+
Type: PT_DOS,
682682
Size: 100 * MiB,
683683
Partitions: []Partition{
684684
{
@@ -703,7 +703,7 @@ func TestRelayout(t *testing.T) {
703703
},
704704
"lvm-gpt": {
705705
pt: &PartitionTable{
706-
Type: "gpt",
706+
Type: PT_GPT,
707707
Size: 100 * MiB,
708708
Partitions: []Partition{
709709
{
@@ -726,7 +726,7 @@ func TestRelayout(t *testing.T) {
726726
},
727727
size: 100 * MiB,
728728
expected: &PartitionTable{
729-
Type: "gpt",
729+
Type: PT_GPT,
730730
Size: 100 * MiB,
731731
Partitions: []Partition{
732732
{
@@ -752,7 +752,7 @@ func TestRelayout(t *testing.T) {
752752
},
753753
"lvm-gpt-multilv": {
754754
pt: &PartitionTable{
755-
Type: "gpt",
755+
Type: PT_GPT,
756756
Size: 100 * MiB,
757757
Partitions: []Partition{
758758
{
@@ -778,7 +778,7 @@ func TestRelayout(t *testing.T) {
778778
},
779779
size: 100 * MiB,
780780
expected: &PartitionTable{
781-
Type: "gpt",
781+
Type: PT_GPT,
782782
Size: 100 * MiB,
783783
Partitions: []Partition{
784784
{
@@ -807,7 +807,7 @@ func TestRelayout(t *testing.T) {
807807
},
808808
"btrfs": {
809809
pt: &PartitionTable{
810-
Type: "gpt",
810+
Type: PT_GPT,
811811
Size: 100 * MiB,
812812
Partitions: []Partition{
813813
{
@@ -831,7 +831,7 @@ func TestRelayout(t *testing.T) {
831831
},
832832
size: 100 * MiB,
833833
expected: &PartitionTable{
834-
Type: "gpt",
834+
Type: PT_GPT,
835835
Size: 100 * MiB,
836836
Partitions: []Partition{
837837
{
@@ -858,7 +858,7 @@ func TestRelayout(t *testing.T) {
858858
},
859859
"simple-dos-grow-pt": {
860860
pt: &PartitionTable{
861-
Type: "dos",
861+
Type: PT_DOS,
862862
Size: 100 * MiB,
863863
Partitions: []Partition{
864864
{
@@ -874,7 +874,7 @@ func TestRelayout(t *testing.T) {
874874
},
875875
size: 100 * MiB,
876876
expected: &PartitionTable{
877-
Type: "dos",
877+
Type: PT_DOS,
878878
Size: 211 * MiB, // grows to fit partitions and header
879879
Partitions: []Partition{
880880
{
@@ -893,7 +893,7 @@ func TestRelayout(t *testing.T) {
893893
},
894894
"simple-gpt-growpt": {
895895
pt: &PartitionTable{
896-
Type: "gpt",
896+
Type: PT_GPT,
897897
Size: 100 * MiB,
898898
Partitions: []Partition{
899899
{
@@ -909,7 +909,7 @@ func TestRelayout(t *testing.T) {
909909
},
910910
size: 42 * MiB,
911911
expected: &PartitionTable{
912-
Type: "gpt",
912+
Type: PT_GPT,
913913
Size: 512 * MiB, // grows to fit partitions, header, and footer
914914
Partitions: []Partition{
915915
{
@@ -928,7 +928,7 @@ func TestRelayout(t *testing.T) {
928928
},
929929
"lvm-gpt-grow": {
930930
pt: &PartitionTable{
931-
Type: "gpt",
931+
Type: PT_GPT,
932932
Size: 10 * MiB,
933933
Partitions: []Partition{
934934
{
@@ -954,7 +954,7 @@ func TestRelayout(t *testing.T) {
954954
},
955955
size: 100 * MiB,
956956
expected: &PartitionTable{
957-
Type: "gpt",
957+
Type: PT_GPT,
958958
Size: 702 * MiB,
959959
Partitions: []Partition{
960960
{
@@ -983,7 +983,7 @@ func TestRelayout(t *testing.T) {
983983
},
984984
"lvm-dos-grow-rootvg": {
985985
pt: &PartitionTable{
986-
Type: "dos",
986+
Type: PT_DOS,
987987
Size: 10 * MiB, // PT is smaller than the sum of Partitions
988988
Partitions: []Partition{
989989
{
@@ -1009,7 +1009,7 @@ func TestRelayout(t *testing.T) {
10091009
},
10101010
size: 99 * MiB,
10111011
expected: &PartitionTable{
1012-
Type: "dos",
1012+
Type: PT_DOS,
10131013
Size: 325 * MiB,
10141014
Partitions: []Partition{
10151015
{
@@ -1038,7 +1038,7 @@ func TestRelayout(t *testing.T) {
10381038
},
10391039
"lvm-gpt-grow-rootvg": {
10401040
pt: &PartitionTable{
1041-
Type: "gpt",
1041+
Type: PT_GPT,
10421042
Size: 10 * MiB,
10431043
Partitions: []Partition{
10441044
{
@@ -1064,7 +1064,7 @@ func TestRelayout(t *testing.T) {
10641064
},
10651065
size: 99 * MiB,
10661066
expected: &PartitionTable{
1067-
Type: "gpt",
1067+
Type: PT_GPT,
10681068
Size: 326 * MiB,
10691069
Partitions: []Partition{
10701070
{

0 commit comments

Comments
 (0)