Blob Blame History Raw
From 179e2fe32705ecc34759630f72dcfdbcf076e526 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Mon, 20 Oct 2014 18:02:57 +0100
Subject: [PATCH] v2v: -i libvirt: Refactor map_source* functions.

Get rid of the awkward map_source* functions, and replace it with
equivalent code which modifies the source disks objects coming back
from Input_libvirtxml.parse_libvirt_xml.

This is just code refactoring.  Apart from the order in which certain
tests are done, this should be equivalent to the previous code.

(cherry picked from commit 3596165282ccf2c5896894ec4e9a71c6da788463)
---
 v2v/input_libvirt.ml     | 32 +++++++++++++++++++++++---------
 v2v/input_libvirtxml.ml  | 31 ++++++++++++-------------------
 v2v/input_libvirtxml.mli | 11 +----------
 3 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/v2v/input_libvirt.ml b/v2v/input_libvirt.ml
index e8b1345..9d2869f 100644
--- a/v2v/input_libvirt.ml
+++ b/v2v/input_libvirt.ml
@@ -82,16 +82,23 @@ object
   method source () =
     if verbose then printf "input_libvirt_vcenter_https: source()\n%!";
 
+    error_if_libvirt_backend ();
+
     (* Get the libvirt XML.  This also checks (as a side-effect)
      * that the domain is not running.  (RHBZ#1138586)
      *)
     let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in
-
-    error_if_libvirt_backend ();
+    let { s_disks = disks } as source =
+      Input_libvirtxml.parse_libvirt_xml ~verbose xml in
 
     let mapf = VCenter.map_path_to_uri verbose parsed_uri scheme server in
-    Input_libvirtxml.parse_libvirt_xml ~verbose
-      ~map_source_file:mapf ~map_source_dev:mapf xml
+    let disks = List.map (
+      fun ({ s_qemu_uri = uri; s_format = format } as disk) ->
+        let uri, format = mapf uri format in
+        { disk with s_qemu_uri = uri; s_format = format }
+    ) disks in
+
+    { source with s_disks = disks }
 end
 
 (* Subclass specialized for handling Xen over SSH. *)
@@ -103,17 +110,24 @@ object
   method source () =
     if verbose then printf "input_libvirt_xen_ssh: source()\n%!";
 
+    error_if_libvirt_backend ();
+    error_if_no_ssh_agent ();
+
     (* Get the libvirt XML.  This also checks (as a side-effect)
      * that the domain is not running.  (RHBZ#1138586)
      *)
     let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in
-
-    error_if_libvirt_backend ();
-    error_if_no_ssh_agent ();
+    let { s_disks = disks } as source =
+      Input_libvirtxml.parse_libvirt_xml ~verbose xml in
 
     let mapf = Xen.map_path_to_uri verbose parsed_uri scheme server in
-    Input_libvirtxml.parse_libvirt_xml ~verbose
-      ~map_source_file:mapf ~map_source_dev:mapf xml
+    let disks = List.map (
+      fun ({ s_qemu_uri = uri; s_format = format } as disk) ->
+        let uri, format = mapf uri format in
+        { disk with s_qemu_uri = uri; s_format = format }
+    ) disks in
+
+    { source with s_disks = disks }
 end
 
 (* Choose the right subclass based on the URI. *)
diff --git a/v2v/input_libvirtxml.ml b/v2v/input_libvirtxml.ml
index 984db18..3b19685 100644
--- a/v2v/input_libvirtxml.ml
+++ b/v2v/input_libvirtxml.ml
@@ -24,13 +24,7 @@ open Common_utils
 open Types
 open Utils
 
-type map_source = string -> string option -> string * string option
-
-let no_map : map_source = fun x y -> x, y
-
-let parse_libvirt_xml ~verbose
-    ?(map_source_file = no_map) ?(map_source_dev = no_map)
-    xml =
+let parse_libvirt_xml ~verbose xml =
   if verbose then
     printf "libvirt xml is:\n%s\n" xml;
 
@@ -144,16 +138,12 @@ let parse_libvirt_xml ~verbose
       match xpath_to_string "@type" "" with
       | "block" ->
         let path = xpath_to_string "source/@dev" "" in
-        if path <> "" then (
-          let path, format = map_source_dev path format in
+        if path <> "" then
           add_disk path format target_dev
-        )
       | "file" ->
         let path = xpath_to_string "source/@file" "" in
-        if path <> "" then (
-          let path, format = map_source_file path format in
+        if path <> "" then
           add_disk path format target_dev
-        )
       | "network" ->
         (* We only handle <source protocol="nbd"> here, and that is
          * intended only for virt-p2v.  Any other network disk is
@@ -265,6 +255,8 @@ object
   method source () =
     let xml = read_whole_file file in
 
+    let { s_disks = disks } as source = parse_libvirt_xml ~verbose xml in
+
     (* When reading libvirt XML from a file (-i libvirtxml) we allow
      * paths to disk images in the libvirt XML to be relative (to the XML
      * file).  Relative paths are in fact not permitted in real libvirt
@@ -272,13 +264,14 @@ object
      * when writing the XML by hand.
      *)
     let dir = Filename.dirname (absolute_path file) in
-    let map_source_file path format =
-      let path =
-        if not (Filename.is_relative path) then path else dir // path in
-      path, format
-    in
+    let disks = List.map (
+      fun ({ s_qemu_uri = path } as disk) ->
+        let path =
+          if not (Filename.is_relative path) then path else dir // path in
+        { disk with s_qemu_uri = path }
+    ) disks in
 
-    parse_libvirt_xml ~verbose ~map_source_file xml
+    { source with s_disks = disks }
 end
 
 let input_libvirtxml = new input_libvirtxml
diff --git a/v2v/input_libvirtxml.mli b/v2v/input_libvirtxml.mli
index d673150..5c10df0 100644
--- a/v2v/input_libvirtxml.mli
+++ b/v2v/input_libvirtxml.mli
@@ -18,18 +18,9 @@
 
 (** [-i libvirtxml] source. *)
 
-type map_source = string -> string option -> string * string option
-(** Map function that takes [path] and [format] parameters, and
-    returns the possibly rewritten [qemu_uri, format] pair. *)
-
-val parse_libvirt_xml : verbose:bool -> ?map_source_file:map_source -> ?map_source_dev:map_source -> string -> Types.source
+val parse_libvirt_xml : verbose:bool -> string -> Types.source
 (** Take libvirt XML and parse it into a {!Types.source} structure.
 
-    The optional [?map_source_file] and [?map_source_dev] functions
-    are used to map [<source file="..."/>] and [<source dev="..."/>]
-    from the XML into QEMU URIs.  If not given, then an identity
-    mapping is used.
-
     This function is also used by {!Input_libvirt}, hence it is
     exported. *)
 
-- 
1.8.3.1