Skip to content

partition: add GPT and MBR selection policies - #423

Merged
deitch merged 1 commit into
diskfs:masterfrom
wow20220809-lab:fix/gpt-mbr-selection-policy
Aug 4, 2026
Merged

partition: add GPT and MBR selection policies#423
deitch merged 1 commit into
diskfs:masterfrom
wow20220809-lab:fix/gpt-mbr-selection-policy

Conversation

@wow20220809-lab

Copy link
Copy Markdown
Contributor

Summary

partition.Read currently returns GPT as soon as GPT can be read. This can
select readable residual GPT metadata even when the disk contains a valid
conventional MBR.

This change introduces two explicit selection policies:

  • GPTWithMBR: prefers a valid conventional MBR while continuing to use GPT
    for protective and hybrid MBR layouts. This is the new default.
  • GPTIgnoreMBR: preserves the previous GPT-first behavior.

The existing partition.Read signature remains unchanged and delegates to
ReadWithPolicy using GPTWithMBR.

Behavior

GPT MBR layout GPTWithMBR GPTIgnoreMBR
Valid No valid MBR GPT GPT
Valid Protective MBR GPT GPT
Valid Hybrid MBR GPT GPT
Valid Conventional MBR without valid 0xEE entry MBR GPT
Invalid Valid MBR MBR MBR
Invalid Invalid MBR Error Error

A protective entry is recognized by type 0xEE with starting LBA 1. Its size
is intentionally not required to span the current disk, since a disk image may
have been copied to a larger device without updating the PMBR size.

Tests

Added coverage for:

  • default GPTWithMBR behavior
  • conventional MBR with readable residual GPT metadata
  • GPTIgnoreMBR
  • protective and hybrid MBR layouts
  • missing or invalid MBR
  • invalid protective entries
  • invalid GPT fallback
  • invalid policy values

Validation performed:

  • go test ./partition/... -count=1
  • go test -race ./partition/... -count=1
  • go vet ./...
  • golangci-lint run ./partition/...

Fixes #422

Make GPTWithMBR the default while preserving the previous GPT-first behavior through GPTIgnoreMBR. Prefer conventional MBR tables over readable residual GPT metadata, while continuing to select GPT for protective and hybrid MBR layouts.

Signed-off-by: wow20220809-lab <wow080924@gmail.com>

@deitch deitch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you.

@deitch
deitch merged commit 6c9bf72 into diskfs:master Aug 4, 2026
20 checks passed
@deitch

deitch commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks, merged in. Who are you @wow20220809-lab ; and how do you use it?

@wow20220809-lab

Copy link
Copy Markdown
Contributor Author

Thanks for merging it!

I'm a cloud security engineer. Detecting security risks in cloud disks is an important part of our work, and go-diskfs helps us identify disk layouts and mount those disks for comprehensive security scanning.

Thank you very much for creating and maintaining this project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

partition.Read selects stale GPT over active MBR — GPT result is returned when ProtectiveMBR is false

2 participants