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

Read data in partition, output to stdout or file #23

Merged
merged 16 commits into from
Mar 23, 2023

Conversation

PizieDust
Copy link
Contributor

part of #14

Outline

  • This PR introduces a new executable read_partition.ml which reads content stored in a partition.
  • This executable uses the cmdliner package to parse and read arguments passed when calling this executable and also generates a lovely manual which can be called by adding the --help flag (I love this package)
  • Basically we read in a disk image, unmarshal it's MBR header to get the partition table, grab the specific partition we want to read from. After getting this partition, we calculate information about it's sectors and then read the data from these sectors.

Usage

After building, the command can be called with:

read_partition.exe test.img 2
  • test.img : The disk image which contains the partition.
  • 2 : The partition number we want to read from.
    Running this command will print the contents of partition 2 to stdout.

To output the contents to a file:

read_partition.exe -f file.txt test.img 2

This will output the contents to a file called file.txt. If this file doesn't exist, the command will create the file.

Simple Test

  • Create a new text file with some text:
echo "Simple file for testing" > sample.txt
  • Create a new disk image with 3 partitions:
fallocate -l test.img 2M`
parted --align none test.img mklabel msdos
parted --align none test.img mkpart primary 1s 2s
parted --align none test.img mkpart primary 3s 4s
parted --align none test.img mkpart primary 5s 6s
  • Add the text file to the partition
dd if=sample.txt of=test.img bs=512 seek=1 conv=notrunc
  • Run the command now to read file from the partition:
read_partition.exe test.img 1`

cc @reynir

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.

Nice!

$ fallocate -l test.img 2M
$ parted --align none test.img mklabel msdos
$ parted --align none test.img mkpart primary 1s 20s
$ tar -cf tmp.tar LICENSE
$ dd if=test.tar of=test.img seek=1 conv=notrunc
20+0 records in
20+0 records out
10240 bytes (10 kB, 10 KiB) copied, 0.000348219 s, 29.4 MB/s
$ dune exec -- bin/read_partition.exe test.img  1 | tar -t
LICENSE

let start_sector, num_sectors, sector_size =
calculate_partition_info partition
in
let buffer = read_partition_data mbr start_sector num_sectors sector_size in
Copy link
Member

Choose a reason for hiding this comment

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

We should incrementally read and write the data as the partition could potentially be larger than what we can fit in memory.

Copy link
Contributor Author

@PizieDust PizieDust Mar 21, 2023

Choose a reason for hiding this comment

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

I've updated it to use a chunk size of 4096 bytes. let me know if to use a bigger chunk size.

@PizieDust PizieDust requested review from reynir and removed request for reynir March 21, 2023 19:18
@reynir reynir merged commit 135c70e into mirage:master Mar 23, 2023
@reynir
Copy link
Member

reynir commented Mar 23, 2023

Thanks!

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)
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