2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 module C = Libvirt.Connect
24 module Cond = Condition
25 module D = Libvirt.Domain
29 type 'a callback = 'a -> unit
34 | Connect of string option * domain list callback
35 | Disk_usage of source * string * int64 callback
36 | Download_dir_find0 of source * string * string * unit callback
37 | Download_dir_tarball of source * string * download_dir_tarball_format * string * unit callback
38 | Download_file of source * string * string * unit callback
39 | Open_domain of string * inspection_data callback
40 | Open_images of (string * string option) list * inspection_data callback
41 | Read_directory of source * string * direntry list callback
49 and inspection_data = {
50 insp_all_filesystems : (string * string) list;
51 insp_oses : inspection_os list;
58 insp_filesystems : string array;
59 insp_hostname : string;
60 insp_major_version : int;
61 insp_minor_version : int;
62 insp_mountpoints : (string * string) list;
63 insp_package_format : string;
64 insp_package_management : string;
65 insp_product_name : string;
67 insp_windows_systemroot : string option;
68 insp_winreg_DEFAULT : string option;
69 insp_winreg_SAM : string option;
70 insp_winreg_SECURITY : string option;
71 insp_winreg_SOFTWARE : string option;
72 insp_winreg_SYSTEM : string option;
75 and source = OS of inspection_os | Volume of string
79 dent_stat : Guestfs.stat;
83 and download_dir_tarball_format = Tar | TGZ | TXZ
85 let rec string_of_command = function
86 | Exit_thread -> "Exit_thread"
87 | Connect (Some name, _) -> sprintf "Connect %s" name
88 | Connect (None, _) -> "Connect NULL"
89 | Disk_usage (src, remotedir, _) ->
90 sprintf "Disk_usage (%s, %s)" (string_of_source src) remotedir
91 | Download_dir_find0 (src, remotedir, localfile, _) ->
92 sprintf "Download_dir_find0 (%s, %s, %s)"
93 (string_of_source src) remotedir localfile
94 | Download_dir_tarball (src, remotedir, format, localfile, _) ->
95 sprintf "Download_dir_tarball (%s, %s, %s, %s)"
96 (string_of_source src) remotedir
97 (string_of_download_dir_tarball_format format) localfile
98 | Download_file (src, remotefile, localfile, _) ->
99 sprintf "Download_file (%s, %s, %s)"
100 (string_of_source src) remotefile localfile
101 | Open_domain (name, _) -> sprintf "Open_domain %s" name
102 | Open_images (images, _) ->
103 sprintf "Open_images %s" (string_of_images images)
104 | Read_directory (src, dir, _) ->
105 sprintf "Read_directory (%s, %s)" (string_of_source src) dir
107 and string_of_images images =
112 | fn, Some format -> sprintf "%s (%s)" fn format)
115 and string_of_source = function
116 | OS { insp_root = root } ->
119 sprintf "Volume %s" dev
121 and string_of_download_dir_tarball_format = function
126 let no_callback _ = ()
128 let failure_hook = ref (fun _ -> ())
129 let busy_hook = ref (fun _ -> ())
130 let idle_hook = ref (fun _ -> ())
131 let status_hook = ref (fun _ -> ())
132 let progress_hook = ref (fun _ -> ())
134 let set_failure_hook cb = failure_hook := cb
135 let set_busy_hook cb = busy_hook := cb
136 let set_idle_hook cb = idle_hook := cb
137 let set_status_hook cb = status_hook := cb
138 let set_progress_hook cb = progress_hook := cb
140 (* Execute a function, while holding a mutex. If the function
141 * fails, ensure we release the mutex before rethrowing the
146 let r = try Left (f ()) with exn -> Right exn in
150 | Right exn -> raise exn
152 (* The queue of commands, and a lock and condition to protect it. *)
154 let q_discard = ref false
155 let q_lock = M.create ()
156 let q_cond = Cond.create ()
158 (* Send a command message to the slave thread. *)
159 let send_to_slave ?fail cmd =
160 debug "sending message %s to slave thread ..." (string_of_command cmd);
163 Q.push (fail, cmd) q;
167 let discard_command_queue () =
171 (* Discard the currently running command. *)
175 let connect ?fail uri cb = send_to_slave ?fail (Connect (uri, cb))
176 let disk_usage ?fail src remotedir cb =
177 send_to_slave ?fail (Disk_usage (src, remotedir, cb))
178 let download_dir_find0 ?fail src remotedir localfile cb =
179 send_to_slave ?fail (Download_dir_find0 (src, remotedir, localfile, cb))
180 let download_dir_tarball ?fail src remotedir format localfile cb =
182 (Download_dir_tarball (src, remotedir, format, localfile, cb))
183 let download_file ?fail src remotefile localfile cb =
184 send_to_slave ?fail (Download_file (src, remotefile, localfile, cb))
185 let open_domain ?fail name cb = send_to_slave ?fail (Open_domain (name, cb))
186 let open_images ?fail images cb = send_to_slave ?fail (Open_images (images, cb))
187 let read_directory ?fail src path cb =
188 send_to_slave ?fail (Read_directory (src, path, cb))
190 (*----- Slave thread starts here -----*)
192 (* Set this to true to exit the thread. *)
195 (* Handles. These are not protected by locks because only the slave
196 * thread has access to them.
201 (* Run the callback unless someone set the q_discard flag while
202 * we were running the command.
204 let callback_if_not_discarded (cb : 'a callback) (arg : 'a) =
205 let discard = with_lock q_lock (fun () -> !q_discard) in
207 GtkThread.async cb arg
209 (* Call 'f ()' with source mounted read-only. Ensure that everything
210 * is unmounted even if an exception is thrown.
212 let with_mount_ro g src (f : unit -> 'a) : 'a =
213 Std.finally (fun () -> g#umount_all ()) (
215 (* Do the mount - could be OS or single volume. *)
217 | Volume dev -> g#mount_ro dev "/";
218 | OS { insp_mountpoints = mps } ->
219 (* Sort the mountpoint keys by length, shortest first. *)
220 let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in
221 let mps = List.sort ~cmp mps in
222 (* Mount the filesystems. *)
224 fun (mp, dev) -> g#mount_ro dev mp
230 (* Update the status bar. *)
232 let f str = GtkThread.async !status_hook str in
236 debug "top of slave loop";
238 (* Get the next command. *)
242 while Q.is_empty q do Cond.wait q_cond q_lock done;
247 debug "slave processing command %s ..." (string_of_command cmd);
250 GtkThread.async !busy_hook ();
253 (* If the user provided an override ?fail parameter to the
254 * original call, call that, else call the global hook.
257 | Some cb -> GtkThread.async cb exn
258 | None -> GtkThread.async !failure_hook exn
261 (* If there are no more commands in the queue, run the idle hook. *)
262 let empty = with_lock q_lock (fun () -> Q.is_empty q) in
263 if empty then GtkThread.async !idle_hook ();
265 if !quit then Thread.exit ();
268 and execute_command = function
273 | Connect (name, cb) ->
275 match name with None -> "default hypervisor" | Some uri -> uri in
276 status "Connecting to %s ..." printable_name;
279 conn := Some (C.connect_readonly ?name ());
281 let conn = get_conn () in
282 let doms = D.get_domains conn [D.ListAll] in
283 let doms = List.map (
285 { dom_id = D.get_id d;
286 dom_name = D.get_name d;
287 dom_state = (D.get_info d).D.state }
289 let cmp { dom_name = n1 } { dom_name = n2 } = compare n1 n2 in
290 let doms = List.sort ~cmp doms in
292 status "Connected to %s" printable_name;
293 callback_if_not_discarded cb doms
295 | Disk_usage (src, remotedir, cb) ->
296 status "Calculating disk usage of %s ..." remotedir;
300 with_mount_ro g src (
305 status "Finished calculating disk usage of %s" remotedir;
306 callback_if_not_discarded cb r
308 | Download_dir_find0 (src, remotedir, localfile, cb) ->
309 status "Downloading %s filenames to %s ..." remotedir localfile;
312 with_mount_ro g src (
314 g#find0 remotedir localfile
317 status "Finished downloading %s" localfile;
318 callback_if_not_discarded cb ()
320 | Download_dir_tarball (src, remotedir, format, localfile, cb) ->
321 status "Downloading %s to %s ..." remotedir localfile;
324 let f = match format with
329 with_mount_ro g src (
331 f remotedir localfile
334 status "Finished downloading %s" localfile;
335 callback_if_not_discarded cb ()
337 | Download_file (src, remotefile, localfile, cb) ->
338 status "Downloading %s to %s ..." remotefile localfile;
341 with_mount_ro g src (
343 g#download remotefile localfile
346 status "Finished downloading %s" localfile;
347 callback_if_not_discarded cb ()
349 | Open_domain (name, cb) ->
350 status "Opening %s ..." name;
352 let conn = get_conn () in
353 let dom = D.lookup_by_name conn name in
354 let xml = D.get_xml_desc dom in
355 let images = get_disk_images_from_xml xml in
356 open_disk_images images cb
358 | Open_images (images, cb) ->
359 status "Opening disk images ...";
361 open_disk_images images cb
363 | Read_directory (src, dir, cb) ->
364 status "Reading directory %s ..." dir;
367 let names, stats, links =
368 with_mount_ro g src (
370 let names = g#ls dir in (* sorted and without . and .. *)
371 let names = Array.to_list names in
372 let stats = lstatlist_wrapper g dir names in
373 let links = readlinklist_wrapper g dir names in
377 let n = List.length names in
378 n = List.length stats && n = List.length links
380 let entries = List.combine (List.combine names stats) links in
381 let entries = List.map (
382 fun ((name, stat), link) ->
383 { dent_name = name; dent_stat = stat; dent_link = link }
386 status "Finished reading directory %s" dir;
387 callback_if_not_discarded cb entries
389 (* Expect to be connected, and return the current libvirt connection. *)
393 | None -> failwith "not connected to libvirt"
398 | None -> failwith "no domain or disk image is open"
400 (* Close all libvirt and libguestfs handles. *)
402 (match !conn with Some conn -> C.close conn | None -> ());
407 (match !g with Some g -> g#close () | None -> ());
410 and get_disk_images_from_xml xml =
411 let xml = Xml.parse_string xml in
413 (* Return the device nodes. *)
416 | Xml.Element ("domain", _, children) ->
420 | Xml.Element ("devices", _, devices) -> Some devices
425 failwith "get_xml_desc didn't return <domain/>" in
427 (* Look for <source attr_name=attr_val/> and return attr_val. *)
428 let rec source_of attr_name = function
430 | Xml.Element ("source", attrs, _) :: rest ->
431 (try Some (List.assoc attr_name attrs)
432 with Not_found -> source_of attr_name rest)
433 | _ :: rest -> source_of attr_name rest
436 (* Look for <driver type=attr_val/> and return attr_val. *)
437 let rec format_of = function
439 | Xml.Element ("driver", attrs, _) :: rest ->
440 (try Some (List.assoc "type" attrs)
441 with Not_found -> format_of rest)
442 | _ :: rest -> format_of rest
445 (* Look for <disk> nodes and return the sources (block devices) of those. *)
449 | Xml.Element ("disk", attrs, disks) ->
452 let typ = List.assoc "type" attrs in
453 if typ = "file" then source_of "file" disks
454 else if typ = "block" then source_of "dev" disks
461 let format = format_of disks in
462 Some (filename, format)
468 (* The common code for Open_domain and Open_images which opens the
469 * libguestfs handle, adds the disks, and launches the appliance.
471 and open_disk_images images cb =
472 debug "opening disk image %s" (string_of_images images);
475 let g' = new Guestfs.guestfs () in
479 g#set_trace (trace ());
481 (* Uncomment the next line to pass the verbose flag from the command
482 * line through to libguestfs. This is not generally necessary since
483 * we are not so interested in debugging libguestfs problems at this
484 * level, and the user can always set LIBGUESTFS_DEBUG=1 if they need
487 (* g#set_verbose (verbose ());*)
489 (* Attach progress bar callback. *)
490 g#set_progress_callback (
491 fun proc_nr serial position total ->
492 debug "progress callback proc_nr=%d serial=%d posn=%Ld total=%Ld"
493 proc_nr serial position total;
494 GtkThread.async !progress_hook (position, total)
500 g#add_drive_opts ~readonly:true filename
501 | filename, Some format ->
502 g#add_drive_opts ~readonly:true ~format filename
507 status "Listing filesystems ...";
509 (* Get list of filesystems. *)
510 let fses = g#list_filesystems () in
512 status "Looking for operating systems ...";
514 (* Perform inspection. This can fail, ignore errors. *)
516 try Array.to_list (g#inspect_os ())
519 debug "inspection failed (error ignored): %s" msg;
522 let oses = List.map (
524 let typ = g#inspect_get_type root in
525 let windows_systemroot =
526 if typ <> "windows" then None
528 try Some (g#inspect_get_windows_systemroot root)
529 with Guestfs.Error _ -> None
532 (* Create most of the OS object that we're going to return. We
533 * have to pass this to with_mount_ro below which is why we need
534 * to partially create it here.
538 insp_arch = g#inspect_get_arch root;
539 insp_distro = g#inspect_get_distro root;
540 insp_filesystems = g#inspect_get_filesystems root;
541 insp_hostname = g#inspect_get_hostname root;
542 insp_major_version = g#inspect_get_major_version root;
543 insp_minor_version = g#inspect_get_minor_version root;
544 insp_mountpoints = g#inspect_get_mountpoints root;
545 insp_package_format = g#inspect_get_package_format root;
546 insp_package_management = g#inspect_get_package_management root;
547 insp_product_name = g#inspect_get_product_name root;
549 insp_windows_systemroot = windows_systemroot;
550 insp_winreg_DEFAULT = None; (* incomplete, see below *)
551 insp_winreg_SAM = None;
552 insp_winreg_SECURITY = None;
553 insp_winreg_SOFTWARE = None;
554 insp_winreg_SYSTEM = None;
557 (* We need to mount the root in order to look for Registry hives. *)
558 let winreg_DEFAULT, winreg_SAM, winreg_SECURITY, winreg_SOFTWARE,
560 match windows_systemroot with
561 | None -> None, None, None, None, None
563 with_mount_ro g (OS os) (
565 let check_for_hive filename =
567 sprintf "%s/system32/config/%s" sysroot filename in
568 try Some (g#case_sensitive_path path)
569 with Guestfs.Error _ -> None
571 check_for_hive "default",
572 check_for_hive "sam",
573 check_for_hive "security",
574 check_for_hive "software",
575 check_for_hive "system"
578 (* Fill in the remaining struct fields. *)
580 insp_winreg_DEFAULT = winreg_DEFAULT;
581 insp_winreg_SAM = winreg_SAM;
582 insp_winreg_SECURITY = winreg_SECURITY;
583 insp_winreg_SOFTWARE = winreg_SOFTWARE;
584 insp_winreg_SYSTEM = winreg_SYSTEM
590 insp_all_filesystems = fses;
594 status "Finished opening disk";
595 callback_if_not_discarded cb data
597 (* guestfs_lstatlist has a "hidden" limit of the protocol message size.
598 * Call this function, but split the list of names into chunks.
600 and lstatlist_wrapper g dir = function
603 let names', names = List.take 1000 names, List.drop 1000 names in
604 let xs = g#lstatlist dir (Array.of_list names') in
605 let xs = Array.to_list xs in
606 xs @ lstatlist_wrapper g dir names
608 (* Same as above for guestfs_readlinklist. *)
609 and readlinklist_wrapper g dir = function
612 let names', names = List.take 1000 names, List.drop 1000 names in
613 let xs = g#readlinklist dir (Array.of_list names') in
614 let xs = Array.to_list xs in
615 xs @ readlinklist_wrapper g dir names
617 (* Start up one slave thread. *)
618 let slave_thread = Thread.create loop ()
620 (* Note the following function is called from the main thread. *)
622 discard_command_queue ();
623 ignore (send_to_slave Exit_thread);
624 Thread.join slave_thread