Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize partitions in a disk image. #26

Merged
merged 14 commits into from
Apr 19, 2023
Merged

Resize partitions in a disk image. #26

merged 14 commits into from
Apr 19, 2023

Conversation

PizieDust
Copy link
Contributor

@PizieDust PizieDust commented Apr 6, 2023

part of #14

Resizing partitions in a disk image

Outline

This PR introduces a new binary resize_partition which resizes a partition to a new size.
We are using the cmdliner package to read arguments from the command line. It also provides a helpful --help flag which can displays a user manual.
Basically we read in a disk image, unmarshal it's MBR header to get the partition table, grab the specific partition we want to resize. After getting this partition, we calculate a new end_sector and create a new partition changing only this end_sector. Then we swap this new partition with it's counterpart in the original partition table and create a new mbr header with the updated partition table. The new mbr header is then written back to the disk image, overwriting just the MBR header.

Usage

After building, the command can be called with:

resize_partition test.img 2 4096
  • test.img : The disk image which contains the partition.
  • 2 : The partition number we want to resize.
  • 4096: the new size of the partition

Outputs

Read initial disk image

dune exec -- bin/mbr_inspect.exe disk.img
MBR fields:
  original_physical_drive: 0
  seconds: 0
  minutes: 0
  hours: 0
  disk_signature: 3153ef95
  Partition 1:
    bootable: false
    chs_begin: (cylinders: 8, heads: 0, sectors: 2)
    ty: 83
    chs_end: (cylinders: 95, heads: 2, sectors: 23)
    lba_begin: 1
    size_sectors: 2006
  Partition 2:
    bootable: false
    chs_begin: (cylinders: 143, heads: 3, sectors: 32)
    ty: 83
    chs_end: (cylinders: 123, heads: 0, sectors: 26)
    lba_begin: 2047
    size_sectors: 1435
  Partition 3:
    bootable: false
    chs_begin: (cylinders: 31, heads: 2, sectors: 3)
    ty: 83
    chs_end: (cylinders: 159, heads: 3, sectors: 32)
    lba_begin: 3522
    size_sectors: 574

Resize 3rd partition

dune exec -- bin/resize_partition.exe disk.img 3 307200

Verify 3rd partition has been resized

MBR fields:
  original_physical_drive: 0
  seconds: 0
  minutes: 0
  hours: 0
  disk_signature: 3153ef95
  Partition 1:
    bootable: false
    chs_begin: (cylinders: 0, heads: 0, sectors: 0)
    ty: 83
    chs_end: (cylinders: 0, heads: 0, sectors: 0)
    lba_begin: 1
    size_sectors: 2006
  Partition 2:
    bootable: false
    chs_begin: (cylinders: 0, heads: 0, sectors: 0)
    ty: 83
    chs_end: (cylinders: 0, heads: 0, sectors: 0)
    lba_begin: 2047
    size_sectors: 1435
  Partition 3:
    bootable: false
    chs_begin: (cylinders: 0, heads: 0, sectors: 0)
    ty: 83
    chs_end: (cylinders: 0, heads: 0, sectors: 0)
    lba_begin: 3522
    size_sectors: 4122

Resizing a partition which may overlap

dune exec -- bin/resize_partition.exe disk.img 1 5000000
resize_partition: internal error, uncaught exception:
                  Failure("Partitions overlap")

cc @reynir

@PizieDust PizieDust marked this pull request as ready for review April 17, 2023 20:25
@PizieDust
Copy link
Contributor Author

Hello @reynir
Could you review this?

Copy link
Member

@reynir reynir left a comment

Choose a reason for hiding this comment

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

This looks good. Besides my one comment could you as well check that the resize doesn't result in the partitions overlapping before writing the new MBR header?

Sorry for the long wait - I was on Easter holiday.

bin/resize_partition.ml Outdated Show resolved Hide resolved
@PizieDust
Copy link
Contributor Author

This looks good. Besides my one comment could you as well check that the resize doesn't result in the partitions overlapping before writing the new MBR header?

While making the new MBR header, we have a check already in the Mbr.make constructor that checks if the partitions overlap.
https://github.com/mirage/ocaml-mbr/blob/master/lib/mbr.ml#L188

If the partitions overlap, it will fail without trying to write the new MBR header. The MBR header is only overwritten if the new MBR header is created successfully.

@PizieDust PizieDust requested a review from reynir April 18, 2023 16:07
Copy link
Member

@reynir reynir left a comment

Choose a reason for hiding this comment

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

While making the new MBR header, we have a check already in the Mbr.make constructor that checks if the partitions overlap.

Nice. In that case it would be nice to write a comment on this.

bin/resize_partition.ml Outdated Show resolved Hide resolved
@PizieDust PizieDust requested a review from reynir April 19, 2023 05:13
@reynir reynir merged commit 42a60a8 into mirage:master Apr 19, 2023
@reynir
Copy link
Member

reynir commented Apr 19, 2023

Thanks! I pushed a small change to the command help text.

reynir added a commit to reynir/opam-repository that referenced this pull request Apr 19, 2023
CHANGES:

* Add optional argument `?disk_signature` to `Mbr.make` (@Burnleydev1, review by @reynir, mirage/ocaml-mbr#19)
* Make the partition type a required argument to `Mbr.Partition.make` and rename it `~partition_type` (@AryanGodara, review by @reynir, mirage/ocaml-mbr#20)
* Add tools for inspecting and modifying MBR, and reading/writing data to partitions. The command line tools are not installed as part of the opam package. The tools are `bin/mbr_inspect.exe`, `bin/read_partition.exe`, `bin/resize_partition.exe` and `bin/write_partition.exe`. (@PizieDust, review by @reynir, mirage/ocaml-mbr#22, mirage/ocaml-mbr#23, mirage/ocaml-mbr#24, mirage/ocaml-mbr#26)
* Remove dependency on `ppx_cstruct` (@reynir, mirage/ocaml-mbr#27)
@PizieDust
Copy link
Contributor Author

amazing. thank you

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.

2 participants