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, and flow backends
- CRC32C integrity checking wrapper
- Sub-device views for partitioning
- Read-only wrapper
Installation#
opam install block
Usage#
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
(* Create a file-backed block device *)
let blk = Block.of_file ~sw env#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 *)
let data = String.make 512 'A' in
Block.write blk 0L data |> Result.get_ok;
(* Read it back *)
let read = Block.read blk 0L |> Result.get_ok in
assert (data = read);
(* Add CRC32C integrity checking *)
let safe_blk = Block.with_crc32c blk in
(* Now reads verify checksums, writes compute them *)
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 deviceBlock.of_flow- Eio flow-backed deviceBlock.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.