Repository navigation
Expand file tree
/
Copy pathdeviceref.go
More file actions
291 lines (267 loc) · 7.97 KB
/
Copy pathdeviceref.go
File metadata and controls
291 lines (267 loc) · 7.97 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bytes"
"fmt"
"slices"
"strconv"
"strings"
"golang.org/x/sys/unix"
)
type refFormat uint8
const (
refPath refFormat = iota // path to the block device, e.g. "/dev/sda".
refGptType // type of gpt partition
refGptUUID // uuid of the gpt partition
refGptUUIDPartoff // offset against a gpt partition with uuid
refGptLabel
refFsUUID
refFsLabel
refHwPath
refWwID
)
// The are many ways a user can specify root partition (using name, fs uuid, fs label, gpt attribute, ...).
// This struct abstracts this information and provides a convenient matching functions.
type deviceRef struct {
format refFormat
data any
}
type gptPartoffData struct {
uuid UUID
offset int
}
func (blk *blkInfo) matchesRef(d *deviceRef) bool {
if d == nil {
return false
}
switch d.format {
case refPath:
path := d.data.(string)
if path == blk.path {
return true
}
return slices.Contains(blk.symlinks, path)
case refFsUUID:
return bytes.Equal(d.data.(UUID), blk.uuid)
case refFsLabel:
return d.data.(string) == blk.label
case refHwPath:
return blk.hwPath != "" && d.data.(string) == blk.hwPath
case refWwID:
return slices.Contains(blk.wwid, d.data.(string))
default:
return false
}
}
func calculateDevPath(parent string, partition int) string {
name := parent
// some drivers use 'p' prefix for the partition number. TODO: find out where it is codified.
if strings.HasPrefix(parent, "/dev/nvme") || strings.HasPrefix(parent, "/dev/mmcblk") {
name += "p"
}
name += strconv.Itoa(partition + 1) // devname partitions start with "1"
return name
}
// checks if the reference is a gpt-specific and if yes then tries to resolve it to a device name
func (blk *blkInfo) resolveGptRef(d *deviceRef) {
if d == nil {
return
}
if !d.dependsOnGpt() {
return
}
gpt := blk.data.(gptData)
for _, p := range gpt.partitions {
switch d.format {
case refGptType:
if bytes.Equal(d.data.(UUID), p.typeGUID) {
partitionPath := calculateDevPath(blk.path, p.num)
if rootAutodiscoveryMode {
info("autodiscovery: partition %s matches root", partitionPath)
if p.attributes&gptPartitionAttributeDoNotAutomount != 0 {
info("autodiscovery: partition %s has 'do not mount' GPT attribute, skip it", partitionPath)
continue
}
if p.attributes&gptPartitionAttributeReadOnly != 0 {
info("autodiscovery: partition %s has 'read-only' GPT attribute", partitionPath)
rootAutodiscoveryMountFlags |= unix.MS_RDONLY
}
}
*d = deviceRef{refPath, partitionPath}
}
case refGptUUID:
if bytes.Equal(d.data.(UUID), p.uuid) {
*d = deviceRef{refPath, calculateDevPath(blk.path, p.num)}
}
case refGptUUIDPartoff:
data := d.data.(gptPartoffData)
if bytes.Equal(data.uuid, p.uuid) {
*d = deviceRef{refPath, calculateDevPath(blk.path, p.num+data.offset)}
}
case refGptLabel:
if d.data.(string) == p.name {
*d = deviceRef{refPath, calculateDevPath(blk.path, p.num)}
}
}
}
if d.format == refWwID {
for _, id := range blk.wwid {
partPrefix := id + "-part"
ref := d.data.(string)
if strings.HasPrefix(ref, partPrefix) {
num, err := strconv.Atoi(ref[len(partPrefix):])
if err != nil {
info("unable to parse partition number for %s", ref)
return
}
*d = deviceRef{refPath, calculateDevPath(blk.path, num-1)}
}
}
} else if d.format == refHwPath {
if blk.hwPath != "" {
partPrefix := blk.hwPath + "-part"
ref := d.data.(string)
if strings.HasPrefix(ref, partPrefix) {
num, err := strconv.Atoi(ref[len(partPrefix):])
if err != nil {
info("unable to parse partition number for %s", ref)
return
}
*d = deviceRef{refPath, calculateDevPath(blk.path, num-1)}
}
}
}
}
// String formats d the way crypttab and the command line write it.
func (d *deviceRef) String() string {
switch d.format {
case refPath:
return d.data.(string)
case refGptType:
return "PARTTYPE=" + d.data.(UUID).toString()
case refGptUUID:
return "PARTUUID=" + d.data.(UUID).toString()
case refGptUUIDPartoff:
data := d.data.(gptPartoffData)
return fmt.Sprintf("PARTUUID=%s/PARTNROFF=%d", data.uuid.toString(), data.offset)
case refGptLabel:
return "PARTLABEL=" + d.data.(string)
case refFsUUID:
return "UUID=" + d.data.(UUID).toString()
case refFsLabel:
return "LABEL=" + d.data.(string)
case refHwPath:
return "HWPATH=" + d.data.(string)
case refWwID:
return "WWID=" + d.data.(string)
}
return fmt.Sprint(d.data)
}
func (d *deviceRef) dependsOnGpt() bool {
return d.format == refGptType ||
d.format == refGptUUID ||
d.format == refGptUUIDPartoff ||
d.format == refGptLabel ||
// both hwpath and wwid might include the parent device reference + "-partNN" suffix
d.format == refHwPath ||
d.format == refWwID
}
// checks whether given partition table contains active EFI service partition
func (gpt *gptData) containsEsp() bool {
for _, p := range gpt.partitions {
if bytes.Equal(activeEfiEspGUID, p.uuid) {
return true
}
}
return false
}
var rootAutodiscoveryGptTypes = map[string]string{
"amd64": "4f68bce3-e8cd-4db1-96e7-fbcaf984b709",
"386": "44479540-f297-41b2-9af7-d131d5f0458a",
"arm": "69dad710-2ce4-4e3c-b16c-21a1d49abed3",
"arm64": "b921b045-1df0-41c3-af44-4c6f280d3fae",
//"itanium": "993d8d3d-f80e-4225-855a-9daf8ed7ea97",
}
func parseDeviceRef(param string) (*deviceRef, error) {
if after, ok := strings.CutPrefix(param, "UUID="); ok {
uuid := after
u, err := parseUUID(uuid)
if err != nil {
return nil, fmt.Errorf("unable to parse UUID parameter %s: %v", param, err)
}
return &deviceRef{refFsUUID, u}, nil
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-uuid/"); ok {
uuid := after
u, err := parseUUID(uuid)
if err != nil {
return nil, fmt.Errorf("unable to parse UUID parameter %s: %v", param, err)
}
return &deviceRef{refFsUUID, u}, nil
}
if after, ok := strings.CutPrefix(param, "LABEL="); ok {
label := after
return &deviceRef{refFsLabel, label}, nil
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-label/"); ok {
label := after
return &deviceRef{refFsLabel, label}, nil
}
if after, ok := strings.CutPrefix(param, "PARTUUID="); ok {
uuid := after
if idx := strings.Index(uuid, "/PARTNROFF="); idx != -1 {
param := uuid[idx+11:]
uuid = uuid[:idx]
partnoff, err := strconv.Atoi(param)
if err != nil {
return nil, fmt.Errorf("unable to parse PARTNROFF= value %s", param)
}
u, err := parseUUID(uuid)
if err != nil {
return nil, fmt.Errorf("unable to parse UUID parameter %s: %v", param, err)
}
return &deviceRef{refGptUUIDPartoff, gptPartoffData{u, partnoff}}, nil
} else {
u, err := parseUUID(uuid)
if err != nil {
return nil, fmt.Errorf("unable to parse UUID parameter %s: %v", param, err)
}
return &deviceRef{refGptUUID, u}, nil
}
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-partuuid/"); ok {
uuid := after
u, err := parseUUID(uuid)
if err != nil {
return nil, fmt.Errorf("unable to parse UUID parameter %s: %v", param, err)
}
return &deviceRef{refGptUUID, u}, nil
}
if after, ok := strings.CutPrefix(param, "PARTLABEL="); ok {
label := after
return &deviceRef{refGptLabel, label}, nil
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-partlabel/"); ok {
label := after
return &deviceRef{refGptLabel, label}, nil
}
if after, ok := strings.CutPrefix(param, "HWPATH="); ok {
path := after
return &deviceRef{refHwPath, path}, nil
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-path/"); ok {
path := after
return &deviceRef{refHwPath, path}, nil
}
if after, ok := strings.CutPrefix(param, "WWID="); ok {
id := after
return &deviceRef{refWwID, id}, nil
}
if after, ok := strings.CutPrefix(param, "/dev/disk/by-id/"); ok {
id := after
return &deviceRef{refWwID, id}, nil
}
if strings.HasPrefix(param, "/dev/") {
return &deviceRef{refPath, param}, nil
}
return nil, fmt.Errorf("unable to parse the device reference")
}