-
Notifications
You must be signed in to change notification settings - Fork 8
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
PizieDust
wants to merge
11
commits into
mirage:master
Choose a base branch
from
PizieDust:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2f645f6
Test
PizieDust 45dd198
display the total size of each partition in bytes
PizieDust 6259ed6
create an mbr formatted disk
PizieDust 98b2149
binary to create mbr formmated disk
PizieDust f084834
display total size of all partitions
PizieDust a22f307
fix linting
PizieDust 5d89da4
fmt linting
PizieDust 8fb035d
fail if more than 4 files (partitions)
PizieDust 6c64ff9
replace exceptions with simple error messages
PizieDust 3b5444d
use 6 as partition type
PizieDust 9f88cde
error out if data size is not a mul of 512
PizieDust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
open Cmdliner | ||
|
||
let create_mbr_disk destination partition_files = | ||
let sector_size = Mbr.sizeof in | ||
let num_partitions = List.length partition_files in | ||
if num_partitions > 4 then | ||
failwith | ||
"Too many partition files. Limit number of files to 4 as MBR supports at \ | ||
most 4 partitions" | ||
else Printf.printf "Total partitions to create: %d\n" num_partitions; | ||
|
||
let partition_sizes = | ||
List.map (fun file -> (Unix.stat file).Unix.st_size) partition_files | ||
in | ||
|
||
let partition_size = List.fold_left ( + ) 0 partition_sizes in | ||
let mbr_size = Mbr.sizeof in | ||
let total_size = partition_size + mbr_size in | ||
|
||
Printf.printf "Total disk size: %d bytes\n" total_size; | ||
|
||
let partitions = | ||
List.mapi | ||
(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 | ||
match | ||
Mbr.Partition.make ~active:false ~partition_type:1 | ||
PizieDust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(Int32.of_int start_sector) | ||
(Int32.of_int num_sectors) | ||
with | ||
| Ok partition -> | ||
Printf.printf " - OK\n"; | ||
partition | ||
| Error msg -> failwith msg) | ||
partition_sizes | ||
in | ||
(* Mbr.make smart constructor checks for partition overlap, more than 1 active partitions and too many partitions *) | ||
let mbr = | ||
match Mbr.make partitions with | ||
| Ok mbr -> mbr | ||
| Error msg -> failwith ("Failed to create MBR: " ^ msg) | ||
in | ||
|
||
let oc = open_out_bin destination in | ||
let mbr_buffer = Cstruct.create Mbr.sizeof in | ||
Mbr.marshal mbr_buffer mbr; | ||
output oc (Cstruct.to_bytes mbr_buffer) 0 Mbr.sizeof; | ||
close_out oc; | ||
Unix.truncate destination total_size | ||
PizieDust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let destination = | ||
let doc = "Output file for the MBR formatted disk image" in | ||
Arg.( | ||
required | ||
& opt (some string) None | ||
& info [ "d"; "destination" ] ~docv:"destination" ~doc) | ||
|
||
let partition_files = | ||
let doc = "Data files to be written to the partitions" in | ||
Arg.(value & pos_all file [] & info [] ~docv:"partition_files" ~doc) | ||
|
||
let cmd = | ||
let doc = "Create an MBR formatted disk image with an MBR header." in | ||
let info = Cmd.info "create_mbr_disk" ~version:"1.0.0" ~doc in | ||
Cmd.v info Term.(const create_mbr_disk $ destination $ partition_files) | ||
|
||
let main () = exit (Cmd.eval cmd) | ||
let () = main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
(executables | ||
(names mbr_inspect read_partition write_partition resize_partition) | ||
(names | ||
mbr_inspect | ||
read_partition | ||
write_partition | ||
resize_partition | ||
create_mbr_disk) | ||
(libraries mbr cstruct cmdliner unix)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.