[Libguestfs] [PATCH 2/3] v2v: Add s_mapping_explanation field to source NIC.

Richard W.M. Jones rjones at redhat.com
Wed Jul 4 12:04:42 UTC 2018


Instead of storing the original network name (s_vnet_orig) in the
source NIC struct and then trying to guess what happened much later
when we're writing target metadata, get our newly created Networks
abstract data type to store the precise mapping explanation.
---
 v2v/create_libvirt_xml.ml                     | 10 +++---
 v2v/create_ovf.ml                             | 10 +++---
 v2v/input_disk.ml                             |  5 +--
 v2v/input_vmx.ml                              |  5 +--
 v2v/networks.ml                               | 32 ++++++++++++++++---
 v2v/networks.mli                              |  5 ++-
 v2v/parse_libvirt_xml.ml                      |  4 +--
 v2v/parse_ovf_from_ova.ml                     |  2 +-
 ...test-v2v-networks-and-bridges-expected.xml |  6 ++--
 v2v/types.ml                                  |  2 +-
 v2v/types.mli                                 |  5 ++-
 11 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/v2v/create_libvirt_xml.ml b/v2v/create_libvirt_xml.ml
index 4b36ffb8e..f5603db99 100644
--- a/v2v/create_libvirt_xml.ml
+++ b/v2v/create_libvirt_xml.ml
@@ -250,7 +250,7 @@ let create_libvirt_xml ?pool source target_buses guestcaps
       | Virtio_net -> "virtio" | E1000 -> "e1000" | RTL8139 -> "rtl8139" in
     List.map (
       fun { s_mac = mac; s_vnet_type = vnet_type;
-            s_vnet = vnet; s_vnet_orig = vnet_orig } ->
+            s_vnet = vnet; s_mapping_explanation = explanation } ->
         let vnet_type_str =
           match vnet_type with
           | Bridge -> "bridge" | Network -> "network" in
@@ -261,11 +261,9 @@ let create_libvirt_xml ?pool source target_buses guestcaps
             e "model" [ "type", net_model ] [];
           ] in
           let children =
-            if vnet_orig <> vnet then
-              Comment (sprintf "%s mapped from \"%s\" to \"%s\""
-                         vnet_type_str vnet_orig vnet) :: children
-            else
-              children in
+            match explanation with
+            | Some explanation -> Comment explanation :: children
+            | None -> children in
           e "interface" [ "type", vnet_type_str ] children in
 
         (match mac with
diff --git a/v2v/create_ovf.ml b/v2v/create_ovf.ml
index 3b866754a..b2a240907 100644
--- a/v2v/create_ovf.ml
+++ b/v2v/create_ovf.ml
@@ -918,7 +918,7 @@ and add_networks nics guestcaps ovf_flavour ovf =
   (* Iterate over the NICs, adding them to the OVF document. *)
   List.iteri (
     fun i { s_mac = mac; s_vnet_type = vnet_type;
-            s_vnet = vnet; s_vnet_orig = vnet_orig } ->
+            s_vnet = vnet; s_mapping_explanation = explanation } ->
       let dev = sprintf "eth%d" i in
 
       let model =
@@ -931,10 +931,10 @@ and add_networks nics guestcaps ovf_flavour ovf =
         bus dev;
         "1" *) in
 
-      if vnet_orig <> vnet then (
-        let c =
-          Comment (sprintf "mapped from \"%s\" to \"%s\"" vnet_orig vnet) in
-        append_child c network_section
+      (match explanation with
+       | None -> ()
+       | Some explanation ->
+          append_child (Comment explanation) network_section
       );
 
       let network = e "Network" ["ovf:name", vnet] [] in
diff --git a/v2v/input_disk.ml b/v2v/input_disk.ml
index 7ecd19fd3..624644532 100644
--- a/v2v/input_disk.ml
+++ b/v2v/input_disk.ml
@@ -72,8 +72,9 @@ class input_disk input_format disk = object
     let network = {
       s_mac = None;
       s_nic_model = None;
-      s_vnet = "default"; s_vnet_orig = "default";
-      s_vnet_type = Network
+      s_vnet = "default";
+      s_vnet_type = Network;
+      s_mapping_explanation = None
     } in
 
     let source = {
diff --git a/v2v/input_vmx.ml b/v2v/input_vmx.ml
index a0b3208f5..1a8015545 100644
--- a/v2v/input_vmx.ml
+++ b/v2v/input_vmx.ml
@@ -372,8 +372,9 @@ and find_nics vmx =
              | Some _ | None -> Network in
            Some (port,
                  { s_mac = mac; s_nic_model = model;
-                   s_vnet = vnet; s_vnet_orig = vnet;
-                   s_vnet_type = vnet_type })
+                   s_vnet = vnet;
+                   s_vnet_type = vnet_type;
+                   s_mapping_explanation = None })
         | _ -> None
     ) nics in
   let nics = List.filter_map identity nics in
diff --git a/v2v/networks.ml b/v2v/networks.ml
index f6da77704..973e193b7 100644
--- a/v2v/networks.ml
+++ b/v2v/networks.ml
@@ -18,6 +18,8 @@
 
 (* Network, bridge mapping. *)
 
+open Printf
+
 open Tools_utils
 open Common_gettext.Gettext
 
