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

Write data to partition, read stdin or file #24

Merged
merged 17 commits into from
Apr 4, 2023
Merged

Conversation

PizieDust
Copy link
Contributor

@PizieDust PizieDust commented Mar 23, 2023

part of #14

Outline

This creates a new file write_partition.ml that can copy a file into a partition, or receive input from stdin and copy it into the partition.
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 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:

write_partition.exe test.img 2
Writing into partition 2
  • test.img : The disk image which contains the partition.
  • 2 : The partition number we want to read from.
  • writing into partition 2: the data to write into partition 2.

To write a file into the partition:

write_partition.exe -d file.txt test.img 2

This will copy all the contents of file.txt into partition 2.

Simple Test

Create a new text file with some text:

echo "Simple file for testing" > file.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 1

write_partition.exe -d file.txt test.img 1

Add some other text to partition 2

write_partition.exe test.img 2
Write this to partition 2

Verify the data was written to both partitions:

read_partition.exe test.img 1
read_partition.exe test.img 2

Observation

The data in partition1 and 2 should be printed to stdout. Partition 1 should contain what was in file.txt and partition 2 should contain what was typed in stdin.

UPDATE

To write multiline comments, you just keep using the enter key to write the next line. To stop writing, you use this character >

@PizieDust
Copy link
Contributor Author

Hello @reynir
I have a blocking issue with this pr.
Currently each time we write to the partition, it overwrites everything that was in the partition.
I tried using Open_append instead of Open_create but this doesn't work. The code compiles correctly but nothing is written to the partition. I am not able to understand why this happens and I am yet to figure out how to write to a partition without overwriting it's contents.

@reynir
Copy link
Member

reynir commented Mar 23, 2023

I tested it briefly with a fresh disk image with one partition. I was able Hello World\n in the first partition except the string Hello World\n started 512 bytes into the first partition. I checked with dune exec -- bin/read_partition.exe test.img 1 | cat -v.

I imagine you don't need neither Open_append and Open_creat. In the latter case it is an error if the disk image doesn't exist.

@reynir
Copy link
Member

reynir commented Mar 23, 2023

Rereading your last sentence: we do want to overwrite the contents of the partition with new data! So that's fine.

@PizieDust
Copy link
Contributor Author

I tested it briefly with a fresh disk image with one partition. I was able Hello World\n in the first partition except the string Hello World\n started 512 bytes into the first partition. I checked with dune exec -- bin/read_partition.exe test.img 1 | cat -v.

Oh thank you for catching this. I kept running the command but the output was so many lines and my terminal couldn't display the full output. I had to output to a file using dune exec --bin/read_partition test.img 1 | cat -v > out.txt and I could see the full output.

@PizieDust
Copy link
Contributor Author

I imagine you don't need neither Open_append and Open_creat. In the latter case it is an error if the disk image doesn't exist.

Very correct. Just tried it without any of them and it still works great.

@PizieDust
Copy link
Contributor Author

I tested it briefly with a fresh disk image with one partition. I was able Hello World\n in the first partition except the string Hello World\n started 512 bytes into the first partition. I checked with dune exec -- bin/read_partition.exe test.img 1 | cat -v.

and haha the reason for this is actually crazy. When I first wrote this I had a major problem which was that after writing to a partition, each time I tried to read from the partition it would indicate that the MBR header was not valid. I realized it was because the code was overwriting the MBR header as well. So I did changed somethings to prevent this, which is also why I was adding the 512 bytes (MBR header size) to the start of where the writing will begin. Good thing the other changes made it work so this was not even necessary and if you didn't notice it I never would have.

@PizieDust
Copy link
Contributor Author

Hello @reynir
I wish to ask if there are any more changes we can add ?

@reynir
Copy link
Member

reynir commented Mar 28, 2023

Sorry for not leaving a review before now.

  • It would be great to "stream" the reads and writes in chunks. The data might not fit in memory.
  • When reading from stdin we can read until EOF instead of just the first line. We might want to write data from stdin that spans multiple lines.
  • It would be great to check if the data fits within the partition. For files this could be done beforehand using stat, or an error could be printed when trying to write beyond the partition.

@PizieDust
Copy link
Contributor Author

Thank you. Let me see how to implement reading and writing in chunks.

@PizieDust
Copy link
Contributor Author

@reynir
I think this PR is now ready.

PizieDust and others added 3 commits April 1, 2023 21:27
Reading is done until EOF is hit. While writing an error is raised if
more is read than what the partition allows for. If the input is a file
the size is checked early against the size reported by `Unix.stat`.
@reynir reynir merged commit cb71b94 into mirage:master Apr 4, 2023
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