Synchronize Virterror with libvirt 4.6.0
[ocaml-libvirt.git] / libvirt / libvirt.ml
1 (* OCaml bindings for libvirt.
2    (C) Copyright 2007-2015 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version,
9    with the OCaml linking exception described in ../COPYING.LIB.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 *)
20
21 type uuid = string
22
23 type xml = string
24
25 type filename = string
26
27 external get_version : ?driver:string -> unit -> int * int = "ocaml_libvirt_get_version"
28
29 let uuid_length = 16
30 let uuid_string_length = 36
31
32 (* http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/80683af867cce6bf8fff273973f70c95.en.html *)
33 type rw = [`R|`W]
34 type ro = [`R]
35
36 module Connect =
37 struct
38   type 'rw t
39
40   type node_info = {
41     model : string;
42     memory : int64;
43     cpus : int;
44     mhz : int;
45     nodes : int;
46     sockets : int;
47     cores : int;
48     threads : int;
49   }
50
51   type credential_type =
52     | CredentialUsername
53     | CredentialAuthname
54     | CredentialLanguage
55     | CredentialCnonce
56     | CredentialPassphrase
57     | CredentialEchoprompt
58     | CredentialNoechoprompt
59     | CredentialRealm
60     | CredentialExternal
61
62   type credential = {
63     typ : credential_type;
64     prompt : string;
65     challenge : string option;
66     defresult : string option;
67   }
68
69   type auth = {
70     credtype : credential_type list;
71     cb : (credential list -> string option list);
72   }
73
74   type list_flag =
75     | ListNoState | ListRunning | ListBlocked
76     | ListPaused | ListShutdown | ListShutoff | ListCrashed
77     | ListActive
78     | ListInactive
79     | ListAll
80
81   external connect : ?name:string -> unit -> rw t = "ocaml_libvirt_connect_open"
82   external connect_readonly : ?name:string -> unit -> ro t = "ocaml_libvirt_connect_open_readonly"
83   external connect_auth : ?name:string -> auth -> rw t = "ocaml_libvirt_connect_open_auth"
84   external connect_auth_readonly : ?name:string -> auth -> ro t = "ocaml_libvirt_connect_open_auth_readonly"
85   external close : [>`R] t -> unit = "ocaml_libvirt_connect_close"
86   external get_type : [>`R] t -> string = "ocaml_libvirt_connect_get_type"
87   external get_version : [>`R] t -> int = "ocaml_libvirt_connect_get_version"
88   external get_hostname : [>`R] t -> string = "ocaml_libvirt_connect_get_hostname"
89   external get_uri : [>`R] t -> string = "ocaml_libvirt_connect_get_uri"
90   external get_max_vcpus : [>`R] t -> ?type_:string -> unit -> int = "ocaml_libvirt_connect_get_max_vcpus"
91   external list_domains : [>`R] t -> int -> int array = "ocaml_libvirt_connect_list_domains"
92   external num_of_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_domains"
93   external get_capabilities : [>`R] t -> xml = "ocaml_libvirt_connect_get_capabilities"
94   external num_of_defined_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_domains"
95   external list_defined_domains : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_domains"
96   external num_of_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_networks"
97   external list_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_networks"
98   external num_of_defined_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_networks"
99   external list_defined_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_networks"
100   external num_of_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_storage_pools"
101   external list_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_storage_pools"
102   external num_of_defined_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_storage_pools"
103   external list_defined_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_storage_pools"
104
105   external get_node_info : [>`R] t -> node_info = "ocaml_libvirt_connect_get_node_info"
106   external node_get_free_memory : [> `R] t -> int64 = "ocaml_libvirt_connect_node_get_free_memory"
107   external node_get_cells_free_memory : [> `R] t -> int -> int -> int64 array = "ocaml_libvirt_connect_node_get_cells_free_memory"
108
109   (* See VIR_NODEINFO_MAXCPUS macro defined in <libvirt.h>. *)
110   let maxcpus_of_node_info { nodes = nodes; sockets = sockets;
111                              cores = cores; threads = threads } =
112     nodes * sockets * cores * threads
113
114   (* See VIR_CPU_MAPLEN macro defined in <libvirt.h>. *)
115   let cpumaplen nr_cpus =
116     (nr_cpus + 7) / 8
117
118   (* See VIR_USE_CPU, VIR_UNUSE_CPU, VIR_CPU_USABLE macros defined in <libvirt.h>. *)
119   let use_cpu cpumap cpu =
120     Bytes.set cpumap (cpu/8)
121       (Char.chr (Char.code (Bytes.get cpumap (cpu/8)) lor (1 lsl (cpu mod 8))))
122   let unuse_cpu cpumap cpu =
123     Bytes.set cpumap (cpu/8)
124       (Char.chr (Char.code (Bytes.get cpumap (cpu/8)) land (lnot (1 lsl (cpu mod 8)))))
125   let cpu_usable cpumaps maplen vcpu cpu =
126     Char.code (Bytes.get cpumaps (vcpu*maplen + cpu/8)) land (1 lsl (cpu mod 8)) <> 0
127
128   external set_keep_alive : [>`R] t -> int -> int -> unit = "ocaml_libvirt_connect_set_keep_alive"
129
130   external const : [>`R] t -> ro t = "%identity"
131 end
132
133 module Virterror =
134 struct
135   type code =
136     | VIR_ERR_OK
137     | VIR_ERR_INTERNAL_ERROR
138     | VIR_ERR_NO_MEMORY
139     | VIR_ERR_NO_SUPPORT
140     | VIR_ERR_UNKNOWN_HOST
141     | VIR_ERR_NO_CONNECT
142     | VIR_ERR_INVALID_CONN
143     | VIR_ERR_INVALID_DOMAIN
144     | VIR_ERR_INVALID_ARG
145     | VIR_ERR_OPERATION_FAILED
146     | VIR_ERR_GET_FAILED
147     | VIR_ERR_POST_FAILED
148     | VIR_ERR_HTTP_ERROR
149     | VIR_ERR_SEXPR_SERIAL
150     | VIR_ERR_NO_XEN
151     | VIR_ERR_XEN_CALL
152     | VIR_ERR_OS_TYPE
153     | VIR_ERR_NO_KERNEL
154     | VIR_ERR_NO_ROOT
155     | VIR_ERR_NO_SOURCE
156     | VIR_ERR_NO_TARGET
157     | VIR_ERR_NO_NAME
158     | VIR_ERR_NO_OS
159     | VIR_ERR_NO_DEVICE
160     | VIR_ERR_NO_XENSTORE
161     | VIR_ERR_DRIVER_FULL
162     | VIR_ERR_CALL_FAILED
163     | VIR_ERR_XML_ERROR
164     | VIR_ERR_DOM_EXIST
165     | VIR_ERR_OPERATION_DENIED
166     | VIR_ERR_OPEN_FAILED
167     | VIR_ERR_READ_FAILED
168     | VIR_ERR_PARSE_FAILED
169     | VIR_ERR_CONF_SYNTAX
170     | VIR_ERR_WRITE_FAILED
171     | VIR_ERR_XML_DETAIL
172     | VIR_ERR_INVALID_NETWORK
173     | VIR_ERR_NETWORK_EXIST
174     | VIR_ERR_SYSTEM_ERROR
175     | VIR_ERR_RPC
176     | VIR_ERR_GNUTLS_ERROR
177     | VIR_WAR_NO_NETWORK
178     | VIR_ERR_NO_DOMAIN
179     | VIR_ERR_NO_NETWORK
180     | VIR_ERR_INVALID_MAC
181     | VIR_ERR_AUTH_FAILED
182     | VIR_ERR_INVALID_STORAGE_POOL
183     | VIR_ERR_INVALID_STORAGE_VOL
184     | VIR_WAR_NO_STORAGE
185     | VIR_ERR_NO_STORAGE_POOL
186     | VIR_ERR_NO_STORAGE_VOL
187     | VIR_WAR_NO_NODE
188     | VIR_ERR_INVALID_NODE_DEVICE
189     | VIR_ERR_NO_NODE_DEVICE
190     | VIR_ERR_NO_SECURITY_MODEL
191     | VIR_ERR_OPERATION_INVALID
192     | VIR_WAR_NO_INTERFACE
193     | VIR_ERR_NO_INTERFACE
194     | VIR_ERR_INVALID_INTERFACE
195     | VIR_ERR_MULTIPLE_INTERFACES
196     | VIR_WAR_NO_NWFILTER
197     | VIR_ERR_INVALID_NWFILTER
198     | VIR_ERR_NO_NWFILTER
199     | VIR_ERR_BUILD_FIREWALL
200     | VIR_WAR_NO_SECRET
201     | VIR_ERR_INVALID_SECRET
202     | VIR_ERR_NO_SECRET
203     | VIR_ERR_CONFIG_UNSUPPORTED
204     | VIR_ERR_OPERATION_TIMEOUT
205     | VIR_ERR_MIGRATE_PERSIST_FAILED
206     | VIR_ERR_HOOK_SCRIPT_FAILED
207     | VIR_ERR_INVALID_DOMAIN_SNAPSHOT
208     | VIR_ERR_NO_DOMAIN_SNAPSHOT
209     | VIR_ERR_INVALID_STREAM
210     | VIR_ERR_ARGUMENT_UNSUPPORTED
211     | VIR_ERR_STORAGE_PROBE_FAILED
212     | VIR_ERR_STORAGE_POOL_BUILT
213     | VIR_ERR_SNAPSHOT_REVERT_RISKY
214     | VIR_ERR_OPERATION_ABORTED
215     | VIR_ERR_AUTH_CANCELLED
216     | VIR_ERR_NO_DOMAIN_METADATA
217     | VIR_ERR_MIGRATE_UNSAFE
218     | VIR_ERR_OVERFLOW
219     | VIR_ERR_BLOCK_COPY_ACTIVE
220     | VIR_ERR_OPERATION_UNSUPPORTED
221     | VIR_ERR_SSH
222     | VIR_ERR_AGENT_UNRESPONSIVE
223     | VIR_ERR_RESOURCE_BUSY
224     | VIR_ERR_ACCESS_DENIED
225     | VIR_ERR_DBUS_SERVICE
226     | VIR_ERR_STORAGE_VOL_EXIST
227     | VIR_ERR_CPU_INCOMPATIBLE
228     | VIR_ERR_XML_INVALID_SCHEMA
229     | VIR_ERR_MIGRATE_FINISH_OK
230     | VIR_ERR_AUTH_UNAVAILABLE
231     | VIR_ERR_NO_SERVER
232     | VIR_ERR_NO_CLIENT
233     | VIR_ERR_AGENT_UNSYNCED
234     | VIR_ERR_LIBSSH
235     | VIR_ERR_DEVICE_MISSING
236     | VIR_ERR_INVALID_NWFILTER_BINDING
237     | VIR_ERR_NO_NWFILTER_BINDING
238     | VIR_ERR_UNKNOWN of int
239
240   let string_of_code = function
241     | VIR_ERR_OK -> "VIR_ERR_OK"
242     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
243     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
244     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
245     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
246     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
247     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
248     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
249     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
250     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
251     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
252     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
253     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
254     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
255     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
256     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
257     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
258     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
259     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
260     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
261     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
262     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
263     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
264     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
265     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
266     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
267     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
268     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
269     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
270     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
271     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
272     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
273     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
274     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
275     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
276     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
277     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
278     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
279     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
280     | VIR_ERR_RPC -> "VIR_ERR_RPC"
281     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
282     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
283     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
284     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
285     | VIR_ERR_INVALID_MAC -> "VIR_ERR_INVALID_MAC"
286     | VIR_ERR_AUTH_FAILED -> "VIR_ERR_AUTH_FAILED"
287     | VIR_ERR_INVALID_STORAGE_POOL -> "VIR_ERR_INVALID_STORAGE_POOL"
288     | VIR_ERR_INVALID_STORAGE_VOL -> "VIR_ERR_INVALID_STORAGE_VOL"
289     | VIR_WAR_NO_STORAGE -> "VIR_WAR_NO_STORAGE"
290     | VIR_ERR_NO_STORAGE_POOL -> "VIR_ERR_NO_STORAGE_POOL"
291     | VIR_ERR_NO_STORAGE_VOL -> "VIR_ERR_NO_STORAGE_VOL"
292     | VIR_WAR_NO_NODE -> "VIR_WAR_NO_NODE"
293     | VIR_ERR_INVALID_NODE_DEVICE -> "VIR_ERR_INVALID_NODE_DEVICE"
294     | VIR_ERR_NO_NODE_DEVICE -> "VIR_ERR_NO_NODE_DEVICE"
295     | VIR_ERR_NO_SECURITY_MODEL -> "VIR_ERR_NO_SECURITY_MODEL"
296     | VIR_ERR_OPERATION_INVALID -> "VIR_ERR_OPERATION_INVALID"
297     | VIR_WAR_NO_INTERFACE -> "VIR_WAR_NO_INTERFACE"
298     | VIR_ERR_NO_INTERFACE -> "VIR_ERR_NO_INTERFACE"
299     | VIR_ERR_INVALID_INTERFACE -> "VIR_ERR_INVALID_INTERFACE"
300     | VIR_ERR_MULTIPLE_INTERFACES -> "VIR_ERR_MULTIPLE_INTERFACES"
301     | VIR_WAR_NO_NWFILTER -> "VIR_WAR_NO_NWFILTER"
302     | VIR_ERR_INVALID_NWFILTER -> "VIR_ERR_INVALID_NWFILTER"
303     | VIR_ERR_NO_NWFILTER -> "VIR_ERR_NO_NWFILTER"
304     | VIR_ERR_BUILD_FIREWALL -> "VIR_ERR_BUILD_FIREWALL"
305     | VIR_WAR_NO_SECRET -> "VIR_WAR_NO_SECRET"
306     | VIR_ERR_INVALID_SECRET -> "VIR_ERR_INVALID_SECRET"
307     | VIR_ERR_NO_SECRET -> "VIR_ERR_NO_SECRET"
308     | VIR_ERR_CONFIG_UNSUPPORTED -> "VIR_ERR_CONFIG_UNSUPPORTED"
309     | VIR_ERR_OPERATION_TIMEOUT -> "VIR_ERR_OPERATION_TIMEOUT"
310     | VIR_ERR_MIGRATE_PERSIST_FAILED -> "VIR_ERR_MIGRATE_PERSIST_FAILED"
311     | VIR_ERR_HOOK_SCRIPT_FAILED -> "VIR_ERR_HOOK_SCRIPT_FAILED"
312     | VIR_ERR_INVALID_DOMAIN_SNAPSHOT -> "VIR_ERR_INVALID_DOMAIN_SNAPSHOT"
313     | VIR_ERR_NO_DOMAIN_SNAPSHOT -> "VIR_ERR_NO_DOMAIN_SNAPSHOT"
314     | VIR_ERR_INVALID_STREAM -> "VIR_ERR_INVALID_STREAM"
315     | VIR_ERR_ARGUMENT_UNSUPPORTED -> "VIR_ERR_ARGUMENT_UNSUPPORTED"
316     | VIR_ERR_STORAGE_PROBE_FAILED -> "VIR_ERR_STORAGE_PROBE_FAILED"
317     | VIR_ERR_STORAGE_POOL_BUILT -> "VIR_ERR_STORAGE_POOL_BUILT"
318     | VIR_ERR_SNAPSHOT_REVERT_RISKY -> "VIR_ERR_SNAPSHOT_REVERT_RISKY"
319     | VIR_ERR_OPERATION_ABORTED -> "VIR_ERR_OPERATION_ABORTED"
320     | VIR_ERR_AUTH_CANCELLED -> "VIR_ERR_AUTH_CANCELLED"
321     | VIR_ERR_NO_DOMAIN_METADATA -> "VIR_ERR_NO_DOMAIN_METADATA"
322     | VIR_ERR_MIGRATE_UNSAFE -> "VIR_ERR_MIGRATE_UNSAFE"
323     | VIR_ERR_OVERFLOW -> "VIR_ERR_OVERFLOW"
324     | VIR_ERR_BLOCK_COPY_ACTIVE -> "VIR_ERR_BLOCK_COPY_ACTIVE"
325     | VIR_ERR_OPERATION_UNSUPPORTED -> "VIR_ERR_OPERATION_UNSUPPORTED"
326     | VIR_ERR_SSH -> "VIR_ERR_SSH"
327     | VIR_ERR_AGENT_UNRESPONSIVE -> "VIR_ERR_AGENT_UNRESPONSIVE"
328     | VIR_ERR_RESOURCE_BUSY -> "VIR_ERR_RESOURCE_BUSY"
329     | VIR_ERR_ACCESS_DENIED -> "VIR_ERR_ACCESS_DENIED"
330     | VIR_ERR_DBUS_SERVICE -> "VIR_ERR_DBUS_SERVICE"
331     | VIR_ERR_STORAGE_VOL_EXIST -> "VIR_ERR_STORAGE_VOL_EXIST"
332     | VIR_ERR_CPU_INCOMPATIBLE -> "VIR_ERR_CPU_INCOMPATIBLE"
333     | VIR_ERR_XML_INVALID_SCHEMA -> "VIR_ERR_XML_INVALID_SCHEMA"
334     | VIR_ERR_MIGRATE_FINISH_OK -> "VIR_ERR_MIGRATE_FINISH_OK"
335     | VIR_ERR_AUTH_UNAVAILABLE -> "VIR_ERR_AUTH_UNAVAILABLE"
336     | VIR_ERR_NO_SERVER -> "VIR_ERR_NO_SERVER"
337     | VIR_ERR_NO_CLIENT -> "VIR_ERR_NO_CLIENT"
338     | VIR_ERR_AGENT_UNSYNCED -> "VIR_ERR_AGENT_UNSYNCED"
339     | VIR_ERR_LIBSSH -> "VIR_ERR_LIBSSH"
340     | VIR_ERR_DEVICE_MISSING -> "VIR_ERR_DEVICE_MISSING"
341     | VIR_ERR_INVALID_NWFILTER_BINDING -> "VIR_ERR_INVALID_NWFILTER_BINDING"
342     | VIR_ERR_NO_NWFILTER_BINDING -> "VIR_ERR_NO_NWFILTER_BINDING"
343     | VIR_ERR_UNKNOWN i -> "VIR_ERR_" ^ string_of_int i
344
345   type domain =
346     | VIR_FROM_NONE
347     | VIR_FROM_XEN
348     | VIR_FROM_XEND
349     | VIR_FROM_XENSTORE
350     | VIR_FROM_SEXPR
351     | VIR_FROM_XML
352     | VIR_FROM_DOM
353     | VIR_FROM_RPC
354     | VIR_FROM_PROXY
355     | VIR_FROM_CONF
356     | VIR_FROM_QEMU
357     | VIR_FROM_NET
358     | VIR_FROM_TEST
359     | VIR_FROM_REMOTE
360     | VIR_FROM_OPENVZ
361     | VIR_FROM_XENXM
362     | VIR_FROM_STATS_LINUX
363     | VIR_FROM_LXC
364     | VIR_FROM_STORAGE
365     | VIR_FROM_NETWORK
366     | VIR_FROM_DOMAIN
367     | VIR_FROM_UML
368     | VIR_FROM_NODEDEV
369     | VIR_FROM_XEN_INOTIFY
370     | VIR_FROM_SECURITY
371     | VIR_FROM_VBOX
372     | VIR_FROM_INTERFACE
373     | VIR_FROM_ONE
374     | VIR_FROM_ESX
375     | VIR_FROM_PHYP
376     | VIR_FROM_SECRET
377     | VIR_FROM_CPU
378     | VIR_FROM_XENAPI
379     | VIR_FROM_NWFILTER
380     | VIR_FROM_HOOK
381     | VIR_FROM_DOMAIN_SNAPSHOT
382     | VIR_FROM_AUDIT
383     | VIR_FROM_SYSINFO
384     | VIR_FROM_STREAMS
385     | VIR_FROM_VMWARE
386     | VIR_FROM_EVENT
387     | VIR_FROM_LIBXL
388     | VIR_FROM_LOCKING
389     | VIR_FROM_HYPERV
390     | VIR_FROM_CAPABILITIES
391     | VIR_FROM_URI
392     | VIR_FROM_AUTH
393     | VIR_FROM_DBUS
394     | VIR_FROM_PARALLELS
395     | VIR_FROM_DEVICE
396     | VIR_FROM_SSH
397     | VIR_FROM_LOCKSPACE
398     | VIR_FROM_INITCTL
399     | VIR_FROM_IDENTITY
400     | VIR_FROM_CGROUP
401     | VIR_FROM_ACCESS
402     | VIR_FROM_SYSTEMD
403     | VIR_FROM_BHYVE
404     | VIR_FROM_CRYPTO
405     | VIR_FROM_FIREWALL
406     | VIR_FROM_POLKIT
407     | VIR_FROM_THREAD
408     | VIR_FROM_ADMIN
409     | VIR_FROM_LOGGING
410     | VIR_FROM_XENXL
411     | VIR_FROM_PERF
412     | VIR_FROM_LIBSSH
413     | VIR_FROM_RESCTRL
414     | VIR_FROM_UNKNOWN of int
415
416   let string_of_domain = function
417     | VIR_FROM_NONE -> "VIR_FROM_NONE"
418     | VIR_FROM_XEN -> "VIR_FROM_XEN"
419     | VIR_FROM_XEND -> "VIR_FROM_XEND"
420     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
421     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
422     | VIR_FROM_XML -> "VIR_FROM_XML"
423     | VIR_FROM_DOM -> "VIR_FROM_DOM"
424     | VIR_FROM_RPC -> "VIR_FROM_RPC"
425     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
426     | VIR_FROM_CONF -> "VIR_FROM_CONF"
427     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
428     | VIR_FROM_NET -> "VIR_FROM_NET"
429     | VIR_FROM_TEST -> "VIR_FROM_TEST"
430     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
431     | VIR_FROM_OPENVZ -> "VIR_FROM_OPENVZ"
432     | VIR_FROM_XENXM -> "VIR_FROM_XENXM"
433     | VIR_FROM_STATS_LINUX -> "VIR_FROM_STATS_LINUX"
434     | VIR_FROM_LXC -> "VIR_FROM_LXC"
435     | VIR_FROM_STORAGE -> "VIR_FROM_STORAGE"
436     | VIR_FROM_NETWORK -> "VIR_FROM_NETWORK"
437     | VIR_FROM_DOMAIN -> "VIR_FROM_DOMAIN"
438     | VIR_FROM_UML -> "VIR_FROM_UML"
439     | VIR_FROM_NODEDEV -> "VIR_FROM_NODEDEV"
440     | VIR_FROM_XEN_INOTIFY -> "VIR_FROM_XEN_INOTIFY"
441     | VIR_FROM_SECURITY -> "VIR_FROM_SECURITY"
442     | VIR_FROM_VBOX -> "VIR_FROM_VBOX"
443     | VIR_FROM_INTERFACE -> "VIR_FROM_INTERFACE"
444     | VIR_FROM_ONE -> "VIR_FROM_ONE"
445     | VIR_FROM_ESX -> "VIR_FROM_ESX"
446     | VIR_FROM_PHYP -> "VIR_FROM_PHYP"
447     | VIR_FROM_SECRET -> "VIR_FROM_SECRET"
448     | VIR_FROM_CPU -> "VIR_FROM_CPU"
449     | VIR_FROM_XENAPI -> "VIR_FROM_XENAPI"
450     | VIR_FROM_NWFILTER -> "VIR_FROM_NWFILTER"
451     | VIR_FROM_HOOK -> "VIR_FROM_HOOK"
452     | VIR_FROM_DOMAIN_SNAPSHOT -> "VIR_FROM_DOMAIN_SNAPSHOT"
453     | VIR_FROM_AUDIT -> "VIR_FROM_AUDIT"
454     | VIR_FROM_SYSINFO -> "VIR_FROM_SYSINFO"
455     | VIR_FROM_STREAMS -> "VIR_FROM_STREAMS"
456     | VIR_FROM_VMWARE -> "VIR_FROM_VMWARE"
457     | VIR_FROM_EVENT -> "VIR_FROM_EVENT"
458     | VIR_FROM_LIBXL -> "VIR_FROM_LIBXL"
459     | VIR_FROM_LOCKING -> "VIR_FROM_LOCKING"
460     | VIR_FROM_HYPERV -> "VIR_FROM_HYPERV"
461     | VIR_FROM_CAPABILITIES -> "VIR_FROM_CAPABILITIES"
462     | VIR_FROM_URI -> "VIR_FROM_URI"
463     | VIR_FROM_AUTH -> "VIR_FROM_AUTH"
464     | VIR_FROM_DBUS -> "VIR_FROM_DBUS"
465     | VIR_FROM_PARALLELS -> "VIR_FROM_PARALLELS"
466     | VIR_FROM_DEVICE -> "VIR_FROM_DEVICE"
467     | VIR_FROM_SSH -> "VIR_FROM_SSH"
468     | VIR_FROM_LOCKSPACE -> "VIR_FROM_LOCKSPACE"
469     | VIR_FROM_INITCTL -> "VIR_FROM_INITCTL"
470     | VIR_FROM_IDENTITY -> "VIR_FROM_IDENTITY"
471     | VIR_FROM_CGROUP -> "VIR_FROM_CGROUP"
472     | VIR_FROM_ACCESS -> "VIR_FROM_ACCESS"
473     | VIR_FROM_SYSTEMD -> "VIR_FROM_SYSTEMD"
474     | VIR_FROM_BHYVE -> "VIR_FROM_BHYVE"
475     | VIR_FROM_CRYPTO -> "VIR_FROM_CRYPTO"
476     | VIR_FROM_FIREWALL -> "VIR_FROM_FIREWALL"
477     | VIR_FROM_POLKIT -> "VIR_FROM_POLKIT"
478     | VIR_FROM_THREAD -> "VIR_FROM_THREAD"
479     | VIR_FROM_ADMIN -> "VIR_FROM_ADMIN"
480     | VIR_FROM_LOGGING -> "VIR_FROM_LOGGING"
481     | VIR_FROM_XENXL -> "VIR_FROM_XENXL"
482     | VIR_FROM_PERF -> "VIR_FROM_PERF"
483     | VIR_FROM_LIBSSH -> "VIR_FROM_LIBSSH"
484     | VIR_FROM_RESCTRL -> "VIR_FROM_RESCTRL"
485     | VIR_FROM_UNKNOWN i -> "VIR_FROM_" ^ string_of_int i
486
487   type level =
488     | VIR_ERR_NONE
489     | VIR_ERR_WARNING
490     | VIR_ERR_ERROR
491     | VIR_ERR_UNKNOWN_LEVEL of int
492
493   let string_of_level = function
494     | VIR_ERR_NONE -> "VIR_ERR_NONE"
495     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
496     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
497     | VIR_ERR_UNKNOWN_LEVEL i -> "VIR_ERR_LEVEL_" ^ string_of_int i
498
499   type t = {
500     code : code;
501     domain : domain;
502     message : string option;
503     level : level;
504     str1 : string option;
505     str2 : string option;
506     str3 : string option;
507     int1 : int32;
508     int2 : int32;
509   }
510
511   let to_string { code = code; domain = domain; message = message } =
512     let buf = Buffer.create 128 in
513     Buffer.add_string buf "libvirt: ";
514     Buffer.add_string buf (string_of_code code);
515     Buffer.add_string buf ": ";
516     Buffer.add_string buf (string_of_domain domain);
517     Buffer.add_string buf ": ";
518     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
519     Buffer.contents buf
520
521   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
522   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
523   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
524   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
525
526   let no_error () =
527     { code = VIR_ERR_OK; domain = VIR_FROM_NONE;
528       message = None; level = VIR_ERR_NONE;
529       str1 = None; str2 = None; str3 = None;
530       int1 = 0_l; int2 = 0_l }
531 end
532
533 exception Virterror of Virterror.t
534 exception Not_supported of string
535
536 let rec map_ignore_errors f = function
537   | [] -> []
538   | x :: xs ->
539       try f x :: map_ignore_errors f xs
540       with Virterror _ -> map_ignore_errors f xs
541
542 module Domain =
543 struct
544   type 'rw t
545
546   type state =
547     | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
548     | InfoShutdown | InfoShutoff | InfoCrashed
549
550   type info = {
551     state : state;
552     max_mem : int64;
553     memory : int64;
554     nr_virt_cpu : int;
555     cpu_time : int64;
556   }
557
558   type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
559
560   type vcpu_info = {
561     number : int;
562     vcpu_state : vcpu_state;
563     vcpu_time : int64;
564     cpu : int;
565   }
566
567   type domain_create_flag =
568   | START_PAUSED
569   | START_AUTODESTROY
570   | START_BYPASS_CACHE
571   | START_FORCE_BOOT
572   | START_VALIDATE
573   let rec int_of_domain_create_flags = function
574     | [] -> 0
575     | START_PAUSED :: flags ->       1 lor int_of_domain_create_flags flags
576     | START_AUTODESTROY :: flags ->  2 lor int_of_domain_create_flags flags
577     | START_BYPASS_CACHE :: flags -> 4 lor int_of_domain_create_flags flags
578     | START_FORCE_BOOT :: flags ->   8 lor int_of_domain_create_flags flags
579     | START_VALIDATE :: flags ->    16 lor int_of_domain_create_flags flags
580
581   type sched_param = string * sched_param_value
582   and sched_param_value =
583     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
584     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
585     | SchedFieldFloat of float | SchedFieldBool of bool
586
587   type typed_param = string * typed_param_value
588   and typed_param_value =
589     | TypedFieldInt32 of int32 | TypedFieldUInt32 of int32
590     | TypedFieldInt64 of int64 | TypedFieldUInt64 of int64
591     | TypedFieldFloat of float | TypedFieldBool of bool
592     | TypedFieldString of string
593
594   type migrate_flag = Live
595
596   type memory_flag = Virtual
597
598   type list_flag =
599     | ListActive
600     | ListInactive
601     | ListAll
602
603   type block_stats = {
604     rd_req : int64;
605     rd_bytes : int64;
606     wr_req : int64;
607     wr_bytes : int64;
608     errs : int64;
609   }
610
611   type interface_stats = {
612     rx_bytes : int64;
613     rx_packets : int64;
614     rx_errs : int64;
615     rx_drop : int64;
616     tx_bytes : int64;
617     tx_packets : int64;
618     tx_errs : int64;
619     tx_drop : int64;
620   }
621
622   type get_all_domain_stats_flag =
623     | GetAllDomainsStatsActive
624     | GetAllDomainsStatsInactive
625     | GetAllDomainsStatsOther
626     | GetAllDomainsStatsPaused
627     | GetAllDomainsStatsPersistent
628     | GetAllDomainsStatsRunning
629     | GetAllDomainsStatsShutoff
630     | GetAllDomainsStatsTransient
631     | GetAllDomainsStatsBacking
632     | GetAllDomainsStatsEnforceStats
633
634   type stats_type =
635     | StatsState | StatsCpuTotal | StatsBalloon | StatsVcpu
636     | StatsInterface | StatsBlock | StatsPerf
637
638   type domain_stats_record = {
639     dom_uuid : uuid;
640     params : typed_param array;
641   }
642
643   (* The maximum size for Domain.memory_peek and Domain.block_peek
644    * supported by libvirt.  This may change with different versions
645    * of libvirt in the future, hence it's a function.
646    *)
647   let max_peek _ = 65536
648
649   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
650   external _create_xml : [>`W] Connect.t -> xml -> int -> rw t = "ocaml_libvirt_domain_create_xml"
651   let create_xml conn xml flags =
652     _create_xml conn xml (int_of_domain_create_flags flags)
653   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
654   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
655   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
656   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
657   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
658   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
659   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
660   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
661   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
662   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
663   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
664   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
665   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
666   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
667   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
668   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
669   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
670   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
671   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
672   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
673   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
674   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
675   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
676   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
677   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
678   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
679   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
680   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
681   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
682   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
683   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
684   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
685   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
686   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
687   external get_cpu_stats : [>`R] t -> typed_param list array = "ocaml_libvirt_domain_get_cpu_stats"
688   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
689   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
690   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
691   external migrate : [>`W] t -> [>`W] Connect.t -> migrate_flag list -> ?dname:string -> ?uri:string -> ?bandwidth:int -> unit -> rw t = "ocaml_libvirt_domain_migrate_bytecode" "ocaml_libvirt_domain_migrate_native"
692   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
693   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
694   external block_peek : [>`W] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
695   external memory_peek : [>`W] t -> memory_flag list -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_memory_peek_bytecode" "ocaml_libvirt_domain_memory_peek_native"
696
697   external get_all_domain_stats : [>`R] Connect.t -> stats_type list -> get_all_domain_stats_flag list -> domain_stats_record array = "ocaml_libvirt_domain_get_all_domain_stats"
698
699   external const : [>`R] t -> ro t = "%identity"
700
701   let get_domains conn flags =
702     (* Old/slow/inefficient method. *)
703     let get_active, get_inactive =
704       if List.mem ListAll flags then
705         (true, true)
706       else
707         (List.mem ListActive flags, List.mem ListInactive flags) in
708     let active_doms =
709       if get_active then (
710         let n = Connect.num_of_domains conn in
711         let ids = Connect.list_domains conn n in
712         let ids = Array.to_list ids in
713         map_ignore_errors (lookup_by_id conn) ids
714       ) else [] in
715
716     let inactive_doms =
717       if get_inactive then (
718         let n = Connect.num_of_defined_domains conn in
719         let names = Connect.list_defined_domains conn n in
720         let names = Array.to_list names in
721         map_ignore_errors (lookup_by_name conn) names
722       ) else [] in
723
724     active_doms @ inactive_doms
725
726   let get_domains_and_infos conn flags =
727     (* Old/slow/inefficient method. *)
728     let get_active, get_inactive =
729       if List.mem ListAll flags then
730         (true, true)
731       else (List.mem ListActive flags, List.mem ListInactive flags) in
732     let active_doms =
733       if get_active then (
734         let n = Connect.num_of_domains conn in
735         let ids = Connect.list_domains conn n in
736         let ids = Array.to_list ids in
737         map_ignore_errors (lookup_by_id conn) ids
738       ) else [] in
739
740     let inactive_doms =
741       if get_inactive then (
742         let n = Connect.num_of_defined_domains conn in
743         let names = Connect.list_defined_domains conn n in
744         let names = Array.to_list names in
745         map_ignore_errors (lookup_by_name conn) names
746       ) else [] in
747
748     let doms = active_doms @ inactive_doms in
749
750     map_ignore_errors (fun dom -> (dom, get_info dom)) doms
751 end
752
753 module Event =
754 struct
755
756   module Defined = struct
757     type t = [
758       | `Added
759       | `Updated
760       | `Unknown of int
761     ]
762
763     let to_string = function
764       | `Added -> "Added"
765       | `Updated -> "Updated"
766       | `Unknown x -> Printf.sprintf "Unknown Defined.detail: %d" x
767
768     let make = function
769       | 0 -> `Added
770       | 1 -> `Updated
771       | x -> `Unknown x (* newer libvirt *)
772   end
773
774   module Undefined = struct
775     type t = [
776       | `Removed
777       | `Unknown of int
778     ]
779
780     let to_string = function
781       | `Removed -> "UndefinedRemoved"
782       | `Unknown x -> Printf.sprintf "Unknown Undefined.detail: %d" x
783
784     let make = function
785       | 0 -> `Removed
786       | x -> `Unknown x (* newer libvirt *)
787   end
788
789   module Started = struct
790     type t = [
791       | `Booted
792       | `Migrated
793       | `Restored
794       | `FromSnapshot
795       | `Wakeup
796       | `Unknown of int
797     ]
798
799     let to_string = function
800       | `Booted -> "Booted"
801       | `Migrated -> "Migrated"
802       | `Restored -> "Restored"
803       | `FromSnapshot -> "FromSnapshot"
804       | `Wakeup -> "Wakeup"
805       | `Unknown x -> Printf.sprintf "Unknown Started.detail: %d" x
806  
807     let make = function
808       | 0 -> `Booted
809       | 1 -> `Migrated
810       | 2 -> `Restored
811       | 3 -> `FromSnapshot
812       | 4 -> `Wakeup
813       | x -> `Unknown x (* newer libvirt *)
814   end
815
816   module Suspended = struct
817     type t = [
818       | `Paused
819       | `Migrated
820       | `IOError
821       | `Watchdog
822       | `Restored
823       | `FromSnapshot
824       | `APIError
825       | `Unknown of int (* newer libvirt *)
826     ]
827
828     let to_string = function
829       | `Paused -> "Paused"
830       | `Migrated -> "Migrated"
831       | `IOError -> "IOError"
832       | `Watchdog -> "Watchdog"
833       | `Restored -> "Restored"
834       | `FromSnapshot -> "FromSnapshot"
835       | `APIError -> "APIError"
836       | `Unknown x -> Printf.sprintf "Unknown Suspended.detail: %d" x
837
838      let make = function
839       | 0 -> `Paused
840       | 1 -> `Migrated
841       | 2 -> `IOError
842       | 3 -> `Watchdog
843       | 4 -> `Restored
844       | 5 -> `FromSnapshot
845       | 6 -> `APIError
846       | x -> `Unknown x (* newer libvirt *)
847   end
848
849   module Resumed = struct
850     type t = [
851       | `Unpaused
852       | `Migrated
853       | `FromSnapshot
854       | `Unknown of int (* newer libvirt *)
855     ]
856
857     let to_string = function
858       | `Unpaused -> "Unpaused"
859       | `Migrated -> "Migrated"
860       | `FromSnapshot -> "FromSnapshot"
861       | `Unknown x -> Printf.sprintf "Unknown Resumed.detail: %d" x
862
863     let make = function
864       | 0 -> `Unpaused
865       | 1 -> `Migrated
866       | 2 -> `FromSnapshot
867       | x -> `Unknown x (* newer libvirt *)
868   end
869
870   module Stopped = struct
871     type t = [
872       | `Shutdown
873       | `Destroyed
874       | `Crashed
875       | `Migrated
876       | `Saved
877       | `Failed
878       | `FromSnapshot
879       | `Unknown of int
880     ]
881     let to_string = function
882       | `Shutdown -> "Shutdown"
883       | `Destroyed -> "Destroyed"
884       | `Crashed -> "Crashed"
885       | `Migrated -> "Migrated"
886       | `Saved -> "Saved"
887       | `Failed -> "Failed"
888       | `FromSnapshot -> "FromSnapshot"
889       | `Unknown x -> Printf.sprintf "Unknown Stopped.detail: %d" x
890
891     let make = function
892       | 0 -> `Shutdown
893       | 1 -> `Destroyed
894       | 2 -> `Crashed
895       | 3 -> `Migrated
896       | 4 -> `Saved
897       | 5 -> `Failed
898       | 6 -> `FromSnapshot
899       | x -> `Unknown x (* newer libvirt *)
900   end
901
902   module PM_suspended = struct
903     type t = [
904       | `Memory
905       | `Disk
906       | `Unknown of int (* newer libvirt *)
907     ]
908
909     let to_string = function
910       | `Memory -> "Memory"
911       | `Disk -> "Disk"
912       | `Unknown x -> Printf.sprintf "Unknown PM_suspended.detail: %d" x
913
914     let make = function
915       | 0 -> `Memory
916       | 1 -> `Disk
917       | x -> `Unknown x (* newer libvirt *)
918   end
919
920   let string_option x = match x with
921     | None -> "None"
922     | Some x' -> "Some " ^ x'
923
924   module Lifecycle = struct
925     type t = [
926       | `Defined of Defined.t
927       | `Undefined of Undefined.t
928       | `Started of Started.t
929       | `Suspended of Suspended.t
930       | `Resumed of Resumed.t
931       | `Stopped of Stopped.t
932       | `Shutdown (* no detail defined yet *)
933       | `PMSuspended of PM_suspended.t
934       | `Unknown of int (* newer libvirt *)
935     ]
936
937     let to_string = function
938       | `Defined x -> "Defined " ^ (Defined.to_string x)
939       | `Undefined x -> "Undefined " ^ (Undefined.to_string x)
940       | `Started x -> "Started " ^ (Started.to_string x)
941       | `Suspended x -> "Suspended " ^ (Suspended.to_string x)
942       | `Resumed x -> "Resumed " ^ (Resumed.to_string x)
943       | `Stopped x -> "Stopped " ^ (Stopped.to_string x)
944       | `Shutdown -> "Shutdown"
945       | `PMSuspended x -> "PMSuspended " ^ (PM_suspended.to_string x)
946       | `Unknown x -> Printf.sprintf "Unknown Lifecycle event: %d" x
947
948     let make (ty, detail) = match ty with
949       | 0 -> `Defined (Defined.make detail)
950       | 1 -> `Undefined (Undefined.make detail)
951       | 2 -> `Started (Started.make detail)
952       | 3 -> `Suspended (Suspended.make detail)
953       | 4 -> `Resumed (Resumed.make detail)
954       | 5 -> `Stopped (Stopped.make detail)
955       | 6 -> `Shutdown
956       | 7 -> `PMSuspended (PM_suspended.make detail)
957       | x -> `Unknown x
958   end
959
960   module Reboot = struct
961     type t = unit
962
963     let to_string _ = "()"
964
965     let make () = ()
966   end
967
968   module Rtc_change = struct
969     type t = int64
970
971     let to_string = Int64.to_string
972
973     let make x = x
974   end
975
976   module Watchdog = struct
977     type t = [
978       | `None
979       | `Pause
980       | `Reset
981       | `Poweroff
982       | `Shutdown
983       | `Debug
984       | `Unknown of int
985     ]
986
987     let to_string = function
988       | `None -> "None"
989       | `Pause -> "Pause"
990       | `Reset -> "Reset"
991       | `Poweroff -> "Poweroff"
992       | `Shutdown -> "Shutdown"
993       | `Debug -> "Debug"
994       | `Unknown x -> Printf.sprintf "Unknown watchdog_action: %d" x
995
996     let make = function
997       | 0 -> `None
998       | 1 -> `Pause
999       | 2 -> `Reset
1000       | 3 -> `Poweroff
1001       | 4 -> `Shutdown
1002       | 5 -> `Debug
1003       | x -> `Unknown x (* newer libvirt *)
1004   end
1005
1006   module Io_error = struct
1007     type action = [
1008       | `None
1009       | `Pause
1010       | `Report
1011       | `Unknown of int (* newer libvirt *)
1012     ]
1013
1014     let string_of_action = function
1015       | `None -> "None"
1016       | `Pause -> "Pause"
1017       | `Report -> "Report"
1018       | `Unknown x -> Printf.sprintf "Unknown Io_error.action: %d" x
1019
1020     let action_of_int = function
1021       | 0 -> `None
1022       | 1 -> `Pause
1023       | 2 -> `Report
1024       | x -> `Unknown x
1025
1026     type t = {
1027       src_path: string option;
1028       dev_alias: string option;
1029       action: action;
1030       reason: string option;
1031     }
1032
1033     let to_string t = Printf.sprintf
1034         "{ Io_error.src_path = %s; dev_alias = %s; action = %s; reason = %s }"
1035         (string_option t.src_path)
1036         (string_option t.dev_alias)
1037         (string_of_action t.action)
1038         (string_option t.reason)
1039
1040     let make (src_path, dev_alias, action, reason) = {
1041         src_path = src_path;
1042         dev_alias = dev_alias;
1043         action = action_of_int action;
1044         reason = reason;
1045     }
1046
1047     let make_noreason (src_path, dev_alias, action) =
1048       make (src_path, dev_alias, action, None)
1049   end
1050
1051   module Graphics_address = struct
1052     type family = [
1053       | `Ipv4
1054       | `Ipv6
1055       | `Unix
1056       | `Unknown of int (* newer libvirt *)
1057     ]
1058
1059     let string_of_family = function
1060       | `Ipv4 -> "IPv4"
1061       | `Ipv6 -> "IPv6"
1062       | `Unix -> "UNIX"
1063       | `Unknown x -> Printf.sprintf "Unknown Graphics_address.family: %d" x
1064
1065     let family_of_int = function
1066       (* no zero *)
1067       | 1 -> `Ipv4
1068       | 2 -> `Ipv6
1069       | 3 -> `Unix
1070       | x -> `Unknown x
1071
1072     type t = {
1073       family: family;         (** Address family *)
1074       node: string option;    (** Address of node (eg IP address, or UNIX path *)
1075       service: string option; (** Service name/number (eg TCP port, or NULL) *)
1076     }
1077
1078     let to_string t = Printf.sprintf
1079       "{ family = %s; node = %s; service = %s }"
1080         (string_of_family t.family)
1081         (string_option t.node)
1082         (string_option t.service)
1083
1084     let make (family, node, service) = {
1085       family = family_of_int family;
1086       node = node;
1087       service = service;
1088     }
1089   end
1090
1091   module Graphics_subject = struct
1092     type identity = {
1093       ty: string option;
1094       name: string option;
1095     }
1096
1097     let string_of_identity t = Printf.sprintf
1098       "{ ty = %s; name = %s }"
1099       (string_option t.ty)
1100       (string_option t.name)
1101
1102     type t = identity list
1103
1104     let to_string ts =
1105       "[ " ^ (String.concat "; " (List.map string_of_identity ts)) ^ " ]"
1106
1107     let make xs =
1108       List.map (fun (ty, name) -> { ty = ty; name = name })
1109         (Array.to_list xs)
1110   end
1111
1112   module Graphics = struct
1113     type phase = [
1114       | `Connect
1115       | `Initialize
1116       | `Disconnect
1117       | `Unknown of int (** newer libvirt *)
1118     ]
1119
1120     let string_of_phase = function
1121       | `Connect -> "Connect"
1122       | `Initialize -> "Initialize"
1123       | `Disconnect -> "Disconnect"
1124       | `Unknown x -> Printf.sprintf "Unknown Graphics.phase: %d" x
1125
1126     let phase_of_int = function
1127       | 0 -> `Connect
1128       | 1 -> `Initialize
1129       | 2 -> `Disconnect
1130       | x -> `Unknown x
1131
1132     type t = {
1133       phase: phase;                (** the phase of the connection *)
1134       local: Graphics_address.t;   (** the local server address *)
1135       remote: Graphics_address.t;  (** the remote client address *)
1136       auth_scheme: string option;  (** the authentication scheme activated *)
1137       subject: Graphics_subject.t; (** the authenticated subject (user) *)
1138     }
1139
1140     let to_string t =
1141       let phase = Printf.sprintf "phase = %s"
1142         (string_of_phase t.phase) in
1143       let local = Printf.sprintf "local = %s"
1144         (Graphics_address.to_string t.local) in
1145       let remote = Printf.sprintf "remote = %s"
1146         (Graphics_address.to_string t.remote) in
1147       let auth_scheme = Printf.sprintf "auth_scheme = %s"
1148         (string_option t.auth_scheme) in
1149       let subject = Printf.sprintf "subject = %s"
1150         (Graphics_subject.to_string t.subject) in
1151       "{ " ^ (String.concat "; " [ phase; local; remote; auth_scheme; subject ]) ^ " }"
1152
1153     let make (phase, local, remote, auth_scheme, subject) = {
1154       phase = phase_of_int phase;
1155       local = Graphics_address.make local;
1156       remote = Graphics_address.make remote;
1157       auth_scheme = auth_scheme;
1158       subject = Graphics_subject.make subject;
1159     }
1160   end
1161
1162   module Control_error = struct
1163     type t = unit
1164
1165     let to_string () = "()"
1166
1167     let make () = ()
1168   end
1169
1170   module Block_job = struct
1171     type ty = [
1172       | `KnownUnknown (* explicitly named UNKNOWN in the spec *)
1173       | `Pull
1174       | `Copy
1175       | `Commit
1176       | `Unknown of int (* newer libvirt *)
1177     ]
1178
1179     let string_of_ty = function
1180       | `KnownUnknown -> "KnownUnknown"
1181       | `Pull -> "Pull"
1182       | `Copy -> "Copy"
1183       | `Commit -> "Commit"
1184       | `Unknown x -> Printf.sprintf "Unknown Block_job.ty: %d" x
1185
1186     let ty_of_int = function
1187       | 0 -> `KnownUnknown
1188       | 1 -> `Pull
1189       | 2 -> `Copy
1190       | 3 -> `Commit
1191       | x -> `Unknown x (* newer libvirt *)
1192
1193     type status = [
1194       | `Completed
1195       | `Failed
1196       | `Cancelled
1197       | `Ready
1198       | `Unknown of int
1199     ]
1200
1201     let string_of_status = function
1202       | `Completed -> "Completed"
1203       | `Failed -> "Failed"
1204       | `Cancelled -> "Cancelled"
1205       | `Ready -> "Ready"
1206       | `Unknown x -> Printf.sprintf "Unknown Block_job.status: %d" x
1207
1208     let status_of_int = function
1209       | 0 -> `Completed
1210       | 1 -> `Failed
1211       | 2 -> `Cancelled
1212       | 3 -> `Ready
1213       | x -> `Unknown x
1214
1215     type t = {
1216       disk: string option;
1217       ty: ty;
1218       status: status;
1219     }
1220
1221     let to_string t = Printf.sprintf "{ disk = %s; ty = %s; status = %s }"
1222       (string_option t.disk)
1223       (string_of_ty t.ty)
1224       (string_of_status t.status)
1225
1226     let make (disk, ty, status) = {
1227       disk = disk;
1228       ty = ty_of_int ty;
1229       status = status_of_int ty;
1230     }
1231   end
1232
1233   module Disk_change = struct
1234     type reason = [
1235       | `MissingOnStart
1236       | `Unknown of int
1237     ]
1238
1239     let string_of_reason = function
1240       | `MissingOnStart -> "MissingOnStart"
1241       | `Unknown x -> Printf.sprintf "Unknown Disk_change.reason: %d" x
1242
1243     let reason_of_int = function
1244       | 0 -> `MissingOnStart
1245       | x -> `Unknown x
1246
1247     type t = {
1248       old_src_path: string option;
1249       new_src_path: string option;
1250       dev_alias: string option;
1251       reason: reason;
1252     }
1253
1254     let to_string t =
1255       let o = Printf.sprintf "old_src_path = %s" (string_option t.old_src_path) in
1256       let n = Printf.sprintf "new_src_path = %s" (string_option t.new_src_path) in
1257       let d = Printf.sprintf "dev_alias = %s" (string_option t.dev_alias) in
1258       let r = string_of_reason t.reason in
1259       "{ " ^ (String.concat "; " [ o; n; d; r ]) ^ " }"
1260
1261     let make (o, n, d, r) = {
1262       old_src_path = o;
1263       new_src_path = n;
1264       dev_alias = d;
1265       reason = reason_of_int r;
1266     }
1267   end
1268
1269   module Tray_change = struct
1270     type reason = [
1271       | `Open
1272       | `Close
1273       | `Unknown of int
1274     ]
1275
1276     let string_of_reason = function
1277       | `Open -> "Open"
1278       | `Close -> "Close"
1279       | `Unknown x -> Printf.sprintf "Unknown Tray_change.reason: %d" x
1280
1281     let reason_of_int = function
1282       | 0 -> `Open
1283       | 1 -> `Close
1284       | x -> `Unknown x
1285
1286     type t = {
1287       dev_alias: string option;
1288       reason: reason;
1289     }
1290
1291     let to_string t = Printf.sprintf
1292       "{ dev_alias = %s; reason = %s }"
1293         (string_option t.dev_alias)
1294         (string_of_reason t.reason)
1295
1296     let make (dev_alias, reason) = {
1297       dev_alias = dev_alias;
1298       reason = reason_of_int reason;
1299     }
1300   end
1301
1302   module PM_wakeup = struct
1303     type reason = [
1304       | `Unknown of int
1305     ]
1306
1307     type t = reason
1308
1309     let to_string = function
1310       | `Unknown x -> Printf.sprintf "Unknown PM_wakeup.reason: %d" x
1311
1312     let make x = `Unknown x
1313   end
1314
1315   module PM_suspend = struct
1316     type reason = [
1317       | `Unknown of int
1318     ]
1319
1320     type t = reason
1321
1322     let to_string = function
1323       | `Unknown x -> Printf.sprintf "Unknown PM_suspend.reason: %d" x
1324
1325     let make x = `Unknown x
1326   end
1327
1328   module Balloon_change = struct
1329     type t = int64
1330
1331     let to_string = Int64.to_string
1332     let make x = x
1333   end
1334
1335   module PM_suspend_disk = struct
1336     type reason = [
1337       | `Unknown of int
1338     ]
1339
1340     type t = reason
1341
1342     let to_string = function
1343       | `Unknown x -> Printf.sprintf "Unknown PM_suspend_disk.reason: %d" x
1344
1345     let make x = `Unknown x
1346   end
1347
1348   type callback =
1349     | Lifecycle     of ([`R] Domain.t -> Lifecycle.t -> unit)
1350     | Reboot        of ([`R] Domain.t -> Reboot.t -> unit)
1351     | RtcChange     of ([`R] Domain.t -> Rtc_change.t -> unit)
1352     | Watchdog      of ([`R] Domain.t -> Watchdog.t -> unit)
1353     | IOError       of ([`R] Domain.t -> Io_error.t -> unit)
1354     | Graphics      of ([`R] Domain.t -> Graphics.t -> unit)
1355     | IOErrorReason of ([`R] Domain.t -> Io_error.t -> unit)
1356     | ControlError  of ([`R] Domain.t -> Control_error.t -> unit)
1357     | BlockJob      of ([`R] Domain.t -> Block_job.t -> unit)
1358     | DiskChange    of ([`R] Domain.t -> Disk_change.t -> unit)
1359     | TrayChange    of ([`R] Domain.t -> Tray_change.t -> unit)
1360     | PMWakeUp      of ([`R] Domain.t -> PM_wakeup.t -> unit)
1361     | PMSuspend     of ([`R] Domain.t -> PM_suspend.t -> unit)
1362     | BalloonChange of ([`R] Domain.t -> Balloon_change.t -> unit)
1363     | PMSuspendDisk of ([`R] Domain.t -> PM_suspend_disk.t -> unit)
1364
1365   type callback_id = int64
1366
1367   let fresh_callback_id =
1368     let next = ref 0L in
1369     fun () ->
1370       let result = !next in
1371       next := Int64.succ !next;
1372       result
1373
1374   let make_table value_name =
1375     let table = Hashtbl.create 16 in
1376     let callback callback_id generic x =
1377       if Hashtbl.mem table callback_id
1378       then Hashtbl.find table callback_id generic x in
1379     let _ = Callback.register value_name callback in
1380     table
1381
1382   let u_table = make_table "Libvirt.u_callback"
1383   let i_table = make_table "Libvirt.i_callback"
1384   let i64_table = make_table "Libvirt.i64_callback"
1385   let i_i_table = make_table "Libvirt.i_i_callback"
1386   let s_i_table = make_table "Libvirt.s_i_callback"
1387   let s_i_i_table = make_table "Libvirt.s_i_i_callback"
1388   let s_s_i_table = make_table "Libvirt.s_s_i_callback"
1389   let s_s_i_s_table = make_table "Libvirt.s_s_i_s_callback"
1390   let s_s_s_i_table = make_table "Libvirt.s_s_s_i_callback"
1391   let i_ga_ga_s_gs_table = make_table "Libvirt.i_ga_ga_s_gs_callback"
1392
1393   external register_default_impl : unit -> unit = "ocaml_libvirt_event_register_default_impl"
1394
1395   external run_default_impl : unit -> unit = "ocaml_libvirt_event_run_default_impl"
1396
1397   external register_any' : 'a Connect.t -> 'a Domain.t option -> callback -> callback_id -> int = "ocaml_libvirt_connect_domain_event_register_any"
1398
1399   external deregister_any' : 'a Connect.t -> int -> unit = "ocaml_libvirt_connect_domain_event_deregister_any"
1400
1401   let our_id_to_libvirt_id = Hashtbl.create 16
1402
1403   let register_any conn ?dom callback =
1404     let id = fresh_callback_id () in
1405     begin match callback with
1406     | Lifecycle f ->
1407         Hashtbl.add i_i_table id (fun dom x ->
1408             f dom (Lifecycle.make x)
1409         )
1410     | Reboot f ->
1411         Hashtbl.add u_table id (fun dom x ->
1412             f dom (Reboot.make x)
1413         )
1414     | RtcChange f ->
1415         Hashtbl.add i64_table id (fun dom x ->
1416             f dom (Rtc_change.make x)
1417         )
1418     | Watchdog f ->
1419         Hashtbl.add i_table id (fun dom x ->
1420             f dom (Watchdog.make x)
1421         ) 
1422     | IOError f ->
1423         Hashtbl.add s_s_i_table id (fun dom x ->
1424             f dom (Io_error.make_noreason x)
1425         )
1426     | Graphics f ->
1427         Hashtbl.add i_ga_ga_s_gs_table id (fun dom x ->
1428             f dom (Graphics.make x)
1429         )
1430     | IOErrorReason f ->
1431         Hashtbl.add s_s_i_s_table id (fun dom x ->
1432             f dom (Io_error.make x)
1433         )
1434     | ControlError f ->
1435         Hashtbl.add u_table id (fun dom x ->
1436             f dom (Control_error.make x)
1437         )
1438     | BlockJob f ->
1439         Hashtbl.add s_i_i_table id (fun dom x ->
1440             f dom (Block_job.make x)
1441         )
1442     | DiskChange f ->
1443         Hashtbl.add s_s_s_i_table id (fun dom x ->
1444             f dom (Disk_change.make x)
1445         )
1446     | TrayChange f ->
1447         Hashtbl.add s_i_table id (fun dom x ->
1448             f dom (Tray_change.make x)
1449         )
1450     | PMWakeUp f ->
1451         Hashtbl.add i_table id (fun dom x ->
1452             f dom (PM_wakeup.make x)
1453         )
1454     | PMSuspend f ->
1455         Hashtbl.add i_table id (fun dom x ->
1456             f dom (PM_suspend.make x)
1457         )
1458     | BalloonChange f ->
1459         Hashtbl.add i64_table id (fun dom x ->
1460             f dom (Balloon_change.make x)
1461         )
1462     | PMSuspendDisk f ->
1463         Hashtbl.add i_table id (fun dom x ->
1464             f dom (PM_suspend_disk.make x)
1465         )
1466     end;
1467     let libvirt_id = register_any' conn dom callback id in
1468     Hashtbl.replace our_id_to_libvirt_id id libvirt_id;
1469     id
1470
1471   let deregister_any conn id =
1472     if Hashtbl.mem our_id_to_libvirt_id id then begin
1473       let libvirt_id = Hashtbl.find our_id_to_libvirt_id id in
1474       deregister_any' conn libvirt_id
1475     end;
1476     Hashtbl.remove our_id_to_libvirt_id id;
1477     Hashtbl.remove u_table id;
1478     Hashtbl.remove i_table id;
1479     Hashtbl.remove i64_table id;
1480     Hashtbl.remove i_i_table id;
1481     Hashtbl.remove s_i_table id;
1482     Hashtbl.remove s_i_i_table id;
1483     Hashtbl.remove s_s_i_table id;
1484     Hashtbl.remove s_s_i_s_table id;
1485     Hashtbl.remove s_s_s_i_table id;
1486     Hashtbl.remove i_ga_ga_s_gs_table id
1487
1488   let timeout_table = Hashtbl.create 16
1489   let _ =
1490     let callback x =
1491       if Hashtbl.mem timeout_table x
1492       then Hashtbl.find timeout_table x () in
1493   Callback.register "Libvirt.timeout_callback" callback
1494
1495   type timer_id = int64
1496
1497   external add_timeout' : 'a Connect.t -> int -> int64 -> int = "ocaml_libvirt_event_add_timeout"
1498
1499   external remove_timeout' : 'a Connect.t -> int -> unit = "ocaml_libvirt_event_remove_timeout"
1500
1501   let our_id_to_timer_id = Hashtbl.create 16
1502   let add_timeout conn ms fn =
1503     let id = fresh_callback_id () in
1504     Hashtbl.add timeout_table id fn;
1505     let timer_id = add_timeout' conn ms id in
1506     Hashtbl.add our_id_to_timer_id id timer_id;
1507     id
1508
1509   let remove_timeout conn id =
1510     if Hashtbl.mem our_id_to_timer_id id then begin
1511       let timer_id = Hashtbl.find our_id_to_timer_id id in
1512       remove_timeout' conn timer_id
1513     end;
1514     Hashtbl.remove our_id_to_timer_id id;
1515     Hashtbl.remove timeout_table id
1516 end
1517
1518 module Network =
1519 struct
1520   type 'rw t
1521
1522   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
1523   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
1524   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
1525   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
1526   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
1527   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
1528   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
1529   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
1530   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
1531   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
1532   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
1533   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
1534   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
1535   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
1536   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
1537   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
1538
1539   external const : [>`R] t -> ro t = "%identity"
1540 end
1541
1542 module Pool =
1543 struct
1544   type 'rw t
1545   type pool_state = Inactive | Building | Running | Degraded
1546   type pool_build_flags = New | Repair | Resize
1547   type pool_delete_flags = Normal | Zeroed
1548   type pool_info = {
1549     state : pool_state;
1550     capacity : int64;
1551     allocation : int64;
1552     available : int64;
1553   }
1554
1555   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
1556   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
1557   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
1558   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
1559   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
1560   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
1561   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
1562   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
1563   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
1564   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
1565   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
1566   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
1567   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
1568   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
1569   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
1570   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
1571   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
1572   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
1573   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
1574   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
1575   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
1576   external const : [>`R] t -> ro t = "%identity"
1577 end
1578
1579 module Volume =
1580 struct
1581   type 'rw t
1582   type vol_type = File | Block
1583   type vol_delete_flags = Normal | Zeroed
1584   type vol_info = {
1585     typ : vol_type;
1586     capacity : int64;
1587     allocation : int64;
1588   }
1589
1590   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
1591   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
1592   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
1593   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
1594   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
1595   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
1596   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
1597   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
1598   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
1599   external create_xml : [>`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
1600   external delete : [>`W] t -> vol_delete_flags -> unit = "ocaml_libvirt_storage_vol_delete"
1601   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
1602   external const : [>`R] t -> ro t = "%identity"
1603 end
1604
1605 (* Initialization. *)
1606 external c_init : unit -> unit = "ocaml_libvirt_init"
1607 let () =
1608   Callback.register_exception
1609     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
1610   Callback.register_exception
1611     "ocaml_libvirt_not_supported" (Not_supported "");
1612   c_init ()