talm

Форк
0
/
partition.go 
121 строка · 3.1 Кб
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/.
4

5
// Package partition provides common utils for system partition format.
6
package partition
7

8
import (
9
	"github.com/dustin/go-humanize"
10
	"github.com/siderolabs/go-blockdevice/blockdevice/partition/gpt"
11

12
	"github.com/siderolabs/talos/pkg/machinery/constants"
13
)
14

15
// Options contains the options for creating a partition.
16
type Options struct {
17
	PartitionLabel     string
18
	PartitionType      Type
19
	Size               uint64
20
	LegacyBIOSBootable bool
21
}
22

23
// NewPartitionOptions returns a new PartitionOptions.
24
func NewPartitionOptions(label string, uki bool) *Options {
25
	return systemPartitionsPartitonOptions(label, uki)
26
}
27

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 {
32
			return part, nil
33
		}
34
	}
35

36
	return nil, nil
37
}
38

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))
43

44
	opts := []gpt.PartitionOption{
45
		gpt.WithPartitionType(partitionOpts.PartitionType),
46
		gpt.WithPartitionName(partitionOpts.PartitionLabel),
47
	}
48

49
	if partitionOpts.Size == 0 {
50
		opts = append(opts, gpt.WithMaximumSize(true))
51
	}
52

53
	if partitionOpts.LegacyBIOSBootable {
54
		opts = append(opts, gpt.WithLegacyBIOSBootableAttribute(true))
55
	}
56

57
	part, err := pt.InsertAt(pos, partitionOpts.Size, opts...)
58
	if err != nil {
59
		return "", err
60
	}
61

62
	partitionName, err := part.Path()
63
	if err != nil {
64
		return "", err
65
	}
66

67
	printf("created %s (%s) size %d blocks", partitionName, partitionOpts.PartitionLabel, part.Length())
68

69
	return partitionName, nil
70
}
71

72
func systemPartitionsPartitonOptions(label string, uki bool) *Options {
73
	switch label {
74
	case constants.EFIPartitionLabel:
75
		partitionOptions := &Options{
76
			PartitionType: EFISystemPartition,
77
			Size:          EFISize,
78
		}
79

80
		if uki {
81
			partitionOptions.Size = EFIUKISize
82
		}
83

84
		return partitionOptions
85
	case constants.BIOSGrubPartitionLabel:
86
		if uki {
87
			panic("BIOS partition is not supported with UKI")
88
		}
89

90
		return &Options{
91
			PartitionType: BIOSBootPartition,
92
			Size:          BIOSGrubSize,
93
		}
94
	case constants.BootPartitionLabel:
95
		if uki {
96
			panic("BOOT partition is not supported with UKI")
97
		}
98

99
		return &Options{
100
			PartitionType: LinuxFilesystemData,
101
			Size:          BootSize,
102
		}
103
	case constants.MetaPartitionLabel:
104
		return &Options{
105
			PartitionType: LinuxFilesystemData,
106
			Size:          MetaSize,
107
		}
108
	case constants.StatePartitionLabel:
109
		return &Options{
110
			PartitionType: LinuxFilesystemData,
111
			Size:          StateSize,
112
		}
113
	case constants.EphemeralPartitionLabel:
114
		return &Options{
115
			PartitionType: LinuxFilesystemData,
116
			Size:          0,
117
		}
118
	default:
119
		return nil
120
	}
121
}
122

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.