upstream: https://github.com/mirage/ocaml-gpt
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 89.1%
Roff 4.1%
PHP 1.5%
Dune 1.3%
Other 3.9%
53 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-gpt https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-gpt
git@git.recoil.org:gazagnaire.org/ocaml-gpt git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-gpt

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

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_lba and ending_lba are positional int64 args)
  • Gpt.Partition.is_zero_partition -- Check if entry is unused
  • Gpt.Partition.codec -- Wire codec for 128-byte partition entries
  • Gpt.Partition.sizeof -- Size in bytes (128)

GPT#

  • Gpt.v -- Create a GPT with partitions, disk geometry, and optional disk GUID
  • Gpt.pp -- Pretty-print
  • Gpt.sizeof -- Header size in bytes (92)

Serialization#

  • Gpt.marshal_header_to_bytes -- Serialize header to string
  • Gpt.marshal_partition_table_to_string -- Serialize partition table to string

Streaming#

  • Gpt.of_string -- Parse header, returning continuation for partition table
  • Gpt.read -- Read header + partition table from bytesrw reader
  • Gpt.write_header -- Write header to bytesrw writer
  • Gpt.write_partition_table -- Write partition table to bytesrw writer

Wire Codecs#

  • Gpt.header_codec -- Wire codec for 92-byte GPT header
  • Gpt.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#

Licence#

ISC