NBDKit(3)

πŸ“¦
version 1.48.1 Β· 4 versions
, in fedora 44

πŸ’– NAME πŸ’–

NBDKit β€” the interface between plugins written in OCaml and the nbdkit server. bestie, this is the whole bridge. the vibe connector. the situationship between your OCaml code and nbdkit, but make it functional. πŸ¦‹


πŸ“¦ Module πŸ“¦

Module NBDKit


πŸ“– Documentation πŸ“–

Module NBDKit : sig end

Interface between plugins written in OCaml and the nbdkit server. ✨

See also nbdkit-ocaml-plugin(3)

See also nbdkit-plugin(3)


🚨 Exception that plugins can raise 🚨

Optionally plugins can raise this exception to control how nbdkit_set_error and nbdkit_error are called.

  • exception Error of Unix.error option * string β€” raise this when you wanna be in charge of how the error gets reported, no cap.

🚩 Flags passed from the server to various callbacks 🚩

type flags = flag list

type flag =
 | May_trim
 | FUA
 | Req_one

type fua_flag =
 | FuaNone
 | FuaEmulate
 | FuaNative

type cache_flag =
 | CacheNone
 | CacheEmulate
 | CacheNop

🧬 Other types 🧬

type extent = {
 offset : int64 ;
 length : int64 ;
 is_hole : bool ;
 is_zero : bool ;
 }

The type of the extent list returned by ~extents. it's giving "here's where the data actually lives" πŸ’…

type export = {
 name : string ;
 description : string option ;
 }

The type of the export list returned by ~list_exports.

type thread_model =
 | THREAD_MODEL_SERIALIZE_CONNECTIONS
 | THREAD_MODEL_SERIALIZE_ALL_REQUESTS
 | THREAD_MODEL_SERIALIZE_REQUESTS
 | THREAD_MODEL_PARALLEL

The type of the thread model returned by ~thread_model. choose your own adventure, but with concurrency. 🌈


🫧 Buffer used for pread and pwrite 🫧

