[Libguestfs] [PATCH v2 1/2] v2v: -o glance: Refactor OpenStack image properties into a new module.

Richard W.M. Jones rjones at redhat.com
Thu Aug 30 09:47:56 UTC 2018


Straightforward refactoring.  Apart from small modifications to clean
up the code and reorder the properties more logically there should be
no functional change.
---
 v2v/Makefile.am                    |   2 +
 v2v/openstack_image_properties.ml  | 100 +++++++++++++++++++++++++++++
 v2v/openstack_image_properties.mli |  34 ++++++++++
 v2v/output_glance.ml               |  85 ++++--------------------
 4 files changed, 148 insertions(+), 73 deletions(-)

diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 73836a1be..6e78ec4fb 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -65,6 +65,7 @@ SOURCES_MLI = \
 	modules_list.mli \
 	name_from_disk.mli \
 	networks.mli \
+	openstack_image_properties.mli \
 	output_glance.mli \
 	output_libvirt.mli \
 	output_local.mli \
@@ -112,6 +113,7 @@ SOURCES_ML = \
 	parse_vmx.ml \
 	parse_libvirt_xml.ml \
 	create_libvirt_xml.ml \
+	openstack_image_properties.ml \
 	qemuopts.ml \
 	input_libvirtxml.ml \
 	input_libvirt_other.ml \
