Skip to content

discover: fix sysfs end-of-partition calculation#7

Open
eriknordmark wants to merge 1 commit into
diskfs:mainfrom
eriknordmark:fix-sysfs-end-calculation
Open

discover: fix sysfs end-of-partition calculation#7
eriknordmark wants to merge 1 commit into
diskfs:mainfrom
eriknordmark:fix-sysfs-end-calculation

Conversation

@eriknordmark
Copy link
Copy Markdown

Summary

The sysfs branch of findDisks (discover.go:175) computed each partition's last-byte position as size - start + 1. That's not the right formula for anything; the sysfs size attribute is the partition's length in sectors, so the inclusive last LBA is start + size - 1. The disk-image branch a few dozen lines up (discover.go:78) already uses the correct formula:

end:    start + p.GetSize() - 1,

Impact

partitionData.end flows into:

  • calculate.go:48, :53, :54, :63, :67, :69, :106, :131, :133, :134 — every free-space and grow-target computation in calculateResizes.
  • run_helpers.go:150 — the shrink-target end in planResizes.

So whenever partitionresizer runs against a real block device (which routes through sysfs) rather than a disk image (which routes through the GPT-table branch), the resize plan is computed against silently wrong partition geometry. The disk-image-only test fixtures (testdata/disk.img, testdata/diskfull.img) hid this from CI.

Fix

One-line change: end := size - start + 1end := start + size - 1, with a comment explaining the formula matches the disk-image branch.

Extend TestFindDisks/all to assert pd.end so this regression can't slip back through.

Test plan

$ go test -run '^TestFindDisks$' ./...
=== RUN   TestFindDisks
--- PASS: TestFindDisks (0.01s)
ok  	github.com/diskfs/partitionresizer	0.014s

Signed-off-by: eriknordmark erik@zededa.com

The sysfs branch of findDisks computed each partition's last-byte
position as `size - start + 1`, which is meaningless arithmetic — the
sysfs `size` attribute is the partition's length in sectors, so the
inclusive last LBA is `start + size - 1`. The disk-image branch a
few dozen lines up uses the correct formula already.

partitionData.end flows into calculateResizes (calculate.go:48/53/
54/63/67/69/106/131/133/134) and run_helpers.go:150 where it
participates in every free-space and shrink-target computation, so
the wrong end on the sysfs path produced silently wrong layouts
whenever partitionresizer is run against a real block device rather
than a disk image. Disk-image-only test fixtures hid it from CI.

Extend TestFindDisks/all to assert pd.end so the regression is
covered.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

1 participant