[Libguestfs] [PATCH 1/4] v2v: Pass output object into the conversion module.

Richard W.M. Jones rjones at redhat.com
Wed Feb 22 18:21:34 UTC 2017


This is a simpler version of patch #1 which uses subtyping to enforce
at compile time that the conversion module can only call a whitelist
of methods on the output object.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html
-------------- next part --------------
>From 56ebec7c2d4f1581f05d6b2d29971ff0f1816721 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Wed, 22 Feb 2017 17:06:16 +0000
Subject: [PATCH] v2v: Pass output object into the conversion module.

Previously the Convert_linux conversion module depended on one feature
of the output module (#keep_serial_console).  This was extracted in an
ad-hoc way from the output module and passed as an extra parameter to
the conversion module.

Instead of doing it this way, just pass the output module into the
conversion module, so it can call output#keep_serial_console itself.

This is just a simplification of the existing code, but otherwise adds
no new features.
---
 v2v/convert_linux.ml   | 4 ++--
 v2v/convert_windows.ml | 2 +-
 v2v/modules_list.ml    | 5 +++--
 v2v/modules_list.mli   | 7 ++++---
 v2v/types.ml           | 3 +++
 v2v/types.mli          | 5 +++++
 v2v/v2v.ml             | 8 ++++----
 7 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/v2v/convert_linux.ml b/v2v/convert_linux.ml
index 7c42791..7d65516 100644
--- a/v2v/convert_linux.ml
+++ b/v2v/convert_linux.ml
@@ -38,7 +38,7 @@ open Linux_kernels
 module G = Guestfs
 
 (* The conversion function. *)
-let rec convert ~keep_serial_console (g : G.guestfs) inspect source rcaps =
+let rec convert (g : G.guestfs) inspect source output rcaps =
   (*----------------------------------------------------------------------*)
   (* Inspect the guest first.  We already did some basic inspection in
    * the common v2v.ml code, but that has to deal with generic guests
@@ -1005,7 +1005,7 @@ let rec convert ~keep_serial_console (g : G.guestfs) inspect source rcaps =
 
   let kernel, virtio = configure_kernel () in
 
-  if keep_serial_console then (
+  if output#keep_serial_console then (
     configure_console ();
     bootloader#configure_console ();
   ) else (
diff --git a/v2v/convert_windows.ml b/v2v/convert_windows.ml
index e259c22..36f47c8 100644
--- a/v2v/convert_windows.ml
+++ b/v2v/convert_windows.ml
@@ -37,7 +37,7 @@ module G = Guestfs
  * time the Windows VM is booted on KVM.
  *)
 
-let convert ~keep_serial_console (g : G.guestfs) inspect source rcaps =
+let convert (g : G.guestfs) inspect source output rcaps =
   (* Get the data directory. *)
   let virt_tools_data_dir =
     try Sys.getenv "VIRT_TOOLS_DATA_DIR"
diff --git a/v2v/modules_list.ml b/v2v/modules_list.ml
index 13dfee2..3ee0bd7 100644
--- a/v2v/modules_list.ml
+++ b/v2v/modules_list.ml
@@ -27,9 +27,10 @@ and register_output_module name = push_front name output_modules
 let input_modules () = List.sort compare !input_modules
 and output_modules () = List.sort compare !output_modules
 
+type inspection_fn = Types.inspect -> bool
+
 type conversion_fn =
-  keep_serial_console:bool ->
-  Guestfs.guestfs -> Types.inspect -> Types.source ->
+  Guestfs.guestfs -> Types.inspect -> Types.source -> Types.output_settings ->
   Types.requested_guestcaps -> Types.guestcaps
 
 let convert_modules = ref []
diff --git a/v2v/modules_list.mli b/v2v/modules_list.mli
index 5db9227..83fdee3 100644
--- a/v2v/modules_list.mli
+++ b/v2v/modules_list.mli
@@ -30,12 +30,13 @@ val input_modules : unit -> string list
 val output_modules : unit -> string list
 (** Return the list of output modules. *)
 
+type inspection_fn = Types.inspect -> bool
+
 type conversion_fn =
-  keep_serial_console:bool ->
-  Guestfs.guestfs -> Types.inspect -> Types.source ->
+  Guestfs.guestfs -> Types.inspect -> Types.source -> Types.output_settings ->
   Types.requested_guestcaps -> Types.guestcaps
 
-val register_convert_module : (Types.inspect -> bool) -> string -> conversion_fn -> unit
+val register_convert_module : inspection_fn -> string -> conversion_fn -> unit
 (** [register_convert_module inspect_fn name fn] registers a
     conversion function [fn] that can accept any guest that matches
     the [inspect_fn] function. *)
diff --git a/v2v/types.ml b/v2v/types.ml
index 9d94dca..1f424a7 100644
--- a/v2v/types.ml
+++ b/v2v/types.ml
@@ -487,3 +487,6 @@ class virtual output = object
   method virtual create_metadata : source -> target list -> target_buses -> guestcaps -> inspect -> target_firmware -> unit
   method keep_serial_console = true
 end
+
+type output_settings = < keep_serial_console : bool;
+                         install_rhev_apt : bool >
diff --git a/v2v/types.mli b/v2v/types.mli
index 48e47b0..ce0212d 100644
--- a/v2v/types.mli
+++ b/v2v/types.mli
@@ -373,3 +373,8 @@ class virtual output : object
   (** Whether this output supports serial consoles (RHV does not). *)
 end
 (** Encapsulates all [-o], etc output arguments as an object. *)
+
+type output_settings = < keep_serial_console : bool;
+                         install_rhev_apt : bool >
+(** This is a subtype of {!output} containing only the settings
+    which have an influence over conversion modules. *)
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index ee00d2e..8514cca 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -103,7 +103,6 @@ let rec main () =
 
   (* Conversion. *)
   let guestcaps =
-    let keep_serial_console = output#keep_serial_console in
     let rcaps =
       match conversion_mode with
       | Copying _ ->
@@ -111,7 +110,7 @@ let rec main () =
       | In_place ->
          rcaps_from_source source in
 
-    do_convert g inspect source keep_serial_console rcaps in
+    do_convert g inspect source output rcaps in
 
   g#umount_all ();
 
@@ -549,7 +548,7 @@ and check_target_free_space mpstats source targets output =
   output#check_target_free_space source targets
 
 (* Conversion. *)
-and do_convert g inspect source keep_serial_console rcaps =
+and do_convert g inspect source output rcaps =
   (match inspect.i_product_name with
   | "unknown" ->
     message (f_"Converting the guest to run on KVM")
@@ -564,7 +563,8 @@ and do_convert g inspect source keep_serial_console rcaps =
         inspect.i_type inspect.i_distro in
   debug "picked conversion module %s" conversion_name;
   debug "requested caps: %s" (string_of_requested_guestcaps rcaps);
-  let guestcaps = convert ~keep_serial_console g inspect source rcaps in
+  let guestcaps =
+    convert g inspect source (output :> Types.output_settings) rcaps in
   debug "%s" (string_of_guestcaps guestcaps);
 
   (* Did we manage to install virtio drivers? *)
-- 
2.9.3



More information about the Libguestfs mailing list