@@ -40,20 +42,42 @@ let map t nic =
   | Network ->
      (try
         let vnet = StringMap.find nic.s_vnet t.network_map in
-        { nic with s_vnet = vnet }
+        { nic with
+          s_vnet = vnet;
+          s_mapping_explanation =
+            Some (sprintf "network mapped from %S to %S"
+                          nic.s_vnet vnet)
+        }
       with Not_found ->
            match t.default_network with
            | None -> nic (* no mapping done *)
-           | Some default_network -> { nic with s_vnet = default_network }
+           | Some default_network ->
+              { nic with
+                s_vnet = default_network;
+                s_mapping_explanation =
+                  Some (sprintf "network mapped from %S to default %S"
+                                nic.s_vnet default_network)
+              }
      )
   | Bridge ->
      (try
         let vnet = StringMap.find nic.s_vnet t.bridge_map in
-        { nic with s_vnet = vnet }
+        { nic with
+          s_vnet = vnet;
+          s_mapping_explanation =
+            Some (sprintf "bridge mapped from %S to %S"
+                          nic.s_vnet vnet)
+        }
       with Not_found ->
            match t.default_bridge with
            | None -> nic (* no mapping done *)
-           | Some default_bridge -> { nic with s_vnet = default_bridge }
+           | Some default_bridge ->
+              { nic with
+                s_vnet = default_bridge;
+                s_mapping_explanation =
+                  Some (sprintf "bridge mapped from %S to default %S"
+                                nic.s_vnet default_bridge)
+              }
      )
 
 let create () = {
diff --git a/v2v/networks.mli b/v2v/networks.mli
index 5090080ac..af2e5a302 100644
--- a/v2v/networks.mli
+++ b/v2v/networks.mli
@@ -45,4 +45,7 @@ val add_default_bridge : t -> string -> unit
 
 val map : t -> Types.source_nic -> Types.source_nic
 (** Apply the mapping to the source NIC, returning the updated
-    NIC with possibly modified [s_vnet] field. *)
+    NIC with possibly modified [s_vnet] field.
+
+    [s_mapping_explanation] is set in the output with an
+    informational message about what was done. *)
diff --git a/v2v/parse_libvirt_xml.ml b/v2v/parse_libvirt_xml.ml
index 03a201e77..cf6593043 100644
--- a/v2v/parse_libvirt_xml.ml
+++ b/v2v/parse_libvirt_xml.ml
@@ -445,8 +445,8 @@ let parse_libvirt_xml ?conn xml =
              s_mac = mac;
              s_nic_model = model;
              s_vnet = vnet;
-             s_vnet_orig = vnet;
-             s_vnet_type = vnet_type
+             s_vnet_type = vnet_type;
+             s_mapping_explanation = None
            } in
            List.push_front nic nics
          in
diff --git a/v2v/parse_ovf_from_ova.ml b/v2v/parse_ovf_from_ova.ml
index 7d4f2f543..25b59db3f 100644
--- a/v2v/parse_ovf_from_ova.ml
+++ b/v2v/parse_ovf_from_ova.ml
@@ -238,8 +238,8 @@ and parse_nics xpathctx =
       s_mac = mac;
       s_nic_model = nic_model;
       s_vnet = vnet;
-      s_vnet_orig = vnet;
       s_vnet_type = vnet_type;
+      s_mapping_explanation = None
     } in
     List.push_front nic nics
   done;
diff --git a/v2v/test-v2v-networks-and-bridges-expected.xml b/v2v/test-v2v-networks-and-bridges-expected.xml
index fef9979b0..7d24d990e 100644
--- a/v2v/test-v2v-networks-and-bridges-expected.xml
+++ b/v2v/test-v2v-networks-and-bridges-expected.xml
@@ -3,7 +3,7 @@
       <source bridge='bridge1'/>
     </interface>
     <interface type='bridge'>
-      <!-- bridge mapped from "bob" to "bridge2" -->
+      <!-- bridge mapped from "bob" to default "bridge2" -->
       <source bridge='bridge2'/>
       <mac address='52:54:00:01:02:03'/>
     </interface>
@@ -23,12 +23,12 @@
       <mac address='52:54:00:01:02:06'/>
     </interface>
     <interface type='network'>
-      <!-- network mapped from "george" to "network4" -->
+      <!-- network mapped from "george" to default "network4" -->
       <source network='network4'/>
       <mac address='52:54:00:01:02:07'/>
     </interface>
     <interface type='network'>
-      <!-- network mapped from "ringo" to "network4" -->
+      <!-- network mapped from "ringo" to default "network4" -->
       <source network='network4'/>
       <mac address='52:54:00:01:02:08'/>
     </interface>
diff --git a/v2v/types.ml b/v2v/types.ml
index 5e4dc8dd4..118d0ccf3 100644
--- a/v2v/types.ml
+++ b/v2v/types.ml
@@ -71,8 +71,8 @@ and source_nic = {
   s_mac : string option;
   s_nic_model : s_nic_model option;
   s_vnet : string;
-  s_vnet_orig : string;
   s_vnet_type : vnet_type;
+  s_mapping_explanation : string option;
 }
 and s_nic_model = Source_other_nic of string |
                   Source_rtl8139 | Source_e1000 | Source_virtio_net
diff --git a/v2v/types.mli b/v2v/types.mli
index 041548227..79c0c1021 100644
--- a/v2v/types.mli
+++ b/v2v/types.mli
@@ -130,8 +130,11 @@ and source_nic = {
   s_mac : string option;                (** MAC address. *)
   s_nic_model : s_nic_model option;     (** Network adapter model. *)
   s_vnet : string;                      (** Source network name. *)
-  s_vnet_orig : string;                 (** Original network (if we map it). *)
   s_vnet_type : vnet_type;              (** Source network type. *)
+  s_mapping_explanation : string option;
+  (** If the NIC or network was mapped, this contains an English
+      explanation of the change which can be written to the target
+      hypervisor metadata for informational purposes. *)
 }
 (** Network adapter models. *)
 and s_nic_model = Source_other_nic of string |
-- 
2.17.1




More information about the Libguestfs mailing list