Remove jobs API.
[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 const : [>`R] t -> ro t = "%identity"
104 end
105
106 module Virterror =
107 struct
108   type code =
109     | VIR_ERR_OK
110     | VIR_ERR_INTERNAL_ERROR
111     | VIR_ERR_NO_MEMORY
112     | VIR_ERR_NO_SUPPORT
113     | VIR_ERR_UNKNOWN_HOST
114     | VIR_ERR_NO_CONNECT
115     | VIR_ERR_INVALID_CONN
116     | VIR_ERR_INVALID_DOMAIN
117     | VIR_ERR_INVALID_ARG
118     | VIR_ERR_OPERATION_FAILED
119     | VIR_ERR_GET_FAILED
120     | VIR_ERR_POST_FAILED
121     | VIR_ERR_HTTP_ERROR
122     | VIR_ERR_SEXPR_SERIAL
123     | VIR_ERR_NO_XEN
124     | VIR_ERR_XEN_CALL
125     | VIR_ERR_OS_TYPE
126     | VIR_ERR_NO_KERNEL
127     | VIR_ERR_NO_ROOT
128     | VIR_ERR_NO_SOURCE
129     | VIR_ERR_NO_TARGET
130     | VIR_ERR_NO_NAME
131     | VIR_ERR_NO_OS
132     | VIR_ERR_NO_DEVICE
133     | VIR_ERR_NO_XENSTORE
134     | VIR_ERR_DRIVER_FULL
135     | VIR_ERR_CALL_FAILED
136     | VIR_ERR_XML_ERROR
137     | VIR_ERR_DOM_EXIST
138     | VIR_ERR_OPERATION_DENIED
139     | VIR_ERR_OPEN_FAILED
140     | VIR_ERR_READ_FAILED
141     | VIR_ERR_PARSE_FAILED
142     | VIR_ERR_CONF_SYNTAX
143     | VIR_ERR_WRITE_FAILED
144     | VIR_ERR_XML_DETAIL
145     | VIR_ERR_INVALID_NETWORK
146     | VIR_ERR_NETWORK_EXIST
147     | VIR_ERR_SYSTEM_ERROR
148     | VIR_ERR_RPC
149     | VIR_ERR_GNUTLS_ERROR
150     | VIR_WAR_NO_NETWORK
151     | VIR_ERR_NO_DOMAIN
152     | VIR_ERR_NO_NETWORK
153     | VIR_ERR_INVALID_MAC
154     | VIR_ERR_AUTH_FAILED
155     | VIR_ERR_INVALID_STORAGE_POOL
156     | VIR_ERR_INVALID_STORAGE_VOL
157     | VIR_WAR_NO_STORAGE
158     | VIR_ERR_NO_STORAGE_POOL
159     | VIR_ERR_NO_STORAGE_VOL
160     | VIR_ERR_UNKNOWN of int
161
162   let string_of_code = function
163     | VIR_ERR_OK -> "VIR_ERR_OK"
164     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
165     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
166     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
167     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
168     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
169     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
170     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
171     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
172     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
173     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
174     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
175     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
176     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
177     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
178     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
179     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
180     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
181     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
182     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
183     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
184     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
185     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
186     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
187     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
188     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
189     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
190     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
191     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
192     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
193     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
194     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
195     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
196     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
197     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
198     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
199     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
200     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
201     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
202     | VIR_ERR_RPC -> "VIR_ERR_RPC"
203     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
204     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
205     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
206     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
207     | VIR_ERR_INVALID_MAC -> "VIR_ERR_INVALID_MAC"
208     | VIR_ERR_AUTH_FAILED -> "VIR_ERR_AUTH_FAILED"
209     | VIR_ERR_INVALID_STORAGE_POOL -> "VIR_ERR_INVALID_STORAGE_POOL"
210     | VIR_ERR_INVALID_STORAGE_VOL -> "VIR_ERR_INVALID_STORAGE_VOL"
211     | VIR_WAR_NO_STORAGE -> "VIR_WAR_NO_STORAGE"
212     | VIR_ERR_NO_STORAGE_POOL -> "VIR_ERR_NO_STORAGE_POOL"
213     | VIR_ERR_NO_STORAGE_VOL -> "VIR_ERR_NO_STORAGE_VOL"
214     | VIR_ERR_UNKNOWN i -> "VIR_ERR_" ^ string_of_int i
215
216   type domain =
217     | VIR_FROM_NONE
218     | VIR_FROM_XEN
219     | VIR_FROM_XEND
220     | VIR_FROM_XENSTORE
221     | VIR_FROM_SEXPR
222     | VIR_FROM_XML
223     | VIR_FROM_DOM
224     | VIR_FROM_RPC
225     | VIR_FROM_PROXY
226     | VIR_FROM_CONF
227     | VIR_FROM_QEMU
228     | VIR_FROM_NET
229     | VIR_FROM_TEST
230     | VIR_FROM_REMOTE
231     | VIR_FROM_OPENVZ
232     | VIR_FROM_XENXM
233     | VIR_FROM_STATS_LINUX
234     | VIR_FROM_STORAGE
235     | VIR_FROM_UNKNOWN of int
236
237   let string_of_domain = function
238     | VIR_FROM_NONE -> "VIR_FROM_NONE"
239     | VIR_FROM_XEN -> "VIR_FROM_XEN"
240     | VIR_FROM_XEND -> "VIR_FROM_XEND"
241     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
242     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
243     | VIR_FROM_XML -> "VIR_FROM_XML"
244     | VIR_FROM_DOM -> "VIR_FROM_DOM"
245     | VIR_FROM_RPC -> "VIR_FROM_RPC"
246     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
247     | VIR_FROM_CONF -> "VIR_FROM_CONF"
248     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
249     | VIR_FROM_NET -> "VIR_FROM_NET"
250     | VIR_FROM_TEST -> "VIR_FROM_TEST"
251     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
252     | VIR_FROM_OPENVZ -> "VIR_FROM_OPENVZ"
253     | VIR_FROM_XENXM -> "VIR_FROM_XENXM"
254     | VIR_FROM_STATS_LINUX -> "VIR_FROM_STATS_LINUX"
255     | VIR_FROM_STORAGE -> "VIR_FROM_STORAGE"
256     | VIR_FROM_UNKNOWN i -> "VIR_FROM_" ^ string_of_int i
257
258   type level =
259     | VIR_ERR_NONE
260     | VIR_ERR_WARNING
261     | VIR_ERR_ERROR
262     | VIR_ERR_UNKNOWN_LEVEL of int
263
264   let string_of_level = function
265     | VIR_ERR_NONE -> "VIR_ERR_NONE"
266     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
267     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
268     | VIR_ERR_UNKNOWN_LEVEL i -> "VIR_ERR_LEVEL_" ^ string_of_int i
269
270   type t = {
271     code : code;
272     domain : domain;
273     message : string option;
274     level : level;
275     str1 : string option;
276     str2 : string option;
277     str3 : string option;
278     int1 : int32;
279     int2 : int32;
280   }
281
282   let to_string { code = code; domain = domain; message = message } =
283     let buf = Buffer.create 128 in
284     Buffer.add_string buf "libvirt: ";
285     Buffer.add_string buf (string_of_code code);
286     Buffer.add_string buf ": ";
287     Buffer.add_string buf (string_of_domain domain);
288     Buffer.add_string buf ": ";
289     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
290     Buffer.contents buf
291
292   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
293   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
294   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
295   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
296
297   let no_error () =
298     { code = VIR_ERR_OK; domain = VIR_FROM_NONE;
299       message = None; level = VIR_ERR_NONE;
300       str1 = None; str2 = None; str3 = None;
301       int1 = 0_l; int2 = 0_l }
302 end
303
304 exception Virterror of Virterror.t
305 exception Not_supported of string
306
307 let rec map_ignore_errors f = function
308   | [] -> []
309   | x :: xs ->
310       try f x :: map_ignore_errors f xs
311       with Virterror _ -> map_ignore_errors f xs
312
313 module Domain =
314 struct
315   type 'rw t
316
317   type state =
318     | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
319     | InfoShutdown | InfoShutoff | InfoCrashed
320
321   type info = {
322     state : state;
323     max_mem : int64;
324     memory : int64;
325     nr_virt_cpu : int;
326     cpu_time : int64;
327   }
328
329   type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
330
331   type vcpu_info = {
332     number : int;
333     vcpu_state : vcpu_state;
334     vcpu_time : int64;
335     cpu : int;
336   }
337
338   type sched_param = string * sched_param_value
339   and sched_param_value =
340     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
341     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
342     | SchedFieldFloat of float | SchedFieldBool of bool
343
344   type migrate_flag = Live
345
346   type memory_flag = Virtual
347
348   type list_flag =
349     | ListActive
350     | ListInactive
351     | ListAll
352
353   type block_stats = {
354     rd_req : int64;
355     rd_bytes : int64;
356     wr_req : int64;
357     wr_bytes : int64;
358     errs : int64;
359   }
360
361   type interface_stats = {
362     rx_bytes : int64;
363     rx_packets : int64;
364     rx_errs : int64;
365     rx_drop : int64;
366     tx_bytes : int64;
367     tx_packets : int64;
368     tx_errs : int64;
369     tx_drop : int64;
370   }
371
372   (* The maximum size for Domain.memory_peek and Domain.block_peek
373    * supported by libvirt.  This may change with different versions
374    * of libvirt in the future, hence it's a function.
375    *)
376   let max_peek _ = 65536
377
378   external list_all_domains : 'a Connect.t -> ?want_info:bool -> list_flag list -> 'a t array * info array = "ocaml_libvirt_connect_list_all_domains"
379
380   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
381   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
382   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
383   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
384   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
385   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
386   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
387   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
388   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
389   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
390   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
391   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
392   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
393   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
394   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
395   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
396   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
397   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
398   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
399   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
400   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
401   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
402   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
403   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
404   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
405   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
406   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
407   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
408   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
409   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
410   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
411   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
412   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
413   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
414   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
415   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
416   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
417   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
418   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"
419   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
420   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
421   external block_peek : [>`W] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
422   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"
423
424   external const : [>`R] t -> ro t = "%identity"
425
426   (* First time we are called, we will check if
427    * virConnectListAllDomains is supported.
428    *)
429   let have_list_all_domains = ref None
430
431   let check_have_list_all_domains conn =
432     match !have_list_all_domains with
433     | Some v -> v
434     | None ->
435         (* Check if virConnectListAllDomains is supported
436          * by this version of libvirt.
437          *)
438         let v =
439           (* libvirt has a short-cut which makes this very quick ... *)
440           try ignore (list_all_domains conn []); true
441           with Not_supported "virConnectListAllDomains" -> false in
442         have_list_all_domains := Some v;
443         v
444
445   let get_domains conn flags =
446     let have_list_all_domains = check_have_list_all_domains conn in
447
448     if have_list_all_domains then (
449       (* Good, we can use the shiny new method. *)
450       let doms, _ = list_all_domains conn ~want_info:false flags in
451       Array.to_list doms
452     )
453     else (
454       (* Old/slow/inefficient method. *)
455       let get_active, get_inactive =
456         if List.mem ListAll flags then
457           (true, true)
458         else
459           (List.mem ListActive flags, List.mem ListInactive flags) in
460       let active_doms =
461         if get_active then (
462           let n = Connect.num_of_domains conn in
463           let ids = Connect.list_domains conn n in
464           let ids = Array.to_list ids in
465           map_ignore_errors (lookup_by_id conn) ids
466         ) else [] in
467
468       let inactive_doms =
469         if get_inactive then (
470           let n = Connect.num_of_defined_domains conn in
471           let names = Connect.list_defined_domains conn n in
472           let names = Array.to_list names in
473           map_ignore_errors (lookup_by_name conn) names
474         ) else [] in
475
476       active_doms @ inactive_doms
477     )
478
479   let get_domains_and_infos conn flags =
480     let have_list_all_domains = check_have_list_all_domains conn in
481
482     if have_list_all_domains then (
483       (* Good, we can use the shiny new method. *)
484       let doms, infos = list_all_domains conn ~want_info:true flags in
485       let doms = Array.to_list doms and infos = Array.to_list infos in
486       List.combine doms infos
487     )
488     else (
489       (* Old/slow/inefficient method. *)
490       let get_active, get_inactive =
491         if List.mem ListAll flags then
492           (true, true)
493         else (List.mem ListActive flags, List.mem ListInactive flags) in
494       let active_doms =
495         if get_active then (
496           let n = Connect.num_of_domains conn in
497           let ids = Connect.list_domains conn n in
498           let ids = Array.to_list ids in
499           map_ignore_errors (lookup_by_id conn) ids
500         ) else [] in
501
502       let inactive_doms =
503         if get_inactive then (
504           let n = Connect.num_of_defined_domains conn in
505           let names = Connect.list_defined_domains conn n in
506           let names = Array.to_list names in
507           map_ignore_errors (lookup_by_name conn) names
508         ) else [] in
509
510       let doms = active_doms @ inactive_doms in
511
512       map_ignore_errors (fun dom -> (dom, get_info dom)) doms
513     )
514 end
515
516 module Network =
517 struct
518   type 'rw t
519
520   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
521   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
522   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
523   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
524   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
525   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
526   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
527   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
528   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
529   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
530   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
531   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
532   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
533   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
534   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
535   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
536
537   external const : [>`R] t -> ro t = "%identity"
538 end
539
540 module Pool =
541 struct
542   type 'rw t
543   type pool_state = Inactive | Building | Running | Degraded
544   type pool_build_flags = New | Repair | Resize
545   type pool_delete_flags = Normal | Zeroed
546   type pool_info = {
547     state : pool_state;
548     capacity : int64;
549     allocation : int64;
550     available : int64;
551   }
552
553   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
554   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
555   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
556   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
557   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
558   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
559   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
560   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
561   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
562   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
563   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
564   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
565   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
566   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
567   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
568   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
569   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
570   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
571   external set_autostart : [`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
572   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
573   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
574   external const : [>`R] t -> ro t = "%identity"
575 end
576
577 module Volume =
578 struct
579   type 'rw t
580   type vol_type = File | Block
581   type vol_delete_flags = Normal | Zeroed
582   type vol_info = {
583     typ : vol_type;
584     capacity : int64;
585     allocation : int64;
586   }
587
588   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
589   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
590   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
591   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
592   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
593   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
594   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
595   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
596   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
597   external create_xml : [`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
598   external delete : [`W] t -> unit = "ocaml_libvirt_storage_vol_delete"
599   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
600   external const : [>`R] t -> ro t = "%identity"
601 end
602
603 (* Initialization. *)
604 external c_init : unit -> unit = "ocaml_libvirt_init"
605 let () =
606   Callback.register_exception
607     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
608   Callback.register_exception
609     "ocaml_libvirt_not_supported" (Not_supported "");
610   c_init ()