diff --git a/partitioning/gpt/gpt.go b/partitioning/gpt/gpt.go index 0679007..488abab 100644 --- a/partitioning/gpt/gpt.go +++ b/partitioning/gpt/gpt.go @@ -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 diff --git a/partitioning/gpt/options.go b/partitioning/gpt/options.go index d49e76c..367b8f8 100644 --- a/partitioning/gpt/options.go +++ b/partitioning/gpt/options.go @@ -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. @@ -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