1be023dc0db1fc1100ae988a2844d8e90fb71bec
[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 list_flag =
52     | ListNoState | ListRunning | ListBlocked
53     | ListPaused | ListShutdown | ListShutoff | ListCrashed
54     | ListActive
55     | ListInactive
56     | ListAll
57
58   external connect : ?name:string -> unit -> rw t = "ocaml_libvirt_connect_open"
59   external connect_readonly : ?name:string -> unit -> ro t = "ocaml_libvirt_connect_open_readonly"
60   external close : [>`R] t -> unit = "ocaml_libvirt_connect_close"
61   external get_type : [>`R] t -> string = "ocaml_libvirt_connect_get_type"
62   external get_version : [>`R] t -> int = "ocaml_libvirt_connect_get_version"
63   external get_hostname : [>`R] t -> string = "ocaml_libvirt_connect_get_hostname"
64   external get_uri : [>`R] t -> string = "ocaml_libvirt_connect_get_uri"
65   external get_max_vcpus : [>`R] t -> ?type_:string -> unit -> int = "ocaml_libvirt_connect_get_max_vcpus"
66   external list_domains : [>`R] t -> int -> int array = "ocaml_libvirt_connect_list_domains"
67   external num_of_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_domains"
68   external get_capabilities : [>`R] t -> xml = "ocaml_libvirt_connect_get_capabilities"
69   external num_of_defined_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_domains"
70   external list_defined_domains : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_domains"
71   external num_of_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_networks"
72   external list_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_networks"
73   external num_of_defined_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_networks"
74   external list_defined_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_networks"
75   external num_of_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_storage_pools"
76   external list_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_storage_pools"
77   external num_of_defined_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_storage_pools"
78   external list_defined_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_storage_pools"
79
80   external get_node_info : [>`R] t -> node_info = "ocaml_libvirt_connect_get_node_info"
81   external node_get_free_memory : [> `R] t -> int64 = "ocaml_libvirt_connect_node_get_free_memory"
82   external node_get_cells_free_memory : [> `R] t -> int -> int -> int64 array = "ocaml_libvirt_connect_node_get_cells_free_memory"
83
84   (* See VIR_NODEINFO_MAXCPUS macro defined in <libvirt.h>. *)
85   let maxcpus_of_node_info { nodes = nodes; sockets = sockets;
86                              cores = cores; threads = threads } =
87     nodes * sockets * cores * threads
88
89   (* See VIR_CPU_MAPLEN macro defined in <libvirt.h>. *)
90   let cpumaplen nr_cpus =
91     (nr_cpus + 7) / 8
92
93   (* See VIR_USE_CPU, VIR_UNUSE_CPU, VIR_CPU_USABLE macros defined in <libvirt.h>. *)
94   let use_cpu cpumap cpu =
95     cpumap.[cpu/8] <-
96       Char.chr (Char.code cpumap.[cpu/8] lor (1 lsl (cpu mod 8)))
97   let unuse_cpu cpumap cpu =
98     cpumap.[cpu/8] <-
99       Char.chr (Char.code cpumap.[cpu/8] land (lnot (1 lsl (cpu mod 8))))
100   let cpu_usable cpumaps maplen vcpu cpu =
101     Char.code cpumaps.[vcpu*maplen + cpu/8] land (1 lsl (cpu mod 8)) <> 0
102
103   external set_keep_alive : [>`R] t -> int -> int -> unit = "ocaml_libvirt_connect_set_keep_alive"
104
105   external const : [>`R] t -> ro t = "%identity"
106 end
107
108 module Virterror =
109 struct
110   type code =
111     | VIR_ERR_OK
112     | VIR_ERR_INTERNAL_ERROR
113     | VIR_ERR_NO_MEMORY
114     | VIR_ERR_NO_SUPPORT
115     | VIR_ERR_UNKNOWN_HOST
116     | VIR_ERR_NO_CONNECT
117     | VIR_ERR_INVALID_CONN
118     | VIR_ERR_INVALID_DOMAIN
119     | VIR_ERR_INVALID_ARG
120     | VIR_ERR_OPERATION_FAILED
121     | VIR_ERR_GET_FAILED
122     | VIR_ERR_POST_FAILED
123     | VIR_ERR_HTTP_ERROR
124     | VIR_ERR_SEXPR_SERIAL
125     | VIR_ERR_NO_XEN
126     | VIR_ERR_XEN_CALL
127     | VIR_ERR_OS_TYPE
128     | VIR_ERR_NO_KERNEL
129     | VIR_ERR_NO_ROOT
130     | VIR_ERR_NO_SOURCE
131     | VIR_ERR_NO_TARGET
132     | VIR_ERR_NO_NAME
133     | VIR_ERR_NO_OS
134     | VIR_ERR_NO_DEVICE
135     | VIR_ERR_NO_XENSTORE
136     | VIR_ERR_DRIVER_FULL
137     | VIR_ERR_CALL_FAILED
138     | VIR_ERR_XML_ERROR
139     | VIR_ERR_DOM_EXIST
140     | VIR_ERR_OPERATION_DENIED
141     | VIR_ERR_OPEN_FAILED
142     | VIR_ERR_READ_FAILED
143     | VIR_ERR_PARSE_FAILED
144     | VIR_ERR_CONF_SYNTAX
145     | VIR_ERR_WRITE_FAILED
146     | VIR_ERR_XML_DETAIL
147     | VIR_ERR_INVALID_NETWORK
148     | VIR_ERR_NETWORK_EXIST
149     | VIR_ERR_SYSTEM_ERROR
150     | VIR_ERR_RPC
151     | VIR_ERR_GNUTLS_ERROR
152     | VIR_WAR_NO_NETWORK
153     | VIR_ERR_NO_DOMAIN
154     | VIR_ERR_NO_NETWORK
155     | VIR_ERR_INVALID_MAC
156     | VIR_ERR_AUTH_FAILED
157     | VIR_ERR_INVALID_STORAGE_POOL
158     | VIR_ERR_INVALID_STORAGE_VOL
159     | VIR_WAR_NO_STORAGE
160     | VIR_ERR_NO_STORAGE_POOL
161     | VIR_ERR_NO_STORAGE_VOL
162     | VIR_ERR_UNKNOWN of int
163
164   let string_of_code = function
165     | VIR_ERR_OK -> "VIR_ERR_OK"
166     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
167     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
168     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
169     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
170     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
171     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
172     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
173     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
174     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
175     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
176     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
177     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
178     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
179     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
180     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
181     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
182     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
183     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
184     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
185     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
186     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
187     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
188     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
189     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
190     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
191     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
192     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
193     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
194     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
195     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
196     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
197     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
198     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
199     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
200     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
201     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
202     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
203     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
204     | VIR_ERR_RPC -> "VIR_ERR_RPC"
205     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
206     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
207     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
208     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
209     | VIR_ERR_INVALID_MAC -> "VIR_ERR_INVALID_MAC"
210     | VIR_ERR_AUTH_FAILED -> "VIR_ERR_AUTH_FAILED"
211     | VIR_ERR_INVALID_STORAGE_POOL -> "VIR_ERR_INVALID_STORAGE_POOL"
212     | VIR_ERR_INVALID_STORAGE_VOL -> "VIR_ERR_INVALID_STORAGE_VOL"
213     | VIR_WAR_NO_STORAGE -> "VIR_WAR_NO_STORAGE"
214     | VIR_ERR_NO_STORAGE_POOL -> "VIR_ERR_NO_STORAGE_POOL"
215     | VIR_ERR_NO_STORAGE_VOL -> "VIR_ERR_NO_STORAGE_VOL"
216     | VIR_ERR_UNKNOWN i -> "VIR_ERR_" ^ string_of_int i
217
218   type domain =
219     | VIR_FROM_NONE
220     | VIR_FROM_XEN
221     | VIR_FROM_XEND
222     | VIR_FROM_XENSTORE
223     | VIR_FROM_SEXPR
224     | VIR_FROM_XML
225     | VIR_FROM_DOM
226     | VIR_FROM_RPC
227     | VIR_FROM_PROXY
228     | VIR_FROM_CONF
229     | VIR_FROM_QEMU
230     | VIR_FROM_NET
231     | VIR_FROM_TEST
232     | VIR_FROM_REMOTE
233     | VIR_FROM_OPENVZ
234     | VIR_FROM_XENXM
235     | VIR_FROM_STATS_LINUX
236     | VIR_FROM_STORAGE
237     | VIR_FROM_UNKNOWN of int
238
239   let string_of_domain = function
240     | VIR_FROM_NONE -> "VIR_FROM_NONE"
241     | VIR_FROM_XEN -> "VIR_FROM_XEN"
242     | VIR_FROM_XEND -> "VIR_FROM_XEND"
243     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
244     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
245     | VIR_FROM_XML -> "VIR_FROM_XML"
246     | VIR_FROM_DOM -> "VIR_FROM_DOM"
247     | VIR_FROM_RPC -> "VIR_FROM_RPC"
248     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
249     | VIR_FROM_CONF -> "VIR_FROM_CONF"
250     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
251     | VIR_FROM_NET -> "VIR_FROM_NET"
252     | VIR_FROM_TEST -> "VIR_FROM_TEST"
253     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
254     | VIR_FROM_OPENVZ -> "VIR_FROM_OPENVZ"
255     | VIR_FROM_XENXM -> "VIR_FROM_XENXM"
256     | VIR_FROM_STATS_LINUX -> "VIR_FROM_STATS_LINUX"
257     | VIR_FROM_STORAGE -> "VIR_FROM_STORAGE"
258     | VIR_FROM_UNKNOWN i -> "VIR_FROM_" ^ string_of_int i
259
260   type level =
261     | VIR_ERR_NONE
262     | VIR_ERR_WARNING
263     | VIR_ERR_ERROR
264     | VIR_ERR_UNKNOWN_LEVEL of int
265
266   let string_of_level = function
267     | VIR_ERR_NONE -> "VIR_ERR_NONE"
268     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
269     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
270     | VIR_ERR_UNKNOWN_LEVEL i -> "VIR_ERR_LEVEL_" ^ string_of_int i
271
272   type t = {
273     code : code;
274     domain : domain;
275     message : string option;
276     level : level;
277     str1 : string option;
278     str2 : string option;
279     str3 : string option;
280     int1 : int32;
281     int2 : int32;
282   }
283
284   let to_string { code = code; domain = domain; message = message } =
285     let buf = Buffer.create 128 in
286     Buffer.add_string buf "libvirt: ";
287     Buffer.add_string buf (string_of_code code);
288     Buffer.add_string buf ": ";
289     Buffer.add_string buf (string_of_domain domain);
290     Buffer.add_string buf ": ";
291     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
292     Buffer.contents buf
293
294   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
295   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
296   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
297   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
298
299   let no_error () =
300     { code = VIR_ERR_OK; domain = VIR_FROM_NONE;
301       message = None; level = VIR_ERR_NONE;
302       str1 = None; str2 = None; str3 = None;
303       int1 = 0_l; int2 = 0_l }
304 end
305
306 exception Virterror of Virterror.t
307 exception Not_supported of string
308
309 let rec map_ignore_errors f = function
310   | [] -> []
311   | x :: xs ->
312       try f x :: map_ignore_errors f xs
313       with Virterror _ -> map_ignore_errors f xs
314
315 module Domain =
316 struct
317   type 'rw t
318
319   type state =
320     | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
321     | InfoShutdown | InfoShutoff | InfoCrashed
322
323   type info = {
324     state : state;
325     max_mem : int64;
326     memory : int64;
327     nr_virt_cpu : int;
328     cpu_time : int64;
329   }
330
331   type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
332
333   type vcpu_info = {
334     number : int;
335     vcpu_state : vcpu_state;
336     vcpu_time : int64;
337     cpu : int;
338   }
339
340   type domain_create_flag =
341   | START_PAUSED
342   | START_AUTODESTROY
343   | START_BYPASS_CACHE
344   | START_FORCE_BOOT
345   | START_VALIDATE
346   let rec int_of_domain_create_flags = function
347     | [] -> 0
348     | START_PAUSED :: flags ->       1 lor int_of_domain_create_flags flags
349     | START_AUTODESTROY :: flags ->  2 lor int_of_domain_create_flags flags
350     | START_BYPASS_CACHE :: flags -> 4 lor int_of_domain_create_flags flags
351     | START_FORCE_BOOT :: flags ->   8 lor int_of_domain_create_flags flags
352     | START_VALIDATE :: flags ->    16 lor int_of_domain_create_flags flags
353
354   type sched_param = string * sched_param_value
355   and sched_param_value =
356     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
357     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
358     | SchedFieldFloat of float | SchedFieldBool of bool
359
360   type typed_param = string * typed_param_value
361   and typed_param_value =
362     | TypedFieldInt32 of int32 | TypedFieldUInt32 of int32
363     | TypedFieldInt64 of int64 | TypedFieldUInt64 of int64
364     | TypedFieldFloat of float | TypedFieldBool of bool
365     | TypedFieldString of string
366
367   type migrate_flag = Live
368
369   type memory_flag = Virtual
370
371   type list_flag =
372     | ListActive
373     | ListInactive
374     | ListAll
375
376   type block_stats = {
377     rd_req : int64;
378     rd_bytes : int64;
379     wr_req : int64;
380     wr_bytes : int64;
381     errs : int64;
382   }
383
384   type interface_stats = {
385     rx_bytes : int64;
386     rx_packets : int64;
387     rx_errs : int64;
388     rx_drop : int64;
389     tx_bytes : int64;
390     tx_packets : int64;
391     tx_errs : int64;
392     tx_drop : int64;
393   }
394
395   (* The maximum size for Domain.memory_peek and Domain.block_peek
396    * supported by libvirt.  This may change with different versions
397    * of libvirt in the future, hence it's a function.
398    *)
399   let max_peek _ = 65536
400
401   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
402   external _create_xml : [>`W] Connect.t -> xml -> int -> rw t = "ocaml_libvirt_domain_create_xml"
403   let create_xml conn xml flags =
404     _create_xml conn xml (int_of_domain_create_flags flags)
405   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
406   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
407   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
408   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
409   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
410   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
411   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
412   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
413   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
414   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
415   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
416   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
417   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
418   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
419   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
420   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
421   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
422   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
423   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
424   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
425   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
426   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
427   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
428   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
429   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
430   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
431   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
432   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
433   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
434   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
435   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
436   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
437   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
438   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
439   external get_cpu_stats : [>`R] t -> typed_param list array = "ocaml_libvirt_domain_get_cpu_stats"
440   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
441   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
442   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
443   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"
444   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
445   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
446   external block_peek : [>`W] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
447   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"
448
449   external const : [>`R] t -> ro t = "%identity"
450
451   let get_domains conn flags =
452     (* Old/slow/inefficient method. *)
453     let get_active, get_inactive =
454       if List.mem ListAll flags then
455         (true, true)
456       else
457         (List.mem ListActive flags, List.mem ListInactive flags) in
458     let active_doms =
459       if get_active then (
460         let n = Connect.num_of_domains conn in
461         let ids = Connect.list_domains conn n in
462         let ids = Array.to_list ids in
463         map_ignore_errors (lookup_by_id conn) ids
464       ) else [] in
465
466     let inactive_doms =
467       if get_inactive then (
468         let n = Connect.num_of_defined_domains conn in
469         let names = Connect.list_defined_domains conn n in
470         let names = Array.to_list names in
471         map_ignore_errors (lookup_by_name conn) names
472       ) else [] in
473
474     active_doms @ inactive_doms
475
476   let get_domains_and_infos conn flags =
477     (* Old/slow/inefficient method. *)
478     let get_active, get_inactive =
479       if List.mem ListAll flags then
480         (true, true)
481       else (List.mem ListActive flags, List.mem ListInactive flags) in
482     let active_doms =
483       if get_active then (
484         let n = Connect.num_of_domains conn in
485         let ids = Connect.list_domains conn n in
486         let ids = Array.to_list ids in
487         map_ignore_errors (lookup_by_id conn) ids
488       ) else [] in
489
490     let inactive_doms =
491       if get_inactive then (
492         let n = Connect.num_of_defined_domains conn in
493         let names = Connect.list_defined_domains conn n in
494         let names = Array.to_list names in
495         map_ignore_errors (lookup_by_name conn) names
496       ) else [] in
497
498     let doms = active_doms @ inactive_doms in
499
500     map_ignore_errors (fun dom -> (dom, get_info dom)) doms
501 end
502
503 module Event =
504 struct
505
506   module Defined = struct
507     type t = [
508       | `Added
509       | `Updated
510       | `Unknown of int
511     ]
512
513     let to_string = function
514       | `Added -> "Added"
515       | `Updated -> "Updated"
516       | `Unknown x -> Printf.sprintf "Unknown Defined.detail: %d" x
517
518     let make = function
519       | 0 -> `Added
520       | 1 -> `Updated
521       | x -> `Unknown x (* newer libvirt *)
522   end
523
524   module Undefined = struct
525     type t = [
526       | `Removed
527       | `Unknown of int
528     ]
529
530     let to_string = function
531       | `Removed -> "UndefinedRemoved"
532       | `Unknown x -> Printf.sprintf "Unknown Undefined.detail: %d" x
533
534     let make = function
535       | 0 -> `Removed
536       | x -> `Unknown x (* newer libvirt *)
537   end
538
539   module Started = struct
540     type t = [
541       | `Booted
542       | `Migrated
543       | `Restored
544       | `FromSnapshot
545       | `Wakeup
546       | `Unknown of int
547     ]
548
549     let to_string = function
550       | `Booted -> "Booted"
551       | `Migrated -> "Migrated"
552       | `Restored -> "Restored"
553       | `FromSnapshot -> "FromSnapshot"
554       | `Wakeup -> "Wakeup"
555       | `Unknown x -> Printf.sprintf "Unknown Started.detail: %d" x
556  
557     let make = function
558       | 0 -> `Booted
559       | 1 -> `Migrated
560       | 2 -> `Restored
561       | 3 -> `FromSnapshot
562       | 4 -> `Wakeup
563       | x -> `Unknown x (* newer libvirt *)
564   end
565
566   module Suspended = struct
567     type t = [
568       | `Paused
569       | `Migrated
570       | `IOError
571       | `Watchdog
572       | `Restored
573       | `FromSnapshot
574       | `APIError
575       | `Unknown of int (* newer libvirt *)
576     ]
577
578     let to_string = function
579       | `Paused -> "Paused"
580       | `Migrated -> "Migrated"
581       | `IOError -> "IOError"
582       | `Watchdog -> "Watchdog"
583       | `Restored -> "Restored"
584       | `FromSnapshot -> "FromSnapshot"
585       | `APIError -> "APIError"
586       | `Unknown x -> Printf.sprintf "Unknown Suspended.detail: %d" x
587
588      let make = function
589       | 0 -> `Paused
590       | 1 -> `Migrated
591       | 2 -> `IOError
592       | 3 -> `Watchdog
593       | 4 -> `Restored
594       | 5 -> `FromSnapshot
595       | 6 -> `APIError
596       | x -> `Unknown x (* newer libvirt *)
597   end
598
599   module Resumed = struct
600     type t = [
601       | `Unpaused
602       | `Migrated
603       | `FromSnapshot
604       | `Unknown of int (* newer libvirt *)
605     ]
606
607     let to_string = function
608       | `Unpaused -> "Unpaused"
609       | `Migrated -> "Migrated"
610       | `FromSnapshot -> "FromSnapshot"
611       | `Unknown x -> Printf.sprintf "Unknown Resumed.detail: %d" x
612
613     let make = function
614       | 0 -> `Unpaused
615       | 1 -> `Migrated
616       | 2 -> `FromSnapshot
617       | x -> `Unknown x (* newer libvirt *)
618   end
619
620   module Stopped = struct
621     type t = [
622       | `Shutdown
623       | `Destroyed
624       | `Crashed
625       | `Migrated
626       | `Saved
627       | `Failed
628       | `FromSnapshot
629       | `Unknown of int
630     ]
631     let to_string = function
632       | `Shutdown -> "Shutdown"
633       | `Destroyed -> "Destroyed"
634       | `Crashed -> "Crashed"
635       | `Migrated -> "Migrated"
636       | `Saved -> "Saved"
637       | `Failed -> "Failed"
638       | `FromSnapshot -> "FromSnapshot"
639       | `Unknown x -> Printf.sprintf "Unknown Stopped.detail: %d" x
640
641     let make = function
642       | 0 -> `Shutdown
643       | 1 -> `Destroyed
644       | 2 -> `Crashed
645       | 3 -> `Migrated
646       | 4 -> `Saved
647       | 5 -> `Failed
648       | 6 -> `FromSnapshot
649       | x -> `Unknown x (* newer libvirt *)
650   end
651
652   module PM_suspended = struct
653     type t = [
654       | `Memory
655       | `Disk
656       | `Unknown of int (* newer libvirt *)
657     ]
658
659     let to_string = function
660       | `Memory -> "Memory"
661       | `Disk -> "Disk"
662       | `Unknown x -> Printf.sprintf "Unknown PM_suspended.detail: %d" x
663
664     let make = function
665       | 0 -> `Memory
666       | 1 -> `Disk
667       | x -> `Unknown x (* newer libvirt *)
668   end
669
670   let string_option x = match x with
671     | None -> "None"
672     | Some x' -> "Some " ^ x'
673
674   module Lifecycle = struct
675     type t = [
676       | `Defined of Defined.t
677       | `Undefined of Undefined.t
678       | `Started of Started.t
679       | `Suspended of Suspended.t
680       | `Resumed of Resumed.t
681       | `Stopped of Stopped.t
682       | `Shutdown (* no detail defined yet *)
683       | `PMSuspended of PM_suspended.t
684       | `Unknown of int (* newer libvirt *)
685     ]
686
687     let to_string = function
688       | `Defined x -> "Defined " ^ (Defined.to_string x)
689       | `Undefined x -> "Undefined " ^ (Undefined.to_string x)
690       | `Started x -> "Started " ^ (Started.to_string x)
691       | `Suspended x -> "Suspended " ^ (Suspended.to_string x)
692       | `Resumed x -> "Resumed " ^ (Resumed.to_string x)
693       | `Stopped x -> "Stopped " ^ (Stopped.to_string x)
694       | `Shutdown -> "Shutdown"
695       | `PMSuspended x -> "PMSuspended " ^ (PM_suspended.to_string x)
696       | `Unknown x -> Printf.sprintf "Unknown Lifecycle event: %d" x
697
698     let make (ty, detail) = match ty with
699       | 0 -> `Defined (Defined.make detail)
700       | 1 -> `Undefined (Undefined.make detail)
701       | 2 -> `Started (Started.make detail)
702       | 3 -> `Suspended (Suspended.make detail)
703       | 4 -> `Resumed (Resumed.make detail)
704       | 5 -> `Stopped (Stopped.make detail)
705       | 6 -> `Shutdown
706       | 7 -> `PMSuspended (PM_suspended.make detail)
707       | x -> `Unknown x
708   end
709
710   module Reboot = struct
711     type t = unit
712
713     let to_string _ = "()"
714
715     let make () = ()
716   end
717
718   module Rtc_change = struct
719     type t = int64
720
721     let to_string = Int64.to_string
722
723     let make x = x
724   end
725
726   module Watchdog = struct
727     type t = [
728       | `None
729       | `Pause
730       | `Reset
731       | `Poweroff
732       | `Shutdown
733       | `Debug
734       | `Unknown of int
735     ]
736
737     let to_string = function
738       | `None -> "None"
739       | `Pause -> "Pause"
740       | `Reset -> "Reset"
741       | `Poweroff -> "Poweroff"
742       | `Shutdown -> "Shutdown"
743       | `Debug -> "Debug"
744       | `Unknown x -> Printf.sprintf "Unknown watchdog_action: %d" x
745
746     let make = function
747       | 0 -> `None
748       | 1 -> `Pause
749       | 2 -> `Reset
750       | 3 -> `Poweroff
751       | 4 -> `Shutdown
752       | 5 -> `Debug
753       | x -> `Unknown x (* newer libvirt *)
754   end
755
756   module Io_error = struct
757     type action = [
758       | `None
759       | `Pause
760       | `Report
761       | `Unknown of int (* newer libvirt *)
762     ]
763
764     let string_of_action = function
765       | `None -> "None"
766       | `Pause -> "Pause"
767       | `Report -> "Report"
768       | `Unknown x -> Printf.sprintf "Unknown Io_error.action: %d" x
769
770     let action_of_int = function
771       | 0 -> `None
772       | 1 -> `Pause
773       | 2 -> `Report
774       | x -> `Unknown x
775
776     type t = {
777       src_path: string option;
778       dev_alias: string option;
779       action: action;
780       reason: string option;
781     }
782
783     let to_string t = Printf.sprintf
784         "{ Io_error.src_path = %s; dev_alias = %s; action = %s; reason = %s }"
785         (string_option t.src_path)
786         (string_option t.dev_alias)
787         (string_of_action t.action)
788         (string_option t.reason)
789
790     let make (src_path, dev_alias, action, reason) = {
791         src_path = src_path;
792         dev_alias = dev_alias;
793         action = action_of_int action;
794         reason = reason;
795     }
796
797     let make_noreason (src_path, dev_alias, action) =
798       make (src_path, dev_alias, action, None)
799   end
800
801   module Graphics_address = struct
802     type family = [
803       | `Ipv4
804       | `Ipv6
805       | `Unix
806       | `Unknown of int (* newer libvirt *)
807     ]
808
809     let string_of_family = function
810       | `Ipv4 -> "IPv4"
811       | `Ipv6 -> "IPv6"
812       | `Unix -> "UNIX"
813       | `Unknown x -> Printf.sprintf "Unknown Graphics_address.family: %d" x
814
815     let family_of_int = function
816       (* no zero *)
817       | 1 -> `Ipv4
818       | 2 -> `Ipv6
819       | 3 -> `Unix
820       | x -> `Unknown x
821
822     type t = {
823       family: family;         (** Address family *)
824       node: string option;    (** Address of node (eg IP address, or UNIX path *)
825       service: string option; (** Service name/number (eg TCP port, or NULL) *)
826     }
827
828     let to_string t = Printf.sprintf
829       "{ family = %s; node = %s; service = %s }"
830         (string_of_family t.family)
831         (string_option t.node)
832         (string_option t.service)
833
834     let make (family, node, service) = {
835       family = family_of_int family;
836       node = node;
837       service = service;
838     }
839   end
840
841   module Graphics_subject = struct
842     type identity = {
843       ty: string option;
844       name: string option;
845     }
846
847     let string_of_identity t = Printf.sprintf
848       "{ ty = %s; name = %s }"
849       (string_option t.ty)
850       (string_option t.name)
851
852     type t = identity list
853
854     let to_string ts =
855       "[ " ^ (String.concat "; " (List.map string_of_identity ts)) ^ " ]"
856
857     let make xs =
858       List.map (fun (ty, name) -> { ty = ty; name = name })
859         (Array.to_list xs)
860   end
861
862   module Graphics = struct
863     type phase = [
864       | `Connect
865       | `Initialize
866       | `Disconnect
867       | `Unknown of int (** newer libvirt *)
868     ]
869
870     let string_of_phase = function
871       | `Connect -> "Connect"
872       | `Initialize -> "Initialize"
873       | `Disconnect -> "Disconnect"
874       | `Unknown x -> Printf.sprintf "Unknown Graphics.phase: %d" x
875
876     let phase_of_int = function
877       | 0 -> `Connect
878       | 1 -> `Initialize
879       | 2 -> `Disconnect
880       | x -> `Unknown x
881
882     type t = {
883       phase: phase;                (** the phase of the connection *)
884       local: Graphics_address.t;   (** the local server address *)
885       remote: Graphics_address.t;  (** the remote client address *)
886       auth_scheme: string option;  (** the authentication scheme activated *)
887       subject: Graphics_subject.t; (** the authenticated subject (user) *)
888     }
889
890     let to_string t =
891       let phase = Printf.sprintf "phase = %s"
892         (string_of_phase t.phase) in
893       let local = Printf.sprintf "local = %s"
894         (Graphics_address.to_string t.local) in
895       let remote = Printf.sprintf "remote = %s"
896         (Graphics_address.to_string t.remote) in
897       let auth_scheme = Printf.sprintf "auth_scheme = %s"
898         (string_option t.auth_scheme) in
899       let subject = Printf.sprintf "subject = %s"
900         (Graphics_subject.to_string t.subject) in
901       "{ " ^ (String.concat "; " [ phase; local; remote; auth_scheme; subject ]) ^ " }"
902
903     let make (phase, local, remote, auth_scheme, subject) = {
904       phase = phase_of_int phase;
905       local = Graphics_address.make local;
906       remote = Graphics_address.make remote;
907       auth_scheme = auth_scheme;
908       subject = Graphics_subject.make subject;
909     }
910   end
911
912   module Control_error = struct
913     type t = unit
914
915     let to_string () = "()"
916
917     let make () = ()
918   end
919
920   module Block_job = struct
921     type ty = [
922       | `KnownUnknown (* explicitly named UNKNOWN in the spec *)
923       | `Pull
924       | `Copy
925       | `Commit
926       | `Unknown of int (* newer libvirt *)
927     ]
928
929     let string_of_ty = function
930       | `KnownUnknown -> "KnownUnknown"
931       | `Pull -> "Pull"
932       | `Copy -> "Copy"
933       | `Commit -> "Commit"
934       | `Unknown x -> Printf.sprintf "Unknown Block_job.ty: %d" x
935
936     let ty_of_int = function
937       | 0 -> `KnownUnknown
938       | 1 -> `Pull
939       | 2 -> `Copy
940       | 3 -> `Commit
941       | x -> `Unknown x (* newer libvirt *)
942
943     type status = [
944       | `Completed
945       | `Failed
946       | `Cancelled
947       | `Ready
948       | `Unknown of int
949     ]
950
951     let string_of_status = function
952       | `Completed -> "Completed"
953       | `Failed -> "Failed"
954       | `Cancelled -> "Cancelled"
955       | `Ready -> "Ready"
956       | `Unknown x -> Printf.sprintf "Unknown Block_job.status: %d" x
957
958     let status_of_int = function
959       | 0 -> `Completed
960       | 1 -> `Failed
961       | 2 -> `Cancelled
962       | 3 -> `Ready
963       | x -> `Unknown x
964
965     type t = {
966       disk: string option;
967       ty: ty;
968       status: status;
969     }
970
971     let to_string t = Printf.sprintf "{ disk = %s; ty = %s; status = %s }"
972       (string_option t.disk)
973       (string_of_ty t.ty)
974       (string_of_status t.status)
975
976     let make (disk, ty, status) = {
977       disk = disk;
978       ty = ty_of_int ty;
979       status = status_of_int ty;
980     }
981   end
982
983   module Disk_change = struct
984     type reason = [
985       | `MissingOnStart
986       | `Unknown of int
987     ]
988
989     let string_of_reason = function
990       | `MissingOnStart -> "MissingOnStart"
991       | `Unknown x -> Printf.sprintf "Unknown Disk_change.reason: %d" x
992
993     let reason_of_int = function
994       | 0 -> `MissingOnStart
995       | x -> `Unknown x
996
997     type t = {
998       old_src_path: string option;
999       new_src_path: string option;
1000       dev_alias: string option;
1001       reason: reason;
1002     }
1003
1004     let to_string t =
1005       let o = Printf.sprintf "old_src_path = %s" (string_option t.old_src_path) in
1006       let n = Printf.sprintf "new_src_path = %s" (string_option t.new_src_path) in
1007       let d = Printf.sprintf "dev_alias = %s" (string_option t.dev_alias) in
1008       let r = string_of_reason t.reason in
1009       "{ " ^ (String.concat "; " [ o; n; d; r ]) ^ " }"
1010
1011     let make (o, n, d, r) = {
1012       old_src_path = o;
1013       new_src_path = n;
1014       dev_alias = d;
1015       reason = reason_of_int r;
1016     }
1017   end
1018
1019   module Tray_change = struct
1020     type reason = [
1021       | `Open
1022       | `Close
1023       | `Unknown of int
1024     ]
1025
1026     let string_of_reason = function
1027       | `Open -> "Open"
1028       | `Close -> "Close"
1029       | `Unknown x -> Printf.sprintf "Unknown Tray_change.reason: %d" x
1030
1031     let reason_of_int = function
1032       | 0 -> `Open
1033       | 1 -> `Close
1034       | x -> `Unknown x
1035
1036     type t = {
1037       dev_alias: string option;
1038       reason: reason;
1039     }
1040
1041     let to_string t = Printf.sprintf
1042       "{ dev_alias = %s; reason = %s }"
1043         (string_option t.dev_alias)
1044         (string_of_reason t.reason)
1045
1046     let make (dev_alias, reason) = {
1047       dev_alias = dev_alias;
1048       reason = reason_of_int reason;
1049     }
1050   end
1051
1052   module PM_wakeup = struct
1053     type reason = [
1054       | `Unknown of int
1055     ]
1056
1057     type t = reason
1058
1059     let to_string = function
1060       | `Unknown x -> Printf.sprintf "Unknown PM_wakeup.reason: %d" x
1061
1062     let make x = `Unknown x
1063   end
1064
1065   module PM_suspend = struct
1066     type reason = [
1067       | `Unknown of int
1068     ]
1069
1070     type t = reason
1071
1072     let to_string = function
1073       | `Unknown x -> Printf.sprintf "Unknown PM_suspend.reason: %d" x
1074
1075     let make x = `Unknown x
1076   end
1077
1078   module Balloon_change = struct
1079     type t = int64
1080
1081     let to_string = Int64.to_string
1082     let make x = x
1083   end
1084
1085   module PM_suspend_disk = struct
1086     type reason = [
1087       | `Unknown of int
1088     ]
1089
1090     type t = reason
1091
1092     let to_string = function
1093       | `Unknown x -> Printf.sprintf "Unknown PM_suspend_disk.reason: %d" x
1094
1095     let make x = `Unknown x
1096   end
1097
1098   type callback =
1099     | Lifecycle     of ([`R] Domain.t -> Lifecycle.t -> unit)
1100     | Reboot        of ([`R] Domain.t -> Reboot.t -> unit)
1101     | RtcChange     of ([`R] Domain.t -> Rtc_change.t -> unit)
1102     | Watchdog      of ([`R] Domain.t -> Watchdog.t -> unit)
1103     | IOError       of ([`R] Domain.t -> Io_error.t -> unit)
1104     | Graphics      of ([`R] Domain.t -> Graphics.t -> unit)
1105     | IOErrorReason of ([`R] Domain.t -> Io_error.t -> unit)
1106     | ControlError  of ([`R] Domain.t -> Control_error.t -> unit)
1107     | BlockJob      of ([`R] Domain.t -> Block_job.t -> unit)
1108     | DiskChange    of ([`R] Domain.t -> Disk_change.t -> unit)
1109     | TrayChange    of ([`R] Domain.t -> Tray_change.t -> unit)
1110     | PMWakeUp      of ([`R] Domain.t -> PM_wakeup.t -> unit)
1111     | PMSuspend     of ([`R] Domain.t -> PM_suspend.t -> unit)
1112     | BalloonChange of ([`R] Domain.t -> Balloon_change.t -> unit)
1113     | PMSuspendDisk of ([`R] Domain.t -> PM_suspend_disk.t -> unit)
1114
1115   type callback_id = int64
1116
1117   let fresh_callback_id =
1118     let next = ref 0L in
1119     fun () ->
1120       let result = !next in
1121       next := Int64.succ !next;
1122       result
1123
1124   let make_table value_name =
1125     let table = Hashtbl.create 16 in
1126     let callback callback_id generic x =
1127       if Hashtbl.mem table callback_id
1128       then Hashtbl.find table callback_id generic x in
1129     let _ = Callback.register value_name callback in
1130     table
1131
1132   let u_table = make_table "Libvirt.u_callback"
1133   let i_table = make_table "Libvirt.i_callback"
1134   let i64_table = make_table "Libvirt.i64_callback"
1135   let i_i_table = make_table "Libvirt.i_i_callback"
1136   let s_i_table = make_table "Libvirt.s_i_callback"
1137   let s_i_i_table = make_table "Libvirt.s_i_i_callback"
1138   let s_s_i_table = make_table "Libvirt.s_s_i_callback"
1139   let s_s_i_s_table = make_table "Libvirt.s_s_i_s_callback"
1140   let s_s_s_i_table = make_table "Libvirt.s_s_s_i_callback"
1141   let i_ga_ga_s_gs_table = make_table "Libvirt.i_ga_ga_s_gs_callback"
1142
1143   external register_default_impl : unit -> unit = "ocaml_libvirt_event_register_default_impl"
1144
1145   external run_default_impl : unit -> unit = "ocaml_libvirt_event_run_default_impl"
1146
1147   external register_any' : 'a Connect.t -> 'a Domain.t option -> callback -> callback_id -> int = "ocaml_libvirt_connect_domain_event_register_any"
1148
1149   external deregister_any' : 'a Connect.t -> int -> unit = "ocaml_libvirt_connect_domain_event_deregister_any"
1150
1151   let our_id_to_libvirt_id = Hashtbl.create 16
1152
1153   let register_any conn ?dom callback =
1154     let id = fresh_callback_id () in
1155     begin match callback with
1156     | Lifecycle f ->
1157         Hashtbl.add i_i_table id (fun dom x ->
1158             f dom (Lifecycle.make x)
1159         )
1160     | Reboot f ->
1161         Hashtbl.add u_table id (fun dom x ->
1162             f dom (Reboot.make x)
1163         )
1164     | RtcChange f ->
1165         Hashtbl.add i64_table id (fun dom x ->
1166             f dom (Rtc_change.make x)
1167         )
1168     | Watchdog f ->
1169         Hashtbl.add i_table id (fun dom x ->
1170             f dom (Watchdog.make x)
1171         ) 
1172     | IOError f ->
1173         Hashtbl.add s_s_i_table id (fun dom x ->
1174             f dom (Io_error.make_noreason x)
1175         )
1176     | Graphics f ->
1177         Hashtbl.add i_ga_ga_s_gs_table id (fun dom x ->
1178             f dom (Graphics.make x)
1179         )
1180     | IOErrorReason f ->
1181         Hashtbl.add s_s_i_s_table id (fun dom x ->
1182             f dom (Io_error.make x)
1183         )
1184     | ControlError f ->
1185         Hashtbl.add u_table id (fun dom x ->
1186             f dom (Control_error.make x)
1187         )
1188     | BlockJob f ->
1189         Hashtbl.add s_i_i_table id (fun dom x ->
1190             f dom (Block_job.make x)
1191         )
1192     | DiskChange f ->
1193         Hashtbl.add s_s_s_i_table id (fun dom x ->
1194             f dom (Disk_change.make x)
1195         )
1196     | TrayChange f ->
1197         Hashtbl.add s_i_table id (fun dom x ->
1198             f dom (Tray_change.make x)
1199         )
1200     | PMWakeUp f ->
1201         Hashtbl.add i_table id (fun dom x ->
1202             f dom (PM_wakeup.make x)
1203         )
1204     | PMSuspend f ->
1205         Hashtbl.add i_table id (fun dom x ->
1206             f dom (PM_suspend.make x)
1207         )
1208     | BalloonChange f ->
1209         Hashtbl.add i64_table id (fun dom x ->
1210             f dom (Balloon_change.make x)
1211         )
1212     | PMSuspendDisk f ->
1213         Hashtbl.add i_table id (fun dom x ->
1214             f dom (PM_suspend_disk.make x)
1215         )
1216     end;
1217     let libvirt_id = register_any' conn dom callback id in
1218     Hashtbl.replace our_id_to_libvirt_id id libvirt_id;
1219     id
1220
1221   let deregister_any conn id =
1222     if Hashtbl.mem our_id_to_libvirt_id id then begin
1223       let libvirt_id = Hashtbl.find our_id_to_libvirt_id id in
1224       deregister_any' conn libvirt_id
1225     end;
1226     Hashtbl.remove our_id_to_libvirt_id id;
1227     Hashtbl.remove u_table id;
1228     Hashtbl.remove i_table id;
1229     Hashtbl.remove i64_table id;
1230     Hashtbl.remove i_i_table id;
1231     Hashtbl.remove s_i_table id;
1232     Hashtbl.remove s_i_i_table id;
1233     Hashtbl.remove s_s_i_table id;
1234     Hashtbl.remove s_s_i_s_table id;
1235     Hashtbl.remove s_s_s_i_table id;
1236     Hashtbl.remove i_ga_ga_s_gs_table id
1237
1238   let timeout_table = Hashtbl.create 16
1239   let _ =
1240     let callback x =
1241       if Hashtbl.mem timeout_table x
1242       then Hashtbl.find timeout_table x () in
1243   Callback.register "Libvirt.timeout_callback" callback
1244
1245   type timer_id = int64
1246
1247   external add_timeout' : 'a Connect.t -> int -> int64 -> int = "ocaml_libvirt_event_add_timeout"
1248
1249   external remove_timeout' : 'a Connect.t -> int -> unit = "ocaml_libvirt_event_remove_timeout"
1250
1251   let our_id_to_timer_id = Hashtbl.create 16
1252   let add_timeout conn ms fn =
1253     let id = fresh_callback_id () in
1254     Hashtbl.add timeout_table id fn;
1255     let timer_id = add_timeout' conn ms id in
1256     Hashtbl.add our_id_to_timer_id id timer_id;
1257     id
1258
1259   let remove_timeout conn id =
1260     if Hashtbl.mem our_id_to_timer_id id then begin
1261       let timer_id = Hashtbl.find our_id_to_timer_id id in
1262       remove_timeout' conn timer_id
1263     end;
1264     Hashtbl.remove our_id_to_timer_id id;
1265     Hashtbl.remove timeout_table id
1266 end
1267
1268 module Network =
1269 struct
1270   type 'rw t
1271
1272   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
1273   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
1274   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
1275   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
1276   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
1277   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
1278   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
1279   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
1280   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
1281   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
1282   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
1283   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
1284   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
1285   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
1286   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
1287   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
1288
1289   external const : [>`R] t -> ro t = "%identity"
1290 end
1291
1292 module Pool =
1293 struct
1294   type 'rw t
1295   type pool_state = Inactive | Building | Running | Degraded
1296   type pool_build_flags = New | Repair | Resize
1297   type pool_delete_flags = Normal | Zeroed
1298   type pool_info = {
1299     state : pool_state;
1300     capacity : int64;
1301     allocation : int64;
1302     available : int64;
1303   }
1304
1305   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
1306   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
1307   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
1308   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
1309   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
1310   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
1311   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
1312   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
1313   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
1314   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
1315   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
1316   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
1317   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
1318   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
1319   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
1320   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
1321   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
1322   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
1323   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
1324   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
1325   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
1326   external const : [>`R] t -> ro t = "%identity"
1327 end
1328
1329 module Volume =
1330 struct
1331   type 'rw t
1332   type vol_type = File | Block
1333   type vol_delete_flags = Normal | Zeroed
1334   type vol_info = {
1335     typ : vol_type;
1336     capacity : int64;
1337     allocation : int64;
1338   }
1339
1340   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
1341   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
1342   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
1343   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
1344   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
1345   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
1346   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
1347   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
1348   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
1349   external create_xml : [>`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
1350   external delete : [>`W] t -> vol_delete_flags -> unit = "ocaml_libvirt_storage_vol_delete"
1351   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
1352   external const : [>`R] t -> ro t = "%identity"
1353 end
1354
1355 (* Initialization. *)
1356 external c_init : unit -> unit = "ocaml_libvirt_init"
1357 let () =
1358   Callback.register_exception
1359     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
1360   Callback.register_exception
1361     "ocaml_libvirt_not_supported" (Not_supported "");
1362   c_init ()