Block device abstraction for OCaml 5 with Eio direct-style I/O and Bytesrw integration
1
fork

Configure Feed

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

OCaml 93.3%
Dune 1.7%
Other 5.0%
41 1 0

Clone this repository

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

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

Download tar.gz
README.md

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 sector
  • Block.write - Write a single sector
  • Block.read_many - Read multiple consecutive sectors
  • Block.write_many - Write multiple consecutive sectors
  • Block.sync - Flush pending writes
  • Block.close - Release resources

Backends#

  • Block.of_memory - In-memory device (for testing)
  • Block.of_string - Read-only device from string
  • Block.of_file - File-backed device (takes Eio.Path.t)
  • Block.of_bigarray - Bigarray-backed device

Wrappers#

  • Block.read_only - Read-only view
  • Block.sub - Sub-device view (for partitions)
  • Block.with_crc32c - CRC32C integrity checking

Bytesrw Integration#

  • Block.to_reader - Create sequential reader from offset
  • Block.to_writer - Create sequential writer from offset

Licence#

ISC License. See LICENSE.md for details.