diff --git a/v2v/openstack_image_properties.ml b/v2v/openstack_image_properties.ml
new file mode 100644
index 000000000..3f199d75e
--- /dev/null
+++ b/v2v/openstack_image_properties.ml
@@ -0,0 +1,100 @@
+(* 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.
+ *)
+
+(* Convert metadata to a list of OpenStack image properties. *)
+
+open Printf
+
+open Std_utils
+
+open Types
+
+let create source target_buses guestcaps inspect target_firmware =
+  let properties = ref [] in
+
+  List.push_back_list properties [
+    "architecture", guestcaps.gcaps_arch;
+    "hypervisor_type", "kvm";
+    "vm_mode", "hvm";
+
+    "hw_disk_bus",
+    (match guestcaps.gcaps_block_bus with
+     | Virtio_blk -> "virtio"
+     | Virtio_SCSI -> "scsi"
+     | IDE -> "ide");
+    "hw_vif_model",
+    (match guestcaps.gcaps_net_bus with
+     | Virtio_net -> "virtio"
+     | E1000 -> "e1000"
+     | RTL8139 -> "rtl8139");
+    "hw_video_model",
+    (match guestcaps.gcaps_video with
+     | QXL -> "qxl"
+     | Cirrus -> "cirrus");
+    "hw_machine_type",
+    (match guestcaps.gcaps_machine with
+     | I440FX -> "pc"
+     | Q35 -> "q35"
+     | Virt -> "virt");
+
+    "os_type", inspect.i_type;
+    "os_distro",
+    (match inspect.i_distro with
+     (* https://docs.openstack.org/python-glanceclient/latest/cli/property-keys.html *)
+     | "archlinux" -> "arch"
+     | "sles" -> "sled"
+     | x -> x (* everything else is the same in libguestfs and OpenStack*)
+    )
+  ];
+
+  (match source.s_cpu_topology with
+   | None ->
+      List.push_back properties ("hw_cpu_sockets", "1");
+      List.push_back properties ("hw_cpu_cores", string_of_int source.s_vcpu);
+   | Some { s_cpu_sockets = sockets; s_cpu_cores = cores;
+            s_cpu_threads = threads } ->
+      List.push_back properties ("hw_cpu_sockets", string_of_int sockets);
+      List.push_back properties ("hw_cpu_cores", string_of_int cores);
+      List.push_back properties ("hw_cpu_threads", string_of_int threads);
+  );
+
+  (match guestcaps.gcaps_block_bus with
+   | Virtio_SCSI ->
+      List.push_back properties ("hw_scsi_model", "virtio-scsi")
+   | Virtio_blk | IDE -> ()
+  );
+
+  (match inspect.i_major_version, inspect.i_minor_version with
+   | 0, 0 -> ()
+   | x, 0 -> List.push_back properties ("os_version", string_of_int x)
+   | x, y -> List.push_back properties ("os_version", sprintf "%d.%d" x y)
+  );
+
+  if guestcaps.gcaps_virtio_rng then
+    List.push_back properties ("hw_rng_model", "virtio");
+  (* XXX Neither memory balloon nor pvpanic are supported by
+   * Glance at this time.
+   *)
+
+  (match target_firmware with
+   | TargetBIOS -> ()
+   | TargetUEFI ->
+      List.push_back properties ("hw_firmware_type", "uefi")
+  );
+
+  !properties
diff --git a/v2v/openstack_image_properties.mli b/v2v/openstack_image_properties.mli
new file mode 100644
index 000000000..22dadc3a4
--- /dev/null
+++ b/v2v/openstack_image_properties.mli
@@ -0,0 +1,34 @@
+(* 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.
+ *)
+
+(** Convert metadata to a list of OpenStack image properties.
+
+    These properties are suitable for use by Glance or Cinder.
+    Note for Cinder there is a difference between properties and
+    image properties (this module implements the latter). *)
+
+val create : Types.source -> Types.target_buses -> Types.guestcaps -> Types.inspect -> Types.target_firmware -> (string * string) list
+(** [create source target_buses guestcaps inspect target_firmware]
+    translates the metadata into a list of image properties suitable
+    for OpenStack.
+
+    The returned list is a set of key=value pairs which can be passed
+    to Glance (using [--property key=value]) or to Cinder.  For
+    Cinder note that you must not use [--property] since that sets
+    volume properties which are different from image properties.
+    Instead use [openstack volume set --image-property key=value ...]. *)
diff --git a/v2v/output_glance.ml b/v2v/output_glance.ml
index 4fd621d90..7e51d5798 100644
--- a/v2v/output_glance.ml
+++ b/v2v/output_glance.ml
@@ -63,74 +63,20 @@ object
     (* Write targets to a temporary local file - see above for reason. *)
     List.map (fun (_, ov) -> TargetFile (tmpdir // ov.ov_sd)) overlays
 
-  method create_metadata source targets _ guestcaps inspect target_firmware =
-    (* Collect the common properties for all the disks. *)
+  method create_metadata source targets
+                         target_buses guestcaps inspect target_firmware =
     let min_ram = source.s_memory /^ 1024L /^ 1024L in
-    let common_properties =
-      let properties = ref [
-        "hw_disk_bus",
-        (match guestcaps.gcaps_block_bus with
-         | Virtio_blk -> "virtio"
-         | Virtio_SCSI -> "scsi"
-         | IDE -> "ide");
-        "hw_vif_model",
-        (match guestcaps.gcaps_net_bus with
-         | Virtio_net -> "virtio"
-         | E1000 -> "e1000"
-         | RTL8139 -> "rtl8139");
-        "hw_video_model",
-        (match guestcaps.gcaps_video with
-         | QXL -> "qxl"
-         | Cirrus -> "cirrus");
-        "hw_machine_type",
-        (match guestcaps.gcaps_machine with
-         | I440FX -> "pc"
-         | Q35 -> "q35"
-         | Virt -> "virt");
-        "architecture", guestcaps.gcaps_arch;
-        "hypervisor_type", "kvm";
-        "vm_mode", "hvm";
-        "os_type", inspect.i_type;
-        "os_distro",
-        (match inspect.i_distro with
-        (* https://docs.openstack.org/python-glanceclient/latest/cli/property-keys.html *)
-         | "archlinux" -> "arch"
-         | "sles" -> "sled"
-         | x -> x (* everything else is the same in libguestfs and OpenStack*)
-        )
-      ] in
-      (match source.s_cpu_topology with
-       | None ->
-          List.push_back properties ("hw_cpu_sockets", "1");
-          List.push_back properties ("hw_cpu_cores", string_of_int source.s_vcpu);
-       | Some { s_cpu_sockets = sockets; s_cpu_cores = cores;
-                s_cpu_threads = threads } ->
-          List.push_back properties ("hw_cpu_sockets", string_of_int sockets);
-          List.push_back properties ("hw_cpu_cores", string_of_int cores);
-          List.push_back properties ("hw_cpu_threads", string_of_int threads);
-      );
-      (match guestcaps.gcaps_block_bus with
-       | Virtio_SCSI ->
-          List.push_back properties ("hw_scsi_model", "virtio-scsi")
-       | Virtio_blk | IDE -> ()
-      );
-      (match inspect.i_major_version, inspect.i_minor_version with
-       | 0, 0 -> ()
-       | x, 0 -> List.push_back properties ("os_version", string_of_int x)
-       | x, y -> List.push_back properties ("os_version", sprintf "%d.%d" x y)
-      );
-      if guestcaps.gcaps_virtio_rng then
-        List.push_back properties ("hw_rng_model", "virtio");
-      (* XXX Neither memory balloon nor pvpanic are supported by
-       * Glance at this time.
-       *)
-      (match target_firmware with
-       | TargetBIOS -> ()
-       | TargetUEFI ->
-          List.push_back properties ("hw_firmware_type", "uefi")
-      );
 
-      !properties in
+    (* Get the image properties. *)
+    let properties =
+      Openstack_image_properties.create source target_buses guestcaps
+                                        inspect target_firmware in
+    let properties =
+      List.flatten (
+        List.map (
+          fun (k, v) -> [ "--property"; sprintf "%s=%s" k v ]
+        ) properties
+      ) in
 
     (* The first disk, assumed to be the system disk, will be called
      * "guestname".  Subsequent disks, assumed to be data disks,
@@ -143,13 +89,6 @@ object
           if i == 0 then source.s_name
           else sprintf "%s-disk%d" source.s_name (i+1) in
 
-        let properties =
-          List.flatten (
-            List.map (
-              fun (k, v) -> [ "--property"; sprintf "%s=%s" k v ]
-            ) common_properties
-          ) in
-
         let target_file =
           match target_file with
           | TargetFile s -> s
-- 
2.18.0




More information about the Libguestfs mailing list