refactor(file): replace fdisk with gptman+mbrman#154
Conversation
Replace fragile fdisk-based partition detection with Rust-native crates: - add gptman (GPT with CRC32 validation) and mbrman (MBR including logical partitions) as dependencies - add src/file/partition.rs: get_partition_data() and is_gpt() - remove locale-sensitive fdisk shell invocation and regex parsing - mbrman iter() correctly reaches logical partitions 5 and 6 - fix pre-existing bug: PartitionInfo.end (last sector) was used as dd count= instead of sector count; rename field to count with correct semantics (ending_lba - starting_lba + 1 for GPT, p.sectors for MBR) - remove fdisk from package.metadata.deb depends - change PartitionInfo fields from String to typed u32/u64 Signed-off-by: Harry Waschkeit <44188360+HarryWaschkeit@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors image partition detection away from shelling out to fdisk and instead uses Rust-native parsing via gptman (GPT) and mbrman (MBR), while also correcting partition sector-count semantics for the dd operations.
Changes:
- Introduces
src/file/partition.rsto read GPT/MBR partition start/count directly from the image. - Updates
get_partition_info()andddinvocation to use typedu32/u64fields and correctcountsemantics. - Adds
gptman/mbrmandependencies and removesfdiskfrom Debian runtime dependencies.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/file/partition.rs |
New partition table parsing utilities using gptman/mbrman. |
src/file/mod.rs |
Wires in the new partition module. |
src/file/functions.rs |
Replaces fdisk+regex partition discovery with Rust-native parsing; fixes dd count= usage. |
Cargo.toml |
Adds gptman/mbrman; removes fdisk from cargo-deb depends. |
Cargo.lock |
Lockfile updates for the newly introduced crates and their transitive deps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
More a theoretical finding: if for some reason the GPT got corrupt in the WIC file, this change will at least bail out in case the upper sector number in the partition is less than the lower one. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Harry Waschkeit <44188360+HarryWaschkeit@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors image partition detection to remove the locale/version-dependent fdisk shell parsing and replace it with Rust-native parsing via gptman (GPT) and mbrman (MBR), while also correcting partition sector count semantics for dd.
Changes:
- Introduce
src/file/partition.rsto detect GPT/MBR and compute(start, count)safely (including GPT end-start+1 semantics). - Update
get_partition_info()to use the new Rust-based partition detection and changePartitionInfofields to typedu32/u64. - Add
gptman/mbrmandependencies and removefdiskfrom the Debian runtime dependency list.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/file/partition.rs |
New module implementing GPT/MBR partition lookup and dd-compatible sector counts. |
src/file/functions.rs |
Replaces fdisk+regex parsing with calls into the new partition module; switches to typed partition fields and uses count. |
src/file/mod.rs |
Wires in the new partition module. |
Cargo.toml |
Adds gptman/mbrman crates; removes fdisk from cargo-deb depends. |
Cargo.lock |
Locks new transitive dependencies from gptman/mbrman. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- remove unused exec_cmd_with_output! macro left over after fdisk removal - partition.rs: capture GPT parse error and attach as context when MBR fallback also fails, avoiding silent misclassification of I/O errors - partition.rs: replace panicking gpt[n] index with iter().find() for safe bounds-checked GPT partition lookup Signed-off-by: Harry Waschkeit <44188360+HarryWaschkeit@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR removes the locale/version-sensitive fdisk shell parsing used for partition discovery and replaces it with Rust-native parsing via gptman (GPT w/ CRC validation) and mbrman (MBR incl. logical partitions), while also fixing the partition sector dd count= semantics.
Changes:
- Introduce
src/file/partition.rsimplementing GPT/MBR partition lookup and GPT detection. - Refactor
get_partition_info()to use the new Rust-native logic and switchPartitionInfofields to typed numeric values (u32/u64). - Add
gptman/mbrmandependencies and removefdiskfrom Debian runtime dependencies.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/file/partition.rs | Adds GPT/MBR partition parsing utilities and sector-count calculation. |
| src/file/mod.rs | Registers the new partition module within file. |
| src/file/functions.rs | Switches partition lookup to the new module and uses count (not inclusive end) for dd. |
| Cargo.toml | Adds gptman/mbrman deps and removes fdisk from cargo-deb depends. |
| Cargo.lock | Updates lockfile for newly added crates and transitive deps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Harry Waschkeit <44188360+HarryWaschkeit@users.noreply.github.com>
JanZachmann
left a comment
There was a problem hiding this comment.
Overall this is a solid improvement — replacing the locale-sensitive fdisk shell-out with native Rust crates eliminates a class of fragile parsing bugs and removes a runtime dependency. The dd count= bug fix is important and well-explained. Code is clean and prior review findings were addressed well.
A few minor items noted inline.
Replace silent truncating cast (partition_num as usize) with u32::try_from(i) for safe, self-documenting comparison in mbr.iter(). Signed-off-by: Harry Waschkeit <44188360+HarryWaschkeit@users.noreply.github.com>
Summary
Replace the fragile
fdisk-based partition detection inget_partition_info()with Rust-nativegptmanandmbrmancrates.Changes:
gptman(GPT with CRC32 validation) andmbrman(MBR including logical partitions) as dependenciessrc/file/partition.rswithget_partition_data()andis_gpt()fdiskshell invocation and regex parsingfdiskfrompackage.metadata.debdependsPartitionInfofields fromStringto typedu32/u64Reason
The previous implementation spawned
fdisk -land parsed its text output with regex — making it locale-sensitive, version-dependent, and requiringfdiskas a runtime system dependency.Additionally, a pre-existing bug was fixed:
PartitionInfo.endheld the last sector (inclusive) fromfdiskoutput and was passed directly asdd count=, butcountexpects the number of sectors. For a partition starting at sector 2048 and ending at 206847, this causedddto copy 206847 sectors instead of the correct 204800.Fixed semantics:
count = ending_lba - starting_lba + 1count = p.sectors(already the sector count)Verification
cargo checkpassescargo fmt -- --checkpassescargo clippy -- -D warningspasses