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

Script to create an MBR formatted disk. #28

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

PizieDust
Copy link
Contributor

closes #14

This PR tackles the final task of issue #14.

Helpful notes from @reynir :

The idea was to pass zero or more files with partition data, some options and a destination and it would write a disk image to the destination with a MBR header and partitions containing the data. I think what I meant with padding was partitions larger than the input data - so containing either zeroes or uninitialized data at the end. And empty sections would be space in between partitions.

Currently we are able to achieve the following:

  • Pass in a destination and some files: The number of files passed gives the number of partitions (max 4)
  • Partition sizes: The sizes of the files are used to calculate the sizes that are allocated for each partition
  • Writing the files to the partitions themselves can be achieved with the script write_partition
  • Inspecting the MBR structure in the newly created disk can be achieved with the script mbr_inspect
  • Reading the contents of the partition or what has been written to it can be done with the script read_partition
  • Resizing the partition can be done with the script resize_partition

This new script can be compiled and executed by running:

dune exec -- bin/create_mbr_disk.exe -d disk.img notes.txt
  • disk.img: The destination (the disk image that will be created in the current directory)
  • notes.txt: A sample file

The above command will create a disk file named disk.img with one partition which has a size equivalent to the size of notes.txt

A helpful menu can be opened by dune exec -- bin/create_mbr_disk.exe --help

Notes:
Some modifications will have to be made to accommodate features like paddings between partitions and restricting how many files should be passed with the script. This PR serves as a starting point.

@reynir reynir self-requested a review June 1, 2023 17:50
@PizieDust
Copy link
Contributor Author

One hiccup with this PR that I am yet to figure out is that when ever the partitions are created and we write data to them, the last partition has some issues and we can't read anything from it. It just displays "END OF FILE"

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.

Cool! I made a few comments. I mentioned yesterday that letting exceptions bubble all the way up is not always so nice: We don't fully control how the error is displayed, and depending on the setup a stack trace may be printed.

Instead of failwith "oh no! an error" we can write (Printf.eprintf "oh no! an error. Exiting...\n%!"; exit 2). This prints the error message to stderr, and we control the exit code (in this case 2). The %! in the format string is to ensure the internal OCaml buffer is flushed.

Alternatively, we can catch all exceptions in the "beginning" of the program and handle them there (e.g. printing nice error messages and choosing a suitable exit code). Or use Printexc.set_uncaught_exception_handler to set a handler.

bin/create_mbr_disk.ml Outdated Show resolved Hide resolved
(fun i size ->
Printf.printf "Creating partition: %d" (i + 1);
let start_sector = (i + 1) * sector_size in
let num_sectors = (size + sector_size - 1) / 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.

Here we round up the size to the nearest sector boundary. This is certainly a valid choice. I lean more towards erroring out as rounding up means we have to think about the (potential) slack at the end of the partition and whether to zero it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@reynir Here I wish to ask, the files to be written to the disk, should they have a size that is an exact multiple of sector_size before it is validated and we error otherwise.

I agree too much slack at the end of the partition will be a waste.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I would do it that way. The user can call truncate to add padding if they need it and it makes sense for the data in question.

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

@reynir I think we have an issue with our CI/CD pipleline

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.

Tools for working with or creating MBR-formatted disk images
2 participants