Clarify the license again for Debian.
[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   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
156   external create_linux_job : [>`W] Connect.t -> xml -> ([`Domain], rw) job_t = "ocaml_libvirt_domain_create_linux_job"
157   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
158   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
159   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
160   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
161   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
162   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
163   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
164   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
165   external save : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_save"
166   external save_job : [>`W] t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_save_job"
167   external restore : [>`W] Connect.t -> filename -> unit = "ocaml_libvirt_domain_restore"
168   external restore_job : [>`W] Connect.t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_restore_job"
169   external core_dump : [>`W] t -> filename -> unit = "ocaml_libvirt_domain_core_dump"
170   external core_dump_job : [>`W] t -> filename -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_core_dump_job"
171   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
172   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
173   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
174   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
175   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
176   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
177   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
178   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
179   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
180   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
181   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
182   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
183   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
184   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
185   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
186   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
187   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
188   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
189   external create_job : [>`W] t -> ([`Domain_nocreate], rw) job_t = "ocaml_libvirt_domain_create_job"
190   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
191   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
192   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
193   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
194   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
195   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
196   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
197   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
198   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"
199   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
200   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
201   external block_peek : [>`R] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
202   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"
203
204   external const : [>`R] t -> ro t = "%identity"
205 end
206
207 module Network =
208 struct
209   type 'rw t
210
211   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
212   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
213   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
214   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
215   external create_xml_job : [>`W] Connect.t -> xml -> ([`Network], rw) job_t = "ocaml_libvirt_network_create_xml_job"
216   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
217   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
218   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
219   external create_job : [>`W] t -> ([`Network_nocreate], rw) job_t = "ocaml_libvirt_network_create_job"
220   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
221   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
222   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
223   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
224   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
225   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
226   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
227   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
228   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
229
230   external const : [>`R] t -> ro t = "%identity"
231 end
232
233 module Pool =
234 struct
235   type 'rw t
236   type pool_state = Inactive | Building | Running | Degraded
237   type pool_build_flags = New | Repair | Resize
238   type pool_delete_flags = Normal | Zeroed
239   type pool_info = {
240     state : pool_state;
241     capacity : int64;
242     allocation : int64;
243     available : int64;
244   }
245
246   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_name"
247   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid"
248   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_pool_lookup_by_uuid_string"
249   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_create_xml"
250   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_storage_pool_define_xml"
251   external build : [>`W] t -> pool_build_flags -> unit = "ocaml_libvirt_storage_pool_build"
252   external undefine : [>`W] t -> unit = "ocaml_libvirt_storage_pool_undefine"
253   external create : [>`W] t -> unit = "ocaml_libvirt_storage_pool_create"
254   external destroy : [>`W] t -> unit = "ocaml_libvirt_storage_pool_destroy"
255   external delete : [>`W] t -> unit = "ocaml_libvirt_storage_pool_delete"
256   external free : [>`R] t -> unit = "ocaml_libvirt_storage_pool_free"
257   external refresh : [`R] t -> unit = "ocaml_libvirt_storage_pool_refresh"
258   external get_name : [`R] t -> string = "ocaml_libvirt_storage_pool_get_name"
259   external get_uuid : [`R] t -> uuid = "ocaml_libvirt_storage_pool_get_uuid"
260   external get_uuid_string : [`R] t -> string = "ocaml_libvirt_storage_pool_get_uuid_string"
261   external get_info : [`R] t -> pool_info = "ocaml_libvirt_storage_pool_get_info"
262   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_pool_get_xml_desc"
263   external get_autostart : [`R] t -> bool = "ocaml_libvirt_storage_pool_get_autostart"
264   external set_autostart : [`W] t -> bool -> unit = "ocaml_libvirt_storage_pool_set_autostart"
265   external num_of_volumes : [`R] t -> int = "ocaml_libvirt_storage_pool_num_of_volumes"
266   external list_volumes : [`R] t -> int -> string array = "ocaml_libvirt_storage_pool_list_volumes"
267   external const : [>`R] t -> ro t = "%identity"
268 end
269
270 module Volume =
271 struct
272   type 'rw t
273   type vol_type = File | Block
274   type vol_delete_flags = Normal | Zeroed
275   type vol_info = {
276     typ : vol_type;
277     capacity : int64;
278     allocation : int64;
279   }
280
281   external lookup_by_name : 'a Pool.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_name"
282   external lookup_by_key : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_key"
283   external lookup_by_path : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_storage_vol_lookup_by_path"
284   external pool_of_volume : 'a t -> 'a Pool.t = "ocaml_libvirt_storage_pool_lookup_by_volume"
285   external get_name : [`R] t -> string = "ocaml_libvirt_storage_vol_get_name"
286   external get_key : [`R] t -> string = "ocaml_libvirt_storage_vol_get_key"
287   external get_path : [`R] t -> string = "ocaml_libvirt_storage_vol_get_path"
288   external get_info : [`R] t -> vol_info = "ocaml_libvirt_storage_vol_get_info"
289   external get_xml_desc : [`R] t -> xml = "ocaml_libvirt_storage_vol_get_xml_desc"
290   external create_xml : [`W] Pool.t -> xml -> unit = "ocaml_libvirt_storage_vol_create_xml"
291   external delete : [`W] t -> unit = "ocaml_libvirt_storage_vol_delete"
292   external free : [>`R] t -> unit = "ocaml_libvirt_storage_vol_free"
293   external const : [>`R] t -> ro t = "%identity"
294 end
295
296 module Job =
297 struct
298   type ('jobclass, 'rw) t = ('jobclass, 'rw) job_t
299   type job_type = Bounded | Unbounded
300   type job_state = Running | Complete | Failed | Cancelled
301   type job_info = {
302     typ : job_type;
303     state : job_state;
304     running_time : int;
305     remaining_time : int;
306     percent_complete : int
307   }
308   external get_info : ('a,'b) t -> job_info = "ocaml_libvirt_job_get_info"
309   external get_domain : ([`Domain], 'a) t -> 'a Domain.t = "ocaml_libvirt_job_get_domain"
310   external get_network : ([`Network], 'a) t -> 'a Network.t = "ocaml_libvirt_job_get_network"
311   external cancel : ('a,'b) t -> unit = "ocaml_libvirt_job_cancel"
312   external free : ('a, [>`R]) t -> unit = "ocaml_libvirt_job_free"
313   external const : ('a, [>`R]) t -> ('a, ro) t = "%identity"
314 end
315
316 module Virterror =
317 struct
318   type code =
319     | VIR_ERR_OK
320     | VIR_ERR_INTERNAL_ERROR
321     | VIR_ERR_NO_MEMORY
322     | VIR_ERR_NO_SUPPORT
323     | VIR_ERR_UNKNOWN_HOST
324     | VIR_ERR_NO_CONNECT
325     | VIR_ERR_INVALID_CONN
326     | VIR_ERR_INVALID_DOMAIN
327     | VIR_ERR_INVALID_ARG
328     | VIR_ERR_OPERATION_FAILED
329     | VIR_ERR_GET_FAILED
330     | VIR_ERR_POST_FAILED
331     | VIR_ERR_HTTP_ERROR
332     | VIR_ERR_SEXPR_SERIAL
333     | VIR_ERR_NO_XEN
334     | VIR_ERR_XEN_CALL
335     | VIR_ERR_OS_TYPE
336     | VIR_ERR_NO_KERNEL
337     | VIR_ERR_NO_ROOT
338     | VIR_ERR_NO_SOURCE
339     | VIR_ERR_NO_TARGET
340     | VIR_ERR_NO_NAME
341     | VIR_ERR_NO_OS
342     | VIR_ERR_NO_DEVICE
343     | VIR_ERR_NO_XENSTORE
344     | VIR_ERR_DRIVER_FULL
345     | VIR_ERR_CALL_FAILED
346     | VIR_ERR_XML_ERROR
347     | VIR_ERR_DOM_EXIST
348     | VIR_ERR_OPERATION_DENIED
349     | VIR_ERR_OPEN_FAILED
350     | VIR_ERR_READ_FAILED
351     | VIR_ERR_PARSE_FAILED
352     | VIR_ERR_CONF_SYNTAX
353     | VIR_ERR_WRITE_FAILED
354     | VIR_ERR_XML_DETAIL
355     | VIR_ERR_INVALID_NETWORK
356     | VIR_ERR_NETWORK_EXIST
357     | VIR_ERR_SYSTEM_ERROR
358     | VIR_ERR_RPC
359     | VIR_ERR_GNUTLS_ERROR
360     | VIR_WAR_NO_NETWORK
361     | VIR_ERR_NO_DOMAIN
362     | VIR_ERR_NO_NETWORK
363     | VIR_ERR_INVALID_MAC
364     | VIR_ERR_AUTH_FAILED
365     | VIR_ERR_INVALID_STORAGE_POOL
366     | VIR_ERR_INVALID_STORAGE_VOL
367     | VIR_WAR_NO_STORAGE
368     | VIR_ERR_NO_STORAGE_POOL
369     | VIR_ERR_NO_STORAGE_VOL
370     | VIR_ERR_UNKNOWN of int
371
372   let string_of_code = function
373     | VIR_ERR_OK -> "VIR_ERR_OK"
374     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
375     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
376     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
377     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
378     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
379     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
380     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
381     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
382     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
383     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
384     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
385     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
386     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
387     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
388     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
389     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
390     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
391     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
392     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
393     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
394     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
395     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
396     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
397     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
398     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
399     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
400     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
401     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
402     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
403     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
404     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
405     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
406     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
407     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
408     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
409     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
410     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
411     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
412     | VIR_ERR_RPC -> "VIR_ERR_RPC"
413     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
414     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
415     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
416     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
417     | VIR_ERR_INVALID_MAC -> "VIR_ERR_INVALID_MAC"
418     | VIR_ERR_AUTH_FAILED -> "VIR_ERR_AUTH_FAILED"
419     | VIR_ERR_INVALID_STORAGE_POOL -> "VIR_ERR_INVALID_STORAGE_POOL"
420     | VIR_ERR_INVALID_STORAGE_VOL -> "VIR_ERR_INVALID_STORAGE_VOL"
421     | VIR_WAR_NO_STORAGE -> "VIR_WAR_NO_STORAGE"
422     | VIR_ERR_NO_STORAGE_POOL -> "VIR_ERR_NO_STORAGE_POOL"
423     | VIR_ERR_NO_STORAGE_VOL -> "VIR_ERR_NO_STORAGE_VOL"
424     | VIR_ERR_UNKNOWN i -> "VIR_ERR_" ^ string_of_int i
425
426   type domain =
427     | VIR_FROM_NONE
428     | VIR_FROM_XEN
429     | VIR_FROM_XEND
430     | VIR_FROM_XENSTORE
431     | VIR_FROM_SEXPR
432     | VIR_FROM_XML
433     | VIR_FROM_DOM
434     | VIR_FROM_RPC
435     | VIR_FROM_PROXY
436     | VIR_FROM_CONF
437     | VIR_FROM_QEMU
438     | VIR_FROM_NET
439     | VIR_FROM_TEST
440     | VIR_FROM_REMOTE
441     | VIR_FROM_OPENVZ
442     | VIR_FROM_XENXM
443     | VIR_FROM_STATS_LINUX
444     | VIR_FROM_STORAGE
445     | VIR_FROM_UNKNOWN of int
446
447   let string_of_domain = function
448     | VIR_FROM_NONE -> "VIR_FROM_NONE"
449     | VIR_FROM_XEN -> "VIR_FROM_XEN"
450     | VIR_FROM_XEND -> "VIR_FROM_XEND"
451     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
452     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
453     | VIR_FROM_XML -> "VIR_FROM_XML"
454     | VIR_FROM_DOM -> "VIR_FROM_DOM"
455     | VIR_FROM_RPC -> "VIR_FROM_RPC"
456     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
457     | VIR_FROM_CONF -> "VIR_FROM_CONF"
458     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
459     | VIR_FROM_NET -> "VIR_FROM_NET"
460     | VIR_FROM_TEST -> "VIR_FROM_TEST"
461     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
462     | VIR_FROM_OPENVZ -> "VIR_FROM_OPENVZ"
463     | VIR_FROM_XENXM -> "VIR_FROM_XENXM"
464     | VIR_FROM_STATS_LINUX -> "VIR_FROM_STATS_LINUX"
465     | VIR_FROM_STORAGE -> "VIR_FROM_STORAGE"
466     | VIR_FROM_UNKNOWN i -> "VIR_FROM_" ^ string_of_int i
467
468   type level =
469     | VIR_ERR_NONE
470     | VIR_ERR_WARNING
471     | VIR_ERR_ERROR
472     | VIR_ERR_UNKNOWN_LEVEL of int
473
474   let string_of_level = function
475     | VIR_ERR_NONE -> "VIR_ERR_NONE"
476     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
477     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
478     | VIR_ERR_UNKNOWN_LEVEL i -> "VIR_ERR_LEVEL_" ^ string_of_int i
479
480   type t = {
481     code : code;
482     domain : domain;
483     message : string option;
484     level : level;
485     str1 : string option;
486     str2 : string option;
487     str3 : string option;
488     int1 : int32;
489     int2 : int32;
490   }
491
492   let to_string { code = code; domain = domain; message = message } =
493     let buf = Buffer.create 128 in
494     Buffer.add_string buf "libvirt: ";
495     Buffer.add_string buf (string_of_code code);
496     Buffer.add_string buf ": ";
497     Buffer.add_string buf (string_of_domain domain);
498     Buffer.add_string buf ": ";
499     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
500     Buffer.contents buf
501
502   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
503   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
504   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
505   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
506
507   let no_error () =
508     { code = VIR_ERR_OK; domain = VIR_FROM_NONE;
509       message = None; level = VIR_ERR_NONE;
510       str1 = None; str2 = None; str3 = None;
511       int1 = 0_l; int2 = 0_l }
512 end
513
514 exception Virterror of Virterror.t
515 exception Not_supported of string
516
517 (* Initialization. *)
518 external c_init : unit -> unit = "ocaml_libvirt_init"
519 let () =
520   Callback.register_exception
521     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
522   Callback.register_exception
523     "ocaml_libvirt_not_supported" (Not_supported "");
524   c_init ()