1guestfs-ocaml(3)            Virtualization Support            guestfs-ocaml(3)
2
3
4

NAME

6       guestfs-ocaml - How to use libguestfs from OCaml
7

SYNOPSIS

9       Module style:
10
11        let g = Guestfs.create () in
12        Guestfs.add_drive_opts g ~format:"raw" ~readonly:true "disk.img";
13        Guestfs.launch g;
14
15       Object-oriented style:
16
17        let g = new Guestfs.guestfs () in
18        g#add_drive_opts ~format:"raw" ~readonly:true "disk.img";
19        g#launch ();
20
21        ocamlfind opt prog.ml -package guestfs -linkpkg -o prog
22       or:
23        ocamlopt -I +guestfs mlguestfs.cmxa prog.ml -o prog
24

DESCRIPTION

26       This manual page documents how to call libguestfs from the OCaml
27       programming language.  This page just documents the differences from
28       the C API and gives some examples.  If you are not familiar with using
29       libguestfs, you also need to read guestfs(3).
30
31   PROGRAMMING STYLES
32       There are two different programming styles supported by the OCaml
33       bindings.  You can use a module style, with each C function mapped to
34       an OCaml function:
35
36        int guestfs_set_verbose (guestfs_h *g, int flag);
37
38       becomes:
39
40        val Guestfs.set_verbose : Guestfs.t -> bool -> unit
41
42       Alternately you can use an object-oriented style, calling methods on
43       the class "Guestfs.guestfs":
44
45        method set_verbose : bool -> unit
46
47       The object-oriented style is usually briefer, and the minor performance
48       penalty isn't noticable in the general overhead of performing
49       libguestfs functions.
50
51   CLOSING THE HANDLE
52       The handle is closed when it is reaped by the garbage collector.
53       Because libguestfs handles include a lot of state, it is also possible
54       to close (and hence free) them explicitly by calling "Guestfs.close" or
55       the "#close" method.
56
57   EXCEPTIONS
58       Errors from libguestfs functions are mapped into the "Guestfs.Error"
59       exception.  This has a single parameter which is the error message (a
60       string).
61
62       Calling any function/method on a closed handle raises
63       "Guestfs.Handle_closed".  The single parameter is the name of the
64       function that you called.
65

EXAMPLE 1: CREATE A DISK IMAGE

67        (* Example showing how to create a disk image. *)
68
69        open Unix
70        open Printf
71
72        let output = "disk.img"
73
74        let () =
75          let g = new Guestfs.guestfs () in
76
77          (* Create a raw-format sparse disk image, 512 MB in size. *)
78          let fd = openfile output [O_WRONLY;O_CREAT;O_TRUNC;O_NOCTTY] 0o666 in
79          ftruncate fd (512 * 1024 * 1024);
80          close fd;
81
82          (* Set the trace flag so that we can see each libguestfs call. *)
83          g#set_trace true;
84
85          (* Set the autosync flag so that the disk will be synchronized
86           * automatically when the libguestfs handle is closed.
87           *)
88          g#set_autosync true;
89
90          (* Attach the disk image to libguestfs. *)
91          g#add_drive_opts ~format:"raw" ~readonly:false output;
92
93          (* Run the libguestfs back-end. *)
94          g#launch ();
95
96          (* Get the list of devices.  Because we only added one drive
97           * above, we expect that this list should contain a single
98           * element.
99           *)
100          let devices = g#list_devices () in
101          if Array.length devices <> 1 then
102            failwith "error: expected a single device from list-devices";
103
104          (* Partition the disk as one single MBR partition. *)
105          g#part_disk devices.(0) "mbr";
106
107          (* Get the list of partitions.  We expect a single element, which
108           * is the partition we have just created.
109           *)
110          let partitions = g#list_partitions () in
111          if Array.length partitions <> 1 then
112            failwith "error: expected a single partition from list-partitions";
113
114          (* Create a filesystem on the partition. *)
115          g#mkfs "ext4" partitions.(0);
116
117          (* Now mount the filesystem so that we can add files. *)
118          g#mount_options "" partitions.(0) "/";
119
120          (* Create some files and directories. *)
121          g#touch "/empty";
122          let message = "Hello, world\n" in
123          g#write "/hello" message;
124          g#mkdir "/foo";
125
126          (* This one uploads the local file /etc/resolv.conf into
127           * the disk image.
128           *)
129          g#upload "/etc/resolv.conf" "/foo/resolv.conf";
130
131          (* Because 'autosync' was set (above) we can just close the handle
132           * and the disk contents will be synchronized.  You can also do
133           * this manually by calling g#umount_all and g#sync.
134           *
135           * Note also that handles are automatically closed if they are
136           * reaped by the garbage collector.  You only need to call close
137           * if you want to close the handle right away.
138           *)
139          g#close ()
140

EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE

142        (* Example showing how to inspect a virtual machine disk. *)
143
144        open Printf
145
146        let disk =
147          if Array.length Sys.argv = 2 then
148            Sys.argv.(1)
149          else
150            failwith "usage: inspect_vm disk.img"
151
152        let () =
153          let g = new Guestfs.guestfs () in
154
155          (* Attach the disk image read-only to libguestfs. *)
156          g#add_drive_opts (*~format:"raw"*) ~readonly:true disk;
157
158          (* Run the libguestfs back-end. *)
159          g#launch ();
160
161          (* Ask libguestfs to inspect for operating systems. *)
162          let roots = g#inspect_os () in
163          if Array.length roots = 0 then
164            failwith "inspect_vm: no operating systems found";
165
166          Array.iter (
167            fun root ->
168              printf "Root device: %s\n" root;
169
170              (* Print basic information about the operating system. *)
171              printf "  Product name: %s\n" (g#inspect_get_product_name root);
172              printf "  Version:      %d.%d\n"
173                (g#inspect_get_major_version root)
174                (g#inspect_get_minor_version root);
175              printf "  Type:         %s\n" (g#inspect_get_type root);
176              printf "  Distro:       %s\n" (g#inspect_get_distro root);
177
178              (* Mount up the disks, like guestfish -i.
179               *
180               * Sort keys by length, shortest first, so that we end up
181               * mounting the filesystems in the correct order.
182               *)
183              let mps = g#inspect_get_mountpoints root in
184              let cmp (a,_) (b,_) =
185                compare (String.length a) (String.length b) in
186              let mps = List.sort cmp mps in
187              List.iter (
188                fun (mp, dev) ->
189                  try g#mount_ro dev mp
190                  with Guestfs.Error msg -> eprintf "%s (ignored)\n" msg
191              ) mps;
192
193              (* If /etc/issue.net file exists, print up to 3 lines. *)
194              let filename = "/etc/issue.net" in
195              if g#is_file filename then (
196                printf "--- %s ---\n" filename;
197                let lines = g#head_n 3 filename in
198                Array.iter print_endline lines
199              );
200
201              (* Unmount everything. *)
202              g#umount_all ()
203          ) roots
204

SEE ALSO

206       guestfs(3), guestfs-examples(3), guestfs-python(3), guestfs-ruby(3),
207       <http://libguestfs.org/>, <http://caml.inria.fr/>.
208

AUTHORS

210       Richard W.M. Jones ("rjones at redhat dot com")
211
213       Copyright (C) 2010 Red Hat Inc. <http://libguestfs.org/>
214
215       The examples in this manual page may be freely copied, modified and
216       distributed without any restrictions.
217
218       This library is free software; you can redistribute it and/or modify it
219       under the terms of the GNU Lesser General Public License as published
220       by the Free Software Foundation; either version 2 of the License, or
221       (at your option) any later version.
222
223       This library is distributed in the hope that it will be useful, but
224       WITHOUT ANY WARRANTY; without even the implied warranty of
225       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
226       Lesser General Public License for more details.
227
228       You should have received a copy of the GNU Lesser General Public
229       License along with this library; if not, write to the Free Software
230       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
231       02110-1301 USA
232
233
234
235libguestfs-1.8.15                 2011-11-10                  guestfs-ocaml(3)
Impressum