1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
// Package partition provides common utils for system partition format.
9
"github.com/dustin/go-humanize"
10
"github.com/siderolabs/go-blockdevice/blockdevice/partition/gpt"
12
"github.com/siderolabs/talos/pkg/machinery/constants"
15
// Options contains the options for creating a partition.
20
LegacyBIOSBootable bool
23
// NewPartitionOptions returns a new PartitionOptions.
24
func NewPartitionOptions(label string, uki bool) *Options {
25
return systemPartitionsPartitonOptions(label, uki)
28
// Locate existing partition on the disk by label.
29
func Locate(pt *gpt.GPT, label string) (*gpt.Partition, error) {
30
for _, part := range pt.Partitions().Items() {
31
if part.Name == label {
39
// Partition creates a new partition on the specified device.
40
// Returns the path to the newly created partition.
41
func Partition(pt *gpt.GPT, pos int, device string, partitionOpts Options, printf func(string, ...any)) (string, error) {
42
printf("partitioning %s - %s %q\n", device, partitionOpts.PartitionLabel, humanize.Bytes(partitionOpts.Size))
44
opts := []gpt.PartitionOption{
45
gpt.WithPartitionType(partitionOpts.PartitionType),
46
gpt.WithPartitionName(partitionOpts.PartitionLabel),
49
if partitionOpts.Size == 0 {
50
opts = append(opts, gpt.WithMaximumSize(true))
53
if partitionOpts.LegacyBIOSBootable {
54
opts = append(opts, gpt.WithLegacyBIOSBootableAttribute(true))
57
part, err := pt.InsertAt(pos, partitionOpts.Size, opts...)
62
partitionName, err := part.Path()
67
printf("created %s (%s) size %d blocks", partitionName, partitionOpts.PartitionLabel, part.Length())
69
return partitionName, nil
72
func systemPartitionsPartitonOptions(label string, uki bool) *Options {
74
case constants.EFIPartitionLabel:
75
partitionOptions := &Options{
76
PartitionType: EFISystemPartition,
81
partitionOptions.Size = EFIUKISize
84
return partitionOptions
85
case constants.BIOSGrubPartitionLabel:
87
panic("BIOS partition is not supported with UKI")
91
PartitionType: BIOSBootPartition,
94
case constants.BootPartitionLabel:
96
panic("BOOT partition is not supported with UKI")
100
PartitionType: LinuxFilesystemData,
103
case constants.MetaPartitionLabel:
105
PartitionType: LinuxFilesystemData,
108
case constants.StatePartitionLabel:
110
PartitionType: LinuxFilesystemData,
113
case constants.EphemeralPartitionLabel:
115
PartitionType: LinuxFilesystemData,