[Libguestfs] [v2v PATCH] convert: warn about "--mac" options that don't match any source NICs

Laszlo Ersek lersek at redhat.com
Sun Apr 10 13:49:51 UTC 2022


Just before we call "Networks.map", collect all "--mac" references (from
"options.network_map" and "options.static_ips"), and check each against
the list of MACs available on the source ("source.s_nics"). Collect and
report unresolved references.

The algorithm added here has quadratic time complexity, kind of undoing
the sophistication of the "Networks.t" type -- but I don't think we expect
a huge number of NICs / MACs.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1685809
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 lib/networks.mli   |  3 +++
 convert/convert.ml | 19 +++++++++++++++++++
 lib/networks.ml    |  2 ++
 3 files changed, 24 insertions(+)

diff --git a/lib/networks.mli b/lib/networks.mli
index 8800e21cbcf0..d1a62bf0cf2e 100644
--- a/lib/networks.mli
+++ b/lib/networks.mli
@@ -55,3 +55,6 @@ val map : t -> Types.source_nic -> Types.source_nic
     MAC address mappings take precedence, followed by network
     and bridge mappings if no MAC address mapping for the NIC can
     be found. *)
+
+val macs : t -> string list
+(** Return all MAC addresses for which address mappings have been added. *)
diff --git a/convert/convert.ml b/convert/convert.ml
index 87fca7252ba3..1e3db6b99780 100644
--- a/convert/convert.ml
+++ b/convert/convert.ml
@@ -47,6 +47,25 @@ type mpstat = {
 }
 
 let rec convert dir options source =
+  let nic_macs = List.filter_map
+                   (fun { s_mac } ->
+                    match s_mac with
+                    | None -> None
+                    | Some mac -> Some (String.lowercase_ascii mac))
+                   source.s_nics
+  and mac_refs1 = Networks.macs options.network_map
+  and mac_refs2 = List.map
+                    (fun { if_mac_addr } -> String.lowercase_ascii if_mac_addr)
+                    options.static_ips in
+  let unresolved_mac_refs =
+    List.filter (fun mac_ref -> not (List.mem mac_ref nic_macs))
+      (mac_refs1 @ mac_refs2) in
+  if unresolved_mac_refs <> [] then (
+    let mac_list = String.concat ", " unresolved_mac_refs in
+    warning (f_"The following --mac addresses do not match any NICs from the \
+                source: %s") mac_list
+  );
+
   let target_nics = List.map (Networks.map options.network_map) source.s_nics in
 
   message (f_"Opening the source");
diff --git a/lib/networks.ml b/lib/networks.ml
index 93250fe40ab0..52646166e496 100644
--- a/lib/networks.ml
+++ b/lib/networks.ml
@@ -103,3 +103,5 @@ let add_default_bridge t o =
   if t.default_bridge <> None then
     error (f_"duplicate -b/--bridge parameter.  Only one default mapping is allowed.");
   t.default_bridge <- Some o
+
+let macs t = StringMap.keys t.macs
-- 
2.19.1.3.g30247aa5d201



More information about the Libguestfs mailing list