[Pkg-libvirt-commits] [libguestfs] 223/266: v2v: Output to OpenStack Glance (-o glance option).

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 3 14:42:04 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag debian/1%1.27.35-1
in repository libguestfs.

commit f01f641fbb1a29b5727c0443f3700b1c871ccfc9
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Fri Aug 29 18:27:20 2014 +0100

    v2v: Output to OpenStack Glance (-o glance option).
---
 po/POTFILES-ml           |   1 +
 v2v/Makefile.am          |   3 ++
 v2v/cmdline.ml           |  16 +++++-
 v2v/output_glance.ml     | 123 +++++++++++++++++++++++++++++++++++++++++++++++
 v2v/output_glance.mli    |  24 +++++++++
 v2v/test-v2v-o-glance.sh |  58 ++++++++++++++++++++++
 v2v/virt-v2v.pod         |  24 ++++++++-
 7 files changed, 246 insertions(+), 3 deletions(-)

diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index ef677be..9804e7a 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -93,6 +93,7 @@ v2v/input_libvirtxml.ml
 v2v/lib_esx.ml
 v2v/lib_linux.ml
 v2v/output_RHEV.ml
+v2v/output_glance.ml
 v2v/output_libvirt.ml
 v2v/output_local.ml
 v2v/stringMap.ml
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index f4baf65..5f865b9 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -36,6 +36,7 @@ SOURCES_MLI = \
 	input_disk.mli \
 	input_libvirt.mli \
 	input_libvirtxml.mli \
+	output_glance.mli \
 	output_libvirt.mli \
 	output_local.mli \
 	output_RHEV.mli \
@@ -56,6 +57,7 @@ SOURCES_ML = \
 	input_libvirt.ml \
 	convert_linux.ml \
 	convert_windows.ml \
+	output_glance.ml \
 	output_libvirt.ml \
 	output_local.ml \
 	output_RHEV.ml \
@@ -195,6 +197,7 @@ TESTS = \
 	test-v2v-machine-readable.sh \
 	test-v2v-networks-and-bridges.sh \
 	test-v2v-no-copy.sh \
