1 (* virsh-like command line tool.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
4 $Id: mlvirsh.ml,v 1.2 2007/08/21 13:24:09 rjones Exp $
10 module C = Libvirt.Connect
11 module D = Libvirt.Domain
12 module N = Libvirt.Network
15 let program_name = Filename.basename Sys.executable_name
17 (* Parse arguments. *)
19 let readonly = ref false
21 let argspec = Arg.align [
22 "-c", Arg.Set_string name, "URI Hypervisor connection URI";
23 "-r", Arg.Set readonly, " Read-only connection";
28 " ^ program_name ^ " [options] [command]
31 " ^ program_name ^ " help
33 Full description of a single command:
34 " ^ program_name ^ " help command
38 let add_extra_arg, get_extra_args =
39 let extra_args = ref [] in
40 let add_extra_arg s = extra_args := s :: !extra_args in
41 let get_extra_args () = List.rev !extra_args in
42 add_extra_arg, get_extra_args
44 let () = Arg.parse argspec add_extra_arg usage_msg
46 let name = match !name with "" -> None | name -> Some name
47 let readonly = !readonly
48 let extra_args = get_extra_args ()
50 (* Read a whole file into memory and return it (as a string). *)
51 let rec input_file filename =
52 let chan = open_in_bin filename in
53 let data = input_all chan in
57 let buf = Buffer.create 16384 in
58 let tmpsize = 16384 in
59 let tmp = String.create tmpsize in
61 while n := input chan tmp 0 tmpsize; !n > 0 do
62 Buffer.add_substring buf tmp 0 !n;
66 (* Hypervisor connection. *)
67 type conn_t = No_connection | RO of Libvirt.ro C.t | RW of Libvirt.rw C.t
68 let conn = ref No_connection
70 let close_connection () =
81 (* Command helper functions.
83 * Each cmd<n> is a function that constructs a command.
84 * string string string ... <--- user types on the command line
86 * arg1 arg2 arg3 ... <--- conversion functions
89 * function f <--- work function
92 * print result <--- printing function
94 * (Note that cmd<n> function constructs and returns the above
95 * function, it isn't the function itself.)
97 * Example: If the function takes one parameter (an int) and
98 * returns a string to be printed, you would use:
100 * cmd1 print_endline f int_of_string
102 let cmd0 print fn = function (* Command with no args. *)
103 | [] -> print (fn ())
104 | _ -> failwith "incorrect number of arguments for function"
106 let cmd1 print fn arg1 = function (* Command with one arg. *)
107 | [str1] -> print (fn (arg1 str1))
108 | _ -> failwith "incorrect number of arguments for function"
110 let cmd2 print fn arg1 arg2 = function (* Command with 2 args. *)
111 | [str1; str2] -> print (fn (arg1 str1) (arg2 str2))
112 | _ -> failwith "incorrect number of arguments for function"
114 let cmd3 print fn arg1 arg2 arg3 = function (* Command with 3 args. *)
115 | [str1; str2; str3] -> print (fn (arg1 str1) (arg2 str2) (arg3 str3))
116 | _ -> failwith "incorrect number of arguments for function"
118 let cmd01 print fn arg1 = function (* Command with 0 or 1 arg. *)
119 | [] -> print (fn None)
120 | [str1] -> print (fn (Some (arg1 str1)))
121 | _ -> failwith "incorrect number of arguments for function"
123 let cmd12 print fn arg1 arg2 = function (* Command with 1 or 2 args. *)
124 | [str1] -> print (fn (arg1 str1) None)
125 | [str1; str2] -> print (fn (arg1 str1) (Some (arg2 str2)))
126 | _ -> failwith "incorrect number of arguments for function"
128 let cmdN print fn = (* Command with any number of args. *)
129 fun args -> print (fn args)
132 (* Get the connection or fail if we don't have one. *)
133 let rec get_full_connection () =
135 | No_connection -> failwith "not connected to the hypervisor"
136 | RO _ -> failwith "tried to do read-write operation on read-only hypervisor connection"
138 and get_readonly_connection () =
140 | No_connection -> failwith "not connected to the hypervisor"
142 | RW conn -> C.const conn
144 and with_full_connection fn =
145 fun () -> fn (get_full_connection ())
147 and with_readonly_connection fn =
148 fun () -> fn (get_readonly_connection ())
149 and arg_full_connection fn =
150 fun str -> fn (get_full_connection ()) str
151 and arg_readonly_connection fn =
152 fun str -> fn (get_readonly_connection ()) str
155 (* Parsing of command arguments. *)
156 let string_of_readonly = function
157 | "readonly" | "read-only" | "ro" -> true
158 | _ -> failwith "flag should be 'readonly'"
160 let string_of_string (str : string) = str in
161 let boolean_of_string = function
162 | "enable" | "enabled" | "on" | "1" | "true" -> true
163 | "disable" | "disabled" | "off" | "0" | "false" -> false
164 | _ -> failwith "setting should be 'on' or 'off'"
166 let domain_of_string conn str =
169 let id = int_of_string str in
170 D.lookup_by_id conn id
172 Failure "int_of_string" ->
173 if String.length str = Libvirt.uuid_string_length then
174 D.lookup_by_uuid_string conn str
176 D.lookup_by_name conn str
179 Libvirt.Virterror err ->
180 failwith ("domain " ^ str ^ ": not found. Additional info: " ^
181 Libvirt.Virterror.to_string err);
183 let network_of_string conn str =
185 if String.length str = Libvirt.uuid_string_length then
186 N.lookup_by_uuid_string conn str
188 N.lookup_by_name conn str
190 Libvirt.Virterror err ->
191 failwith ("network " ^ str ^ ": not found. Additional info: " ^
192 Libvirt.Virterror.to_string err);
194 let rec parse_sched_params = function
196 | [_] -> failwith "expected field value pairs, but got an odd number of arguments"
197 | field :: value :: rest ->
198 (* XXX We only support the UINT type at the moment. *)
199 (field, D.SchedFieldUInt32 (Int32.of_string value))
200 :: parse_sched_params rest
202 let cpumap_of_string str =
203 let c = get_readonly_connection () in
204 let info = C.get_node_info c in
206 String.make (C.cpumaplen (C.maxcpus_of_node_info info)) '\000' in
207 List.iter (C.use_cpu cpumap)
208 (List.map int_of_string (String.nsplit str ","));
212 (* Printing of command results. *)
213 let no_return _ = () in
214 let print_int i = print_endline (string_of_int i) in
215 let print_int64 i = print_endline (Int64.to_string i) in
216 let print_bool b = print_endline (string_of_bool b) in
217 let print_version v =
218 let major = v / 1000000 in
219 let minor = (v - major * 1000000) / 1000 in
220 let release = (v - major * 1000000 - minor * 1000) in
221 printf "%d.%d.%d\n" major minor release
223 let string_of_domain_state = function
224 | D.InfoNoState -> "unknown"
225 | D.InfoRunning -> "running"
226 | D.InfoBlocked -> "blocked"
227 | D.InfoPaused -> "paused"
228 | D.InfoShutdown -> "shutdown"
229 | D.InfoShutoff -> "shutoff"
230 | D.InfoCrashed -> "crashed"
232 let string_of_vcpu_state = function
233 | D.VcpuOffline -> "offline"
234 | D.VcpuRunning -> "running"
235 | D.VcpuBlocked -> "blocked"
237 let print_domain_array doms =
241 try sprintf "%d" (D.get_id dom)
242 with Libvirt.Virterror _ -> "" in
244 try sprintf "%s" (D.get_name dom)
245 with Libvirt.Virterror _ -> "" in
248 let { D.state = state } = D.get_info dom in
249 string_of_domain_state state
250 with Libvirt.Virterror _ -> "" in
251 printf "%5s %-30s %s\n" id name state
254 let print_network_array nets =
257 printf "%s\n" (N.get_name net)
260 let print_node_info info =
261 printf "model: %s\n" info.C.model;
262 printf "memory: %Ld K\n" info.C.memory;
263 printf "cpus: %d\n" info.C.cpus;
264 printf "mhz: %d\n" info.C.mhz;
265 printf "nodes: %d\n" info.C.nodes;
266 printf "sockets: %d\n" info.C.sockets;
267 printf "cores: %d\n" info.C.cores;
268 printf "threads: %d\n" info.C.threads;
270 let print_domain_state { D.state = state } =
271 print_endline (string_of_domain_state state)
273 let print_domain_info info =
274 printf "state: %s\n" (string_of_domain_state info.D.state);
275 printf "max_mem: %Ld K\n" info.D.max_mem;
276 printf "memory: %Ld K\n" info.D.memory;
277 printf "nr_virt_cpu: %d\n" info.D.nr_virt_cpu;
278 printf "cpu_time: %Ld ns\n" info.D.cpu_time;
280 let print_sched_param_array params =
285 | D.SchedFieldInt32 i -> printf " %ld\n" i
286 | D.SchedFieldUInt32 i -> printf " %lu\n" i
287 | D.SchedFieldInt64 i -> printf " %Ld\n" i
288 | D.SchedFieldUInt64 i -> printf " %Lu\n" i
289 | D.SchedFieldFloat f -> printf " %g\n" f
290 | D.SchedFieldBool b -> printf " %b\n" b
293 let print_vcpu_info (ncpus, vcpu_infos, cpumaps, maplen, maxcpus) =
294 for n = 0 to ncpus-1 do
295 printf "virtual CPU: %d\n" n;
296 printf " on physical CPU: %d\n" vcpu_infos.(n).D.cpu;
297 printf " current state: %s\n"
298 (string_of_vcpu_state vcpu_infos.(n).D.vcpu_state);
299 printf " CPU time: %Ld ns\n" vcpu_infos.(n).D.vcpu_time;
300 printf " CPU affinity: ";
301 for m = 0 to maxcpus-1 do
302 print_char (if C.cpu_usable cpumaps maplen n m then 'y' else '-')
307 let print_block_stats { D.rd_req = rd_req; rd_bytes = rd_bytes;
308 wr_req = wr_req; wr_bytes = wr_bytes;
310 if rd_req >= 0L then printf "read requests: %Ld\n" rd_req;
311 if rd_bytes >= 0L then printf "read bytes: %Ld\n" rd_bytes;
312 if wr_req >= 0L then printf "write requests: %Ld\n" wr_req;
313 if wr_bytes >= 0L then printf "write bytes: %Ld\n" wr_bytes;
314 if errs >= 0L then printf "errors: %Ld\n" errs;
315 and print_interface_stats { D.rx_bytes = rx_bytes; rx_packets = rx_packets;
316 rx_errs = rx_errs; rx_drop = rx_drop;
317 tx_bytes = tx_bytes; tx_packets = tx_packets;
318 tx_errs = tx_errs; tx_drop = tx_drop } =
319 if rx_bytes >= 0L then printf "rx bytes: %Ld\n" rx_bytes;
320 if rx_packets >= 0L then printf "rx packets: %Ld\n" rx_packets;
321 if rx_errs >= 0L then printf "rx errs: %Ld\n" rx_errs;
322 if rx_drop >= 0L then printf "rx dropped: %Ld\n" rx_drop;
323 if tx_bytes >= 0L then printf "tx bytes: %Ld\n" tx_bytes;
324 if tx_packets >= 0L then printf "tx packets: %Ld\n" tx_packets;
325 if tx_errs >= 0L then printf "tx errs: %Ld\n" tx_errs;
326 if tx_drop >= 0L then printf "tx dropped: %Ld\n" tx_drop;
329 (* List of commands. *)
332 cmd2 no_return D.attach_device
333 (arg_full_connection domain_of_string) input_file,
334 "Attach device to domain.";
336 cmd2 no_return D.set_autostart
337 (arg_full_connection domain_of_string) boolean_of_string,
338 "Set whether a domain autostarts at boot.";
340 cmd0 print_endline (with_readonly_connection C.get_capabilities),
341 "Returns capabilities of hypervisor/driver.";
343 cmd0 no_return close_connection,
344 "Close an existing hypervisor connection.";
347 (fun name readonly ->
350 | None | Some false -> conn := RW (C.connect ~name ())
351 | Some true -> conn := RO (C.connect_readonly ~name ())
352 ) string_of_string string_of_readonly,
353 "Open a new hypervisor connection.";
356 (fun xml -> D.create_linux (get_full_connection ()) xml) input_file,
357 "Create a domain from an XML file.";
360 (fun xml -> D.define_xml (get_full_connection ()) xml) input_file,
361 "Define (but don't start) a domain from an XML file.";
363 cmd2 no_return D.detach_device
364 (arg_full_connection domain_of_string) input_file,
365 "Detach device from domain.";
367 cmd1 no_return D.destroy (arg_full_connection domain_of_string),
370 cmd2 print_block_stats D.block_stats
371 (arg_readonly_connection domain_of_string) string_of_string,
372 "Display the block device statistics for a domain.";
374 cmd1 print_int D.get_id (arg_readonly_connection domain_of_string),
375 "Print the ID of a domain.";
377 cmd2 print_interface_stats D.interface_stats
378 (arg_readonly_connection domain_of_string) string_of_string,
379 "Display the network interface statistics for a domain.";
381 cmd1 print_domain_info D.get_info
382 (arg_readonly_connection domain_of_string),
383 "Print the domain info.";
385 cmd1 print_int64 D.get_max_memory
386 (arg_readonly_connection domain_of_string),
387 "Print the max memory (in kilobytes) of a domain.";
389 cmd1 print_int D.get_max_vcpus
390 (arg_readonly_connection domain_of_string),
391 "Print the max VCPUs of a domain.";
393 cmd1 print_endline D.get_name
394 (arg_readonly_connection domain_of_string),
395 "Print the name of a domain.";
397 cmd1 print_endline D.get_os_type
398 (arg_readonly_connection domain_of_string),
399 "Print the OS type of a domain.";
401 cmd1 print_domain_state D.get_info
402 (arg_readonly_connection domain_of_string),
403 "Print the domain state.";
405 cmd1 print_endline D.get_uuid_string
406 (arg_readonly_connection domain_of_string),
407 "Print the UUID of a domain.";
409 cmd2 no_return D.core_dump
410 (arg_full_connection domain_of_string) string_of_string,
411 "Core dump a domain to a file for analysis.";
413 cmd1 print_endline D.get_xml_desc
414 (arg_full_connection domain_of_string),
415 "Print the XML description of a domain.";
417 cmd1 print_bool D.get_autostart
418 (arg_readonly_connection domain_of_string),
419 "Print whether a domain autostarts at boot.";
421 cmd0 print_endline (with_readonly_connection C.get_hostname),
422 "Print the hostname.";
424 cmd0 print_domain_array
426 let c = get_readonly_connection () in
427 let n = C.num_of_domains c in
428 let domids = C.list_domains c n in
429 Array.map (D.lookup_by_id c) domids),
430 "List the running domains.";
432 cmd0 print_domain_array
434 let c = get_readonly_connection () in
435 let n = C.num_of_defined_domains c in
436 let domnames = C.list_defined_domains c n in
437 Array.map (D.lookup_by_name c) domnames),
438 "List the defined but not running domains.";
440 cmd0 no_return (fun () -> exit 0),
441 "Quit the interactive terminal.";
443 cmd0 print_int (fun () -> C.get_max_vcpus (get_readonly_connection ()) ()),
444 "Print the max VCPUs available.";
446 cmd2 no_return N.set_autostart
447 (arg_full_connection network_of_string) boolean_of_string,
448 "Set whether a network autostarts at boot.";
450 cmd1 print_endline N.get_bridge_name
451 (arg_readonly_connection network_of_string),
452 "Print the bridge name of a network.";
455 (fun xml -> N.create_xml (get_full_connection ()) xml) input_file,
456 "Create a network from an XML file.";
459 (fun xml -> N.define_xml (get_full_connection ()) xml) input_file,
460 "Define (but don't start) a network from an XML file.";
462 cmd1 no_return N.destroy (arg_full_connection network_of_string),
463 "Destroy a network.";
465 cmd1 print_endline N.get_xml_desc
466 (arg_full_connection network_of_string),
467 "Print the XML description of a network.";
469 cmd1 print_bool N.get_autostart
470 (arg_full_connection network_of_string),
471 "Print whether a network autostarts at boot.";
473 cmd0 print_network_array
475 let c = get_readonly_connection () in
476 let n = C.num_of_networks c in
477 let nets = C.list_networks c n in
478 Array.map (N.lookup_by_name c) nets),
479 "List the active networks.";
481 cmd0 print_network_array
483 let c = get_readonly_connection () in
484 let n = C.num_of_defined_networks c in
485 let nets = C.list_defined_networks c n in
486 Array.map (N.lookup_by_name c) nets),
487 "List the defined but inactive networks.";
489 cmd1 print_endline N.get_name
490 (arg_readonly_connection network_of_string),
491 "Print the name of a network.";
493 cmd1 no_return N.create
494 (arg_full_connection network_of_string),
495 "Start a previously defined inactive network.";
497 cmd1 no_return N.undefine
498 (arg_full_connection network_of_string),
499 "Undefine an inactive network.";
501 cmd1 print_endline N.get_uuid_string
502 (arg_readonly_connection network_of_string),
503 "Print the UUID of a network.";
505 cmd0 print_node_info (with_readonly_connection C.get_node_info),
506 "Print node information.";
508 cmd1 no_return D.reboot (arg_full_connection domain_of_string),
512 fun path -> D.restore (get_full_connection ()) path
514 "Restore a domain from the named file.";
516 cmd1 no_return D.resume (arg_full_connection domain_of_string),
519 cmd2 no_return D.save
520 (arg_full_connection domain_of_string) string_of_string,
521 "Save a domain to a file.";
523 cmd1 print_sched_param_array (
525 let n = snd (D.get_scheduler_type dom) in
526 D.get_scheduler_parameters dom n
527 ) (arg_readonly_connection domain_of_string),
528 "Get the current scheduler parameters for a domain.";
532 | [] -> failwith "expecting domain followed by field value pairs"
534 let conn = get_full_connection () in
535 let dom = domain_of_string conn dom in
536 let params = parse_sched_params pairs in
537 let params = Array.of_list params in
538 D.set_scheduler_parameters dom params
540 "Set the scheduler parameters for a domain.";
543 (fun dom -> fst (D.get_scheduler_type dom))
544 (arg_readonly_connection domain_of_string),
545 "Get the scheduler type.";
547 cmd2 no_return D.set_memory
548 (arg_full_connection domain_of_string) Int64.of_string,
549 "Set the memory used by the domain (in kilobytes).";
551 cmd2 no_return D.set_max_memory
552 (arg_full_connection domain_of_string) Int64.of_string,
553 "Set the maximum memory used by the domain (in kilobytes).";
555 cmd1 no_return D.shutdown
556 (arg_full_connection domain_of_string),
557 "Gracefully shutdown a domain.";
559 cmd1 no_return D.create
560 (arg_full_connection domain_of_string),
561 "Start a previously defined inactive domain.";
563 cmd1 no_return D.suspend
564 (arg_full_connection domain_of_string),
567 cmd0 print_endline (with_readonly_connection C.get_type),
568 "Print the driver name";
570 cmd1 no_return D.undefine
571 (arg_full_connection domain_of_string),
572 "Undefine an inactive domain.";
574 cmd0 print_endline (with_readonly_connection C.get_uri),
575 "Print the canonical URI.";
577 cmd1 print_vcpu_info (
579 let c = get_readonly_connection () in
580 let info = C.get_node_info c in
581 let dominfo = D.get_info dom in
582 let maxcpus = C.maxcpus_of_node_info info in
583 let maplen = C.cpumaplen maxcpus in
584 let maxinfo = dominfo.D.nr_virt_cpu in
585 let ncpus, vcpu_infos, cpumaps = D.get_vcpus dom maxinfo maplen in
586 ncpus, vcpu_infos, cpumaps, maplen, maxcpus
587 ) (arg_readonly_connection domain_of_string),
588 "Pin domain VCPU to a list of physical CPUs.";
590 cmd3 no_return D.pin_vcpu
591 (arg_full_connection domain_of_string) int_of_string cpumap_of_string,
592 "Pin domain VCPU to a list of physical CPUs.";
594 cmd2 no_return D.set_vcpus
595 (arg_full_connection domain_of_string) int_of_string,
596 "Set the number of virtual CPUs assigned to a domain.";
598 cmd0 print_version (with_readonly_connection C.get_version),
599 "Print the driver version";
604 | None -> (* List of commands. *)
607 fun (cmd, _, description) ->
608 sprintf "%-12s %s" cmd description
611 "\n\nUse '" ^ program_name ^ " help command' for help on a command."
613 | Some command -> (* Full description of one command. *)
615 let (command, _, description) =
616 List.find (fun (c, _, _) -> c = command) commands in
617 sprintf "%s %s\n\n%s" program_name command description
620 failwith ("help: " ^ command ^ ": command not found");
625 cmd01 print_endline help string_of_string,
626 "Print list of commands or full description of one command.";
629 (* Execute a command. *)
630 let do_command command args =
632 let (_, cmd, _) = List.find (fun (c, _, _) -> c = command) commands in
636 failwith (command ^ ": command not found");
641 (* Interactive mode. *)
642 let rec interactive_mode () =
645 | No_connection -> "mlvirsh(no connection)$ "
646 | RO _ -> "mlvirsh(ro)$ "
647 | RW _ -> "mlvirsh# " in
649 let command = read_line () in
650 (match String.nsplit command " " with
653 do_command command args
655 Gc.full_major (); (* Free up all unreachable domain and network objects. *)
658 (* Connect to hypervisor. Allow the connection to fail. *)
662 if readonly then RO (C.connect_readonly ?name ())
663 else RW (C.connect ?name ())
665 Libvirt.Virterror err ->
666 eprintf "%s: %s\n" program_name (Libvirt.Virterror.to_string err);
671 (* Execute the command on the command line, if there was one.
672 * Otherwise go into interactive mode.
674 (match extra_args with
676 do_command command args
678 try interactive_mode () with End_of_file -> ()
681 (* If we are connected to a hypervisor, close the connection. *)
684 (* A good way to find heap bugs: *)
687 | Libvirt.Virterror err ->
688 eprintf "%s: %s\n" program_name (Libvirt.Virterror.to_string err)
690 eprintf "%s: %s\n" program_name msg