gpt#
A library to manipulate GUID Partition Tables in pure OCaml.
Overview#
Pure OCaml library for reading and writing GUID Partition Tables (GPT). Useful for creating bootable disk images with modern partition layouts.
Features#
- Full GPT header and partition table encoding/decoding
- Wire codecs for zero-copy header and partition entry access
- Streaming I/O with bytesrw readers/writers
- Protective MBR generation
- CRC-32 integrity checking
- Partition GUID and type GUID support via uuidm
Installation#
Install with opam:
$ opam install nox-gpt
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-gpt
Usage#
Creating partitions and GPT#
let type_guid =
Uuidm.of_string "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |> Option.get
let part =
match Gpt.Partition.make ~type_guid ~attributes:0L 2048L 1048575L with
| Ok p -> p
| Error e -> failwith e
let gpt =
match Gpt.v ~disk_sectors:2097152L ~sector_size:512 [ part ] with
| Ok g -> g
| Error e -> failwith e
let () = Fmt.pr "%a@." Gpt.pp gpt
Reading from bytes#
let parse_gpt ~read_sectors header_bytes =
match Gpt.of_string header_bytes ~sector_size:512 with
| Error e -> failwith e
| Ok (`Read_partition_table (lba, num_sectors), k) ->
let table_bytes = read_sectors lba num_sectors in
(match k table_bytes with
| Ok gpt -> gpt
| Error e -> failwith e)
Streaming I/O with bytesrw#
let read_gpt reader =
match Gpt.read reader ~sector_size:512 with
| Ok gpt -> gpt
| Error e -> failwith e
let write_gpt writer gpt =
Gpt.write_header writer ~sector_size:512 ~primary:true gpt;
Gpt.write_partition_table writer ~sector_size:512 gpt
Protective MBR#
let mbr = Gpt.protective_mbr ~sector_size:512 gpt
API#
Partition#
Gpt.Partition.make-- Create a partition entry (starting_lbaandending_lbaare positionalint64args)Gpt.Partition.is_zero_partition-- Check if entry is unusedGpt.Partition.codec-- Wire codec for 128-byte partition entriesGpt.Partition.sizeof-- Size in bytes (128)
GPT#
Gpt.v-- Create a GPT with partitions, disk geometry, and optional disk GUIDGpt.pp-- Pretty-printGpt.sizeof-- Header size in bytes (92)
Serialization#
Gpt.marshal_header_to_bytes-- Serialize header to stringGpt.marshal_partition_table_to_string-- Serialize partition table to string
Streaming#
Gpt.of_string-- Parse header, returning continuation for partition tableGpt.read-- Read header + partition table from bytesrw readerGpt.write_header-- Write header to bytesrw writerGpt.write_partition_table-- Write partition table to bytesrw writer
Wire Codecs#
Gpt.header_codec-- Wire codec for 92-byte GPT headerGpt.Partition.codec-- Wire codec for 128-byte partition entry
Interop#
Gpt.protective_mbr-- Create a protective MBR for GPT-formatted disks
Requirements#
- OCaml >= 5.1
- bytesrw, wire, uuidm, checkseum, mbr
References#
- UEFI Specification -- Chapter 5: GUID Partition Table Format
Licence#
ISC