Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly process GPT with gaps #111

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions partitioning/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,12 @@ func (t *Table) allocatableRanges() []allocatableRange {

for {
for partitionIdx < len(t.entries) {
if t.entries[partitionIdx] == nil {
partitionIdx++
if t.entries[partitionIdx] != nil {
break
}

break
// skip empty entries
partitionIdx++
}

var highLBA uint64
Expand Down Expand Up @@ -634,12 +635,14 @@ func (t *Table) syncKernel() error {
}
}

err = t.dev.KernelPartitionAdd(no,
myEntry.FirstLBA*uint64(t.sectorSize),
(myEntry.LastLBA-myEntry.FirstLBA+1)*uint64(t.sectorSize),
)
if err != nil {
return fmt.Errorf("failed to add partition %d: %w", no, err)
if myEntry != nil {
err = t.dev.KernelPartitionAdd(no,
myEntry.FirstLBA*uint64(t.sectorSize),
(myEntry.LastLBA-myEntry.FirstLBA+1)*uint64(t.sectorSize),
)
if err != nil {
return fmt.Errorf("failed to add partition %d: %w", no, err)
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions partitioning/gpt/gpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,30 @@ func TestGPT(t *testing.T) {
allocator: func(t *testing.T, table *gpt.Table) {
t.Helper()

// allocate 3 1G partitions first, and delete the middle one
// allocate 4 1G partitions first, and delete two in the middle

require.NoError(t, allocateError(table.AllocatePartition(1*GiB, "1G1", partType1,
gpt.WithUniqueGUID(uuid.MustParse("DA66737E-1ED4-4DDF-B98C-70CEBFE3ADA0")),
)))
require.NoError(t, allocateError(table.AllocatePartition(1*GiB, "1G2", partType1)))
require.NoError(t, allocateError(table.AllocatePartition(1*GiB, "1G3", partType2,
require.NoError(t, allocateError(table.AllocatePartition(1*GiB, "1G3", partType1)))
require.NoError(t, allocateError(table.AllocatePartition(1*GiB, "1G4", partType2,
gpt.WithUniqueGUID(uuid.MustParse("3D0FE86B-7791-4659-B564-FC49A542866D")),
)))

require.NoError(t, table.DeletePartition(1))
require.NoError(t, table.DeletePartition(2))

// allocate smaller partitions to fill the gap
// gap is 2 GiB, while the tail available space is < 2 GiB, so small partitions will be appended to the end
require.NoError(t, allocateError(table.AllocatePartition(200*MiB, "200M", partType2,
gpt.WithUniqueGUID(uuid.MustParse("EE1A711E-DE12-4D9F-98FF-672F7AD638F8")),
)))
require.NoError(t, allocateError(table.AllocatePartition(400*MiB, "400M", partType2,
gpt.WithUniqueGUID(uuid.MustParse("15E609C8-9775-4E86-AF59-8A87E7C03FAB")),
)))

// partition that doesn't fit the gap will be appended to the end
require.NoError(t, allocateError(table.AllocatePartition(500*MiB, "500M", partType2,
// bigger partition will fill the gap
require.NoError(t, allocateError(table.AllocatePartition(1500*MiB, "1500M", partType2,
gpt.WithUniqueGUID(uuid.MustParse("15E609C8-9775-4E86-AF59-8A87E7C03FAC")),
)))
},
Expand Down
10 changes: 5 additions & 5 deletions partitioning/gpt/testdata/mix-allocate.gdisk
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 2048, last usable sector is 12582878
Partitions will be aligned on 2048-sector boundaries
Total free space is 6133727 sectors (2.9 GiB)
Total free space is 4085727 sectors (1.9 GiB)

Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB EF00 1G1
2 2099200 2508799 200.0 MiB 8E00 200M
3 2508800 3327999 400.0 MiB 8E00 400M
4 4196352 6293503 1024.0 MiB 8E00 1G3
5 6293504 7317503 500.0 MiB 8E00 500M
3 2099200 5171199 1.5 GiB 8E00 1500M
4 6293504 8390655 1024.0 MiB 8E00 1G4
5 8390656 8800255 200.0 MiB 8E00 200M
6 8800256 9619455 400.0 MiB 8E00 400M
8 changes: 4 additions & 4 deletions partitioning/gpt/testdata/mix-allocate.sfdisk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ last-lba: 12582878
sector-size: 512

start= 2048, size= 2097152, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=DA66737E-1ED4-4DDF-B98C-70CEBFE3ADA0, name="1G1"
start= 2099200, size= 409600, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=EE1A711E-DE12-4D9F-98FF-672F7AD638F8, name="200M"
start= 2508800, size= 819200, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=15E609C8-9775-4E86-AF59-8A87E7C03FAB, name="400M"
start= 4196352, size= 2097152, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=3D0FE86B-7791-4659-B564-FC49A542866D, name="1G3"
start= 6293504, size= 1024000, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=15E609C8-9775-4E86-AF59-8A87E7C03FAC, name="500M"
start= 2099200, size= 3072000, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=15E609C8-9775-4E86-AF59-8A87E7C03FAC, name="1500M"
start= 6293504, size= 2097152, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=3D0FE86B-7791-4659-B564-FC49A542866D, name="1G4"
start= 8390656, size= 409600, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=EE1A711E-DE12-4D9F-98FF-672F7AD638F8, name="200M"
start= 8800256, size= 819200, type=E6D6D379-F507-44C2-A23C-238F2A3DF928, uuid=15E609C8-9775-4E86-AF59-8A87E7C03FAB, name="400M"