2 * Copyright (C) 2010-2015 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.
29 module C = Libvirt.Connect
30 module Cond = Condition
31 module D = Libvirt.Domain
35 module UTF8 = CamomileLibraryDefault.Camomile.UTF8
38 type 'a callback = 'a -> unit
43 | Checksum_file of source * string * string * string callback
44 | Connect of string option * domain list callback
45 | Disk_usage of source * string * int64 callback
46 | Download_dir_find0 of source * string * string * unit callback
47 | Download_dir_tarball of source * string * download_dir_tarball_format * string * unit callback
48 | Download_file of source * string * string * bool * unit callback
49 | File_information of source * string * string callback
50 | File_xattrs of source * string * G.xattr array callback
51 | List_applications of inspection_os * G.application array callback
52 | Open_domain of string * inspection_data callback
53 | Open_images of (string * string option) list * inspection_data callback
54 | Read_directory of source * string * direntry list callback
55 | Reopen of inspection_data callback
56 | Run_command of string * unit callback
58 let rec string_of_command = function
59 | Exit_thread -> "Exit_thread"
60 | Checksum_file (src, pathname, csumtype, _) ->
61 sprintf "Checksum_file (%s, %s, %s)"
62 (string_of_source src) pathname csumtype
63 | Connect (Some name, _) -> sprintf "Connect %s" name
64 | Connect (None, _) -> "Connect NULL"
65 | Disk_usage (src, remotedir, _) ->
66 sprintf "Disk_usage (%s, %s)" (string_of_source src) remotedir
67 | Download_dir_find0 (src, remotedir, localfile, _) ->
68 sprintf "Download_dir_find0 (%s, %s, %s)"
69 (string_of_source src) remotedir localfile
70 | Download_dir_tarball (src, remotedir, format, localfile, _) ->
71 sprintf "Download_dir_tarball (%s, %s, %s, %s)"
72 (string_of_source src) remotedir
73 (string_of_download_dir_tarball_format format) localfile
74 | Download_file (src, remotefile, localfile, check, _) ->
75 sprintf "Download_file (%s, %s, %s, %b)"
76 (string_of_source src) remotefile localfile check
77 | File_information (src, pathname, _) ->
78 sprintf "File_information (%s, %s)" (string_of_source src) pathname
79 | File_xattrs (src, pathname, _) ->
80 sprintf "File_xattrs (%s, %s)" (string_of_source src) pathname
81 | List_applications (os, _) ->
82 sprintf "List_applications %s" os.insp_root
83 | Open_domain (name, _) -> sprintf "Open_domain %s" name
84 | Open_images (images, _) ->
85 sprintf "Open_images %s" (string_of_images images)
86 | Read_directory (src, dir, _) ->
87 sprintf "Read_directory (%s, %s)" (string_of_source src) dir
90 | Run_command (cmd, _) ->
91 sprintf "Run_command %s" cmd
93 and string_of_images images =
98 | fn, Some format -> sprintf "%s (%s)" fn format)
101 and string_of_source = function
102 | OS { insp_root = root } ->
105 sprintf "Volume %s" dev
107 and string_of_download_dir_tarball_format = function
112 let no_callback _ = ()
114 let failure_hook = ref (fun _ -> ())
115 let busy_hook = ref (fun _ -> ())
116 let idle_hook = ref (fun _ -> ())
117 let status_hook = ref (fun _ -> ())
118 let progress_hook = ref (fun _ -> ())
120 let set_failure_hook cb = failure_hook := cb
121 let set_busy_hook cb = busy_hook := cb
122 let set_idle_hook cb = idle_hook := cb
123 let set_status_hook cb = status_hook := cb
124 let set_progress_hook cb = progress_hook := cb
126 (* Execute a function, while holding a mutex. If the function
127 * fails, ensure we release the mutex before rethrowing the
132 let r = try Left (f ()) with exn -> Right exn in
136 | Right exn -> raise exn
138 (* The queue of commands, and a lock and condition to protect it. *)
140 let q_discard = ref false
141 let q_lock = M.create ()
142 let q_cond = Cond.create ()
144 (* Send a command message to the slave thread. *)
145 let send_to_slave ?fail cmd =
146 debug "sending message %s to slave thread ..." (string_of_command cmd);
149 Q.push (fail, cmd) q;
153 let discard_command_queue () =
157 (* Discard the currently running command. *)
161 let checksum_file ?fail src pathname csumtype cb =
162 send_to_slave ?fail (Checksum_file (src, pathname, csumtype, cb))
163 let connect ?fail uri cb = send_to_slave ?fail (Connect (uri, cb))
164 let disk_usage ?fail src remotedir cb =
165 send_to_slave ?fail (Disk_usage (src, remotedir, cb))
166 let download_dir_find0 ?fail src remotedir localfile cb =
167 send_to_slave ?fail (Download_dir_find0 (src, remotedir, localfile, cb))
168 let download_dir_tarball ?fail src remotedir format localfile cb =
170 (Download_dir_tarball (src, remotedir, format, localfile, cb))
171 let download_file ?fail src remotefile localfile cb =
172 send_to_slave ?fail (Download_file (src, remotefile, localfile, false, cb))
173 let download_file_if_not_exist ?fail src remotefile localfile cb =
174 send_to_slave ?fail (Download_file (src, remotefile, localfile, true, cb))
175 let file_information ?fail src pathname cb =
176 send_to_slave ?fail (File_information (src, pathname, cb))
177 let file_xattrs ?fail src pathname cb =
178 send_to_slave ?fail (File_xattrs (src, pathname, cb))
179 let list_applications ?fail os cb =
180 send_to_slave ?fail (List_applications (os, cb))
181 let open_domain ?fail name cb = send_to_slave ?fail (Open_domain (name, cb))
182 let open_images ?fail images cb = send_to_slave ?fail (Open_images (images, cb))
183 let read_directory ?fail src path cb =
184 send_to_slave ?fail (Read_directory (src, path, cb))
185 let reopen ?fail cb =
186 send_to_slave ?fail (Reopen cb)
187 let run_command ?fail cmd cb =
188 send_to_slave ?fail (Run_command (cmd, 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 (* Last Open_domain or Open_images command. This is so we can implement
202 * the Reopen command.
204 let last_open = ref None
206 (* Run the callback unless someone set the q_discard flag while
207 * we were running the command.
209 let callback_if_not_discarded (cb : 'a callback) (arg : 'a) =
210 let discard = with_lock q_lock (fun () -> !q_discard) in
212 GtkThread.async cb arg
214 (* Update the status bar. *)
216 let f str = GtkThread.async !status_hook str in
220 debug "top of slave loop";
222 (* Get the next command. *)
226 while Q.is_empty q do Cond.wait q_cond q_lock done;
231 debug "slave processing command %s ..." (string_of_command cmd);
234 GtkThread.async !busy_hook ();
237 (* If the user provided an override ?fail parameter to the
238 * original call, call that, else call the global hook.
241 | Some cb -> GtkThread.async cb exn
242 | None -> GtkThread.async !failure_hook exn
245 (* If there are no more commands in the queue, run the idle hook. *)
246 let empty = with_lock q_lock (fun () -> Q.is_empty q) in
247 if empty then GtkThread.async !idle_hook ();
249 if !quit then Thread.exit ();
252 and execute_command = function
257 | Checksum_file (src, pathname, csumtype, cb) ->
258 status "Calculating %s checksum of %s ..." csumtype pathname;
262 with_mount_ro g src (
264 g#checksum csumtype pathname
267 status "Finished calculating %s checksum of %s" csumtype pathname;
268 callback_if_not_discarded cb r
270 | Connect (name, cb) ->
272 match name with None -> "default hypervisor" | Some uri -> uri in
273 status "Connecting to %s ..." printable_name;
276 conn := Some (C.connect_readonly ?name ());
278 let conn = get_conn () in
279 let doms = D.get_domains conn [D.ListAll] in
280 let doms = List.map (
282 { dom_id = D.get_id d;
283 dom_name = D.get_name d;
284 dom_state = (D.get_info d).D.state }
286 let cmp { dom_name = n1 } { dom_name = n2 } = UTF8.compare n1 n2 in
287 let doms = List.sort ~cmp doms in
289 status "Connected to %s" printable_name;
290 callback_if_not_discarded cb doms
292 | Disk_usage (src, remotedir, cb) ->
293 status "Calculating disk usage of %s ..." remotedir;
297 with_mount_ro g src (
302 status "Finished calculating disk usage of %s" remotedir;
303 callback_if_not_discarded cb r
305 | Download_dir_find0 (src, remotedir, localfile, cb) ->
306 status "Downloading %s filenames to %s ..." remotedir localfile;
309 with_mount_ro g src (
311 g#find0 remotedir localfile
314 status "Finished downloading %s" localfile;
315 callback_if_not_discarded cb ()
317 | Download_dir_tarball (src, remotedir, format, localfile, cb) ->
318 status "Downloading %s to %s ..." remotedir localfile;
321 let f = match format with
322 | Tar -> fun a b -> g#tar_out a b
326 with_mount_ro g src (
328 f remotedir localfile
331 status "Finished downloading %s" localfile;
332 callback_if_not_discarded cb ()
334 | Download_file (src, remotefile, localfile, check, cb) ->
335 if not check || not (local_file_exists localfile) then (
336 status "Downloading %s to %s ..." remotefile localfile;
339 with_mount_ro g src (
341 g#download remotefile localfile
344 status "Finished downloading %s" localfile
346 callback_if_not_discarded cb ()
348 | File_information (src, pathname, cb) ->
349 status "Calculating file information for %s ..." pathname;
353 with_mount_ro g src (
358 status "Finished calculating file information for %s" pathname;
359 callback_if_not_discarded cb r
361 | File_xattrs (src, pathname, cb) ->
362 status "Getting file xattrs for %s ..." pathname;
366 with_mount_ro g src (
371 status "Finished calculating file information for %s" pathname;
372 callback_if_not_discarded cb r
374 | List_applications (os, cb) ->
375 status "Listing applications ...";
379 with_mount_ro g (OS os) (
381 g#inspect_list_applications os.insp_root
384 status "Finished listing applications";
385 callback_if_not_discarded cb r
387 | Open_domain (name, cb) as cmd ->
388 status "Opening %s ..." name;
390 let conn = get_conn () in
391 let dom = D.lookup_by_name conn name in
392 let xml = D.get_xml_desc dom in
393 let images = get_disk_images_from_xml xml in
394 open_disk_images images cb cmd
396 | Open_images (images, cb) as cmd ->
397 status "Opening disk images ...";
399 open_disk_images images cb cmd
401 | Read_directory (src, dir, cb) ->
402 status "Reading directory %s ..." dir;
405 let names, stats, links =
406 with_mount_ro g src (
408 let names = g#ls dir in (* sorted and without . and .. *)
409 let stats = lstatlist g dir names in
410 let links = readlinks g dir names (Array.of_list stats) in
411 Array.to_list names, stats, links
414 let n = List.length names in
415 n = List.length stats && n = List.length links
417 let entries = List.combine (List.combine names stats) links in
418 let entries = List.map (
419 fun ((name, stat), link) ->
420 { dent_name = name; dent_stat = stat; dent_link = link }
423 status "Finished reading directory %s" dir;
424 callback_if_not_discarded cb entries
427 (* Execute the last_open command, if there was one. But note
428 * that we have to replace the callback in the saved command with
429 * the new callback passed by the main thread to reopen.
431 (match !last_open with
432 | Some (Open_domain (name, _)) ->
433 execute_command (Open_domain (name, cb))
434 | Some (Open_images (images, _)) ->
435 execute_command (Open_images (images, cb))
437 () (* invalid_arg? *)
439 assert false (* should never happen *)
442 | Run_command (cmd, cb) ->
443 status "Running %s ..." cmd;
445 if Sys.command cmd <> 0 then
446 failwith "External command failed: %s" cmd;
448 status "Finished %s ..." cmd;
450 callback_if_not_discarded cb ()
452 (* Expect to be connected, and return the current libvirt connection. *)
456 | None -> failwith "not connected to libvirt"
461 | None -> failwith "no domain or disk image is open"
463 (* Close all libvirt and libguestfs handles. *)
465 (match !conn with Some conn -> C.close conn | None -> ());
471 (match !g with Some g -> g#close () | None -> ());
474 and get_disk_images_from_xml xml =
475 let xml = Xml.parse_string xml in
477 (* Return the device nodes. *)
480 | Xml.Element ("domain", _, children) ->
484 | Xml.Element ("devices", _, devices) -> Some devices
489 failwith "get_xml_desc didn't return <domain/>" in
491 (* Look for <source attr_name=attr_val/> and return attr_val. *)
492 let rec source_of attr_name = function
494 | Xml.Element ("source", attrs, _) :: rest ->
495 (try Some (List.assoc attr_name attrs)
496 with Not_found -> source_of attr_name rest)
497 | _ :: rest -> source_of attr_name rest
500 (* Look for <driver type=attr_val/> and return attr_val. *)
501 let rec format_of = function
503 | Xml.Element ("driver", attrs, _) :: rest ->
504 (try Some (List.assoc "type" attrs)
505 with Not_found -> format_of rest)
506 | _ :: rest -> format_of rest
509 (* Look for <disk> nodes and return the sources (block devices) of those. *)
513 | Xml.Element ("disk", attrs, disks) ->
516 let typ = List.assoc "type" attrs in
517 if typ = "file" then source_of "file" disks
518 else if typ = "block" then source_of "dev" disks
525 let format = format_of disks in
526 Some (filename, format)
532 (* The common code for Open_domain and Open_images which opens the
533 * libguestfs handle, adds the disks, and launches the appliance.
535 and open_disk_images images cb cmd =
536 debug "opening disk image %s" (string_of_images images);
539 let g' = new G.guestfs () in
543 g#set_trace (trace ());
545 (* Uncomment the next line to pass the verbose flag from the command
546 * line through to libguestfs. This is not generally necessary since
547 * we are not so interested in debugging libguestfs problems at this
548 * level, and the user can always set LIBGUESTFS_DEBUG=1 if they need
551 (* g#set_verbose (verbose ());*)
553 (* Attach progress bar callback. *)
555 g#set_event_callback (
556 fun event handle buf array ->
557 if event == G.EVENT_PROGRESS && Array.length array >= 4 then (
558 let proc_nr = array.(0)
559 and serial = array.(1)
560 and position = array.(2)
561 and total = array.(3) in
562 debug "progress callback proc_nr=%Ld serial=%Ld posn=%Ld total=%Ld"
563 proc_nr serial position total;
564 GtkThread.async !progress_hook (position, total)
566 ) [ G.EVENT_PROGRESS ]
572 g#add_drive_opts ~readonly:true filename
573 | filename, Some format ->
574 g#add_drive_opts ~readonly:true ~format filename
579 status "Listing filesystems ...";
581 (* Get list of filesystems. *)
582 let fses = g#list_filesystems () in
584 status "Looking for operating systems ...";
586 (* Perform inspection. This can fail, ignore errors. *)
588 try Array.to_list (g#inspect_os ())
591 debug "inspection failed (error ignored): %s" msg;
594 let oses = List.map (
596 let typ = g#inspect_get_type root in
597 let windows_current_control_set =
598 if typ <> "windows" then None
600 try Some (g#inspect_get_windows_current_control_set root)
601 with G.Error _ -> None
603 let windows_systemroot =
604 if typ <> "windows" then None
606 try Some (g#inspect_get_windows_systemroot root)
607 with G.Error _ -> None
610 (* Create most of the OS object that we're going to return. We
611 * have to pass this to with_mount_ro below which is why we need
612 * to partially create it here.
616 insp_arch = g#inspect_get_arch root;
617 insp_distro = g#inspect_get_distro root;
618 insp_drive_mappings = g#inspect_get_drive_mappings root;
619 insp_filesystems = g#inspect_get_filesystems root;
620 insp_hostname = g#inspect_get_hostname root;
621 insp_major_version = g#inspect_get_major_version root;
622 insp_minor_version = g#inspect_get_minor_version root;
623 insp_mountpoints = g#inspect_get_mountpoints root;
624 insp_package_format = g#inspect_get_package_format root;
625 insp_package_management = g#inspect_get_package_management root;
626 insp_product_name = g#inspect_get_product_name root;
627 insp_product_variant = g#inspect_get_product_variant root;
629 insp_windows_current_control_set = windows_current_control_set;
630 insp_windows_systemroot = windows_systemroot;
631 insp_winreg_DEFAULT = None; (* incomplete, see below *)
632 insp_winreg_SAM = None;
633 insp_winreg_SECURITY = None;
634 insp_winreg_SOFTWARE = None;
635 insp_winreg_SYSTEM = None;
638 (* We need to mount the root in order to look for Registry hives. *)
639 let winreg_DEFAULT, winreg_SAM, winreg_SECURITY, winreg_SOFTWARE,
641 match windows_systemroot with
642 | None -> None, None, None, None, None
644 with_mount_ro g (OS os) (
646 let check_for_hive filename =
648 sprintf "%s/system32/config/%s" sysroot filename in
649 try Some (g#case_sensitive_path path)
650 with G.Error _ -> None
652 check_for_hive "default",
653 check_for_hive "sam",
654 check_for_hive "security",
655 check_for_hive "software",
656 check_for_hive "system"
659 (* Fill in the remaining struct fields. *)
661 insp_winreg_DEFAULT = winreg_DEFAULT;
662 insp_winreg_SAM = winreg_SAM;
663 insp_winreg_SECURITY = winreg_SECURITY;
664 insp_winreg_SOFTWARE = winreg_SOFTWARE;
665 insp_winreg_SYSTEM = winreg_SYSTEM
671 insp_all_filesystems = fses;
675 status "Finished opening disk";
677 (* Save the command, in case user does Reopen. *)
678 last_open := Some cmd;
680 callback_if_not_discarded cb data
682 (* Start up one slave thread. *)
683 let slave_thread = Thread.create loop ()
685 (* Note the following function is called from the main thread. *)
687 discard_command_queue ();
688 ignore (send_to_slave Exit_thread);
689 Thread.join slave_thread