Skip to content

Commit

Permalink
ignore GPT partitions that are marked unused by UUID (#178)
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <avi@deitcher.net>
  • Loading branch information
deitch committed Apr 9, 2023
1 parent a78c15f commit 5b6d86c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions partition/gpt/partition.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gpt

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand All @@ -15,6 +16,8 @@ import (
// PartitionEntrySize fixed size of a GPT partition entry
const PartitionEntrySize = 128

var zeroUUIDBytes = make([]byte, 16)

// Partition represents the structure of a single partition on the disk
type Partition struct {
Start uint64 // start sector for the partition
Expand Down Expand Up @@ -90,6 +93,9 @@ func partitionFromBytes(b []byte, logicalSectorSize, physicalSectorSize int) (*P
return nil, fmt.Errorf("data for partition was %d bytes instead of expected %d", len(b), PartitionEntrySize)
}
// is it all zeroes?
if bytes.Equal(b[0:16], zeroUUIDBytes) {
return nil, nil
}
typeGUID, err := uuid.FromBytes(bytesToUUIDBytes(b[0:16]))
if err != nil {
return nil, fmt.Errorf("unable to read partition type GUID: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions partition/gpt/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ func readPartitionArrayBytes(b []byte, entrySize, logicalSectorSize, physicalSec
if err != nil {
return nil, fmt.Errorf("error reading partition entry %d: %v", i, err)
}
if p == nil {
continue
}
// augment partition information
p.Size = (p.End - p.Start + 1) * uint64(logicalSectorSize)
parts = append(parts, p)
Expand Down

0 comments on commit 5b6d86c

Please sign in to comment.