-
Notifications
You must be signed in to change notification settings - Fork 458
/
format.go
103 lines (89 loc) · 2.9 KB
/
format.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
// 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 (
"fmt"
"github.com/siderolabs/go-blockdevice/blockdevice"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/makefs"
)
// FormatOptions contains format parameters.
type FormatOptions struct {
Label string
FileSystemType FileSystemType
Force bool
}
// NewFormatOptions creates a new format options.
func NewFormatOptions(label string) *FormatOptions {
return systemPartitionsFormatOptions(label)
}
// Format zeroes the device and formats it using filesystem type provided.
func Format(devname string, t *FormatOptions, printf func(string, ...any)) error {
if t.FileSystemType == FilesystemTypeNone {
return zeroPartition(devname, printf)
}
opts := []makefs.Option{makefs.WithForce(t.Force), makefs.WithLabel(t.Label)}
printf("formatting the partition %q as %q with label %q\n", devname, t.FileSystemType, t.Label)
switch t.FileSystemType {
case FilesystemTypeVFAT:
return makefs.VFAT(devname, opts...)
case FilesystemTypeXFS:
return makefs.XFS(devname, opts...)
default:
return fmt.Errorf("unsupported filesystem type: %q", t.FileSystemType)
}
}
// zeroPartition fills the partition with zeroes.
func zeroPartition(devname string, printf func(string, ...any)) (err error) {
printf("zeroing out %q", devname)
part, err := blockdevice.Open(devname, blockdevice.WithExclusiveLock(true))
if err != nil {
return err
}
defer part.Close() //nolint:errcheck
return part.FastWipe()
}
func systemPartitionsFormatOptions(label string) *FormatOptions {
switch label {
case constants.EFIPartitionLabel:
return &FormatOptions{
Label: constants.EFIPartitionLabel,
FileSystemType: FilesystemTypeVFAT,
Force: true,
}
case constants.BIOSGrubPartitionLabel:
return &FormatOptions{
Label: constants.BIOSGrubPartitionLabel,
FileSystemType: FilesystemTypeNone,
Force: true,
}
case constants.BootPartitionLabel:
return &FormatOptions{
Label: constants.BootPartitionLabel,
FileSystemType: FilesystemTypeXFS,
Force: true,
}
case constants.MetaPartitionLabel:
return &FormatOptions{
Label: constants.MetaPartitionLabel,
FileSystemType: FilesystemTypeNone,
Force: true,
}
case constants.StatePartitionLabel:
return &FormatOptions{
Label: constants.StatePartitionLabel,
FileSystemType: FilesystemTypeXFS,
Force: true,
}
case constants.EphemeralPartitionLabel:
return &FormatOptions{
Label: constants.EphemeralPartitionLabel,
FileSystemType: FilesystemTypeXFS,
Force: true,
}
default:
return nil
}
}