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