1 (* This is a virtual machine graphical viewer tool.
2 * Written by Richard W.M. Jones, Sept. 2009.
4 * It demonstrates some complex programming techniques: OCaml, Gtk+,
5 * threads, and use of both libguestfs and libvirt from threads.
7 * You will need the following installed in order to compile it:
8 * - ocaml (http://caml.inria.fr/)
9 * - ocamlfind (http://projects.camlcity.org/projects/findlib.html/)
10 * - extlib (http://code.google.com/p/ocaml-extlib/)
11 * - lablgtk2 (http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgtk.html
12 * - xml-light (http://tech.motion-twin.com/xmllight.html)
13 * - cduce and ocamlduce (http://cduce.org/)
14 * - ocaml-libvirt (http://libvirt.org/ocaml)
17 * Note that most/all of these are available as packages via Fedora,
18 * Debian, Ubuntu or GODI. You won't need to compile them from source.
20 * You will also need to configure libguestfs:
21 * ./configure --enable-ocaml-viewer
23 * All programs in the ocaml/examples subdirectory, including this
24 * one, may be freely copied without any restrictions.
27 (* Architecturally, there is one main thread which does all the Gtk
28 * calls, and one slave thread which executes all libguestfs and
29 * libvirt calls. The main thread sends commands to the slave thread,
30 * which are delivered in a queue and acted on in sequence. Responses
31 * are delivered back to the main thread as commands finish.
33 * The commands are just OCaml objects (type: Slave.command). The
34 * queue of commands is an OCaml Queue. The responses are sent by adding
35 * idle events to the glib main loop[1].
37 * If a command fails, it causes the input queue to be cleared. In
38 * this case, a failure response is sent to the main loop which
39 * causes the display to be reset and possibly an error message to
42 * The global variables [conn], [dom] and [g] are the libvirt
43 * connection, current domain, and libguestfs handle respectively.
44 * Because these can be accessed by both threads, they are
45 * protected from the main thread by access methods which
46 * (a) prevent the main thread from using them unlocked, and
47 * (b) prevent the main thread from doing arbitrary / long-running
48 * operations on them (the main thread must send a command instead).
50 * [1] http://library.gnome.org/devel/gtk-faq/stable/x499.html
56 let (//) = Filename.concat
58 (* Short names for commonly used modules. *)
59 module C = Libvirt.Connect
61 module D = Libvirt.Domain
66 let verbose = ref false (* Verbose mode. *)
69 let f str = if !verbose then ( prerr_string str; prerr_newline () ) in
72 (*----------------------------------------------------------------------*)
73 (* Slave thread. The signature describes what operations the main
74 * thread can perform, and protects the locked internals of the
78 type 'a callback = 'a -> unit
81 pt_name : string; (** device / LV name *)
82 pt_size : int64; (** in bytes *)
83 pt_content : string; (** the output of the 'file' command *)
84 pt_statvfs : G.statvfs option; (** None if not mountable *)
87 val no_callback : 'a callback
88 (** Use this as the callback if you don't want a callback. *)
90 val set_failure_callback : exn callback -> unit
91 (** Set the function that is called in the main thread whenever
92 there is a command failure in the slave. The command queue
93 is cleared before this is sent. [exn] is the exception
94 associated with the failure. *)
96 val set_busy_callback : [`Busy|`Idle] callback -> unit
97 (** Set the function that is called in the main thread whenever
98 the slave thread goes busy or idle. *)
100 val exit_thread : unit -> unit
101 (** [exit_thread ()] causes the slave thread to exit. *)
103 val connect : string option -> string option callback -> unit
104 (** [connect uri cb] connects to libvirt [uri], and calls [cb]
105 if it completes successfully. Any previous connection is
106 automatically cleaned up and disconnected. *)
108 val get_domains : string list callback -> unit
109 (** [get_domains cb] gets the list of active domains from libvirt,
110 and calls [cb domains] with the names of those domains. *)
112 val open_domain : string -> partinfo list callback -> unit
113 (** [open_domain dom cb] sets the domain [dom] as the current
114 domain, and launches a libguestfs handle for it. Any previously
115 current domain and libguestfs handle is closed. Once the
116 libguestfs handle is opened (which usually takes some time),
117 callback [cb] is called with the list of partitions found
120 val slave_loop : unit -> unit
121 (** The slave thread's main loop, running in the slave thread. *)
128 pt_statvfs : G.statvfs option;
131 (* Commands sent by the main thread to the slave thread. When
132 * [cmd] is successfully completed, [callback] will be delivered
133 * (in the main thread). If [cmd] fails, then the global error
134 * callback will be delivered in the main thread.
138 | Connect of string option * string option callback
139 | Get_domains of string list callback
140 | Open_domain of string * partinfo list callback
141 and 'a callback = 'a -> unit
143 let string_of_command = function
144 | Exit_thread -> "Exit_thread"
145 | Connect (None, _) -> "Connect [no uri]"
146 | Connect (Some uri, _) -> "Connect " ^ uri
147 | Get_domains _ -> "Get_domains"
148 | Open_domain (name, _) -> "Open_domain " ^ name
150 let no_callback _ = ()
152 let failure_cb = ref (fun _ -> ())
153 let set_failure_callback cb = failure_cb := cb
155 let busy_cb = ref (fun _ -> ())
156 let set_busy_callback cb = busy_cb := cb
158 (* Execute a function, while holding a mutex. If the function
159 * fails, ensure we release the mutex before rethrowing the
162 type ('a, 'b) choice = Either of 'a | Or of 'b
165 let r = try Either (f ()) with exn -> Or exn in
169 | Or exn -> raise exn
171 let q = Q.create () (* queue of commands *)
172 let q_lock = M.create ()
173 let q_cond = Cd.create ()
175 (* Send a command message to the slave thread. *)
176 let send_to_slave c =
177 debug "sending to slave: %s" (string_of_command c);
185 with_lock q_lock (fun () -> Q.clear q);
186 send_to_slave Exit_thread
189 send_to_slave (Connect (uri, cb))
192 send_to_slave (Get_domains cb)
194 let open_domain dom cb =
195 send_to_slave (Open_domain (dom, cb))
197 (* These are not protected by a mutex because we don't allow
198 * any references to these objects to escape from the slave
201 let conn = ref None (* libvirt connection *)
202 let dom = ref None (* libvirt domain *)
203 let g = ref None (* libguestfs handle *)
207 let rec slave_loop () =
208 debug "Slave.slave_loop: waiting for a command";
212 while Q.is_empty q do
213 Cd.wait q_cond q_lock
219 debug "Slave.slave_loop: executing: %s" (string_of_command c);
223 debug "Slave.slave_loop: command succeeded";
225 (* If an exception is thrown, it means the command failed. In
226 * this case we clear the command queue and deliver the failure
227 * callback in the main thread.
229 debug "Slave.slave_loop: command failed";
232 with_lock q_lock (fun () -> Q.clear q);
233 GtkThread.async !failure_cb exn
236 if !quit then Thread.exit ();
239 and exec_command = function
241 quit := true; (* quit first in case disconnect_all throws an exn *)
244 | Connect (name, cb) ->
246 conn := Some (C.connect_readonly ?name ());
250 let conn = Option.get !conn in
251 let doms = D.get_domains conn [D.ListAll] in
252 (* Only return the names, so that the libvirt objects
253 * aren't leaked outside the slave thread.
255 let doms = List.map D.get_name doms in
258 | Open_domain (domname, cb) ->
259 let conn = Option.get !conn in
261 dom := Some (D.lookup_by_name conn domname);
262 let dom = Option.get !dom in
264 (* Get the devices. *)
265 let xml = D.get_xml_desc dom in
266 let devs = get_devices_from_xml xml in
268 (* Create the libguestfs handle and launch it. *)
269 let g' = G.create () in
270 List.iter (G.add_drive_ro g') devs;
274 (* Get the list of partitions. *)
275 let parts = Array.to_list (G.list_partitions g') in
276 (* Remove any which are PVs. *)
277 let pvs = Array.to_list (G.pvs g') in
278 let parts = List.filter (fun part -> not (List.mem part pvs)) parts in
279 let lvs = Array.to_list (G.lvs g') in
280 let parts = parts @ lvs in
282 let parts = List.map (
284 (* Find out the size of each partition. *)
285 let size = G.blockdev_getsize64 g' part in
287 (* Find out what's on each partition. *)
288 let content = G.file g' part in
290 (* Try to mount it. *)
293 G.mount_ro g' part "/";
294 Some (G.statvfs g' "/")
298 { pt_name = part; pt_size = size; pt_content = content;
299 pt_statvfs = statvfs }
302 (* Call the callback. *)
305 (* Close all libvirt/libguestfs handles. *)
306 and disconnect_all () =
308 (match !conn with Some conn -> C.close conn | None -> ());
311 (* Close dom and libguestfs handles. *)
312 and disconnect_dom () =
313 (match !g with Some g -> G.close g | None -> ());
315 (match !dom with Some dom -> D.free dom | None -> ());
318 and get_devices_from_xml xml =
319 (* Lengthy discussion of the merits or otherwise of this code here:
320 * http://groups.google.com/group/fa.caml/browse_thread/thread/48e05d49b0f21b8a/5296bceb31ebfff3
322 let xml = Xmllight_loader.from_string xml in
323 let xs = {{ [xml] }} in
324 let xs = {{ (((xs.(<domain..>_)) / .(<devices..>_)) / .(<disk..>_)) / }} in
325 let xs = {{ map xs with
326 | <source dev=(Latin1 & s) ..>_
327 | <source file=(Latin1 & s) ..>_ -> [s]
331 (* End of slave thread code. *)
332 (*----------------------------------------------------------------------*)
335 type display_state = {
336 window : GWindow.window;
337 vmlist_set : string list -> unit;
338 throbber_set : [`Busy|`Idle] -> unit;
339 da : GMisc.drawing_area;
340 draw : GDraw.drawable;
341 drawing_area_repaint : unit -> unit;
342 set_statusbar : string -> unit;
343 clear_statusbar : unit -> unit;
344 pango_large_context : GPango.context_rw;
345 pango_small_context : GPango.context_rw;
348 (* This is called in the main thread whenever a command fails in the
349 * slave thread. The command queue has been cleared before this is
350 * called, so our job here is to reset the main window, and if
351 * necessary to turn the exception into an error message.
354 let title = "Error" in
355 let msg = Printexc.to_string exn in
356 debug "failure callback: %s" msg;
357 let icon = GMisc.image () in
358 icon#set_stock `DIALOG_ERROR;
359 icon#set_icon_size `DIALOG;
360 GToolbox.message_box ~title ~icon msg
362 (* This is called in the main thread when the slave thread transitions
365 let busy ds state = ds.throbber_set state
367 (* Main window and callbacks from menu etc. *)
368 let main_window opened_domain repaint =
369 let window_title = "Virtual machine graphical viewer" in
370 let window = GWindow.window ~width:800 ~height:600 ~title:window_title () in
371 let vbox = GPack.vbox ~packing:window#add () in
374 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
375 let factory = new GMenu.factory menubar in
376 let accel_group = factory#accel_group in
377 let connect_menu = factory#add_submenu "_Connect" in
379 let factory = new GMenu.factory connect_menu ~accel_group in
380 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
383 let quit _ = GMain.quit (); false in
384 ignore (window#connect#destroy ~callback:GMain.quit);
385 ignore (window#event#connect#delete ~callback:quit);
386 ignore (quit_item#connect#activate
387 ~callback:(fun () -> ignore (quit ()); ()));
389 (* Top status area. *)
390 let hbox = GPack.hbox ~border_width:4 ~packing:vbox#pack () in
391 ignore (GMisc.label ~text:"Guest: " ~packing:hbox#pack ());
394 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
395 let vmlist_set names =
396 let combo, (model, column) = vmcombo in
400 let row = model#append () in
401 model#set ~row ~column name
405 (* Throbber, http://faq.pygtk.org/index.py?req=show&file=faq23.037.htp *)
406 let static = Throbber.static () in
407 (*let animation = Throbber.animation () in*)
409 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
410 let throbber_set = function
411 | `Busy -> (*throbber#set_pixbuf animation*)
412 (* Workaround because no binding for GdkPixbufAnimation: *)
413 let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
414 throbber#set_file file
415 | `Idle -> throbber#set_pixbuf static
419 let da = GMisc.drawing_area ~packing:(vbox#pack ~expand:true ~fill:true) () in
421 let draw = new GDraw.drawable da#misc#window in
422 window#set_geometry_hints ~min_size:(80,80) (da :> GObj.widget);
424 (* Calling this can be used to force a redraw of the drawing area. *)
425 let drawing_area_repaint () = GtkBase.Widget.queue_draw da#as_widget in
427 (* Pango contexts used to draw large and small text. *)
428 let pango_large_context = da#misc#create_pango_context in
429 pango_large_context#set_font_description (Pango.Font.from_string "Sans 12");
430 let pango_small_context = da#misc#create_pango_context in
431 pango_small_context#set_font_description (Pango.Font.from_string "Sans 8");
433 (* Status bar at the bottom of the screen. *)
435 let statusbar = GMisc.statusbar ~packing:vbox#pack () in
436 let context = statusbar#new_context ~name:"Standard" in
437 ignore (context#push window_title);
440 ignore (context#push msg)
442 let clear_statusbar () = set_statusbar "" in
444 (* Display the window and enter Gtk+ main loop. *)
446 window#add_accel_group accel_group;
448 (* display_state which is threaded through all the other callbacks,
449 * allowing callbacks to update the window.
452 { window = window; vmlist_set = vmlist_set; throbber_set = throbber_set;
453 da = da; draw = draw; drawing_area_repaint = drawing_area_repaint;
454 set_statusbar = set_statusbar; clear_statusbar = clear_statusbar;
455 pango_large_context = pango_large_context;
456 pango_small_context = pango_small_context; } in
458 (* Set up some callbacks which require access to the display_state. *)
460 let combo, (model, column) = vmcombo in
461 combo#connect#changed
464 match combo#active_iter with
467 let name = model#get ~row ~column in
468 ds.set_statusbar (sprintf "Opening %s ..." name);
469 Slave.open_domain name (opened_domain ds))
472 ignore (da#event#connect#expose ~callback:(repaint ds));
476 (* Partition info for the current domain, if one is loaded. *)
479 (* This is called in the main thread when we've connected to libvirt. *)
480 let rec connected ds uri =
481 debug "connected callback";
484 | None -> "Connected to libvirt"
485 | Some uri -> sprintf "Connected to %s" uri in
486 ds.set_statusbar msg;
487 Slave.get_domains (got_domains ds)
489 (* This is called in the main thread when we've got the list of domains. *)
490 and got_domains ds doms =
491 debug "got_domains callback: (%s)" (String.concat " " doms);
494 (* This is called when we have opened a domain. *)
495 and opened_domain ds parts' =
496 debug "opened_domain callback";
497 ds.clear_statusbar ();
498 parts := Some parts';
499 ds.drawing_area_repaint ()
505 real_repaint ds parts
509 and real_repaint ds parts =
510 let width, height = ds.draw#size in
511 ds.draw#set_background `WHITE;
512 ds.draw#set_foreground `WHITE;
513 ds.draw#rectangle ~x:0 ~y:0 ~width ~height ~filled:true ();
515 let sum = List.fold_left Int64.add 0L in
516 let totsize = sum (List.map (fun { Slave.pt_size = size } -> size) parts) in
518 let scale = (float height -. 16.) /. Int64.to_float totsize in
520 (* Calculate the height in pixels of each partition, if we were to
521 * display it at a true relative size.
525 fun ({ Slave.pt_size = size } as part) ->
526 let h = scale *. Int64.to_float size in
532 eprintf "real_repaint: before borrowing:\n";
535 eprintf "%s\t%g pix\n" part.Slave.pt_name h
540 (* Now adjust the heights of small partitions so they "borrow" some
541 * height from the larger partitions.
544 let rec borrow needed = function
546 | (h, part) :: parts ->
547 let spare = h -. min_h in
548 if spare >= needed then (
549 needed, (h -. needed, part) :: parts
550 ) else if spare > 0. then (
551 let needed = needed -. spare in
552 let spare', parts = borrow needed parts in
553 spare +. spare', (h -. spare, part) :: parts
555 let spare', parts = borrow needed parts in
556 spare', (h, part) :: parts
559 let rec loop = function
560 | parts, [] -> List.rev parts
561 | prev, ((h, part) :: parts) ->
562 let needed = min_h -. h in
564 if needed > 0. then (
565 (* Find some spare height in a succeeding partition(s). *)
566 let spare, parts = borrow needed parts in
567 (* Or if not, in a preceeding partition(s). *)
569 if spare = 0. then borrow needed prev else spare, prev in
570 h +. spare, prev, parts
574 loop (((h, part) :: prev), parts)
576 let parts = loop ([], parts) in
580 eprintf "real_repaint: after borrowing:\n";
583 eprintf "%s\t%g pix\n" part.Slave.pt_name h
588 (* Calculate the proportion space used in each partition. *)
589 let parts = List.map (
592 match part.Slave.pt_statvfs with
594 | Some { G.bavail = bavail; blocks = blocks } ->
595 let num = Int64.to_float (Int64.sub blocks bavail) in
596 let denom = Int64.to_float blocks in
604 fun y (h, used, part) ->
605 (* This partition occupies pixels 8+y .. 8+y+h-1 *)
606 let yb = 8 + int_of_float y
607 and yt = 8 + int_of_float (y +. h) in
609 ds.draw#set_foreground `WHITE;
610 ds.draw#rectangle ~x:8 ~y:yb ~width:(width-16) ~height:(yt-yb)
614 if used < 0.6 then `NAME "grey"
615 else if used < 0.8 then `NAME "pink"
616 else if used < 0.9 then `NAME "hot pink"
618 ds.draw#set_foreground col;
619 let w = int_of_float (used *. (float width -. 16.)) in
620 ds.draw#rectangle ~x:8 ~y:yb ~width:w ~height:(yt-yb) ~filled:true ();
622 ds.draw#set_foreground `BLACK;
623 ds.draw#rectangle ~x:8 ~y:yb ~width:(width-16) ~height:(yt-yb) ();
625 (* Large text - the device name. *)
626 let txt = ds.pango_large_context#create_layout in
627 Pango.Layout.set_text txt part.Slave.pt_name;
628 let fore = `NAME "dark slate grey" in
629 ds.draw#put_layout ~x:12 ~y:(yb+4) ~fore txt;
631 let { Pango.height = txtheight; Pango.width = txtwidth } =
632 Pango.Layout.get_pixel_extent txt in
634 (* Small text below - the content. *)
635 let txt = ds.pango_small_context#create_layout in
636 Pango.Layout.set_text txt part.Slave.pt_content;
638 ds.draw#put_layout ~x:12 ~y:(yb+4+txtheight) ~fore txt;
640 (* Small text right - size. *)
642 match part.Slave.pt_statvfs with
643 | None -> printable_size part.Slave.pt_size
644 | Some { G.blocks = blocks; bsize = bsize } ->
645 let bytes = Int64.mul blocks bsize in
646 let pc = 100. *. used in
647 sprintf "%s (%.1f%% used)" (printable_size bytes) pc in
648 let txt = ds.pango_small_context#create_layout in
649 Pango.Layout.set_text txt size;
650 ds.draw#put_layout ~x:(16+txtwidth) ~y:(yb+4) ~fore txt;
656 and printable_size bytes =
657 if bytes < 16_384L then sprintf "%Ld bytes" bytes
658 else if bytes < 16_777_216L then
659 sprintf "%Ld KiB" (Int64.div bytes 1024L)
660 else if bytes < 17_179_869_184L then
661 sprintf "%Ld MiB" (Int64.div bytes 1_048_576L)
663 sprintf "%Ld GiB" (Int64.div bytes 1_073_741_824L)
665 let default_uri = ref ""
667 let argspec = Arg.align [
668 "-verbose", Arg.Set verbose, "Verbose mode";
669 "-connect", Arg.Set_string default_uri, "Connect to libvirt URI";
673 failwith (sprintf "%s: unknown command line argument"
674 (Filename.basename Sys.executable_name))
679 %s: graphical virtual machine disk usage viewer
682 (Filename.basename Sys.executable_name)
685 Arg.parse argspec anon_fun usage_msg;
687 (* Start up the slave thread. *)
688 let slave = Thread.create Slave.slave_loop () in
690 (* Set up the display. *)
691 let ds = main_window opened_domain repaint in
693 Slave.set_failure_callback (failure ds);
694 Slave.set_busy_callback (busy ds);
695 let uri = match !default_uri with "" -> None | s -> Some s in
696 Slave.connect uri (connected ds);
698 (* Run the main thread. When this returns, the application has been closed. *)
701 (* Tell the slave thread to exit and wait for it to do so. *)
702 Slave.exit_thread ();