+ 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 <domain/>" in
+
+ (* Look for <source attr_name=attr_val/> 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 <driver type=attr_val/> 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 <disk> 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 G.guestfs () in
+ g := Some g';
+ let g = g' in
+
+ g#set_trace (trace ());
+
+ (* 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. *)
+ ignore (
+ g#set_event_callback (
+ fun g event handle buf array ->
+ if event == G.EVENT_PROGRESS && Array.length array >= 4 then (
+ let proc_nr = array.(0)
+ and serial = array.(1)
+ and position = array.(2)
+ and total = array.(3) in
+ debug "progress callback proc_nr=%Ld serial=%Ld posn=%Ld total=%Ld"
+ proc_nr serial position total;
+ GtkThread.async !progress_hook (position, total)
+ )
+ ) [ G.EVENT_PROGRESS ]
+ );
+
+ 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 ();
+
+ status "Listing filesystems ...";
+
+ (* Get list of filesystems. *)
+ let fses = g#list_filesystems () in
+
+ status "Looking for operating systems ...";
+
+ (* Perform inspection. This can fail, ignore errors. *)
+ let roots =
+ try Array.to_list (g#inspect_os ())
+ with
+ G.Error msg ->
+ debug "inspection failed (error ignored): %s" msg;
+ [] in
+
+ let oses = List.map (
+ fun root ->
+ let typ = g#inspect_get_type root in
+ let windows_systemroot =
+ if typ <> "windows" then None
+ else (
+ try Some (g#inspect_get_windows_systemroot root)
+ with G.Error _ -> None
+ ) in
+
+ (* Create most of the OS object that we're going to return. We
+ * have to pass this to with_mount_ro below which is why we need
+ * to partially create it here.
+ *)
+ let os = {
+ 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 = typ;
+ insp_windows_systemroot = windows_systemroot;
+ insp_winreg_DEFAULT = None; (* incomplete, see below *)
+ insp_winreg_SAM = None;
+ insp_winreg_SECURITY = None;
+ insp_winreg_SOFTWARE = None;
+ insp_winreg_SYSTEM = None;
+ } in
+
+ (* We need to mount the root in order to look for Registry hives. *)
+ let winreg_DEFAULT, winreg_SAM, winreg_SECURITY, winreg_SOFTWARE,
+ winreg_SYSTEM =
+ match windows_systemroot with
+ | None -> None, None, None, None, None
+ | Some sysroot ->
+ with_mount_ro g (OS os) (
+ fun () ->
+ let check_for_hive filename =
+ let path =
+ sprintf "%s/system32/config/%s" sysroot filename in
+ try Some (g#case_sensitive_path path)
+ with G.Error _ -> None
+ in
+ check_for_hive "default",
+ check_for_hive "sam",
+ check_for_hive "security",
+ check_for_hive "software",
+ check_for_hive "system"
+ ) in
+
+ (* Fill in the remaining struct fields. *)
+ let os = { os with
+ insp_winreg_DEFAULT = winreg_DEFAULT;
+ insp_winreg_SAM = winreg_SAM;
+ insp_winreg_SECURITY = winreg_SECURITY;
+ insp_winreg_SOFTWARE = winreg_SOFTWARE;
+ insp_winreg_SYSTEM = winreg_SYSTEM
+ } in
+ os
+ ) roots in
+
+ let data = {
+ insp_all_filesystems = fses;
+ insp_oses = oses;
+ } in
+
+ status "Finished opening disk";
+ callback_if_not_discarded cb data