1 (* OCaml bindings for libvirt.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 external get_version : ?driver:string -> unit -> int * int = "ocaml_libvirt_get_version"
27 let uuid_string_length = 36
29 (* http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/80683af867cce6bf8fff273973f70c95.en.html *)
48 external connect : ?name:string -> unit -> rw t = "ocaml_libvirt_connect_open"
49 external connect_readonly : ?name:string -> unit -> ro t = "ocaml_libvirt_connect_open_readonly"
50 external close : [>`R] t -> unit = "ocaml_libvirt_connect_close"
51 external get_type : [>`R] t -> string = "ocaml_libvirt_connect_get_type"
52 external get_version : [>`R] t -> int = "ocaml_libvirt_connect_get_version"
53 external get_hostname : [>`R] t -> string = "ocaml_libvirt_connect_get_hostname"
54 external get_uri : [>`R] t -> string = "ocaml_libvirt_connect_get_uri"
55 external get_max_vcpus : [>`R] t -> ?type_:string -> unit -> int = "ocaml_libvirt_connect_get_max_vcpus"
56 external list_domains : [>`R] t -> int -> int array = "ocaml_libvirt_connect_list_domains"
57 external num_of_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_domains"
58 external get_capabilities : [>`R] t -> string = "ocaml_libvirt_connect_get_capabilities"
59 external num_of_defined_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_domains"
60 external list_defined_domains : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_domains"
61 external num_of_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_networks"
62 external list_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_networks"
63 external num_of_defined_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_networks"
64 external list_defined_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_networks"
65 external get_node_info : [>`R] t -> node_info = "ocaml_libvirt_connect_get_node_info"
67 (* See VIR_NODEINFO_MAXCPUS macro defined in <libvirt.h>. *)
68 let maxcpus_of_node_info { nodes = nodes; sockets = sockets;
69 cores = cores; threads = threads } =
70 nodes * sockets * cores * threads
72 (* See VIR_CPU_MAPLEN macro defined in <libvirt.h>. *)
73 let cpumaplen nr_cpus =
76 (* See VIR_USE_CPU, VIR_UNUSE_CPU, VIR_CPU_USABLE macros defined in <libvirt.h>. *)
77 let use_cpu cpumap cpu =
79 Char.chr (Char.code cpumap.[cpu/8] lor (1 lsl (cpu mod 8)))
80 let unuse_cpu cpumap cpu =
82 Char.chr (Char.code cpumap.[cpu/8] land (lnot (1 lsl (cpu mod 8))))
83 let cpu_usable cpumaps maplen vcpu cpu =
84 Char.code cpumaps.[vcpu*maplen + cpu/8] land (1 lsl (cpu mod 8)) <> 0
86 external const : [>`R] t -> ro t = "%identity"
92 type 'rw t = 'rw dom * 'rw Connect.t
95 | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
96 | InfoShutdown | InfoShutoff | InfoCrashed
106 type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
110 vcpu_state : vcpu_state;
115 type sched_param = string * sched_param_value
116 and sched_param_value =
117 | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
118 | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
119 | SchedFieldFloat of float | SchedFieldBool of bool
121 type migrate_flag = Live
131 type interface_stats = {
142 external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
143 external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
144 external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
145 external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
146 external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
147 external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
148 external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
149 external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
150 external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
151 external save : [>`W] t -> string -> unit = "ocaml_libvirt_domain_save"
152 external restore : [>`W] Connect.t -> string -> unit = "ocaml_libvirt_domain_restore"
153 external core_dump : [>`W] t -> string -> unit = "ocaml_libvirt_domain_core_dump"
154 external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
155 external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
156 external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
157 external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
158 external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
159 external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
160 external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
161 external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
162 external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
163 external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
164 external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
165 external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
166 external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
167 external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
168 external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
169 external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
170 external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
171 external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
172 external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
173 external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
174 external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
175 external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
176 external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
177 external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
178 external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
179 external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
180 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"
181 external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
182 external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
184 external const : [>`R] t -> ro t = "%identity"
190 type 'rw t = 'rw net * 'rw Connect.t
192 external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
193 external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
194 external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
195 external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
196 external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
197 external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
198 external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
199 external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
200 external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
201 external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
202 external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
203 external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
204 external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
205 external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
206 external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
207 external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
209 external const : [>`R] t -> ro t = "%identity"
216 | VIR_ERR_INTERNAL_ERROR
219 | VIR_ERR_UNKNOWN_HOST
221 | VIR_ERR_INVALID_CONN
222 | VIR_ERR_INVALID_DOMAIN
223 | VIR_ERR_INVALID_ARG
224 | VIR_ERR_OPERATION_FAILED
226 | VIR_ERR_POST_FAILED
228 | VIR_ERR_SEXPR_SERIAL
239 | VIR_ERR_NO_XENSTORE
240 | VIR_ERR_DRIVER_FULL
241 | VIR_ERR_CALL_FAILED
244 | VIR_ERR_OPERATION_DENIED
245 | VIR_ERR_OPEN_FAILED
246 | VIR_ERR_READ_FAILED
247 | VIR_ERR_PARSE_FAILED
248 | VIR_ERR_CONF_SYNTAX
249 | VIR_ERR_WRITE_FAILED
251 | VIR_ERR_INVALID_NETWORK
252 | VIR_ERR_NETWORK_EXIST
253 | VIR_ERR_SYSTEM_ERROR
255 | VIR_ERR_GNUTLS_ERROR
260 let string_of_code = function
261 | VIR_ERR_OK -> "VIR_ERR_OK"
262 | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
263 | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
264 | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
265 | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
266 | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
267 | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
268 | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
269 | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
270 | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
271 | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
272 | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
273 | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
274 | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
275 | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
276 | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
277 | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
278 | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
279 | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
280 | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
281 | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
282 | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
283 | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
284 | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
285 | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
286 | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
287 | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
288 | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
289 | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
290 | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
291 | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
292 | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
293 | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
294 | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
295 | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
296 | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
297 | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
298 | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
299 | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
300 | VIR_ERR_RPC -> "VIR_ERR_RPC"
301 | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
302 | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
303 | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
304 | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
311 let string_of_level = function
312 | VIR_ERR_NONE -> "VIR_ERR_NONE"
313 | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
314 | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
332 let string_of_domain = function
333 | VIR_FROM_NONE -> "VIR_FROM_NONE"
334 | VIR_FROM_XEN -> "VIR_FROM_XEN"
335 | VIR_FROM_XEND -> "VIR_FROM_XEND"
336 | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
337 | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
338 | VIR_FROM_XML -> "VIR_FROM_XML"
339 | VIR_FROM_DOM -> "VIR_FROM_DOM"
340 | VIR_FROM_RPC -> "VIR_FROM_RPC"
341 | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
342 | VIR_FROM_CONF -> "VIR_FROM_CONF"
343 | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
344 | VIR_FROM_NET -> "VIR_FROM_NET"
345 | VIR_FROM_TEST -> "VIR_FROM_TEST"
346 | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
351 message : string option;
353 conn : ro Connect.t option;
354 dom : ro Domain.t option;
355 str1 : string option;
356 str2 : string option;
357 str3 : string option;
360 net : ro Network.t option;
363 let to_string { code = code; domain = domain; message = message } =
364 let buf = Buffer.create 128 in
365 Buffer.add_string buf "libvirt: ";
366 Buffer.add_string buf (string_of_code code);
367 Buffer.add_string buf ": ";
368 Buffer.add_string buf (string_of_domain domain);
369 Buffer.add_string buf ": ";
370 (match message with Some msg -> Buffer.add_string buf msg | None -> ());
373 external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
374 external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
375 external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
376 external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
379 { code = VIR_ERR_OK; domain = VIR_FROM_NONE; message = None;
380 level = VIR_ERR_NONE; conn = None; dom = None;
381 str1 = None; str2 = None; str3 = None;
382 int1 = 0_l; int2 = 0_l; net = None }
385 exception Virterror of Virterror.t
387 (* Initialization. *)
388 external c_init : unit -> unit = "ocaml_libvirt_init"
390 Callback.register_exception
391 "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));