X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=slave.ml;h=f72a5aa159b99824842ba419bc1db53831f9c977;hb=817c4be2e7b8e960f650dbc52745cad84b7cae34;hp=ba45cfbd572056a0e8520a18850162a855114144;hpb=277d7009668cce99d0534d780c3984675bf20cd0;p=guestfs-browser.git
diff --git a/slave.ml b/slave.ml
index ba45cfb..f72a5aa 100644
--- a/slave.ml
+++ b/slave.ml
@@ -1,5 +1,5 @@
(* Guestfs Browser.
- * Copyright (C) 2010 Red Hat Inc.
+ * Copyright (C) 2010-2011 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,89 +17,102 @@
*)
open ExtList
-open Printf
+open ExtString
+
open Utils
+open Slave_types
+open Slave_utils
+
+open Printf
+
module C = Libvirt.Connect
module Cond = Condition
module D = Libvirt.Domain
module G = Guestfs
module M = Mutex
module Q = Queue
+module UTF8 = CamomileLibraryDefault.Camomile.UTF8
+
type 'a callback = 'a -> unit
(* The commands. *)
type command =
| Exit_thread
- | Connect of string option * unit callback
- | Get_domains of domain list callback
- | Open_domain of string * rw_flag callback
- | Open_images of string list * rw_flag callback
- | Get_volumes of volume callback
- | Read_directory of string * string * direntry list callback
- | Disk_usage of string * string * int64 callback
- | Export_dir_to of export_t * string * string * string * unit callback
-
-and domain = {
- dom_id : int;
- dom_name : string;
- dom_state : D.state;
-}
-
-and rw_flag = RO | RW
-
-and volume = {
- vol_device : string;
- vol_type : string;
- vol_label : string;
- vol_uuid : string;
- vol_statvfs : Guestfs.statvfs;
-}
-
-and direntry = {
- dent_name : string;
- dent_stat : Guestfs.stat;
- dent_link : string;
-}
-
-and export_t =
- | Export_tar
- | Export_tgz
- | Export_checksums of string
- | Export_list
+ | Checksum_file of source * string * string * string callback
+ | Connect of string option * domain list callback
+ | Disk_usage of source * string * int64 callback
+ | Download_dir_find0 of source * string * string * unit callback
+ | Download_dir_tarball of source * string * download_dir_tarball_format * string * unit callback
+ | Download_file of source * string * string * bool * unit callback
+ | File_information of source * string * string callback
+ | Open_domain of string * inspection_data callback
+ | Open_images of (string * string option) list * inspection_data callback
+ | Read_directory of source * string * direntry list callback
+ | Run_command of string * unit callback
let rec string_of_command = function
| Exit_thread -> "Exit_thread"
+ | Checksum_file (src, pathname, csumtype, _) ->
+ sprintf "Checksum_file (%s, %s, %s)"
+ (string_of_source src) pathname csumtype
| Connect (Some name, _) -> sprintf "Connect %s" name
| Connect (None, _) -> "Connect NULL"
- | Get_domains _ -> "Get_domains"
+ | Disk_usage (src, remotedir, _) ->
+ sprintf "Disk_usage (%s, %s)" (string_of_source src) remotedir
+ | Download_dir_find0 (src, remotedir, localfile, _) ->
+ sprintf "Download_dir_find0 (%s, %s, %s)"
+ (string_of_source src) remotedir localfile
+ | Download_dir_tarball (src, remotedir, format, localfile, _) ->
+ sprintf "Download_dir_tarball (%s, %s, %s, %s)"
+ (string_of_source src) remotedir
+ (string_of_download_dir_tarball_format format) localfile
+ | Download_file (src, remotefile, localfile, check, _) ->
+ sprintf "Download_file (%s, %s, %s, %b)"
+ (string_of_source src) remotefile localfile check
+ | File_information (src, pathname, _) ->
+ sprintf "File_information (%s, %s)" (string_of_source src) pathname
| Open_domain (name, _) -> sprintf "Open_domain %s" name
| Open_images (images, _) ->
- sprintf "Open_images [%s]" (String.concat "; " images)
- | Get_volumes _ -> "Get_volumes"
- | Read_directory (dev, dir, _) -> sprintf "Read_directory %s %s" dev dir
- | Disk_usage (dev, dir, _) -> sprintf "Disk_usage %s %s" dev dir
- | Export_dir_to (t, dev, dir, file, _) ->
- sprintf "Export_dir_to %s %s %s %s" (string_of_export_t t) dev dir file
-
-and string_of_export_t = function
- | Export_tar -> "Export_tar"
- | Export_tgz -> "Export_tgz"
- | Export_checksums alg -> sprintf "Export_checksums %s" alg
- | Export_list -> "Export_list"
-
-and string_of_rw_flag = function RO -> "RO" | RW -> "RW"
+ sprintf "Open_images %s" (string_of_images images)
+ | Read_directory (src, dir, _) ->
+ sprintf "Read_directory (%s, %s)" (string_of_source src) dir
+ | Run_command (cmd, _) ->
+ sprintf "Run_command %s" cmd
+
+and string_of_images images =
+ "[" ^
+ String.concat "; "
+ (List.map (function
+ | fn, None -> fn
+ | fn, Some format -> sprintf "%s (%s)" fn format)
+ images) ^ "]"
+
+and string_of_source = function
+ | OS { insp_root = root } ->
+ sprintf "OS %s" root
+ | Volume dev ->
+ sprintf "Volume %s" dev
+
+and string_of_download_dir_tarball_format = function
+ | Tar -> "Tar"
+ | TGZ -> "TGZ"
+ | TXZ -> "TXZ"
let no_callback _ = ()
let failure_hook = ref (fun _ -> ())
let busy_hook = ref (fun _ -> ())
let idle_hook = ref (fun _ -> ())
+let status_hook = ref (fun _ -> ())
+let progress_hook = ref (fun _ -> ())
let set_failure_hook cb = failure_hook := cb
let set_busy_hook cb = busy_hook := cb
let set_idle_hook cb = idle_hook := cb
+let set_status_hook cb = status_hook := cb
+let set_progress_hook cb = progress_hook := cb
(* Execute a function, while holding a mutex. If the function
* fails, ensure we release the mutex before rethrowing the
@@ -115,29 +128,49 @@ let with_lock m f =
(* The queue of commands, and a lock and condition to protect it. *)
let q = Q.create ()
+let q_discard = ref false
let q_lock = M.create ()
let q_cond = Cond.create ()
(* Send a command message to the slave thread. *)
-let send_to_slave cmd =
+let send_to_slave ?fail cmd =
debug "sending message %s to slave thread ..." (string_of_command cmd);
with_lock q_lock (
fun () ->
- Q.push cmd q;
+ Q.push (fail, cmd) q;
Cond.signal q_cond
)
-let discard_command_queue () = with_lock q_lock (fun () -> Q.clear q)
+let discard_command_queue () =
+ with_lock q_lock (
+ fun () ->
+ Q.clear q;
+ (* Discard the currently running command. *)
+ q_discard := true
+ )
-let connect uri cb = send_to_slave (Connect (uri, cb))
-let get_domains cb = send_to_slave (Get_domains cb)
-let get_volumes cb = send_to_slave (Get_volumes cb)
-let open_domain name cb = send_to_slave (Open_domain (name, cb))
-let open_images images cb = send_to_slave (Open_images (images, cb))
-let read_directory dev dir cb = send_to_slave (Read_directory (dev, dir, cb))
-let disk_usage dev dir cb = send_to_slave (Disk_usage (dev, dir, cb))
-let export_dir_to t dev dir file cb =
- send_to_slave (Export_dir_to (t, dev, dir, file, cb))
+let checksum_file ?fail src pathname csumtype cb =
+ send_to_slave ?fail (Checksum_file (src, pathname, csumtype, cb))
+let connect ?fail uri cb = send_to_slave ?fail (Connect (uri, cb))
+let disk_usage ?fail src remotedir cb =
+ send_to_slave ?fail (Disk_usage (src, remotedir, cb))
+let download_dir_find0 ?fail src remotedir localfile cb =
+ send_to_slave ?fail (Download_dir_find0 (src, remotedir, localfile, cb))
+let download_dir_tarball ?fail src remotedir format localfile cb =
+ send_to_slave ?fail
+ (Download_dir_tarball (src, remotedir, format, localfile, cb))
+let download_file ?fail src remotefile localfile cb =
+ send_to_slave ?fail (Download_file (src, remotefile, localfile, false, cb))
+let download_file_if_not_exist ?fail src remotefile localfile cb =
+ send_to_slave ?fail (Download_file (src, remotefile, localfile, true, cb))
+let file_information ?fail src pathname cb =
+ send_to_slave ?fail (File_information (src, pathname, cb))
+let open_domain ?fail name cb = send_to_slave ?fail (Open_domain (name, cb))
+let open_images ?fail images cb = send_to_slave ?fail (Open_images (images, cb))
+let read_directory ?fail src path cb =
+ send_to_slave ?fail (Read_directory (src, path, cb))
+let run_command ?fail cmd cb =
+ send_to_slave ?fail (Run_command (cmd, cb))
(*----- Slave thread starts here -----*)
@@ -150,46 +183,48 @@ let quit = ref false
let conn = ref None
let g = ref None
-(* Call 'f ()' with 'dev' mounted read-only. Ensure that everything
- * is unmounted even if an exception is thrown.
+(* Run the callback unless someone set the q_discard flag while
+ * we were running the command.
*)
-let with_mount_ro g dev (f : unit -> 'a) : 'a =
- Std.finally (fun () -> G.umount_all g) (
- fun () ->
- G.mount_ro g dev "/";
- f ()
- ) ()
+let callback_if_not_discarded (cb : 'a callback) (arg : 'a) =
+ let discard = with_lock q_lock (fun () -> !q_discard) in
+ if not discard then
+ GtkThread.async cb arg
+
+(* Update the status bar. *)
+let status fs =
+ let f str = GtkThread.async !status_hook str in
+ ksprintf f fs
let rec loop () =
- debug "thread id %d: top of slave loop ..." (Thread.id (Thread.self ()));
+ debug "top of slave loop";
(* Get the next command. *)
- let cmd =
+ let fail, cmd =
with_lock q_lock (
fun () ->
- while Q.is_empty q do
- Cond.wait q_cond q_lock
- done;
+ while Q.is_empty q do Cond.wait q_cond q_lock done;
+ q_discard := false;
Q.pop q
) in
- debug "thread id %d: slave processing command %s ..."
- (Thread.id (Thread.self ())) (string_of_command cmd);
+ debug "slave processing command %s ..." (string_of_command cmd);
(try
GtkThread.async !busy_hook ();
- execute_command cmd;
+ execute_command cmd
with exn ->
- (* If a command fails, clear the command queue and run the
- * failure hook in the main thread.
+ (* If the user provided an override ?fail parameter to the
+ * original call, call that, else call the global hook.
*)
- discard_command_queue ();
- GtkThread.async !failure_hook exn
+ match fail with
+ | Some cb -> GtkThread.async cb exn
+ | None -> GtkThread.async !failure_hook exn
);
(* If there are no more commands in the queue, run the idle hook. *)
- let r = with_lock q_lock (fun () -> Q.is_empty q) in
- if r then GtkThread.async !idle_hook ();
+ let empty = with_lock q_lock (fun () -> Q.is_empty q) in
+ if empty then GtkThread.async !idle_hook ();
if !quit then Thread.exit ();
loop ()
@@ -199,12 +234,27 @@ and execute_command = function
quit := true;
close_all ()
+ | Checksum_file (src, pathname, csumtype, cb) ->
+ status "Calculating %s checksum of %s ..." csumtype pathname;
+
+ let g = get_g () in
+ let r =
+ with_mount_ro g src (
+ fun () ->
+ g#checksum csumtype pathname
+ ) in
+
+ status "Finished calculating %s checksum of %s" csumtype pathname;
+ callback_if_not_discarded cb r
+
| Connect (name, cb) ->
+ let printable_name =
+ match name with None -> "default hypervisor" | Some uri -> uri in
+ status "Connecting to %s ..." printable_name;
+
close_all ();
conn := Some (C.connect_readonly ?name ());
- GtkThread.async cb ()
- | Get_domains cb ->
let conn = get_conn () in
let doms = D.get_domains conn [D.ListAll] in
let doms = List.map (
@@ -213,50 +263,106 @@ and execute_command = function
dom_name = D.get_name d;
dom_state = (D.get_info d).D.state }
) doms in
- let cmp { dom_name = n1 } { dom_name = n2 } = compare n1 n2 in
+ let cmp { dom_name = n1 } { dom_name = n2 } = UTF8.compare n1 n2 in
let doms = List.sort ~cmp doms in
- GtkThread.async cb doms
+
+ status "Connected to %s" printable_name;
+ callback_if_not_discarded cb doms
+
+ | Disk_usage (src, remotedir, cb) ->
+ status "Calculating disk usage of %s ..." remotedir;
+
+ let g = get_g () in
+ let r =
+ with_mount_ro g src (
+ fun () ->
+ g#du remotedir
+ ) in
+
+ status "Finished calculating disk usage of %s" remotedir;
+ callback_if_not_discarded cb r
+
+ | Download_dir_find0 (src, remotedir, localfile, cb) ->
+ status "Downloading %s filenames to %s ..." remotedir localfile;
+
+ let g = get_g () in
+ with_mount_ro g src (
+ fun () ->
+ g#find0 remotedir localfile
+ );
+
+ status "Finished downloading %s" localfile;
+ callback_if_not_discarded cb ()
+
+ | Download_dir_tarball (src, remotedir, format, localfile, cb) ->
+ status "Downloading %s to %s ..." remotedir localfile;
+
+ let g = get_g () in
+ let f = match format with
+ | Tar -> g#tar_out
+ | TGZ -> g#tgz_out
+ | TXZ -> g#txz_out
+ in
+ with_mount_ro g src (
+ fun () ->
+ f remotedir localfile
+ );
+
+ status "Finished downloading %s" localfile;
+ callback_if_not_discarded cb ()
+
+ | Download_file (src, remotefile, localfile, check, cb) ->
+ if not check || not (local_file_exists localfile) then (
+ status "Downloading %s to %s ..." remotefile localfile;
+
+ let g = get_g () in
+ with_mount_ro g src (
+ fun () ->
+ g#download remotefile localfile
+ );
+
+ status "Finished downloading %s" localfile
+ );
+ callback_if_not_discarded cb ()
+
+ | File_information (src, pathname, cb) ->
+ status "Calculating file information for %s ..." pathname;
+
+ let g = get_g () in
+ let r =
+ with_mount_ro g src (
+ fun () ->
+ g#file pathname
+ ) in
+
+ status "Finished calculating file information for %s" pathname;
+ callback_if_not_discarded cb r
| Open_domain (name, cb) ->
+ status "Opening %s ..." name;
+
let conn = get_conn () in
let dom = D.lookup_by_name conn name in
- (* Only permit writes to shut off domains. This isn't foolproof
- * since the user could start up the domain while we're running,
- * which would cause disk corruption. Until we can negotiate a
- * feasible locking scheme with libvirt/qemu, this is the best we
- * can do.
- *)
- let rw = write_flag () && (D.get_info dom).D.state = D.InfoShutoff in
- let rw = if rw then RW else RO in
let xml = D.get_xml_desc dom in
let images = get_disk_images_from_xml xml in
- open_disk_images rw images cb
+ open_disk_images images cb
| Open_images (images, cb) ->
- let rw = write_flag () in
- let rw = if rw then RW else RO in
- open_disk_images rw images cb
+ status "Opening disk images ...";
+
+ open_disk_images images cb
+
+ | Read_directory (src, dir, cb) ->
+ status "Reading directory %s ..." dir;
- | Get_volumes cb ->
- let g = get_g () in
- (* Devices which directly contain filesystems (RHBZ#590167). *)
- let devices = G.list_devices g in
- Array.iter (if_mountable_vol g cb) devices;
- let partitions = G.list_partitions g in
- Array.iter (if_mountable_vol g cb) partitions;
- let lvs = G.lvs g in
- Array.iter (if_mountable_vol g cb) lvs
-
- | Read_directory (dev, dir, cb) ->
let g = get_g () in
let names, stats, links =
- with_mount_ro g dev (
+ with_mount_ro g src (
fun () ->
- let names = G.ls g dir in (* sorted and without . and .. *)
- let names = Array.to_list names in
- let stats = lstatlist_wrapper g dir names in
- let links = readlinklist_wrapper g dir names in
- names, stats, links
+ let names = g#ls dir in (* sorted and without . and .. *)
+ let stats = lstatlist g dir names in
+ let links = readlinks g dir names (Array.of_list stats) in
+ Array.to_list names, stats, links
) in
assert (
let n = List.length names in
@@ -267,24 +373,19 @@ and execute_command = function
fun ((name, stat), link) ->
{ dent_name = name; dent_stat = stat; dent_link = link }
) entries in
- GtkThread.async cb entries
- | Disk_usage (dev, dir, cb) ->
- let g = get_g () in
- let kb = with_mount_ro g dev (fun () -> G.du g dir) in
- GtkThread.async cb kb
+ status "Finished reading directory %s" dir;
+ callback_if_not_discarded cb entries
- | Export_dir_to (t, dev, dir, file, cb) ->
- let g = get_g () in
- with_mount_ro g dev (
- fun () ->
- (match t with
- | Export_tar -> G.tar_out g
- | Export_tgz -> G.tgz_out g
- | Export_checksums alg -> G.checksums_out g alg
- | Export_list -> G.find0 g) dir file
- );
- GtkThread.async cb ()
+ | Run_command (cmd, cb) ->
+ status "Running %s ..." cmd;
+
+ if Sys.command cmd <> 0 then
+ failwith "External command failed: %s" cmd;
+
+ status "Finished %s ..." cmd;
+
+ callback_if_not_discarded cb ()
(* Expect to be connected, and return the current libvirt connection. *)
and get_conn () =
@@ -304,11 +405,13 @@ and close_all () =
close_g ()
and close_g () =
- (match !g with Some g -> G.close g | None -> ());
+ (match !g with Some g -> g#close () | None -> ());
g := None
and get_disk_images_from_xml xml =
let xml = Xml.parse_string xml in
+
+ (* Return the device nodes. *)
let devices =
match xml with
| Xml.Element ("domain", _, children) ->
@@ -321,20 +424,44 @@ and get_disk_images_from_xml xml =
List.concat devices
| _ ->
failwith "get_xml_desc didn't return " in
- let rec source_of = function (*