/
gpt.go
489 lines (381 loc) · 10.9 KB
/
gpt.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// 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 gpt
import (
"encoding/binary"
"errors"
"fmt"
"math"
"os"
"github.com/google/uuid"
"github.com/siderolabs/go-blockdevice/blockdevice/blkpg"
"github.com/siderolabs/go-blockdevice/blockdevice/lba"
)
const (
// MagicEFIPart is the magic string in the GPT header signature field.
MagicEFIPart = "EFI PART"
// HeaderSize is the GUID partition table header size in bytes.
HeaderSize = 92
)
var (
// ErrPartitionTableDoesNotExist indicates that the partition table does not exist.
ErrPartitionTableDoesNotExist = errors.New("block device partition table does not exist")
// ErrHeaderCRCMismatch indicates that the header CRC does not match what is on disk.
ErrHeaderCRCMismatch = errors.New("block device partition table header CRC mismatch")
// ErrEntriesCRCMismatch indicates that the partitions array CRC does not match what is on disk.
ErrEntriesCRCMismatch = errors.New("block device partition table entries CRC mismatch")
)
type outOfSpaceError struct {
error
}
func (outOfSpaceError) OutOfSpaceError() {}
// GPT represents the GUID Partition Table.
type GPT struct {
f *os.File
l *lba.LBA
h *Header
e *Partitions
markMBRBootable bool
}
// Open attempts to open a partition table on f.
func Open(f *os.File) (*GPT, error) {
buf := make([]byte, 16)
// PMBR protective entry starts at 446. The partition type is at offset
// 4 from the start of the PMBR protective entry.
n, err := f.ReadAt(buf, 446)
if err != nil {
return nil, err
}
if n != len(buf) {
return nil, fmt.Errorf("incomplete read: %d != %d", n, len(buf))
}
// For GPT, the partition type should be 0xEE (EFI GPT).
if buf[4] == 0xEE {
l, err := lba.NewLBA(f)
if err != nil {
return nil, err
}
h := &Header{LBA: l}
if err = h.verifySignature(); err != nil {
return nil, ErrPartitionTableDoesNotExist
}
g := &GPT{
f: f,
l: l,
h: h,
e: &Partitions{h: h, devname: f.Name()},
markMBRBootable: buf[0] == 0x80,
}
return g, nil
}
return nil, ErrPartitionTableDoesNotExist
}
// New creates an in-memory partition table.
func New(f *os.File, setters ...Option) (*GPT, error) {
opts, err := NewDefaultOptions(setters...)
if err != nil {
return nil, err
}
l, err := lba.NewLBA(f)
if err != nil {
return nil, err
}
h := &Header{LBA: l}
h.Signature = MagicEFIPart
h.Revision = binary.LittleEndian.Uint32([]byte{0x00, 0x00, 0x01, 0x00})
h.Size = HeaderSize
h.CurrentLBA = 1
h.EntriesLBA = opts.PartitionEntriesStartLBA
h.NumberOfPartitionEntries = 128
h.PartitionEntrySize = 128
h.FirstUsableLBA = opts.PartitionEntriesStartLBA + 32
h.BackupLBA = uint64(l.TotalSectors - 1)
h.LastUsableLBA = h.BackupLBA - 33
guuid, err := uuid.NewRandom()
if err != nil {
return nil, fmt.Errorf("failed to generate UUID for new partition table: %w", err)
}
h.GUUID = guuid
g := &GPT{
f: f,
l: l,
h: h,
e: &Partitions{h: h, devname: f.Name()},
markMBRBootable: opts.MarkMBRBootable,
}
return g, nil
}
// Read reads the partition table on disk and updates the in-memory representation.
func (g *GPT) Read() error {
err := g.h.read()
if err != nil {
return err
}
err = g.e.read()
if err != nil {
return err
}
g.renumberPartitions()
return nil
}
func (g *GPT) Write() error {
pmbr, err := g.newPMBR(g.h)
if err != nil {
return err
}
err = g.l.WriteAt(0, 0x00, pmbr)
if err != nil {
return err
}
// NB: Write the partitions first so that the header CRC calculations are
// correct.
data, err := g.e.write()
if err != nil {
return err
}
err = g.l.WriteAt(int64(g.h.EntriesLBA), 0x00, data)
if err != nil {
return err
}
err = g.l.WriteAt(int64(g.h.LastUsableLBA+1), 0x00, data)
if err != nil {
return err
}
err = g.h.write()
if err != nil {
return err
}
if err = g.f.Sync(); err != nil {
return err
}
if err = g.syncKernelPartitions(); err != nil {
return fmt.Errorf("failed to sync kernel partitions: %w", err)
}
return nil
}
// Header returns the partition table header.
func (g *GPT) Header() *Header {
if g.h == nil {
return &Header{}
}
return g.h
}
// Partitions returns the partition table partitions.
func (g *GPT) Partitions() *Partitions {
if g.e == nil {
return &Partitions{}
}
return g.e
}
// Add adds a partition to the end of the list.
func (g *GPT) Add(size uint64, setters ...PartitionOption) (*Partition, error) {
return g.InsertAt(len(g.e.p), size, setters...)
}
// InsertAt inserts partition before the partition at the position idx.
//
// If idx == 0, it inserts new partition as the first partition, etc., idx == 1 as the second, etc.
func (g *GPT) InsertAt(idx int, size uint64, setters ...PartitionOption) (*Partition, error) {
opts := NewDefaultPartitionOptions(setters...)
// find the minimum and maximum LBAs available
var minLBA, maxLBA uint64
minLBA = g.h.FirstUsableLBA
if opts.Offset != 0 {
minLBA = opts.Offset / uint64(g.l.LogicalBlockSize)
}
for i := idx - 1; i >= 0; i-- {
if g.e.p[i] != nil {
if opts.Offset == 0 {
minLBA = g.e.p[i].LastLBA + 1
} else if g.e.p[i].LastLBA >= minLBA {
return nil, outOfSpaceError{fmt.Errorf("requested partition with offset %d bytes, overlapping partition %d", opts.Offset, i)}
}
break
}
}
maxLBA = g.h.LastUsableLBA
// Find the maximum LBAs available.
for i := idx; i < len(g.e.p); i++ {
if g.e.p[i] != nil {
maxLBA = g.e.p[i].FirstLBA - 1
break
}
}
// Find partition boundaries.
var start, end uint64
start = g.l.AlignToPhysicalBlockSize(minLBA, true)
if opts.MaximumSize {
end = g.l.AlignToPhysicalBlockSize(maxLBA+1, false) - 1
if end < start {
return nil, outOfSpaceError{fmt.Errorf("requested partition with maximum size, but no space available")}
}
} else {
// In GPT, partition end is inclusive.
end = start + size/uint64(g.l.LogicalBlockSize) - 1
if end > maxLBA {
// Convert the total available LBAs to units of bytes.
available := (maxLBA - start) * uint64(g.l.LogicalBlockSize)
return nil, outOfSpaceError{fmt.Errorf("requested partition size %d, available is %d (%d too many bytes)", size, available, size-available)}
}
}
uuid, err := uuid.NewRandom()
if err != nil {
return nil, err
}
partition := &Partition{
Type: opts.Type,
ID: uuid,
FirstLBA: start,
LastLBA: end,
Attributes: opts.Attibutes,
Name: opts.Name,
devname: g.e.devname,
}
g.e.p = append(g.e.p[:idx], append([]*Partition{partition}, g.e.p[idx:]...)...)
g.renumberPartitions()
return partition, nil
}
// Delete deletes a partition.
func (g *GPT) Delete(p *Partition) error {
index := -1
for i, part := range g.e.p {
if part == nil {
continue
}
if part.ID == p.ID {
index = i
break
}
}
if index == -1 {
return fmt.Errorf("partition not found")
}
g.e.p[index] = nil
g.renumberPartitions()
return nil
}
// Resize resizes a partition to next one if exists.
func (g *GPT) Resize(part *Partition) (bool, error) {
idx := int(part.Number - 1)
if len(g.e.p) < idx {
return false, fmt.Errorf("unknown partition %d, only %d available", part.Number, len(g.e.p))
}
maxLBA := g.h.LastUsableLBA
maxLBA = g.l.AlignToPhysicalBlockSize(maxLBA+1, false) - 1
for i := idx + 1; i < len(g.e.p); i++ {
if g.e.p[i] != nil {
maxLBA = g.e.p[i].FirstLBA - 1
break
}
}
if part.LastLBA >= maxLBA {
return false, nil
}
part.LastLBA = maxLBA
g.e.p[idx] = part
return true, nil
}
// Repair repairs the partition table.
func (g *GPT) Repair() error {
g.h.BackupLBA = uint64(g.l.TotalSectors - 1)
g.h.LastUsableLBA = g.h.BackupLBA - 33
return nil
}
// References:
// - https://en.wikipedia.org/wiki/GUID_Partition_Table#Protective_MBR_(LBA_0)
// - https://www.syslinux.org/wiki/index.php?title=Doc/gpt
// - https://en.wikipedia.org/wiki/Master_boot_record
// - http://www.rodsbooks.com/gdisk/bios.html
func (g *GPT) newPMBR(h *Header) ([]byte, error) {
p, err := g.l.ReadAt(0, 0, 512)
if err != nil {
return nil, err
}
// Boot signature.
copy(p[510:], []byte{0x55, 0xaa})
// PMBR protective entry.
b := p[446 : 446+16]
if g.markMBRBootable {
// Some BIOSes in legacy mode won't boot from a disk unless there is at least one
// partition in the MBR marked bootable. Mark this partition as bootable.
b[0] = 0x80
} else {
b[0] = 0x00
}
// Partition type: EFI data partition.
b[4] = 0xee
// CHS for the start of the partition
copy(b[1:4], []byte{0x00, 0x02, 0x00})
// CHS for the end of the partition
copy(b[5:8], []byte{0xff, 0xff, 0xff})
// Partition start LBA.
binary.LittleEndian.PutUint32(b[8:12], 1)
// Partition length in sectors.
// This might overflow uint32, so check accordingly
if h.BackupLBA > math.MaxUint32 {
binary.LittleEndian.PutUint32(b[12:16], uint32(math.MaxUint32))
} else {
binary.LittleEndian.PutUint32(b[12:16], uint32(h.BackupLBA))
}
return p, nil
}
func (g *GPT) renumberPartitions() {
// In gpt, partition numbers aren't stored, so numbers are just in-memory representation.
idx := int32(1)
for i := range g.e.p {
if g.e.p[i] == nil {
continue
}
g.e.p[i].Number = idx
idx++
}
}
func (g *GPT) syncKernelPartitions() error {
kernelPartitions, err := blkpg.GetKernelPartitions(g.f)
if err != nil {
return err
}
// filter out nil partitions
newPartitions := make([]*Partition, 0, len(g.e.p))
for _, part := range g.e.p {
if part == nil {
continue
}
newPartitions = append(newPartitions, part)
}
var i int
// find partitions matching exactly or partitions which can be simply resized
for i = 0; i < len(kernelPartitions) && i < len(newPartitions); i++ {
kernelPart := kernelPartitions[i]
newPart := newPartitions[i]
// non-contiguous kernel partition table, stop
if kernelPart.No != i+1 {
break
}
// skip partitions without any changes
if uint64(kernelPart.Start) == newPart.FirstLBA && uint64(kernelPart.Length) == newPart.Length() {
continue
}
// resizing a partition which is the last one in the kernel list (no overlaps)
if uint64(kernelPart.Start) == newPart.FirstLBA && i == len(kernelPartitions)-1 {
if err := blkpg.InformKernelOfResize(g.f, newPart.FirstLBA, newPart.Length(), newPart.Number); err != nil {
return err
}
continue
}
// partitions don't match, stop
break
}
// process remaining partitions: delete all the kernel partitions left, add new partitions from in-memory set
for j := i; j < len(kernelPartitions); j++ {
if err := blkpg.InformKernelOfDelete(g.f, 0, 0, int32(kernelPartitions[j].No)); err != nil {
return err
}
}
for j := i; j < len(newPartitions); j++ {
if err := blkpg.InformKernelOfAdd(g.f, newPartitions[j].FirstLBA, newPartitions[j].Length(), newPartitions[j].Number); err != nil {
return err
}
}
return nil
}