No change, just code refactoring.
---
v2v/Makefile.am | 2 ++
v2v/cmdline.ml | 47 +++++++++++----------------
v2v/cmdline.mli | 16 +--------
v2v/networks.ml | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
v2v/networks.mli | 48 +++++++++++++++++++++++++++
v2v/v2v.ml | 19 +----------
6 files changed, 155 insertions(+), 61 deletions(-)
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 335e1a59d..adf48eca7 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -65,6 +65,7 @@ SOURCES_MLI = \
linux_kernels.mli \
modules_list.mli \
name_from_disk.mli \
+ networks.mli \
output_glance.mli \
output_libvirt.mli \
output_local.mli \
@@ -136,6 +137,7 @@ SOURCES_ML = \
output_vdsm.ml \
inspect_source.ml \
target_bus_assignment.ml \
+ networks.ml \
cmdline.ml \
v2v.ml
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index eaa11dba0..218200a12 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -28,18 +28,12 @@ open Getopt.OptionName
open Types
open Utils
-module NetTypeAndName = struct
- type t = Types.vnet_type * string option
- let compare = Pervasives.compare
-end
-module NetworkMap = Map.Make (NetTypeAndName)
-
type cmdline = {
compressed : bool;
debug_overlays : bool;
do_copy : bool;
in_place : bool;
- network_map : string NetworkMap.t;
+ network_map : Networks.t;
output_alloc : output_allocation;
output_format : string option;
output_name : string option;
@@ -99,26 +93,24 @@ let parse_cmdline () =
set_input_option_compat k v
in
- let network_map = ref NetworkMap.empty in
- let add_network, add_bridge =
- let add flag name t str =
- match String.split ":" str with
- | "", "" ->
- error (f_"invalid %s parameter") flag
- | out, "" | "", out ->
- let key = t, None in
- if NetworkMap.mem key !network_map then
- error (f_"duplicate %s parameter. Only one default mapping is
allowed.") flag;
- network_map := NetworkMap.add key out !network_map
- | in_, out ->
- let key = t, Some in_ in
- if NetworkMap.mem key !network_map then
- error (f_"duplicate %s parameter. Duplicate mappings specified for %s
ā%sā.") flag name in_;
- network_map := NetworkMap.add key out !network_map
- in
- let add_network str = add "-n/--network" (s_"network") Network
str
- and add_bridge str = add "-b/--bridge" (s_"bridge") Bridge str
in
- add_network, add_bridge
+ let network_map = Networks.create () in
+ let add_network str =
+ match String.split ":" str with
+ | "", "" ->
+ error (f_"invalid -n/--network parameter")
+ | out, "" | "", out ->
+ Networks.add_default_network network_map out
+ | in_, out ->
+ Networks.add_network network_map in_ out
+ in
+ let add_bridge str =
+ match String.split ":" str with
+ | "", "" ->
+ error (f_"invalid -b/--bridge parameter")
+ | out, "" | "", out ->
+ Networks.add_default_bridge network_map out
+ | in_, out ->
+ Networks.add_bridge network_map in_ out
in
let no_trim_warning _ =
@@ -316,7 +308,6 @@ read the man page virt-v2v(1).
error (f_"unknown input transport ā-it %sā") transport in
let in_place = !in_place in
let machine_readable = !machine_readable in
- let network_map = !network_map in
let output_alloc =
match !output_alloc with
| `Not_set | `Sparse -> Sparse
diff --git a/v2v/cmdline.mli b/v2v/cmdline.mli
index 0e1d54f40..87d36bf5d 100644
--- a/v2v/cmdline.mli
+++ b/v2v/cmdline.mli
@@ -18,26 +18,12 @@
(** Command line argument parsing. *)
-module NetTypeAndName : sig
- type t = Types.vnet_type * string option
- (** To find the mapping for a specific named network or bridge, use
- the key [(Network|Bridge, Some name)]. To find the default mapping
- use [(Network|Bridge, None)]. *)
- val compare : t -> t -> int
-end
-module NetworkMap : sig
- type key = NetTypeAndName.t
- type 'a t = 'a Map.Make(NetTypeAndName).t
- val mem : key -> 'a t -> bool
- val find : key -> 'a t -> 'a
-end
-
type cmdline = {
compressed : bool;
debug_overlays : bool;
do_copy : bool;
in_place : bool;
- network_map : string NetworkMap.t;
+ network_map : Networks.t;
output_alloc : Types.output_allocation;
output_format : string option;
output_name : string option;
diff --git a/v2v/networks.ml b/v2v/networks.ml
new file mode 100644
index 000000000..f6da77704
--- /dev/null
+++ b/v2v/networks.ml
@@ -0,0 +1,84 @@
+(* virt-v2v
+ * Copyright (C) 2009-2018 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Network, bridge mapping. *)
+
+open Tools_utils
+open Common_gettext.Gettext
+
+open Types
+
+type t = {
+ (* For networks we use this to map a named network, or use the
+ * default network if no named network exists.
+ *)
+ mutable network_map : string StringMap.t;
+ mutable default_network : string option;
+
+ (* Same as above but for bridges. *)
+ mutable bridge_map : string StringMap.t;
+ mutable default_bridge : string option;
+}
+
+let map t nic =
+ match nic.s_vnet_type with
+ | Network ->
+ (try
+ let vnet = StringMap.find nic.s_vnet t.network_map in
+ { nic with 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 }
+ )
+ | Bridge ->
+ (try
+ let vnet = StringMap.find nic.s_vnet t.bridge_map in
+ { nic with 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 }
+ )
+
+let create () = {
+ network_map = StringMap.empty;
+ default_network = None;
+ bridge_map = StringMap.empty;
+ default_bridge = None
+}
+
+let add_network t i o =
+ if StringMap.mem i t.network_map then
+ error (f_"duplicate -n/--network parameter. Duplicate mappings specified for
network %s.") i;
+ t.network_map <- StringMap.add i o t.network_map
+
+let add_default_network t o =
+ if t.default_network <> None then
+ error (f_"duplicate -n/--network parameter. Only one default mapping is
allowed.");
+ t.default_network <- Some o
+
+let add_bridge t i o =
+ if StringMap.mem i t.bridge_map then
+ error (f_"duplicate -b/--bridge parameter. Duplicate mappings specified for
bridge %s.") i;
+ t.bridge_map <- StringMap.add i o t.bridge_map
+
+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
diff --git a/v2v/networks.mli b/v2v/networks.mli
new file mode 100644
index 000000000..5090080ac
--- /dev/null
+++ b/v2v/networks.mli
@@ -0,0 +1,48 @@
+(* virt-v2v
+ * Copyright (C) 2009-2018 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(** Network, bridge mapping. *)
+
+type t (** The map. *)
+
+val create : unit -> t
+(** Create an empty mapping. *)
+
+val add_network : t -> string -> string -> unit
+(** Add a network mapping from C<in> to C<out>.
+
+ Equivalent to the [--network in:out] option. *)
+
+val add_default_network : t -> string -> unit
+(** Add a default network mapping.
+
+ Equivalent to the [--network out] option. *)
+
+val add_bridge : t -> string -> string -> unit
+(** Add a bridge mapping from C<in> to C<out>.
+
+ Equivalent to the [--bridge in:out] option. *)
+
+val add_default_bridge : t -> string -> unit
+(** Add a default bridge mapping.
+
+ Equivalent to the [--bridge out] option. *)
+
+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. *)
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index 75f60c9c3..21eba4d55 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -256,24 +256,7 @@ and set_source_name cmdline source =
(* Map networks and bridges. *)
and set_source_networks_and_bridges cmdline source =
- 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 = NetworkMap.find (t, Some vnet) cmdline.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 = NetworkMap.find (t, None) cmdline.network_map in
- { nic with s_vnet = new_name }
- with Not_found ->
- (* Not found, so return the original NIC unchanged. *)
- nic
- ) source.s_nics in
-
+ let nics = List.map (Networks.map cmdline.network_map) source.s_nics in
{ source with s_nics = nics }
and overlay_dir = (open_guestfs ())#get_cachedir ()
--
2.17.1