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 noticeable 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          (* Attach the disk image to libguestfs. *)
86          g#add_drive_opts ~format:"raw" ~readonly:false output;
87
88          (* Run the libguestfs back-end. *)
89          g#launch ();
90
91          (* Get the list of devices.  Because we only added one drive
92           * above, we expect that this list should contain a single
93           * element.
94           *)
95          let devices = g#list_devices () in
96          if Array.length devices <> 1 then
97            failwith "error: expected a single device from list-devices";
98
99          (* Partition the disk as one single MBR partition. *)
100          g#part_disk devices.(0) "mbr";
101
102          (* Get the list of partitions.  We expect a single element, which
103           * is the partition we have just created.
104           *)
105          let partitions = g#list_partitions () in
106          if Array.length partitions <> 1 then
107            failwith "error: expected a single partition from list-partitions";
108
109          (* Create a filesystem on the partition. *)
110          g#mkfs "ext4" partitions.(0);
111
112          (* Now mount the filesystem so that we can add files. *)
113          g#mount partitions.(0) "/";
114
115          (* Create some files and directories. *)
116          g#touch "/empty";
117          let message = "Hello, world\n" in
118          g#write "/hello" message;
119          g#mkdir "/foo";
120
121          (* This one uploads the local file /etc/resolv.conf into
122           * the disk image.
123           *)
124          g#upload "/etc/resolv.conf" "/foo/resolv.conf";
125
126          (* Because we wrote to the disk and we want to detect write
127           * errors, call g#shutdown.  You don't need to do this:
128           * g#close will do it implicitly.
129           *)
130          g#shutdown ();
131
132          (* Note also that handles are automatically closed if they are
133           * reaped by the garbage collector.  You only need to call close
134           * if you want to close the handle right away.
135           *)
136          g#close ()
137

EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE

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

SEE ALSO

203       guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-java(3),
204       guestfs-lua(3), guestfs-perl(3), guestfs-python(3), guestfs-recipes(1),
205       guestfs-ruby(3), http://libguestfs.org/, http://caml.inria.fr/.
206

AUTHORS

208       Richard W.M. Jones ("rjones at redhat dot com")
209
211       Copyright (C) 2010-2012 Red Hat Inc.
212

LICENSE

214       This manual page contains examples which we hope you will use in your
215       programs.  The examples may be freely copied, modified and distributed
216       for any purpose without any restrictions.
217

BUGS

219       To get a list of bugs against libguestfs, use this link:
220       https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
221
222       To report a new bug against libguestfs, use this link:
223       https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
224
225       When reporting a bug, please supply:
226
227       ·   The version of libguestfs.
228
229       ·   Where you got libguestfs (eg. which Linux distro, compiled from
230           source, etc)
231
232       ·   Describe the bug accurately and give a way to reproduce it.
233
234       ·   Run libguestfs-test-tool(1) and paste the complete, unedited output
235           into the bug report.
236
237
238
239libguestfs-1.20.11                2013-08-27                  guestfs-ocaml(3)
Impressum