(* Guestfs Browser. * Copyright (C) 2010 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) open ExtList open Printf open Utils module C = Libvirt.Connect module Cond = Condition module D = Libvirt.Domain module M = Mutex module Q = Queue type 'a callback = 'a -> unit (* The commands. *) type command = | Exit_thread | Connect of string option * domain list 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 and domain = { dom_id : int; dom_name : string; dom_state : D.state; } and inspection_data = { insp_all_filesystems : (string * string) list; insp_oses : inspection_os list; } and inspection_os = { insp_root : string; insp_arch : string; insp_distro : string; insp_filesystems : string array; insp_hostname : string; insp_major_version : int; insp_minor_version : int; insp_mountpoints : (string * string) list; insp_package_format : string; insp_package_management : string; insp_product_name : string; insp_type : string; insp_windows_systemroot : string option; } and source = OS of inspection_os | Volume of string and direntry = { dent_name : string; dent_stat : Guestfs.stat; dent_link : string; } let rec string_of_command = function | Exit_thread -> "Exit_thread" | Connect (Some name, _) -> sprintf "Connect %s" name | Connect (None, _) -> "Connect NULL" | Open_domain (name, _) -> sprintf "Open_domain %s" name | Open_images (images, _) -> sprintf "Open_images %s" (string_of_images images) | Read_directory (OS { insp_root = root }, dir, _) -> sprintf "Read_directory (OS %s, %s)" root dir | Read_directory (Volume dev, dir, _) -> sprintf "Read_directory (Volume %s, %s)" dev dir and string_of_images images = "[" ^ String.concat "; " (List.map (function | fn, None -> fn | fn, Some format -> sprintf "%s (%s)" fn format) images) ^ "]" let no_callback _ = () let failure_hook = ref (fun _ -> ()) let busy_hook = ref (fun _ -> ()) let idle_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_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 * exception. *) let with_lock m f = M.lock m; let r = try Left (f ()) with exn -> Right exn in M.unlock m; match r with | Left r -> r | Right exn -> raise exn (* 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 ?fail cmd = debug "sending message %s to slave thread ..." (string_of_command cmd); with_lock q_lock ( fun () -> Q.push (fail, cmd) q; Cond.signal q_cond ) let discard_command_queue () = with_lock q_lock ( fun () -> Q.clear q; (* Discard the currently running command. *) q_discard := true ) let connect ?fail uri cb = send_to_slave ?fail (Connect (uri, 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)) (*----- Slave thread starts here -----*) (* Set this to true to exit the thread. *) let quit = ref false (* Handles. These are not protected by locks because only the slave * thread has access to them. *) let conn = ref None let g = ref None (* Run the callback unless someone set the q_discard flag while * we were running the command. *) 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 (* Call 'f ()' with source mounted read-only. Ensure that everything * is unmounted even if an exception is thrown. *) let with_mount_ro g src (f : unit -> 'a) : 'a = Std.finally (fun () -> g#umount_all ()) ( fun () -> (* Do the mount - could be OS or single volume. *) (match src with | Volume dev -> g#mount_ro dev "/"; | OS { insp_mountpoints = mps } -> (* Sort the mountpoint keys by length, shortest first. *) let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in let mps = List.sort ~cmp mps in (* Mount the filesystems. *) List.iter ( fun (mp, dev) -> g#mount_ro dev mp ) mps ); f () ) () let rec loop () = debug "top of slave loop"; (* Get the next command. *) let fail, cmd = with_lock q_lock ( fun () -> while Q.is_empty q do Cond.wait q_cond q_lock done; q_discard := false; Q.pop q ) in debug "slave processing command %s ..." (string_of_command cmd); (try GtkThread.async !busy_hook (); execute_command cmd with exn -> (* If the user provided an override ?fail parameter to the * original call, call that, else call the global hook. *) 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 empty = with_lock q_lock (fun () -> Q.is_empty q) in if empty then GtkThread.async !idle_hook (); if !quit then Thread.exit (); loop () and execute_command = function | Exit_thread -> quit := true; close_all () | Connect (name, cb) -> close_all (); conn := Some (C.connect_readonly ?name ()); let conn = get_conn () in let doms = D.get_domains conn [D.ListAll] in let doms = List.map ( fun d -> { dom_id = D.get_id d; 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 doms = List.sort ~cmp doms in callback_if_not_discarded cb doms | Open_domain (name, cb) -> let conn = get_conn () in let dom = D.lookup_by_name conn name in let xml = D.get_xml_desc dom in let images = get_disk_images_from_xml xml in open_disk_images images cb | Open_images (images, cb) -> open_disk_images images cb | Read_directory (src, dir, cb) -> let g = get_g () in let names, stats, links = with_mount_ro g src ( fun () -> let names = g#ls 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 ) in assert ( let n = List.length names in n = List.length stats && n = List.length links ); let entries = List.combine (List.combine names stats) links in let entries = List.map ( fun ((name, stat), link) -> { dent_name = name; dent_stat = stat; dent_link = link } ) entries in callback_if_not_discarded cb entries (* Expect to be connected, and return the current libvirt connection. *) and get_conn () = match !conn with | Some conn -> conn | None -> failwith "not connected to libvirt" and get_g () = match !g with | Some g -> g | None -> failwith "no domain or disk image is open" (* Close all libvirt and libguestfs handles. *) and close_all () = (match !conn with Some conn -> C.close conn | None -> ()); conn := None; close_g () and close_g () = (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) -> let devices = List.filter_map ( function | Xml.Element ("devices", _, devices) -> Some devices | _ -> None ) children in List.concat devices | _ -> failwith "get_xml_desc didn't return " in (* Look for and return attr_val. *) let rec source_of attr_name = function | [] -> None | Xml.Element ("source", attrs, _) :: rest -> (try Some (List.assoc attr_name attrs) with Not_found -> source_of attr_name rest) | _ :: rest -> source_of attr_name rest in (* Look for and return attr_val. *) let rec format_of = function | [] -> None | Xml.Element ("driver", attrs, _) :: rest -> (try Some (List.assoc "type" attrs) with Not_found -> format_of rest) | _ :: rest -> format_of rest in (* Look for nodes and return the sources (block devices) of those. *) let blkdevs = List.filter_map ( function | Xml.Element ("disk", attrs, disks) -> let filename = try let typ = List.assoc "type" attrs in if typ = "file" then source_of "file" disks else if typ = "block" then source_of "dev" disks else None with Not_found -> None in (match filename with | None -> None | Some filename -> let format = format_of disks in Some (filename, format) ); | _ -> None ) devices in blkdevs (* The common code for Open_domain and Open_images which opens the * libguestfs handle, adds the disks, and launches the appliance. *) and open_disk_images images cb = debug "opening disk image %s" (string_of_images images); close_g (); let g' = new Guestfs.guestfs () in g := Some g'; let g = g' in (* Uncomment the next line to pass the verbose flag from the command * line through to libguestfs. This is not generally necessary since * we are not so interested in debugging libguestfs problems at this * level, and the user can always set LIBGUESTFS_DEBUG=1 if they need * to. *) (* g#set_verbose (verbose ());*) (* Attach progress bar callback. *) g#set_progress_callback ( fun proc_nr serial position total -> debug "progress callback proc_nr=%d serial=%d posn=%Ld total=%Ld" proc_nr serial position total; GtkThread.async !progress_hook (position, total) ); List.iter ( function | filename, None -> g#add_drive_opts ~readonly:true filename | filename, Some format -> g#add_drive_opts ~readonly:true ~format filename ) images; g#launch (); (* Get list of filesystems. *) let fses = g#list_filesystems () in (* Perform inspection. This can fail, ignore errors. *) let roots = try Array.to_list (g#inspect_os ()) with Guestfs.Error msg -> debug "inspection failed (error ignored): %s" msg; [] in let oses = List.map ( fun root -> { insp_root = root; insp_arch = g#inspect_get_arch root; insp_distro = g#inspect_get_distro root; insp_filesystems = g#inspect_get_filesystems root; insp_hostname = g#inspect_get_hostname root; insp_major_version = g#inspect_get_major_version root; insp_minor_version = g#inspect_get_minor_version root; insp_mountpoints = g#inspect_get_mountpoints root; insp_package_format = g#inspect_get_package_format root; insp_package_management = g#inspect_get_package_management root; insp_product_name = g#inspect_get_product_name root; insp_type = g#inspect_get_type root; insp_windows_systemroot = try Some (g#inspect_get_windows_systemroot root) with Guestfs.Error _ -> None } ) roots in let data = { insp_all_filesystems = fses; insp_oses = oses; } in callback_if_not_discarded cb data (* guestfs_lstatlist has a "hidden" limit of the protocol message size. * Call this function, but split the list of names into chunks. *) and lstatlist_wrapper g dir = function | [] -> [] | names -> let names', names = List.take 1000 names, List.drop 1000 names in let xs = g#lstatlist dir (Array.of_list names') in let xs = Array.to_list xs in xs @ lstatlist_wrapper g dir names (* Same as above for guestfs_readlinklist. *) and readlinklist_wrapper g dir = function | [] -> [] | names -> let names', names = List.take 1000 names, List.drop 1000 names in let xs = g#readlinklist dir (Array.of_list names') in let xs = Array.to_list xs in xs @ readlinklist_wrapper g dir names (* Start up one slave thread. *) let slave_thread = Thread.create loop () (* Note the following function is called from the main thread. *) let exit_thread () = discard_command_queue (); ignore (send_to_slave Exit_thread); Thread.join slave_thread