-
Notifications
You must be signed in to change notification settings - Fork 458
/
partition.go
121 lines (101 loc) · 3.08 KB
/
partition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package partition provides common utils for system partition format.
package partition
import (
"github.com/dustin/go-humanize"
"github.com/siderolabs/go-blockdevice/blockdevice/partition/gpt"
"github.com/siderolabs/talos/pkg/machinery/constants"
)
// Options contains the options for creating a partition.
type Options struct {
PartitionLabel string
PartitionType Type
Size uint64
LegacyBIOSBootable bool
}
// NewPartitionOptions returns a new PartitionOptions.
func NewPartitionOptions(label string, uki bool) *Options {
return systemPartitionsPartitonOptions(label, uki)
}
// Locate existing partition on the disk by label.
func Locate(pt *gpt.GPT, label string) (*gpt.Partition, error) {
for _, part := range pt.Partitions().Items() {
if part.Name == label {
return part, nil
}
}
return nil, nil
}
// Partition creates a new partition on the specified device.
// Returns the path to the newly created partition.
func Partition(pt *gpt.GPT, pos int, device string, partitionOpts Options, printf func(string, ...any)) (string, error) {
printf("partitioning %s - %s %q\n", device, partitionOpts.PartitionLabel, humanize.Bytes(partitionOpts.Size))
opts := []gpt.PartitionOption{
gpt.WithPartitionType(partitionOpts.PartitionType),
gpt.WithPartitionName(partitionOpts.PartitionLabel),
}
if partitionOpts.Size == 0 {
opts = append(opts, gpt.WithMaximumSize(true))
}
if partitionOpts.LegacyBIOSBootable {
opts = append(opts, gpt.WithLegacyBIOSBootableAttribute(true))
}
part, err := pt.InsertAt(pos, partitionOpts.Size, opts...)
if err != nil {
return "", err
}
partitionName, err := part.Path()
if err != nil {
return "", err
}
printf("created %s (%s) size %d blocks", partitionName, partitionOpts.PartitionLabel, part.Length())
return partitionName, nil
}
func systemPartitionsPartitonOptions(label string, uki bool) *Options {
switch label {
case constants.EFIPartitionLabel:
partitionOptions := &Options{
PartitionType: EFISystemPartition,
Size: EFISize,
}
if uki {
partitionOptions.Size = EFIUKISize
}
return partitionOptions
case constants.BIOSGrubPartitionLabel:
if uki {
panic("BIOS partition is not supported with UKI")
}
return &Options{
PartitionType: BIOSBootPartition,
Size: BIOSGrubSize,
}
case constants.BootPartitionLabel:
if uki {
panic("BOOT partition is not supported with UKI")
}
return &Options{
PartitionType: LinuxFilesystemData,
Size: BootSize,
}
case constants.MetaPartitionLabel:
return &Options{
PartitionType: LinuxFilesystemData,
Size: MetaSize,
}
case constants.StatePartitionLabel:
return &Options{
PartitionType: LinuxFilesystemData,
Size: StateSize,
}
case constants.EphemeralPartitionLabel:
return &Options{
PartitionType: LinuxFilesystemData,
Size: 0,
}
default:
return nil
}
}