Skip to content

Commit

Permalink
v2v: Move the with_hive function to the Windows module.
Browse files Browse the repository at this point in the history
Just refactoring.
  • Loading branch information
rwmjones committed Nov 15, 2015
1 parent 8cb6600 commit 40701b7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
43 changes: 17 additions & 26 deletions v2v/convert_windows.ml
Expand Up @@ -39,8 +39,6 @@ module G = Guestfs
* time the Windows VM is booted on KVM.
*)

type ('a, 'b) maybe = Either of 'a | Or of 'b

let convert ~keep_serial_console (g : G.guestfs) inspect source =
(* Get the data directory. *)
let virt_tools_data_dir =
Expand All @@ -67,33 +65,24 @@ let convert ~keep_serial_console (g : G.guestfs) inspect source =
rhev_apt_exe msg;
None in

(* Get the Windows %systemroot%. *)
let systemroot = g#inspect_get_windows_systemroot inspect.i_root in

(* This is a wrapper that handles opening and closing the hive
* properly around a function [f]. If [~write] is [true] then the
* hive is opened for writing and committed at the end if the
* function returned without error.
*)
let rec with_hive name ~write f =
let filename = sprintf "%s/system32/config/%s" systemroot name in
(* Get the software and system hive files. *)
let software_hive_filename =
let filename = sprintf "%s/system32/config/software" systemroot in
let filename = g#case_sensitive_path filename in
let verbose = verbose () in
g#hivex_open ~write ~verbose (* ~debug:verbose *) filename;
let r =
try
let root = g#hivex_root () in
let ret = f root in
if write then g#hivex_commit None;
Either ret
with exn ->
Or exn in
g#hivex_close ();
match r with Either ret -> ret | Or exn -> raise exn
filename in

let system_hive_filename =
let filename = sprintf "%s/system32/config/system" systemroot in
let filename = g#case_sensitive_path filename in
filename in

(* Find the given node in the current hive, relative to the starting
* point. Raises [Not_found] if the node is not found.
*)
and get_node node = function
let rec get_node node = function
| [] -> node
| x :: xs ->
let node = g#hivex_node_get_child node x in
Expand Down Expand Up @@ -135,7 +124,8 @@ let convert ~keep_serial_console (g : G.guestfs) inspect source =
with
Not_found -> false
in
with_hive "software" ~write:false check_group_policy in
Windows.with_hive g software_hive_filename ~write:false
check_group_policy in

(* Warn if Windows guest has AV installed. *)
let has_antivirus = Windows.detect_antivirus inspect in
Expand Down Expand Up @@ -179,7 +169,8 @@ let convert ~keep_serial_console (g : G.guestfs) inspect source =
with
Not_found -> None
in
with_hive "software" ~write:false find_xenpv_uninst in
Windows.with_hive g software_hive_filename ~write:false
find_xenpv_uninst in

(*----------------------------------------------------------------------*)
(* Perform the conversion of the Windows guest. *)
Expand Down Expand Up @@ -694,10 +685,10 @@ echo uninstalling Xen PV driver

(* Open the system hive and update it. *)
let block_driver, net_driver, video_driver =
with_hive "system" ~write:true update_system_hive in
Windows.with_hive g system_hive_filename ~write:true update_system_hive in

(* Open the software hive and update it. *)
with_hive "software" ~write:true update_software_hive;
Windows.with_hive g software_hive_filename ~write:true update_software_hive;

fix_ntfs_heads ();

Expand Down
21 changes: 21 additions & 0 deletions v2v/windows.ml
Expand Up @@ -173,3 +173,24 @@ and virtio_iso_path_matches_guest_os path inspect =
module UNIT_TESTS = struct
let virtio_iso_path_matches_guest_os = virtio_iso_path_matches_guest_os
end

(* This is a wrapper that handles opening and closing the hive
* properly around a function [f]. If [~write] is [true] then the
* hive is opened for writing and committed at the end if the
* function returned without error.
*)
type ('a, 'b) maybe = Either of 'a | Or of 'b

let with_hive (g : Guestfs.guestfs) hive_filename ~write f =
let verbose = verbose () in
g#hivex_open ~write ~verbose (* ~debug:verbose *) hive_filename;
let r =
try
let root = g#hivex_root () in
let ret = f root in
if write then g#hivex_commit None;
Either ret
with exn ->
Or exn in
g#hivex_close ();
match r with Either ret -> ret | Or exn -> raise exn
6 changes: 6 additions & 0 deletions v2v/windows.mli
Expand Up @@ -29,6 +29,12 @@ val copy_virtio_drivers : Guestfs.guestfs -> Types.inspect -> string -> string -
drivers were copied, or [false] if no suitable drivers were
found. *)

val with_hive : Guestfs.guestfs -> string -> write:bool -> (int64 -> 'a) -> 'a
(** This is a wrapper that handles opening and closing the hive
properly around a function [f root]. If [~write] is [true] then
the hive is opened for writing and committed at the end if the
function returned without error. *)

(**/**)

(* The following function is only exported for unit tests. *)
Expand Down

0 comments on commit 40701b7

Please sign in to comment.