1libnbd-ocaml(3) LIBNBD libnbd-ocaml(3)
2
3
4
6 libnbd-ocaml - how to use libnbd from OCaml
7
9 let nbd = NBD.create () in
10 NBD.connect_uri nbd "nbd://localhost";
11 let size = NBD.get_size nbd in
12 printf "%Ld\n" size;
13 NBD.close ()
14
15 Alternate syntax which ensures that close is called even if an
16 exception is thrown:
17
18 let size =
19 NBD.with_handle (
20 fun nbd ->
21 NBD.connect_uri nbd "nbd://localhost";
22 NBD.get_size nbd
23 ) in
24 printf "%Ld\n" size
25
26 To compile:
27
28 ocamlopt -I +nbd mlnbd.cmxa prog.ml -o prog
29
30 or using findlib:
31
32 ocamlfind opt -package nbd -linkpkg prog.ml -o prog
33
35 This manual page documents how to use libnbd to access Network Block
36 Device (NBD) servers from the OCaml programming language.
37
38 The OCaml bindings work very similarly to the C bindings so you should
39 start by reading libnbd(3).
40
41 For OCaml API documentation see NBD(3).
42
44 Create a libnbd handle of type "NBD.t" by calling "NBD.create ()".
45
46 You can either close the handle explicitly by calling "NBD.close" or it
47 will be closed automatically when it is garbage collected. If you call
48 any other method on a handle which you have explicitly closed then the
49 API will throw an "NBD.Closed" exception.
50
51 "NBD.with_handle" can be used to make sure the handle is closed in a
52 timely manner. See the example in the "SYNOPSIS" above.
53
55 Libnbd errors are turned automatically into exceptions of type:
56
57 NBD.Error (str, Unix.error option)
58
59 The first exception parameter is a string which is the printable error
60 message. The second is the OCaml "Unix.error" code, if available (see
61 nbd_get_errno(3)).
62
63 Callbacks with "int ref" error parameter
64 Some callbacks take an error parameter of type "int ref", corresponding
65 to the "int *error" passed to those callbacks in C. See also:
66 "Callbacks with "int *error" parameter" in libnbd(3)
67
68 If an error occurs during the callback you can update the "int" in the
69 reference, setting it to a C-compatible errno. To convert an OCaml
70 "Unix.error" into a C-compatible errno call "NBD.errno_of_unix_error".
71
73 This directory contains examples written in OCaml:
74
75 https://gitlab.com/nbdkit/libnbd/tree/master/ocaml/examples
76
78 libnbd(3), NBD(3).
79
81 Richard W.M. Jones
82
84 Copyright (C) 2019-2021 Red Hat Inc.
85
87 This library is free software; you can redistribute it and/or modify it
88 under the terms of the GNU Lesser General Public License as published
89 by the Free Software Foundation; either version 2 of the License, or
90 (at your option) any later version.
91
92 This library is distributed in the hope that it will be useful, but
93 WITHOUT ANY WARRANTY; without even the implied warranty of
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95 Lesser General Public License for more details.
96
97 You should have received a copy of the GNU Lesser General Public
98 License along with this library; if not, write to the Free Software
99 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
100 02110-1301 USA
101
102
103
104libnbd-1.14.2 2023-01-03 libnbd-ocaml(3)