Add Domain.max_peek function to determine maximum size of
[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 type ('a, 'b) job_t
37
38 module Connect =
39 struct
40   type 'rw t
41
42   type node_info = {
43     model : string;
44     memory : int64;
45     cpus : int;
46     mhz : int;
47     nodes : int;
48     sockets : int;
49     cores : int;
50     threads : int;
51   }
52
53   external connect : ?name:string -> unit -> rw t = "ocaml_libvirt_connect_open"
54   external connect_readonly : ?name:string -> unit -> ro t = "ocaml_libvirt_connect_open_readonly"
55   external close : [>`R] t -> unit = "ocaml_libvirt_connect_close"
56   external get_type : [>`R] t -> string = "ocaml_libvirt_connect_get_type"
57   external get_version : [>`R] t -> int = "ocaml_libvirt_connect_get_version"
58   external get_hostname : [>`R] t -> string = "ocaml_libvirt_connect_get_hostname"
59   external get_uri : [>`R] t -> string = "ocaml_libvirt_connect_get_uri"
60   external get_max_vcpus : [>`R] t -> ?type_:string -> unit -> int = "ocaml_libvirt_connect_get_max_vcpus"
61   external list_domains : [>`R] t -> int -> int array = "ocaml_libvirt_connect_list_domains"
62   external num_of_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_domains"
63   external get_capabilities : [>`R] t -> xml = "ocaml_libvirt_connect_get_capabilities"
64   external num_of_defined_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_domains"
65   external list_defined_domains : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_domains"
66   external num_of_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_networks"
67   external list_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_networks"
68   external num_of_defined_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_networks"
69   external list_defined_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_networks"
70   external num_of_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_storage_pools"
71   external list_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_storage_pools"
72   external num_of_defined_pools : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_storage_pools"
73   external list_defined_pools : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_storage_pools"
74
75   external get_node_info : [>`R] t -> node_info = "ocaml_libvirt_connect_get_node_info"
76   external node_get_free_memory : [> `R] t -> int64 = "ocaml_libvirt_connect_node_get_free_memory"
77   external node_get_cells_free_memory : [> `R] t -> int -> int -> int64 array = "ocaml_libvirt_connect_node_get_cells_free_memory"
78
79   (* See VIR_NODEINFO_MAXCPUS macro defined in <libvirt.h>. *)
80   let maxcpus_of_node_info { nodes = nodes; sockets = sockets;
81                              cores = cores; threads = threads } =
82     nodes * sockets * cores * threads
83
84   (* See VIR_CPU_MAPLEN macro defined in <libvirt.h>. *)
85   let cpumaplen nr_cpus =
86     (nr_cpus + 7) / 8
87
88   (* See VIR_USE_CPU, VIR_UNUSE_CPU, VIR_CPU_USABLE macros defined in <libvirt.h>. *)
89   let use_cpu cpumap cpu =
90     cpumap.[cpu/8] <-
91       Char.chr (Char.code cpumap.[cpu/8] lor (1 lsl (cpu mod 8)))
92   let unuse_cpu cpumap cpu =
93     cpumap.[cpu/8] <-
94       Char.chr (Char.code cpumap.[cpu/8] land (lnot (1 lsl (cpu mod 8))))
95   let cpu_usable cpumaps maplen vcpu cpu =
96     Char.code cpumaps.[vcpu*maplen + cpu/8] land (1 lsl (cpu mod 8)) <> 0
97
98   external const : [>`R] t -> ro t = "%identity"
99 end
100
101 module Domain =
102 struct
103   type 'rw t
104
105   type state =
106     | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
107     | InfoShutdown | InfoShutoff | InfoCrashed
108
109   type info = {
110     state : state;
111     max_mem : int64;
112     memory : int64;
113     nr_virt_cpu : int;
114     cpu_time : int64;
115   }
116
117   type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
118
119   type vcpu_info = {
120     number : int;
121     vcpu_state : vcpu_state;
122     vcpu_time : int64;
123     cpu : int;
124   }
125
126   type sched_param = string * sched_param_value
127   and sched_param_value =
128     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
129     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
130     | SchedFieldFloat of float | SchedFieldBool of bool
131
132   type migrate_flag = Live
133
134   type memory_flag = Virtual
135
136   type block_stats = {
137     rd_req : int64;
138     rd_bytes : int64;
139     wr_req : int64;
140     wr_bytes : int64;
141     errs : int64;
142   }
143
144   type interface_stats = {
145     rx_bytes : int64;
146     rx_packets : int64;
147     rx_errs : int64;
148     rx_drop : int64;
149     tx_bytes : int64;
150     tx_packets : int64;
151     tx_errs : int64;
152     tx_drop : int64;
153   }
154
155   (* The maximum size for Domain.memory_peek and Domain.block_peek
156    * supported by libvirt.  This may change with different versions
157    * of libvirt in the future, hence it's a function.
158    *)
159   let max_peek _ = 65536
160
161   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
162   external create_linux_job : [>`W] Connect.t -> xml -> ([`Domain], rw) job_t = "ocaml_libvirt_domain_create_linux_job"
163   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
164   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
165   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
166   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
167   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
168   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
169   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
170   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
171   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
172   external save_job : [>`W] t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_save_job"
173   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
174   external restore_job : [>`W] Connect.t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_restore_job"
175   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
176   external core_dump_job : [>`W] t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_core_dump_job"
177   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
178   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
179   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
180   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
181   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
182   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
183   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
184   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
185   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
186   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
187   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
188   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
189   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
190   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
191   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
192   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
193   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
194   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
195   external create_job : [>`W] t -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_create_job"
196   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
197   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
198   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
199   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
200   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
201   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
202   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
203   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
204   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"
205   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
206   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
207   external block_peek : [>`R] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
208   external memory_peek : [>`R] t -> memory_flag list -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_memory_peek_bytecode" "ocaml_libvirt_domain_memory_peek_native"
209
210   external const : [>`R] t -> ro t = "%identity"
211 end
212
213 module Network =
214 struct
215   type 'rw t
216
217   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
218   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
219   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
220   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
221   external create_xml_job : [>`W] Connect.t -> xml -> ([`Network], rw) job_t = "ocaml_libvirt_network_create_xml_job"
222   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
223   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
224   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
225   external create_job : [>`W] t -> ([`Network_nocreate], rw) job_t = "ocaml_libvirt_network_create_job"
226   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
227   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
228   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
229   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
230   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
231   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
232   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
233   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
234   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
235
236   external const : [>`R] t -> ro t = "%identity"
237 end
238
239 module Pool =
240 struct
241   type 'rw t
242   type pool_state = Inactive | Building | Running | Degraded
243   type pool_build_flags = New | Repair | Resize
244   type pool_delete_flags = Normal | Zeroed
245   type pool_info = {
246     state : pool_state;
247     capacity : int64;
248     allocation : int64;
249     available : int64;
250   }
251
252   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
253   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
254   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
255   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
256   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
257   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
258   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
259   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
260   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
261   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
262   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
263   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
264   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
265   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
266   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
267   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
268   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
269   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
270   external set_autostart : [`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
271   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
272   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
273   external const : [>`R] t -> ro t = "%identity"
274 end
275
276 module Volume =
277 struct
278   type 'rw t
279   type vol_type = File | Block
280   type vol_delete_flags = Normal | Zeroed
281   type vol_info = {
282     typ : vol_type;
283     capacity : int64;
284     allocation : int64;
285   }
286
287   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
288   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
289   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
290   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
291   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
292   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
293   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
294   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
295   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
296   external create_xml : [`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
297   external delete : [`W] t -> unit = "ocaml_libvirt_storage_vol_delete"
298   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
299   external const : [>`R] t -> ro t = "%identity"
300 end
301
302 module Job =
303 struct
304   type ('jobclass, 'rw) t = ('jobclass, 'rw) job_t
305   type job_type = Bounded | Unbounded
306   type job_state = Running | Complete | Failed | Cancelled
307   type job_info = {
308     typ : job_type;
309     state : job_state;
310     running_time : int;
311     remaining_time : int;
312     percent_complete : int
313   }
314   external get_info : ('a,'b) t -> job_info = "ocaml_libvirt_job_get_info"
315   external get_domain : ([`Domain], 'a) t -> 'a Domain.t = "ocaml_libvirt_job_get_domain"
316   external get_network : ([`Network], 'a) t -> 'a Network.t = "ocaml_libvirt_job_get_network"
317   external cancel : ('a,'b) t -> unit = "ocaml_libvirt_job_cancel"
318   external free : ('a, [>`R]) t -> unit = "ocaml_libvirt_job_free"
319   external const : ('a, [>`R]) t -> ('a, ro) t = "%identity"
320 end
321
322 module Virterror =
323 struct
324   type code =
325     | VIR_ERR_OK
326     | VIR_ERR_INTERNAL_ERROR
327     | VIR_ERR_NO_MEMORY
328     | VIR_ERR_NO_SUPPORT
329     | VIR_ERR_UNKNOWN_HOST
330     | VIR_ERR_NO_CONNECT
331     | VIR_ERR_INVALID_CONN
332     | VIR_ERR_INVALID_DOMAIN
333     | VIR_ERR_INVALID_ARG
334     | VIR_ERR_OPERATION_FAILED
335     | VIR_ERR_GET_FAILED
336     | VIR_ERR_POST_FAILED
337     | VIR_ERR_HTTP_ERROR
338     | VIR_ERR_SEXPR_SERIAL
339     | VIR_ERR_NO_XEN
340     | VIR_ERR_XEN_CALL
341     | VIR_ERR_OS_TYPE
342     | VIR_ERR_NO_KERNEL
343     | VIR_ERR_NO_ROOT
344     | VIR_ERR_NO_SOURCE
345     | VIR_ERR_NO_TARGET
346     | VIR_ERR_NO_NAME
347     | VIR_ERR_NO_OS
348     | VIR_ERR_NO_DEVICE
349     | VIR_ERR_NO_XENSTORE
350     | VIR_ERR_DRIVER_FULL
351     | VIR_ERR_CALL_FAILED
352     | VIR_ERR_XML_ERROR
353     | VIR_ERR_DOM_EXIST
354     | VIR_ERR_OPERATION_DENIED
355     | VIR_ERR_OPEN_FAILED
356     | VIR_ERR_READ_FAILED
357     | VIR_ERR_PARSE_FAILED
358     | VIR_ERR_CONF_SYNTAX
359     | VIR_ERR_WRITE_FAILED
360     | VIR_ERR_XML_DETAIL
361     | VIR_ERR_INVALID_NETWORK
362     | VIR_ERR_NETWORK_EXIST
363     | VIR_ERR_SYSTEM_ERROR
364     | VIR_ERR_RPC
365     | VIR_ERR_GNUTLS_ERROR
366     | VIR_WAR_NO_NETWORK
367     | VIR_ERR_NO_DOMAIN
368     | VIR_ERR_NO_NETWORK
369     | VIR_ERR_INVALID_MAC
370     | VIR_ERR_AUTH_FAILED
371     | VIR_ERR_INVALID_STORAGE_POOL
372     | VIR_ERR_INVALID_STORAGE_VOL
373     | VIR_WAR_NO_STORAGE
374     | VIR_ERR_NO_STORAGE_POOL
375     | VIR_ERR_NO_STORAGE_VOL
376     | VIR_ERR_UNKNOWN of int
377
378   let string_of_code = function
379     | VIR_ERR_OK -> "VIR_ERR_OK"
380     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
381     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
382     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
383     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
384     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
385     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
386     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
387     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
388     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
389     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
390     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
391     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
392     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
393     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
394     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
395     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
396     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
397     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
398     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
399     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
400     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
401     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
402     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
403     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
404     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
405     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
406     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
407     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
408     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
409     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
410     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
411     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
412     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
413     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
414     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
415     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
416     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
417     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
418     | VIR_ERR_RPC -> "VIR_ERR_RPC"
419     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
420     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
421     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
422     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
423     | VIR_ERR_INVALID_MAC -> "VIR_ERR_INVALID_MAC"
424     | VIR_ERR_AUTH_FAILED -> "VIR_ERR_AUTH_FAILED"
425     | VIR_ERR_INVALID_STORAGE_POOL -> "VIR_ERR_INVALID_STORAGE_POOL"
426     | VIR_ERR_INVALID_STORAGE_VOL -> "VIR_ERR_INVALID_STORAGE_VOL"
427     | VIR_WAR_NO_STORAGE -> "VIR_WAR_NO_STORAGE"
428     | VIR_ERR_NO_STORAGE_POOL -> "VIR_ERR_NO_STORAGE_POOL"
429     | VIR_ERR_NO_STORAGE_VOL -> "VIR_ERR_NO_STORAGE_VOL"
430     | VIR_ERR_UNKNOWN i -> "VIR_ERR_" ^ string_of_int i
431
432   type domain =
433     | VIR_FROM_NONE
434     | VIR_FROM_XEN
435     | VIR_FROM_XEND
436     | VIR_FROM_XENSTORE
437     | VIR_FROM_SEXPR
438     | VIR_FROM_XML
439     | VIR_FROM_DOM
440     | VIR_FROM_RPC
441     | VIR_FROM_PROXY
442     | VIR_FROM_CONF
443     | VIR_FROM_QEMU
444     | VIR_FROM_NET
445     | VIR_FROM_TEST
446     | VIR_FROM_REMOTE
447     | VIR_FROM_OPENVZ
448     | VIR_FROM_XENXM
449     | VIR_FROM_STATS_LINUX
450     | VIR_FROM_STORAGE
451     | VIR_FROM_UNKNOWN of int
452
453   let string_of_domain = function
454     | VIR_FROM_NONE -> "VIR_FROM_NONE"
455     | VIR_FROM_XEN -> "VIR_FROM_XEN"
456     | VIR_FROM_XEND -> "VIR_FROM_XEND"
457     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
458     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
459     | VIR_FROM_XML -> "VIR_FROM_XML"
460     | VIR_FROM_DOM -> "VIR_FROM_DOM"
461     | VIR_FROM_RPC -> "VIR_FROM_RPC"
462     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
463     | VIR_FROM_CONF -> "VIR_FROM_CONF"
464     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
465     | VIR_FROM_NET -> "VIR_FROM_NET"
466     | VIR_FROM_TEST -> "VIR_FROM_TEST"
467     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
468     | VIR_FROM_OPENVZ -> "VIR_FROM_OPENVZ"
469     | VIR_FROM_XENXM -> "VIR_FROM_XENXM"
470     | VIR_FROM_STATS_LINUX -> "VIR_FROM_STATS_LINUX"
471     | VIR_FROM_STORAGE -> "VIR_FROM_STORAGE"
472     | VIR_FROM_UNKNOWN i -> "VIR_FROM_" ^ string_of_int i
473
474   type level =
475     | VIR_ERR_NONE
476     | VIR_ERR_WARNING
477     | VIR_ERR_ERROR
478     | VIR_ERR_UNKNOWN_LEVEL of int
479
480   let string_of_level = function
481     | VIR_ERR_NONE -> "VIR_ERR_NONE"
482     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
483     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
484     | VIR_ERR_UNKNOWN_LEVEL i -> "VIR_ERR_LEVEL_" ^ string_of_int i
485
486   type t = {
487     code : code;
488     domain : domain;
489     message : string option;
490     level : level;
491     str1 : string option;
492     str2 : string option;
493     str3 : string option;
494     int1 : int32;
495     int2 : int32;
496   }
497
498   let to_string { code = code; domain = domain; message = message } =
499     let buf = Buffer.create 128 in
500     Buffer.add_string buf "libvirt: ";
501     Buffer.add_string buf (string_of_code code);
502     Buffer.add_string buf ": ";
503     Buffer.add_string buf (string_of_domain domain);
504     Buffer.add_string buf ": ";
505     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
506     Buffer.contents buf
507
508   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
509   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
510   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
511   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
512
513   let no_error () =
514     { code = VIR_ERR_OK; domain = VIR_FROM_NONE;
515       message = None; level = VIR_ERR_NONE;
516       str1 = None; str2 = None; str3 = None;
517       int1 = 0_l; int2 = 0_l }
518 end
519
520 exception Virterror of Virterror.t
521 exception Not_supported of string
522
523 (* Initialization. *)
524 external c_init : unit -> unit = "ocaml_libvirt_init"
525 let () =
526   Callback.register_exception
527     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
528   Callback.register_exception
529     "ocaml_libvirt_not_supported" (Not_supported "");
530   c_init ()