*)
open ExtList
-open Printf
+open ExtString
+open CamomileLibrary
+open Default.Camomile
+
open Utils
+open Printf
+
module C = Libvirt.Connect
module Cond = Condition
module D = Libvirt.Domain
+module G = Guestfs
module M = Mutex
module Q = Queue
insp_product_name : string;
insp_type : string;
insp_windows_systemroot : string option;
+ insp_winreg_DEFAULT : string option;
+ insp_winreg_SAM : string option;
+ insp_winreg_SECURITY : string option;
+ insp_winreg_SOFTWARE : string option;
+ insp_winreg_SYSTEM : string option;
}
and source = OS of inspection_os | Volume of string
and direntry = {
dent_name : string;
- dent_stat : Guestfs.stat;
+ dent_stat : G.stat;
dent_link : string;
}
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
status "Connected to %s" printable_name;
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
+ let links = readlink_wrapper g dir names stats in
names, stats, links
) in
assert (
debug "opening disk image %s" (string_of_images images);
close_g ();
- let g' = new Guestfs.guestfs () in
+ let g' = new G.guestfs () in
g := Some g';
let g = g' in
let roots =
try Array.to_list (g#inspect_os ())
with
- Guestfs.Error msg ->
+ G.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
- }
+ 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;
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
+(* For each entry which is a symlink, read the destination of the
+ * symlink. This is non-trivial because on Windows we cannot use
+ * readlink but need to instead parse the reparse data from NTFS.
+ *)
+and readlink_wrapper g dir names stats =
+ (* Is the directory on an NTFS filesystem? *)
+ let dev = get_mounted_device g dir in
+ if g#vfs_type dev <> "ntfs" then (
+ (* Not NTFS, use the fast g#readlinklist method. *)
+ let rec 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
+ in
+ readlinklist_wrapper g dir names
+ )
+ else (
+ (* NTFS: look up each symlink individually. *)
+ List.map (
+ fun (name, stat) ->
+ if not (is_symlink stat.G.mode) then ""
+ else
+ let path = if dir = "/" then dir ^ name else dir ^ "/" ^ name in
+ try
+ let _, display = get_ntfs_reparse_data g path in
+ display
+ with exn ->
+ debug "get_ntfs_reparse_data: %s: failed: %s"
+ path (Printexc.to_string exn);
+ "?"
+ ) (List.combine names stats)
+ )
+
+(* See:
+ * https://bugzilla.redhat.com/show_bug.cgi?id=663407
+ * http://git.annexia.org/?p=libguestfs.git;a=commit;h=3a3836b933b80c4f9f2c767fda4f8b459f998db2
+ * http://www.tuxera.com/community/ntfs-3g-advanced/junction-points-and-symbolic-links/
+ * http://www.tuxera.com/community/ntfs-3g-advanced/extended-attributes/
+ * http://www.codeproject.com/KB/winsdk/junctionpoints.aspx
+ *)
+and get_ntfs_reparse_data g path =
+ let data = g#lgetxattr path "system.ntfs_reparse_data" in
+ let link, display =
+ bitmatch Bitstring.bitstring_of_string data with
+ (* IO_REPARSE_TAG_MOUNT_POINT *)
+ | { 0xa0000003_l : 32 : littleendian;
+ _ : 16 : littleendian; (* data length - ignore it *)
+ _ : 16 : littleendian; (* reserved *)
+ link_offset : 16 : littleendian;
+ link_len : 16 : littleendian;
+ display_offset : 16 : littleendian;
+ display_len : 16 : littleendian;
+ link : link_len * 8 :
+ string, offset (8 * (link_offset + 0x10));
+ display : display_len * 8 :
+ string, offset (8 * (display_offset + 0x10)) } ->
+ (* These strings should always be valid UTF16LE, but the caller
+ * is prepared to catch any exception if this fails.
+ *)
+ let link = windows_string_to_utf8 link in
+ let display = windows_string_to_utf8 display in
+ link, display
+ | { 0xa0000003_l : 32 : littleendian } ->
+ invalid_arg (
+ sprintf "%s: could not parse IO_REPARSE_TAG_MOUNT_POINT data" path
+ )
+
+ (* IO_REPARSE_TAG_SYMLINK *)
+ | { 0xa000000c_l : 32 : littleendian;
+ _ : 16 : littleendian; (* data length - ignore it *)
+ _ : 16 : littleendian; (* reserved *)
+ link_offset : 16 : littleendian;
+ link_len : 16 : littleendian;
+ display_offset : 16 : littleendian;
+ display_len : 16 : littleendian;
+ link : link_len * 8 :
+ string, offset (8 * (link_offset + 0x14));
+ display : display_len * 8 :
+ string, offset (8 * (display_offset + 0x14)) } ->
+ let link = windows_string_to_utf8 link in
+ let display = windows_string_to_utf8 display in
+ link, display
+ | { 0xa000000c_l : 32 : littleendian } ->
+ invalid_arg (
+ sprintf "%s: could not parse IO_REPARSE_TAG_SYMLINK data" path
+ )
+
+ | { i : 32 : littleendian } ->
+ invalid_arg (
+ sprintf "%s: reparse data of type 0x%lx is not supported" path i
+ )
+ | { _ } ->
+ invalid_arg (sprintf "%s: reparse data is too short" path) in
+
+ link, display
+
+(* Given a path which is located somewhere on a mountpoint, return the
+ * device name. This works by using g#mountpoints and then looking for
+ * the mount path with the longest match.
+ *)
+and get_mounted_device g path =
+ let mps = g#mountpoints () in
+ let mps = List.map (
+ fun (dev, mp) ->
+ if String.starts_with path mp then dev, String.length mp else dev, 0
+ ) mps in
+ let cmp (_,n1) (_,n2) = compare n2 n1 in
+ let mps = List.sort ~cmp mps in
+ match mps with
+ | [] ->
+ invalid_arg (sprintf "%s: not mounted" path)
+ | (_,0) :: _ ->
+ invalid_arg (sprintf "%s: not found on any filesystem" path)
+ | (dev,_) :: _ -> dev
(* Start up one slave thread. *)
let slave_thread = Thread.create loop ()