+	test-v2v-o-glance.sh \
 	test-v2v-o-libvirt.sh \
 	test-v2v-o-rhev.sh \
 	test-v2v-o-rhev-options.sh \
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index f16c224..917d1c9 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -73,6 +73,7 @@ let parse_cmdline () =
 
   let output_mode = ref `Libvirt in
   let set_output_mode = function
+    | "glance" -> output_mode := `Glance
     | "libvirt" -> output_mode := `Libvirt
     | "local" -> output_mode := `Local
     | "ovirt" | "rhev" -> output_mode := `RHEV
@@ -113,7 +114,7 @@ let parse_cmdline () =
     "--machine-readable", Arg.Set machine_readable, " " ^ s_"Make output machine readable";
     "--network", Arg.String add_network,    "in:out " ^ s_"Map network 'in' to 'out'";
     "--no-copy", Arg.Clear do_copy,         " " ^ s_"Just write the metadata";
-    "-o",        Arg.String set_output_mode, "libvirt|local|rhev " ^ s_"Set output mode (default: libvirt)";
+    "-o",        Arg.String set_output_mode, "libvirt|local|rhev|glance " ^ s_"Set output mode (default: libvirt)";
     "-oa",       Arg.String set_output_alloc, "sparse|preallocated " ^ s_"Set output allocation mode";
     "-oc",       Arg.Set_string output_conn, "uri " ^ s_"Libvirt URI";
     "-of",       Arg.Set_string output_format, "raw|qcow2 " ^ s_"Set output format";
@@ -152,6 +153,8 @@ let parse_cmdline () =
 
  virt-v2v -i disk -o local -os /var/tmp disk.img
 
+ virt-v2v -i disk -o glance -os glance_image_name
+
 There is a companion front-end called \"virt-p2v\" which comes as an
 ISO or CD image that can be booted on physical machines.
 
@@ -236,6 +239,17 @@ read the man page virt-v2v(1).
   (* Parse the output mode. *)
   let output =
     match output_mode with
+    | `Glance ->
+      if output_storage = "" then
+        error (f_"-o glance: output image name was not specified, use '-os glance_image_name'");
+      if output_conn <> None then
+        error (f_"-o glance: -oc option cannot be used in this output mode");
+      if vmtype <> None then
+        error (f_"--vmtype option can only be used with '-o rhev'");
+      if not do_copy then
+        error (f_"--no-copy and '-o glance' cannot be used at the same time");
+      Output_glance.output_glance verbose output_storage
+
     | `Libvirt ->
       let output_storage =
         if output_storage = "" then "default" else output_storage in
diff --git a/v2v/output_glance.ml b/v2v/output_glance.ml
new file mode 100644
index 0000000..6a34a9e
--- /dev/null
+++ b/v2v/output_glance.ml
@@ -0,0 +1,123 @@
+(* virt-v2v
+ * Copyright (C) 2009-2014 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.
+ *)
+
+open Printf
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Types
+open Utils
+
+class output_glance verbose image_name =
+  (* Although glance can slurp in a stream from stdin, unfortunately
+   * 'qemu-img convert' cannot write to a stream (although I guess
+   * it could be implemented at least for raw).  Therefore we have
+   * to write to a temporary file.  XXX
+   *)
+  let tmpdir =
+    let t = Mkdtemp.temp_dir "glance." "" in
+    rmdir_on_exit t;
+    t in
+object
+  inherit output verbose
+
+  method as_options = sprintf "-o glance -os %s" image_name
+
+  method prepare_targets source targets =
+    (* This does nothing useful except to check that the user has
+     * supplied all the correct auth environment variables to make
+     * 'glance' commands work as the current user.  If not then the
+     * program exits early.
+     *)
+    if Sys.command "glance image-list > /dev/null" <> 0 then
+      error (f_"glance: glance client is not installed or set up correctly.  You may need to set environment variables or source a script to enable authentication.  See preceding messages for details.");
+
+    (* OpenStack only supports single image VMs, I think? *)
+    let nr_targets = List.length targets in
+    if nr_targets <> 1 then
+      error (f_"glance: OpenStack conversion only supports virtual machines with a single disk image.  This VM has %d") nr_targets;
+
+    List.map (
+      fun t ->
+        let target_file = tmpdir // t.target_overlay.ov_sd in
+        { t with target_file = target_file }
+    ) targets
+
+  method create_metadata source targets guestcaps inspect =
+    (* Upload the disk image (there should only be one - see above). *)
+    let { target_file = target_file; target_format = target_format } =
+      List.hd targets in
+    let cmd =
+      sprintf "glance image-create --name %s --disk-format=%s --container-format=bare %s"
+        (quote source.s_name) (quote target_format) target_file in
+    if verbose then printf "%s\n%!" cmd;
+    if Sys.command cmd <> 0 then
+      error (f_"glance: image upload to glance failed, see earlier errors");
+
+    (* Set the properties (ie. metadata). *)
+    let min_ram = source.s_memory /^ 1024L /^ 1024L in
+    let properties = [
+      "hw_vif_model",
+      (match guestcaps.gcaps_block_bus with
+      | Virtio_blk -> "virtio"
+      | IDE -> "ide");
+      "hw_disk_bus",
+      (match guestcaps.gcaps_net_bus with
+      | Virtio_net -> "virtio"
+      | E1000 -> "e1000"
+      | RTL8139 -> "rtl8139");
+      "architecture", source.s_arch;
+      "hypervisor_type", "kvm";
+      "vm_mode", "hvm";
+      "os_type", inspect.i_type;
+      "os_version",
+      sprintf "%d.%d" inspect.i_major_version inspect.i_minor_version;
+      "os_distro",
+      (match inspect.i_distro with
+      (* http://docs.openstack.org/grizzly/openstack-compute/admin/content/image-metadata.html *)
+      | "archlinux" -> "arch"
+      | "sles" -> "sled"
+      | x -> x (* everything else is the same in libguestfs and OpenStack *)
+      )
+    ] in
+
+    (* Set the properties one at a time so if any fails we can give
+     * a warning message (failing to set properties is not fatal).
+     *)
+    let cmd =
+      sprintf "glance image-update --min-ram %Ld %s"
+        min_ram (quote source.s_name) in
+    if verbose then printf "%s\n%!" cmd;
+    if Sys.command cmd <> 0 then
+      warning ~prog (f_"glance: failed to set --min-ram to %Ld (MB) (ignored)")
+        min_ram;
+
+    List.iter (
+      fun (k,v) ->
+        let cmd =
+          sprintf "glance image-update --property %s=%s %s"
+            (quote k) (quote v) (quote source.s_name) in
+        if verbose then printf "%s\n%!" cmd;
+        if Sys.command cmd <> 0 then
+          warning ~prog (f_"glance: failed to set property '%s' to '%s' (ignored)")
+            k v
+    ) properties
+end
+
+let output_glance = new output_glance
diff --git a/v2v/output_glance.mli b/v2v/output_glance.mli
new file mode 100644
index 0000000..b6bcd22
--- /dev/null
+++ b/v2v/output_glance.mli
@@ -0,0 +1,24 @@
+(* virt-v2v
+ * Copyright (C) 2009-2014 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.
+ *)
+
+(** [-o glance] target. *)
+
+val output_glance : bool -> string -> Types.output
+(** [output_glance verbose filename] creates and returns a new
+    {!Types.output} object specialized for writing output to OpenStack
+    glance. *)
diff --git a/v2v/test-v2v-o-glance.sh b/v2v/test-v2v-o-glance.sh
new file mode 100755
index 0000000..7f52343
--- /dev/null
+++ b/v2v/test-v2v-o-glance.sh
@@ -0,0 +1,58 @@
+#!/bin/bash -
+# libguestfs virt-v2v test script
+# Copyright (C) 2014 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.
+
+# Test -o glance.
+
+unset CDPATH
+export LANG=C
+set -e
+
+if [ -n "$SKIP_TEST_V2V_O_GLANCE_SH" ]; then
+    echo "$0: test skipped because environment variable is set"
+    exit 77
+fi
+
+if [ "$(../fish/guestfish get-backend)" = "uml" ]; then
+    echo "$0: test skipped because UML backend does not support network"
+    exit 77
+fi
+
+abs_top_builddir="$(cd ..; pwd)"
+libvirt_uri="test://$abs_top_builddir/tests/guests/guests.xml"
+
+f=../tests/guests/windows.img
+if ! test -f $f || ! test -s $f; then
+    echo "$0: test skipped because phony Windows image was not created"
+    exit 77
+fi
+
+virt_tools_data_dir=${VIRT_TOOLS_DATA_DIR:-/usr/share/virt-tools}
+if ! test -r $virt_tools_data_dir/rhsrvany.exe; then
+    echo "$0: test skipped because rhsrvany.exe is not installed"
+    exit 77
+fi
+
+# We don't want to upload to the real glance, so introduce a fake
+# glance binary.
+ln -sf "$(which echo)" glance
+
+$VG ./virt-v2v --debug-gc \
+    -i libvirt -ic "$libvirt_uri" windows \
+    -o glance -os test
+
+rm glance
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
index 45182ae..ee9cebf 100644
--- a/v2v/virt-v2v.pod
+++ b/v2v/virt-v2v.pod
@@ -13,6 +13,8 @@ virt-v2v - Convert a guest to use KVM
 
  virt-v2v -i disk -o local -os /var/tmp disk.img
 
+ virt-v2v -i disk -o glance -os glance_image_name
+
 =head1 DESCRIPTION
 
 Virt-v2v converts guests from a foreign hypervisor to run on KVM,
@@ -34,8 +36,8 @@ libguestfs E<ge> 1.28.
                     └──> │ conversion │ ──┘
  -i libvirt ───────────> │ server     │ ────────> -o libvirt
   (default)         ┌──> │            │ ──┐        (default)
-                    │    │            │   │
- -i libvirtxml ─────┘    │            │   └────────> -o rhev
+                    │    │            │ ─┐└──────> -o glance
+ -i libvirtxml ─────┘    │            │  └─────────> -o rhev
                          └────────────┘
 
 Virt-v2v has a number of possible input and output modes, selected
@@ -53,6 +55,8 @@ testing).
 I<-i libvirtxml> is used to read from libvirt XML files.  This is the
 method used by L<virt-p2v(1)> behind the scenes.
 
+I<-o glance> is used for writing to OpenStack Glance.
+
 I<-o libvirt> is used for writing to any libvirt target.  Libvirt can
 connect to local or remote KVM hypervisors.  The I<-oc> option selects
 the precise libvirt target.
@@ -96,6 +100,13 @@ Note that after conversion, the guest will appear in the RHEV-M Export
 Storage Domain, from where you will need to import it using the RHEV-M
 user interface.
 
+=head2 Convert disk image to OpenStack glance
+
+Given a disk image from another hypervisor that you want to convert to
+run on OpenStack (only KVM-based OpenStack is supported), you can do:
+
+ virt-v2v -i disk disk.img -o glance -os glance_image_name
+
 =head2 Convert disk image to disk image
 
 Given a disk image from another hypervisor that you want to convert to
@@ -213,6 +224,13 @@ interested in looking at the metadata.
 This option is not compatible with I<-o libvirt> since it would create
 a faulty guest (one with no disks).
 
+This option is not compatible with I<-o glance> for technical reasons.
+
+=item B<-o glance>
+
+Set the output method to OpenStack Glance.  In this mode the converted
+guest is uploaded to Glance as the image named in the I<-os> parameter.
+
 =item B<-o libvirt>
 
 Set the output method to I<libvirt>.  This is the default.
@@ -279,6 +297,8 @@ the output name is the same as the input name.
 
 The location of the storage for the converted guest.
 
+For I<-o glance>, this is the Glance image name.
+
 For I<-o libvirt>, this is a libvirt directory pool
 (see S<C<virsh pool-list>>).
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list