Skip to content
Open
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
7 changes: 6 additions & 1 deletion partitioning/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ func (t *Table) init(lastLBA uint64) {

if t.firstUsableLBA == 0 {
t.firstUsableLBA = t.primaryPartitionsLBA + uint64(lbasForEntries)
t.firstUsableLBA = (t.firstUsableLBA + t.alignment - 1) / t.alignment * t.alignment // 2048 with 512 sector size, 256 with 4096 sector size

// only align the LBA when the compatibility flag is not set. This alignment can cause Windows to corrupt the
// partition table.
if !t.options.CompatFirstUsableLBA {
t.firstUsableLBA = (t.firstUsableLBA + t.alignment - 1) / t.alignment * t.alignment // 2048 with 512 sector size, 256 with 4096 sector size
}
}

t.lastUsableLBA = t.secondaryPartitionsLBA - 1
Expand Down
15 changes: 15 additions & 0 deletions partitioning/gpt/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ type Options struct {
// Number of LBAs to skip before the writing partition entries.
SkipLBAs uint

// If true, the first usable LBA will be the first LBA after the partition table.
// Otherwise (default), the first usable LBA will be set to the first LBA after
// the partition table aligned on a 1MiB boundary. Modern Windows versions can
// corrupt partition tables on disk eject when this flag is not set.
CompatFirstUsableLBA bool

// DiskGUID is a GUID for the disk.
//
// If not set, on partition table creation, a new GUID is generated.
Expand Down Expand Up @@ -51,6 +57,15 @@ func WithDiskGUID(guid uuid.UUID) Option {
}
}

// WithCompatFirstUsableLBA sets the first usable LBA to the first LBA after the
// partition table instead of a 1MiB-aligned value. This can prevent modern
// Windows versions from clobbering the partition table on disk eject.
func WithCompatFirstUsableLBA() Option {
return func(o *Options) {
o.CompatFirstUsableLBA = true
}
}

// PartitionOptions configure a partition.
type PartitionOptions struct {
UniqueGUID uuid.UUID
Expand Down