9c9368adf324640b57ffda05d954b10febdee077
[ocaml-libvirt.git] / libvirt / libvirt.ml
1 (* OCaml bindings for libvirt.
2    (C) Copyright 2007 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 sched_param = string * sched_param_value
341   and sched_param_value =
342     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
343     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
344     | SchedFieldFloat of float | SchedFieldBool of bool
345
346   type typed_param = string * typed_param_value
347   and typed_param_value =
348     | TypedFieldInt32 of int32 | TypedFieldUInt32 of int32
349     | TypedFieldInt64 of int64 | TypedFieldUInt64 of int64
350     | TypedFieldFloat of float | TypedFieldBool of bool
351     | TypedFieldString of string
352
353   type migrate_flag = Live
354
355   type memory_flag = Virtual
356
357   type list_flag =
358     | ListActive
359     | ListInactive
360     | ListAll
361
362   type block_stats = {
363     rd_req : int64;
364     rd_bytes : int64;
365     wr_req : int64;
366     wr_bytes : int64;
367     errs : int64;
368   }
369
370   type interface_stats = {
371     rx_bytes : int64;
372     rx_packets : int64;
373     rx_errs : int64;
374     rx_drop : int64;
375     tx_bytes : int64;
376     tx_packets : int64;
377     tx_errs : int64;
378     tx_drop : int64;
379   }
380
381   (* The maximum size for Domain.memory_peek and Domain.block_peek
382    * supported by libvirt.  This may change with different versions
383    * of libvirt in the future, hence it's a function.
384    *)
385   let max_peek _ = 65536
386
387   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
388   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
389   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
390   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
391   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
392   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
393   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
394   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
395   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
396   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
397   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
398   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
399   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
400   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
401   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
402   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
403   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
404   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
405   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
406   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
407   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
408   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
409   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
410   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
411   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
412   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
413   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
414   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
415   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
416   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
417   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
418   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
419   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
420   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
421   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
422   external get_cpu_stats : [>`R] t -> typed_param list array = "ocaml_libvirt_domain_get_cpu_stats"
423   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
424   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
425   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
426   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"
427   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
428   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
429   external block_peek : [>`W] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
430   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"
431
432   external const : [>`R] t -> ro t = "%identity"
433
434   let get_domains conn flags =
435     (* Old/slow/inefficient method. *)
436     let get_active, get_inactive =
437       if List.mem ListAll flags then
438         (true, true)
439       else
440         (List.mem ListActive flags, List.mem ListInactive flags) in
441     let active_doms =
442       if get_active then (
443         let n = Connect.num_of_domains conn in
444         let ids = Connect.list_domains conn n in
445         let ids = Array.to_list ids in
446         map_ignore_errors (lookup_by_id conn) ids
447       ) else [] in
448
449     let inactive_doms =
450       if get_inactive then (
451         let n = Connect.num_of_defined_domains conn in
452         let names = Connect.list_defined_domains conn n in
453         let names = Array.to_list names in
454         map_ignore_errors (lookup_by_name conn) names
455       ) else [] in
456
457     active_doms @ inactive_doms
458
459   let get_domains_and_infos conn flags =
460     (* Old/slow/inefficient method. *)
461     let get_active, get_inactive =
462       if List.mem ListAll flags then
463         (true, true)
464       else (List.mem ListActive flags, List.mem ListInactive flags) in
465     let active_doms =
466       if get_active then (
467         let n = Connect.num_of_domains conn in
468         let ids = Connect.list_domains conn n in
469         let ids = Array.to_list ids in
470         map_ignore_errors (lookup_by_id conn) ids
471       ) else [] in
472
473     let inactive_doms =
474       if get_inactive then (
475         let n = Connect.num_of_defined_domains conn in
476         let names = Connect.list_defined_domains conn n in
477         let names = Array.to_list names in
478         map_ignore_errors (lookup_by_name conn) names
479       ) else [] in
480
481     let doms = active_doms @ inactive_doms in
482
483     map_ignore_errors (fun dom -> (dom, get_info dom)) doms
484 end
485
486 module Event =
487 struct
488
489   module Defined = struct
490     type t = [
491       | `Added
492       | `Updated
493       | `Unknown of int
494     ]
495
496     let to_string = function
497       | `Added -> "Added"
498       | `Updated -> "Updated"
499       | `Unknown x -> Printf.sprintf "Unknown Defined.detail: %d" x
500
501     let make = function
502       | 0 -> `Added
503       | 1 -> `Updated
504       | x -> `Unknown x (* newer libvirt *)
505   end
506
507   module Undefined = struct
508     type t = [
509       | `Removed
510       | `Unknown of int
511     ]
512
513     let to_string = function
514       | `Removed -> "UndefinedRemoved"
515       | `Unknown x -> Printf.sprintf "Unknown Undefined.detail: %d" x
516
517     let make = function
518       | 0 -> `Removed
519       | x -> `Unknown x (* newer libvirt *)
520   end
521
522   module Started = struct
523     type t = [
524       | `Booted
525       | `Migrated
526       | `Restored
527       | `FromSnapshot
528       | `Wakeup
529       | `Unknown of int
530     ]
531
532     let to_string = function
533       | `Booted -> "Booted"
534       | `Migrated -> "Migrated"
535       | `Restored -> "Restored"
536       | `FromSnapshot -> "FromSnapshot"
537       | `Wakeup -> "Wakeup"
538       | `Unknown x -> Printf.sprintf "Unknown Started.detail: %d" x
539  
540     let make = function
541       | 0 -> `Booted
542       | 1 -> `Migrated
543       | 2 -> `Restored
544       | 3 -> `FromSnapshot
545       | 4 -> `Wakeup
546       | x -> `Unknown x (* newer libvirt *)
547   end
548
549   module Suspended = struct
550     type t = [
551       | `Paused
552       | `Migrated
553       | `IOError
554       | `Watchdog
555       | `Restored
556       | `FromSnapshot
557       | `APIError
558       | `Unknown of int (* newer libvirt *)
559     ]
560
561     let to_string = function
562       | `Paused -> "Paused"
563       | `Migrated -> "Migrated"
564       | `IOError -> "IOError"
565       | `Watchdog -> "Watchdog"
566       | `Restored -> "Restored"
567       | `FromSnapshot -> "FromSnapshot"
568       | `APIError -> "APIError"
569       | `Unknown x -> Printf.sprintf "Unknown Suspended.detail: %d" x
570
571      let make = function
572       | 0 -> `Paused
573       | 1 -> `Migrated
574       | 2 -> `IOError
575       | 3 -> `Watchdog
576       | 4 -> `Restored
577       | 5 -> `FromSnapshot
578       | 6 -> `APIError
579       | x -> `Unknown x (* newer libvirt *)
580   end
581
582   module Resumed = struct
583     type t = [
584       | `Unpaused
585       | `Migrated
586       | `FromSnapshot
587       | `Unknown of int (* newer libvirt *)
588     ]
589
590     let to_string = function
591       | `Unpaused -> "Unpaused"
592       | `Migrated -> "Migrated"
593       | `FromSnapshot -> "FromSnapshot"
594       | `Unknown x -> Printf.sprintf "Unknown Resumed.detail: %d" x
595
596     let make = function
597       | 0 -> `Unpaused
598       | 1 -> `Migrated
599       | 2 -> `FromSnapshot
600       | x -> `Unknown x (* newer libvirt *)
601   end
602
603   module Stopped = struct
604     type t = [
605       | `Shutdown
606       | `Destroyed
607       | `Crashed
608       | `Migrated
609       | `Saved
610       | `Failed
611       | `FromSnapshot
612       | `Unknown of int
613     ]
614     let to_string = function
615       | `Shutdown -> "Shutdown"
616       | `Destroyed -> "Destroyed"
617       | `Crashed -> "Crashed"
618       | `Migrated -> "Migrated"
619       | `Saved -> "Saved"
620       | `Failed -> "Failed"
621       | `FromSnapshot -> "FromSnapshot"
622       | `Unknown x -> Printf.sprintf "Unknown Stopped.detail: %d" x
623
624     let make = function
625       | 0 -> `Shutdown
626       | 1 -> `Destroyed
627       | 2 -> `Crashed
628       | 3 -> `Migrated
629       | 4 -> `Saved
630       | 5 -> `Failed
631       | 6 -> `FromSnapshot
632       | x -> `Unknown x (* newer libvirt *)
633   end
634
635   module PM_suspended = struct
636     type t = [
637       | `Memory
638       | `Disk
639       | `Unknown of int (* newer libvirt *)
640     ]
641
642     let to_string = function
643       | `Memory -> "Memory"
644       | `Disk -> "Disk"
645       | `Unknown x -> Printf.sprintf "Unknown PM_suspended.detail: %d" x
646
647     let make = function
648       | 0 -> `Memory
649       | 1 -> `Disk
650       | x -> `Unknown x (* newer libvirt *)
651   end
652
653   let string_option x = match x with
654     | None -> "None"
655     | Some x' -> "Some " ^ x'
656
657   module Lifecycle = struct
658     type t = [
659       | `Defined of Defined.t
660       | `Undefined of Undefined.t
661       | `Started of Started.t
662       | `Suspended of Suspended.t
663       | `Resumed of Resumed.t
664       | `Stopped of Stopped.t
665       | `Shutdown (* no detail defined yet *)
666       | `PMSuspended of PM_suspended.t
667       | `Unknown of int (* newer libvirt *)
668     ]
669
670     let to_string = function
671       | `Defined x -> "Defined " ^ (Defined.to_string x)
672       | `Undefined x -> "Undefined " ^ (Undefined.to_string x)
673       | `Started x -> "Started " ^ (Started.to_string x)
674       | `Suspended x -> "Suspended " ^ (Suspended.to_string x)
675       | `Resumed x -> "Resumed " ^ (Resumed.to_string x)
676       | `Stopped x -> "Stopped " ^ (Stopped.to_string x)
677       | `Shutdown -> "Shutdown"
678       | `PMSuspended x -> "PMSuspended " ^ (PM_suspended.to_string x)
679       | `Unknown x -> Printf.sprintf "Unknown Lifecycle event: %d" x
680
681     let make (ty, detail) = match ty with
682       | 0 -> `Defined (Defined.make detail)
683       | 1 -> `Undefined (Undefined.make detail)
684       | 2 -> `Started (Started.make detail)
685       | 3 -> `Suspended (Suspended.make detail)
686       | 4 -> `Resumed (Resumed.make detail)
687       | 5 -> `Stopped (Stopped.make detail)
688       | 6 -> `Shutdown
689       | 7 -> `PMSuspended (PM_suspended.make detail)
690       | x -> `Unknown x
691   end
692
693   module Reboot = struct
694     type t = unit
695
696     let to_string _ = "()"
697
698     let make () = ()
699   end
700
701   module Rtc_change = struct
702     type t = int64
703
704     let to_string = Int64.to_string
705
706     let make x = x
707   end
708
709   module Watchdog = struct
710     type t = [
711       | `None
712       | `Pause
713       | `Reset
714       | `Poweroff
715       | `Shutdown
716       | `Debug
717       | `Unknown of int
718     ]
719
720     let to_string = function
721       | `None -> "None"
722       | `Pause -> "Pause"
723       | `Reset -> "Reset"
724       | `Poweroff -> "Poweroff"
725       | `Shutdown -> "Shutdown"
726       | `Debug -> "Debug"
727       | `Unknown x -> Printf.sprintf "Unknown watchdog_action: %d" x
728
729     let make = function
730       | 0 -> `None
731       | 1 -> `Pause
732       | 2 -> `Reset
733       | 3 -> `Poweroff
734       | 4 -> `Shutdown
735       | 5 -> `Debug
736       | x -> `Unknown x (* newer libvirt *)
737   end
738
739   module Io_error = struct
740     type action = [
741       | `None
742       | `Pause
743       | `Report
744       | `Unknown of int (* newer libvirt *)
745     ]
746
747     let string_of_action = function
748       | `None -> "None"
749       | `Pause -> "Pause"
750       | `Report -> "Report"
751       | `Unknown x -> Printf.sprintf "Unknown Io_error.action: %d" x
752
753     let action_of_int = function
754       | 0 -> `None
755       | 1 -> `Pause
756       | 2 -> `Report
757       | x -> `Unknown x
758
759     type t = {
760       src_path: string option;
761       dev_alias: string option;
762       action: action;
763       reason: string option;
764     }
765
766     let to_string t = Printf.sprintf
767         "{ Io_error.src_path = %s; dev_alias = %s; action = %s; reason = %s }"
768         (string_option t.src_path)
769         (string_option t.dev_alias)
770         (string_of_action t.action)
771         (string_option t.reason)
772
773     let make (src_path, dev_alias, action, reason) = {
774         src_path = src_path;
775         dev_alias = dev_alias;
776         action = action_of_int action;
777         reason = reason;
778     }
779
780     let make_noreason (src_path, dev_alias, action) =
781       make (src_path, dev_alias, action, None)
782   end
783
784   module Graphics_address = struct
785     type family = [
786       | `Ipv4
787       | `Ipv6
788       | `Unix
789       | `Unknown of int (* newer libvirt *)
790     ]
791
792     let string_of_family = function
793       | `Ipv4 -> "IPv4"
794       | `Ipv6 -> "IPv6"
795       | `Unix -> "UNIX"
796       | `Unknown x -> Printf.sprintf "Unknown Graphics_address.family: %d" x
797
798     let family_of_int = function
799       (* no zero *)
800       | 1 -> `Ipv4
801       | 2 -> `Ipv6
802       | 3 -> `Unix
803       | x -> `Unknown x
804
805     type t = {
806       family: family;         (** Address family *)
807       node: string option;    (** Address of node (eg IP address, or UNIX path *)
808       service: string option; (** Service name/number (eg TCP port, or NULL) *)
809     }
810
811     let to_string t = Printf.sprintf
812       "{ family = %s; node = %s; service = %s }"
813         (string_of_family t.family)
814         (string_option t.node)
815         (string_option t.service)
816
817     let make (family, node, service) = {
818       family = family_of_int family;
819       node = node;
820       service = service;
821     }
822   end
823
824   module Graphics_subject = struct
825     type identity = {
826       ty: string option;
827       name: string option;
828     }
829
830     let string_of_identity t = Printf.sprintf
831       "{ ty = %s; name = %s }"
832       (string_option t.ty)
833       (string_option t.name)
834
835     type t = identity list
836
837     let to_string ts =
838       "[ " ^ (String.concat "; " (List.map string_of_identity ts)) ^ " ]"
839
840     let make xs =
841       List.map (fun (ty, name) -> { ty = ty; name = name })
842         (Array.to_list xs)
843   end
844
845   module Graphics = struct
846     type phase = [
847       | `Connect
848       | `Initialize
849       | `Disconnect
850       | `Unknown of int (** newer libvirt *)
851     ]
852
853     let string_of_phase = function
854       | `Connect -> "Connect"
855       | `Initialize -> "Initialize"
856       | `Disconnect -> "Disconnect"
857       | `Unknown x -> Printf.sprintf "Unknown Graphics.phase: %d" x
858
859     let phase_of_int = function
860       | 0 -> `Connect
861       | 1 -> `Initialize
862       | 2 -> `Disconnect
863       | x -> `Unknown x
864
865     type t = {
866       phase: phase;                (** the phase of the connection *)
867       local: Graphics_address.t;   (** the local server address *)
868       remote: Graphics_address.t;  (** the remote client address *)
869       auth_scheme: string option;  (** the authentication scheme activated *)
870       subject: Graphics_subject.t; (** the authenticated subject (user) *)
871     }
872
873     let to_string t =
874       let phase = Printf.sprintf "phase = %s"
875         (string_of_phase t.phase) in
876       let local = Printf.sprintf "local = %s"
877         (Graphics_address.to_string t.local) in
878       let remote = Printf.sprintf "remote = %s"
879         (Graphics_address.to_string t.remote) in
880       let auth_scheme = Printf.sprintf "auth_scheme = %s"
881         (string_option t.auth_scheme) in
882       let subject = Printf.sprintf "subject = %s"
883         (Graphics_subject.to_string t.subject) in
884       "{ " ^ (String.concat "; " [ phase; local; remote; auth_scheme; subject ]) ^ " }"
885
886     let make (phase, local, remote, auth_scheme, subject) = {
887       phase = phase_of_int phase;
888       local = Graphics_address.make local;
889       remote = Graphics_address.make remote;
890       auth_scheme = auth_scheme;
891       subject = Graphics_subject.make subject;
892     }
893   end
894
895   module Control_error = struct
896     type t = unit
897
898     let to_string () = "()"
899
900     let make () = ()
901   end
902
903   module Block_job = struct
904     type ty = [
905       | `KnownUnknown (* explicitly named UNKNOWN in the spec *)
906       | `Pull
907       | `Copy
908       | `Commit
909       | `Unknown of int (* newer libvirt *)
910     ]
911
912     let string_of_ty = function
913       | `KnownUnknown -> "KnownUnknown"
914       | `Pull -> "Pull"
915       | `Copy -> "Copy"
916       | `Commit -> "Commit"
917       | `Unknown x -> Printf.sprintf "Unknown Block_job.ty: %d" x
918
919     let ty_of_int = function
920       | 0 -> `KnownUnknown
921       | 1 -> `Pull
922       | 2 -> `Copy
923       | 3 -> `Commit
924       | x -> `Unknown x (* newer libvirt *)
925
926     type status = [
927       | `Completed
928       | `Failed
929       | `Cancelled
930       | `Ready
931       | `Unknown of int
932     ]
933
934     let string_of_status = function
935       | `Completed -> "Completed"
936       | `Failed -> "Failed"
937       | `Cancelled -> "Cancelled"
938       | `Ready -> "Ready"
939       | `Unknown x -> Printf.sprintf "Unknown Block_job.status: %d" x
940
941     let status_of_int = function
942       | 0 -> `Completed
943       | 1 -> `Failed
944       | 2 -> `Cancelled
945       | 3 -> `Ready
946       | x -> `Unknown x
947
948     type t = {
949       disk: string option;
950       ty: ty;
951       status: status;
952     }
953
954     let to_string t = Printf.sprintf "{ disk = %s; ty = %s; status = %s }"
955       (string_option t.disk)
956       (string_of_ty t.ty)
957       (string_of_status t.status)
958
959     let make (disk, ty, status) = {
960       disk = disk;
961       ty = ty_of_int ty;
962       status = status_of_int ty;
963     }
964   end
965
966   module Disk_change = struct
967     type reason = [
968       | `MissingOnStart
969       | `Unknown of int
970     ]
971
972     let string_of_reason = function
973       | `MissingOnStart -> "MissingOnStart"
974       | `Unknown x -> Printf.sprintf "Unknown Disk_change.reason: %d" x
975
976     let reason_of_int = function
977       | 0 -> `MissingOnStart
978       | x -> `Unknown x
979
980     type t = {
981       old_src_path: string option;
982       new_src_path: string option;
983       dev_alias: string option;
984       reason: reason;
985     }
986
987     let to_string t =
988       let o = Printf.sprintf "old_src_path = %s" (string_option t.old_src_path) in
989       let n = Printf.sprintf "new_src_path = %s" (string_option t.new_src_path) in
990       let d = Printf.sprintf "dev_alias = %s" (string_option t.dev_alias) in
991       let r = string_of_reason t.reason in
992       "{ " ^ (String.concat "; " [ o; n; d; r ]) ^ " }"
993
994     let make (o, n, d, r) = {
995       old_src_path = o;
996       new_src_path = n;
997       dev_alias = d;
998       reason = reason_of_int r;
999     }
1000   end
1001
1002   module Tray_change = struct
1003     type reason = [
1004       | `Open
1005       | `Close
1006       | `Unknown of int
1007     ]
1008
1009     let string_of_reason = function
1010       | `Open -> "Open"
1011       | `Close -> "Close"
1012       | `Unknown x -> Printf.sprintf "Unknown Tray_change.reason: %d" x
1013
1014     let reason_of_int = function
1015       | 0 -> `Open
1016       | 1 -> `Close
1017       | x -> `Unknown x
1018
1019     type t = {
1020       dev_alias: string option;
1021       reason: reason;
1022     }
1023
1024     let to_string t = Printf.sprintf
1025       "{ dev_alias = %s; reason = %s }"
1026         (string_option t.dev_alias)
1027         (string_of_reason t.reason)
1028
1029     let make (dev_alias, reason) = {
1030       dev_alias = dev_alias;
1031       reason = reason_of_int reason;
1032     }
1033   end
1034
1035   module PM_wakeup = struct
1036     type reason = [
1037       | `Unknown of int
1038     ]
1039
1040     type t = reason
1041
1042     let to_string = function
1043       | `Unknown x -> Printf.sprintf "Unknown PM_wakeup.reason: %d" x
1044
1045     let make x = `Unknown x
1046   end
1047
1048   module PM_suspend = struct
1049     type reason = [
1050       | `Unknown of int
1051     ]
1052
1053     type t = reason
1054
1055     let to_string = function
1056       | `Unknown x -> Printf.sprintf "Unknown PM_suspend.reason: %d" x
1057
1058     let make x = `Unknown x
1059   end
1060
1061   module Balloon_change = struct
1062     type t = int64
1063
1064     let to_string = Int64.to_string
1065     let make x = x
1066   end
1067
1068   module PM_suspend_disk = struct
1069     type reason = [
1070       | `Unknown of int
1071     ]
1072
1073     type t = reason
1074
1075     let to_string = function
1076       | `Unknown x -> Printf.sprintf "Unknown PM_suspend_disk.reason: %d" x
1077
1078     let make x = `Unknown x
1079   end
1080
1081   type callback =
1082     | Lifecycle     of ([`R] Domain.t -> Lifecycle.t -> unit)
1083     | Reboot        of ([`R] Domain.t -> Reboot.t -> unit)
1084     | RtcChange     of ([`R] Domain.t -> Rtc_change.t -> unit)
1085     | Watchdog      of ([`R] Domain.t -> Watchdog.t -> unit)
1086     | IOError       of ([`R] Domain.t -> Io_error.t -> unit)
1087     | Graphics      of ([`R] Domain.t -> Graphics.t -> unit)
1088     | IOErrorReason of ([`R] Domain.t -> Io_error.t -> unit)
1089     | ControlError  of ([`R] Domain.t -> Control_error.t -> unit)
1090     | BlockJob      of ([`R] Domain.t -> Block_job.t -> unit)
1091     | DiskChange    of ([`R] Domain.t -> Disk_change.t -> unit)
1092     | TrayChange    of ([`R] Domain.t -> Tray_change.t -> unit)
1093     | PMWakeUp      of ([`R] Domain.t -> PM_wakeup.t -> unit)
1094     | PMSuspend     of ([`R] Domain.t -> PM_suspend.t -> unit)
1095     | BalloonChange of ([`R] Domain.t -> Balloon_change.t -> unit)
1096     | PMSuspendDisk of ([`R] Domain.t -> PM_suspend_disk.t -> unit)
1097
1098   type callback_id = int64
1099
1100   let fresh_callback_id =
1101     let next = ref 0L in
1102     fun () ->
1103       let result = !next in
1104       next := Int64.succ !next;
1105       result
1106
1107   let make_table value_name =
1108     let table = Hashtbl.create 16 in
1109     let callback callback_id generic x =
1110       if Hashtbl.mem table callback_id
1111       then Hashtbl.find table callback_id generic x in
1112     let _ = Callback.register value_name callback in
1113     table
1114
1115   let u_table = make_table "Libvirt.u_callback"
1116   let i_table = make_table "Libvirt.i_callback"
1117   let i64_table = make_table "Libvirt.i64_callback"
1118   let i_i_table = make_table "Libvirt.i_i_callback"
1119   let s_i_table = make_table "Libvirt.s_i_callback"
1120   let s_i_i_table = make_table "Libvirt.s_i_i_callback"
1121   let s_s_i_table = make_table "Libvirt.s_s_i_callback"
1122   let s_s_i_s_table = make_table "Libvirt.s_s_i_s_callback"
1123   let s_s_s_i_table = make_table "Libvirt.s_s_s_i_callback"
1124   let i_ga_ga_s_gs_table = make_table "Libvirt.i_ga_ga_s_gs_callback"
1125
1126   external register_default_impl : unit -> unit = "ocaml_libvirt_event_register_default_impl"
1127
1128   external run_default_impl : unit -> unit = "ocaml_libvirt_event_run_default_impl"
1129
1130   external register_any' : 'a Connect.t -> 'a Domain.t option -> callback -> callback_id -> int = "ocaml_libvirt_connect_domain_event_register_any"
1131
1132   external deregister_any' : 'a Connect.t -> int -> unit = "ocaml_libvirt_connect_domain_event_deregister_any"
1133
1134   let our_id_to_libvirt_id = Hashtbl.create 16
1135
1136   let register_any conn ?dom callback =
1137     let id = fresh_callback_id () in
1138     begin match callback with
1139     | Lifecycle f ->
1140         Hashtbl.add i_i_table id (fun dom x ->
1141             f dom (Lifecycle.make x)
1142         )
1143     | Reboot f ->
1144         Hashtbl.add u_table id (fun dom x ->
1145             f dom (Reboot.make x)
1146         )
1147     | RtcChange f ->
1148         Hashtbl.add i64_table id (fun dom x ->
1149             f dom (Rtc_change.make x)
1150         )
1151     | Watchdog f ->
1152         Hashtbl.add i_table id (fun dom x ->
1153             f dom (Watchdog.make x)
1154         ) 
1155     | IOError f ->
1156         Hashtbl.add s_s_i_table id (fun dom x ->
1157             f dom (Io_error.make_noreason x)
1158         )
1159     | Graphics f ->
1160         Hashtbl.add i_ga_ga_s_gs_table id (fun dom x ->
1161             f dom (Graphics.make x)
1162         )
1163     | IOErrorReason f ->
1164         Hashtbl.add s_s_i_s_table id (fun dom x ->
1165             f dom (Io_error.make x)
1166         )
1167     | ControlError f ->
1168         Hashtbl.add u_table id (fun dom x ->
1169             f dom (Control_error.make x)
1170         )
1171     | BlockJob f ->
1172         Hashtbl.add s_i_i_table id (fun dom x ->
1173             f dom (Block_job.make x)
1174         )
1175     | DiskChange f ->
1176         Hashtbl.add s_s_s_i_table id (fun dom x ->
1177             f dom (Disk_change.make x)
1178         )
1179     | TrayChange f ->
1180         Hashtbl.add s_i_table id (fun dom x ->
1181             f dom (Tray_change.make x)
1182         )
1183     | PMWakeUp f ->
1184         Hashtbl.add i_table id (fun dom x ->
1185             f dom (PM_wakeup.make x)
1186         )
1187     | PMSuspend f ->
1188         Hashtbl.add i_table id (fun dom x ->
1189             f dom (PM_suspend.make x)
1190         )
1191     | BalloonChange f ->
1192         Hashtbl.add i64_table id (fun dom x ->
1193             f dom (Balloon_change.make x)
1194         )
1195     | PMSuspendDisk f ->
1196         Hashtbl.add i_table id (fun dom x ->
1197             f dom (PM_suspend_disk.make x)
1198         )
1199     end;
1200     let libvirt_id = register_any' conn dom callback id in
1201     Hashtbl.replace our_id_to_libvirt_id id libvirt_id;
1202     id
1203
1204   let deregister_any conn id =
1205     if Hashtbl.mem our_id_to_libvirt_id id then begin
1206       let libvirt_id = Hashtbl.find our_id_to_libvirt_id id in
1207       deregister_any' conn libvirt_id
1208     end;
1209     Hashtbl.remove our_id_to_libvirt_id id;
1210     Hashtbl.remove u_table id;
1211     Hashtbl.remove i_table id;
1212     Hashtbl.remove i64_table id;
1213     Hashtbl.remove i_i_table id;
1214     Hashtbl.remove s_i_table id;
1215     Hashtbl.remove s_i_i_table id;
1216     Hashtbl.remove s_s_i_table id;
1217     Hashtbl.remove s_s_i_s_table id;
1218     Hashtbl.remove s_s_s_i_table id;
1219     Hashtbl.remove i_ga_ga_s_gs_table id
1220
1221   let timeout_table = Hashtbl.create 16
1222   let _ =
1223     let callback x =
1224       if Hashtbl.mem timeout_table x
1225       then Hashtbl.find timeout_table x () in
1226   Callback.register "Libvirt.timeout_callback" callback
1227
1228   type timer_id = int64
1229
1230   external add_timeout' : 'a Connect.t -> int -> int64 -> int = "ocaml_libvirt_event_add_timeout"
1231
1232   external remove_timeout' : 'a Connect.t -> int -> unit = "ocaml_libvirt_event_remove_timeout"
1233
1234   let our_id_to_timer_id = Hashtbl.create 16
1235   let add_timeout conn ms fn =
1236     let id = fresh_callback_id () in
1237     Hashtbl.add timeout_table id fn;
1238     let timer_id = add_timeout' conn ms id in
1239     Hashtbl.add our_id_to_timer_id id timer_id;
1240     id
1241
1242   let remove_timeout conn id =
1243     if Hashtbl.mem our_id_to_timer_id id then begin
1244       let timer_id = Hashtbl.find our_id_to_timer_id id in
1245       remove_timeout' conn timer_id
1246     end;
1247     Hashtbl.remove our_id_to_timer_id id;
1248     Hashtbl.remove timeout_table id
1249 end
1250
1251 module Network =
1252 struct
1253   type 'rw t
1254
1255   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
1256   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
1257   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
1258   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
1259   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
1260   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
1261   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
1262   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
1263   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
1264   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
1265   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
1266   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
1267   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
1268   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
1269   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
1270   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
1271
1272   external const : [>`R] t -> ro t = "%identity"
1273 end
1274
1275 module Pool =
1276 struct
1277   type 'rw t
1278   type pool_state = Inactive | Building | Running | Degraded
1279   type pool_build_flags = New | Repair | Resize
1280   type pool_delete_flags = Normal | Zeroed
1281   type pool_info = {
1282     state : pool_state;
1283     capacity : int64;
1284     allocation : int64;
1285     available : int64;
1286   }
1287
1288   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
1289   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
1290   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
1291   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
1292   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
1293   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
1294   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
1295   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
1296   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
1297   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
1298   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
1299   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
1300   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
1301   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
1302   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
1303   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
1304   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
1305   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
1306   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
1307   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
1308   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
1309   external const : [>`R] t -> ro t = "%identity"
1310 end
1311
1312 module Volume =
1313 struct
1314   type 'rw t
1315   type vol_type = File | Block
1316   type vol_delete_flags = Normal | Zeroed
1317   type vol_info = {
1318     typ : vol_type;
1319     capacity : int64;
1320     allocation : int64;
1321   }
1322
1323   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
1324   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
1325   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
1326   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
1327   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
1328   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
1329   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
1330   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
1331   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
1332   external create_xml : [>`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
1333   external delete : [>`W] t -> vol_delete_flags -> unit = "ocaml_libvirt_storage_vol_delete"
1334   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
1335   external const : [>`R] t -> ro t = "%identity"
1336 end
1337
1338 (* Initialization. *)
1339 external c_init : unit -> unit = "ocaml_libvirt_init"
1340 let () =
1341   Callback.register_exception
1342     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
1343   Callback.register_exception
1344     "ocaml_libvirt_not_supported" (Not_supported "");
1345   c_init ()