[Libguestfs] [PATCH v2 02/17] v2v: factor out opening input VM

Roman Kagan rkagan at virtuozzo.com
Tue Aug 11 17:00:21 UTC 2015


Opening the source VM and amending the properties in its internal
representation in accordance with command-line options fit nicely into
two isolated functions.

Signed-off-by: Roman Kagan <rkagan at virtuozzo.com>
---
 v2v/v2v.ml | 71 +++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 38 insertions(+), 33 deletions(-)

diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index 9cb4a27..a2cf249 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -43,21 +43,7 @@ let print_mpstat chan { mp_dev = dev; mp_path = path;
 
 let () = Random.self_init ()
 
-let rec main () =
-  (* Handle the command line. *)
-  let input, output,
-    debug_gc, debug_overlays, do_copy, network_map, no_trim,
-    output_alloc, output_format, output_name, print_source, root_choice =
-    Cmdline.parse_cmdline () in
-
-  (* Print the version, easier than asking users to tell us. *)
-  if verbose () then
-    printf "%s: %s %s (%s)\n%!"
-      prog Config.package_name Config.package_version Config.host_cpu;
-
-  if debug_gc then
-    at_exit (fun () -> Gc.compact());
-
+let open_source input print_source =
   message (f_"Opening the source %s") input#as_options;
   let source = input#source () in
 
@@ -68,7 +54,6 @@ let rec main () =
     printf "%s\n" (string_of_source source);
     exit 0
   );
-
   if verbose () then printf "%s%!" (string_of_source source);
 
   (match source.s_hypervisor with
@@ -87,6 +72,9 @@ let rec main () =
       assert (disk.s_qemu_uri <> "");
   ) source.s_disks;
 
+  source
+
+let amend_source source output_name network_map =
   (* Map source name. *)
   let source =
     match output_name with
@@ -97,26 +85,43 @@ let rec main () =
     | Some name -> { source with s_name = name } in
 
   (* Map networks and bridges. *)
-  let source =
-    let { s_nics = nics } = source in
-    let nics = List.map (
-      fun ({ s_vnet_type = t; s_vnet = vnet } as nic) ->
+  let nics = List.map (
+    fun ({ s_vnet_type = t; s_vnet = vnet } as nic) ->
+      try
+        (* Look for a --network or --bridge parameter which names this
+         * network/bridge (eg. --network in:out).
+         *)
+        let new_name = List.assoc (t, vnet) network_map in
+        { nic with s_vnet = new_name }
+      with Not_found ->
         try
-          (* Look for a --network or --bridge parameter which names this
-           * network/bridge (eg. --network in:out).
-           *)
-          let new_name = List.assoc (t, vnet) network_map in
+          (* Not found, so look for a default mapping (eg. --network out). *)
+          let new_name = List.assoc (t, "") network_map in
           { nic with s_vnet = new_name }
         with Not_found ->
-          try
-            (* Not found, so look for a default mapping (eg. --network out). *)
-            let new_name = List.assoc (t, "") network_map in
-            { nic with s_vnet = new_name }
-          with Not_found ->
-            (* Not found, so return the original NIC unchanged. *)
-            nic
-    ) nics in
-    { source with s_nics = nics } in
+          (* Not found, so return the original NIC unchanged. *)
+          nic
+  ) source.s_nics in
+
+  { source with s_nics = nics }
+
+let rec main () =
+  (* Handle the command line. *)
+  let input, output,
+    debug_gc, debug_overlays, do_copy, network_map, no_trim,
+    output_alloc, output_format, output_name, print_source, root_choice =
+    Cmdline.parse_cmdline () in
+
+  (* Print the version, easier than asking users to tell us. *)
+  if verbose () then
+    printf "%s: %s %s (%s)\n%!"
+      prog Config.package_name Config.package_version Config.host_cpu;
+
+  if debug_gc then
+    at_exit (fun () -> Gc.compact());
+
+  let source = open_source input print_source in
+  let source = amend_source source output_name network_map in
 
   (* Create a qcow2 v3 overlay to protect the source image(s).  There
    * is a specific reason to use the newer qcow2 variant: Because the
-- 
2.4.3




More information about the Libguestfs mailing list