block#
Block device abstraction for OCaml 5 with Eio direct-style I/O and Bytesrw integration.
Overview#
This library provides a minimal block device interface inspired by MirageOS mirage-block but using Eio's effects-based concurrency instead of Lwt.
Features#
- Eio direct-style I/O (no monads)
- Bytesrw integration for streaming access
- Memory, file, string, and bigarray backends
- CRC32C integrity checking wrapper
- Sub-device views for partitioning
- Read-only wrapper
Installation#
Install with opam:
$ opam install block
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 block
Usage#
let run () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
(* Create a file-backed block device *)
let blk =
Block.of_file ~sw
Eio.Path.(fs / "disk.img")
~sector_size:512 ~create:1000L ()
in
let info = Block.info blk in
Printf.printf "Sectors: %Ld\n" info.sectors;
(* Write a sector, then read it back *)
let data = String.make 512 'A' in
Block.write blk 0L data |> Result.get_ok;
let read = Block.read blk 0L |> Result.get_ok in
assert (data = read);
(* Wrap with CRC32C: reads verify checksums, writes compute them *)
let safe_blk = Block.with_crc32c blk in
Block.close safe_blk
API#
Core Operations#
Block.info- Get device info (sector size, count, read/write)Block.read- Read a single sectorBlock.write- Write a single sectorBlock.read_many- Read multiple consecutive sectorsBlock.write_many- Write multiple consecutive sectorsBlock.sync- Flush pending writesBlock.close- Release resources
Backends#
Block.of_memory- In-memory device (for testing)Block.of_string- Read-only device from stringBlock.of_file- File-backed device (takesEio.Path.t)Block.of_bigarray- Bigarray-backed device
Wrappers#
Block.read_only- Read-only viewBlock.sub- Sub-device view (for partitions)Block.with_crc32c- CRC32C integrity checking
Bytesrw Integration#
Block.to_reader- Create sequential reader from offsetBlock.to_writer- Create sequential writer from offset
Related Work#
- mirage-block - MirageOS block interface (Lwt-based)
- mirage-block-unix - Unix backend for mirage-block
Licence#
ISC License. See LICENSE.md for details.