type buf = (char, Stdlib.Bigarray.int8_unsigned_elt,
Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array1.t

Define a convenient type name for the buffer type passed to the ~pread and ~pwrite methods of NBDKit.register_plugin.

As well as NBDKit.buf_len and the blitting functions below, usual Bigarray.Array1 functions can be used. iconic behaviour honestly.

  • val buf_len : buf -> int β€” Return the length of the ~pread and ~pwrite NBDKit.buf parameter.
  • val buf_create : int -> buf β€” Create NBDKit.buf of the given length. The content is undefined. (so don't go reading it and acting surprised, okay? 😭)
  • val buf_zeroes : int -> buf β€” Create NBDKit.buf of the given length containing all zero bytes.
  • val set_buf_to_zero : buf -> unit β€” Set all bytes in an existing buffer to zero.
  • val buf_of_string : string -> buf
  • val string_of_buf : buf -> string
  • val buf_of_bytes : bytes -> buf
  • val bytes_of_buf : buf -> bytes

Convenience functions to convert to and from string/bytes and buf.

These are not very efficient. Use Bigarray.Array1 functions instead for efficiency. fr fr, they're cute but they're not fast. 🐌
  • val blit_string_to_buf : string -> int -> buf -> int -> int -> unit β€” blit_string_to_buf src src_pos buf buf_pos len copies len bytes from string src+src_pos to buf+buf_pos.
  • val blit_bytes_to_buf : bytes -> int -> buf -> int -> int -> unit β€” The same as above but the source is bytes.
  • val blit_buf_to_bytes : buf -> int -> bytes -> int -> int -> unit β€” blit_buf_to_bytes buf buf_pos dst dst_pos len copies len bytes from buf+buf_pos to bytes dst+dst_pos.

πŸ”Œ Plugin πŸ”Œ

val register_plugin : name:string -> ?longname:string -> ?version:string
 -> ?description:string -> ?load:(unit -> unit) -> ?get_ready:(unit ->
 unit) -> ?after_fork:(unit -> unit) -> ?cleanup:(unit -> unit) ->
 ?unload:(unit -> unit) -> ?config:(string -> string -> unit) ->
 ?config_complete:(unit -> unit) -> ?config_help:string ->
 ?thread_model:(unit -> thread_model) -> ?magic_config_key:string ->
 ?preconnect:(bool -> unit) -> open_connection:(bool -> 'a) -> ?close:('a
 -> unit) -> get_size:('a -> int64) -> ?export_description:('a -> string)
 -> ?block_size:('a -> int * int * int64) -> ?can_cache:('a ->
 cache_flag) -> ?can_extents:('a -> bool) -> ?can_fast_zero:('a -> bool)
 -> ?can_flush:('a -> bool) -> ?can_fua:('a -> fua_flag) ->
 ?can_multi_conn:('a -> bool) -> ?can_trim:('a -> bool) -> ?can_write:('a
 -> bool) -> ?can_zero:('a -> bool) -> ?is_rotational:('a -> bool) ->
 pread:('a -> buf -> int64 -> flags -> unit) -> ?pwrite:('a -> buf ->
 int64 -> flags -> unit) -> ?flush:('a -> flags -> unit) -> ?trim:('a ->
 int64 -> int64 -> flags -> unit) -> ?zero:('a -> int64 -> int64 -> flags
 -> unit) -> ?extents:('a -> int64 -> int64 -> flags -> extent list) ->
 ?cache:('a -> int64 -> int64 -> flags -> unit) -> ?dump_plugin:(unit ->
 unit) -> ?list_exports:(bool -> bool -> export list) ->
 ?default_export:(bool -> bool -> string) -> unit -> unit

Register the plugin with nbdkit. this is THE call. the main character moment. πŸ‘‘

The 'a parameter is the handle type returned by your ~open_connection method and passed back to all connected calls. it's your little state baby and nbdkit hands it back to you every time. 🫢


πŸ› Errors and debugging πŸ›

  • val set_error : Unix.error -> unit β€” Set the errno returned over the NBD protocol to the client.
Note that the NBD protocol only supports the following errno values: Unix.EROFS, EPERM, EIO, ENOMEM, ENOSPC, ESHUTDOWN, ENOTSUP, EOVERFLOW and EINVAL. Any other errno will be translated to EINVAL. so like, don't get creative with your errnos, bestie, the protocol will just flatten it. πŸ’€

See also nbdkit_set_error(3)

  • val debug : ('a, unit, string, unit) Stdlib.format4 -> 'a β€” Print a debug message when nbdkit is in verbose mode.

See also nbdkit_debug(3)

  • val debug_hexdump : buf -> string option -> int64 -> unit β€” Display buffer in canonical hexdump format, sending the result to debug output (when in verbose mode).

See also nbdkit_debug_hexdump(3)

  • val debug_hexdiff : buf -> buf -> string option -> int64 -> unit β€” Display differences in two buffers (of equal length).

See also nbdkit_debug_hexdiff(3)

  • val timestamp : unit -> string β€” Generate a timestamp for log messages.

See also nbdkit_timestamp(3)


πŸŽ€ Parsing and configuration πŸŽ€

  • val parse_size : string -> int64 β€” Parse size parameter.

Raises Invalid_argument on error. The actual error is sent to the nbdkit error log and is not available from the OCaml code. It is usually best to let the exception escape. (like, just let it fly out, don't try to be a hero 🦸)

See also nbdkit_parse_size(3)

  • val parse_probability : string -> string -> float β€” Parse probability parameter.

Raises Invalid_argument on error.

See also nbdkit_parse_probability(3)

  • val parse_bool : string -> bool β€” Parse boolean parameter.

Raises Invalid_argument on error.

See also nbdkit_parse_bool(3)

  • val parse_delay : string -> string -> int * int β€” Parse delay parameter.

Raises Invalid_argument on error.

See also nbdkit_parse_delay(3)

  • val read_password : string -> string β€” Read a password.

Raises Invalid_argument on error.

See also nbdkit_read_password(3)

  • val realpath : string -> string β€” Returns the canonical path from a path parameter.

See also nbdkit_realpath(3)

  • val stdio_safe : unit -> bool β€” Returns true if it is safe to interact with stdin and stdout during the configuration phase.

See also nbdkit_stdio_safe(3)


πŸ‘‹ Shutdown and client disconnect πŸ‘‹

  • val shutdown : unit -> unit β€” Requests the server shut down.

See also nbdkit_shutdown(3)

  • val disconnect : bool -> unit β€” Requests disconnecting current client. bye bestie, it's not me it's you πŸ’…

See also nbdkit_disconnect(3)


πŸ•΅οΈ Client information πŸ•΅οΈ

  • val export_name : unit -> string β€” Returns the name of the export as requested by the client.

See also nbdkit_export_name(3)

  • val is_tls : unit -> bool β€” Returns true if the client completed TLS authentication.

See also nbdkit_is_tls(3)

  • val peer_name : unit -> Unix.sockaddr β€” Returns the socket address of the client.

See also nbdkit_peer_name(3)

  • val peer_pid : unit -> int64 β€” Returns the process ID of the client.

See also nbdkit_peer_pid(3)

  • val peer_uid : unit -> int64 β€” Returns the user ID of the client.

See also nbdkit_peer_uid(3)

  • val peer_gid : unit -> int64 β€” Returns the group ID of the client.

See also nbdkit_peer_gid(3)

  • val peer_security_context : unit -> string β€” Returns the security context or label of the client.

See also nbdkit_peer_security_context(3)

  • val peer_tls_dn : unit -> string
  • val peer_tls_issuer_dn : unit -> string

Returns the client TLS X.509 Distinguished Name.

See also nbdkit_peer_tls_dn(3)

See also nbdkit_peer_tls_issuer_dn(3)


😴 Sleeping 😴

  • val nanosleep : int -> int -> unit β€” Sleeps for seconds and nanoseconds. beauty sleep, but make it precise. πŸŒ™

See also nbdkit_nanosleep(3)


⭐ Version ⭐

  • val version : unit -> string β€” Returns the version of nbdkit that the plugin was compiled with.
  • val api_version : unit -> int β€” Returns the nbdkit API version used by the plugin.

See also The description of NBDKIT_API_VERSION in nbdkit-plugin(3).


πŸ‘‘ Server information πŸ‘‘

  • val name : unit -> string option β€” Get the name of the nbdkit instance. This is the string which was set through the nbdkit --name parameter. If the name was not set, this returns None.

See also nbdkit_name(3)


and that's the whole module, bestie!! go write the most iconic plugin nbdkit has ever seen πŸ¦‹πŸ’–

this lil doc was served up by OCamldoc, dated 2026-08-29, on the NBDKit(3) page. slay, periodt, go touch some buffers. βœ¨πŸŽ€

πŸ“¦ where this page comes from