Blame SOURCES/0035-v2v-Implement-input-from-nbdkit-vddk-plugin-RHBZ-147.patch

151578
From 9542f12a306e05b73f5c4ecd4e4e61e03797098a Mon Sep 17 00:00:00 2001
151578
From: "Richard W.M. Jones" <rjones@redhat.com>
151578
Date: Wed, 30 Aug 2017 17:14:03 +0100
151578
Subject: [PATCH] v2v: Implement input from nbdkit vddk plugin (RHBZ#1477912).
151578
151578
(cherry picked from commit bbda5a6a68846acf971e0f4cbf83cacfa083d6e9)
151578
---
151578
 v2v/Makefile.am            |   2 +
151578
 v2v/cmdline.ml             |  53 +++++++-
151578
 v2v/input_libvirt.ml       |  16 ++-
151578
 v2v/input_libvirt.mli      |   8 +-
151578
 v2v/input_libvirt_vddk.ml  | 314 +++++++++++++++++++++++++++++++++++++++++++++
151578
 v2v/input_libvirt_vddk.mli |  24 ++++
151578
 v2v/types.ml               |  12 ++
151578
 v2v/types.mli              |  13 ++
151578
 v2v/virt-v2v.pod           | 155 ++++++++++++++++++++++
151578
 9 files changed, 588 insertions(+), 9 deletions(-)
151578
 create mode 100644 v2v/input_libvirt_vddk.ml
151578
 create mode 100644 v2v/input_libvirt_vddk.mli
151578
151578
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
151578
index 0df759eca..87776a509 100644
151578
--- a/v2v/Makefile.am
151578
+++ b/v2v/Makefile.am
151578
@@ -35,6 +35,7 @@ SOURCES_MLI = \
151578
 	input_libvirt.mli \
151578
 	input_libvirt_other.mli \
151578
 	input_libvirt_vcenter_https.mli \
151578
+	input_libvirt_vddk.mli \
151578
 	input_libvirt_xen_ssh.mli \
151578
 	input_libvirtxml.mli \
151578
 	input_ova.mli \
151578
@@ -89,6 +90,7 @@ SOURCES_ML = \
151578
 	input_libvirtxml.ml \
151578
 	input_libvirt_other.ml \
151578
 	input_libvirt_vcenter_https.ml \
151578
+	input_libvirt_vddk.ml \
151578
 	input_libvirt_xen_ssh.ml \
151578
 	input_libvirt.ml \
151578
 	input_ova.ml \
151578
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
151578
index 6b105886c..3050104d0 100644
151578
--- a/v2v/cmdline.ml
151578
+++ b/v2v/cmdline.ml
151578
@@ -63,6 +63,15 @@ let parse_cmdline () =
151578
   let output_name = ref None in
151578
   let output_storage = ref None in
151578
   let password_file = ref None in
151578
+  let vddk = ref None in
151578
+  let vddk_config = ref None in
151578
+  let vddk_cookie = ref None in
151578
+  let vddk_nfchostport = ref None in
151578
+  let vddk_port = ref None in
151578
+  let vddk_snapshot = ref None in
151578
+  let vddk_thumbprint = ref None in
151578
+  let vddk_transports = ref None in
151578
+  let vddk_vimapiver = ref None in
151578
   let vdsm_vm_uuid = ref None in
151578
   let vdsm_ovf_output = ref None in (* default "." *)
151578
 
151578
@@ -201,6 +210,24 @@ let parse_cmdline () =
151578
                                             s_"Use password from file";
151578
     [ L"print-source" ], Getopt.Set print_source, s_"Print source and stop";
151578
     [ L"root" ],    Getopt.String ("ask|... ", set_root_choice), s_"How to choose root filesystem";
151578
+    [ L"vddk" ],     Getopt.String ("libpath", set_string_option_once "--vddk" vddk),
151578
+                                            s_"Use nbdkit VDDK plugin";
151578
+    [ L"vddk-config" ], Getopt.String ("filename", set_string_option_once "--vddk-config" vddk_config),
151578
+                                            s_"Set VDDK config file";
151578
+    [ L"vddk-cookie" ], Getopt.String ("cookie", set_string_option_once "--vddk-cookie" vddk_cookie),
151578
+                                            s_"Set VDDK cookie";
151578
+    [ L"vddk-nfchostport" ], Getopt.String ("nfchostport", set_string_option_once "--vddk-nfchostport" vddk_nfchostport),
151578
+                                            s_"Set VDDK nfchostport";
151578
+    [ L"vddk-port" ], Getopt.String ("port", set_string_option_once "--vddk-port" vddk_port),
151578
+                                            s_"Set VDDK port";
151578
+    [ L"vddk-snapshot" ], Getopt.String ("snapshot-moref", set_string_option_once "--vddk-snapshot" vddk_snapshot),
151578
+                                            s_"Set VDDK snapshot";
151578
+    [ L"vddk-thumbprint" ], Getopt.String ("thumbprint", set_string_option_once "--vddk-thumbprint" vddk_thumbprint),
151578
+                                            s_"Set VDDK thumbprint";
151578
+    [ L"vddk-transports" ], Getopt.String ("transports", set_string_option_once "--vddk-transports" vddk_transports),
151578
+                                            s_"Set VDDK transports";
151578
+    [ L"vddk-vimapiver" ], Getopt.String ("apiver", set_string_option_once "--vddk-vimapiver" vddk_vimapiver),
151578
+                                            s_"Set VDDK vimapiver";
151578
     [ L"vdsm-compat" ], Getopt.Symbol ("0.10|1.1", ["0.10"; "1.1"], set_vdsm_compat), s_"Write qcow2 with compat=0.10|1.1";
151578
     [ L"vdsm-image-uuid" ], Getopt.String ("uuid", add_vdsm_image_uuid), s_"Output image UUID(s)";
151578
     [ L"vdsm-vol-uuid" ], Getopt.String ("uuid", add_vdsm_vol_uuid), s_"Output vol UUID(s)";
151578
@@ -263,6 +290,29 @@ read the man page virt-v2v(1).
151578
   let print_source = !print_source in
151578
   let qemu_boot = !qemu_boot in
151578
   let root_choice = !root_choice in
151578
+  let vddk_options =
151578
+    match !vddk with
151578
+    | Some libdir ->
151578
+      Some { vddk_libdir = libdir;
151578
+             vddk_config = !vddk_config;
151578
+             vddk_cookie = !vddk_cookie;
151578
+             vddk_nfchostport = !vddk_nfchostport;
151578
+             vddk_port = !vddk_port;
151578
+             vddk_snapshot = !vddk_snapshot;
151578
+             vddk_thumbprint = !vddk_thumbprint;
151578
+             vddk_transports = !vddk_transports;
151578
+             vddk_vimapiver = !vddk_vimapiver }
151578
+    | None ->
151578
+      if !vddk_config <> None ||
151578
+         !vddk_cookie <> None ||
151578
+         !vddk_nfchostport <> None ||
151578
+         !vddk_port <> None ||
151578
+         !vddk_snapshot <> None ||
151578
+         !vddk_thumbprint <> None ||
151578
+         !vddk_transports <> None ||
151578
+         !vddk_vimapiver <> None then
151578
+        error (f_"‘--vddk-*’ options should only be used when conversion via the nbdkit VDDK plugin has been enabled, ie. using ‘--vddk’.");
151578
+      None in
151578
   let vdsm_compat = !vdsm_compat in
151578
   let vdsm_image_uuids = List.rev !vdsm_image_uuids in
151578
   let vdsm_vol_uuids = List.rev !vdsm_vol_uuids in
151578
@@ -278,6 +328,7 @@ read the man page virt-v2v(1).
151578
     printf "libguestfs-rewrite\n";
151578
     printf "vcenter-https\n";
151578
     printf "xen-ssh\n";
151578
+    printf "vddk\n";
151578
     printf "colours-option\n";
151578
     printf "vdsm-compat-option\n";
151578
     List.iter (printf "input:%s\n") (Modules_list.input_modules ());
151578
@@ -316,7 +367,7 @@ read the man page virt-v2v(1).
151578
         | [guest] -> guest
151578
         | _ ->
151578
           error (f_"expecting a libvirt guest name on the command line") in
151578
-      Input_libvirt.input_libvirt dcpath password input_conn guest
151578
+      Input_libvirt.input_libvirt dcpath vddk_options password input_conn guest
151578
 
151578
     | `LibvirtXML ->
151578
       (* -i libvirtxml: Expecting a filename (XML file). *)
151578
diff --git a/v2v/input_libvirt.ml b/v2v/input_libvirt.ml
151578
index 0bc386430..e8143b6ad 100644
151578
--- a/v2v/input_libvirt.ml
151578
+++ b/v2v/input_libvirt.ml
151578
@@ -27,7 +27,7 @@ open Types
151578
 open Utils
151578
 
151578
 (* Choose the right subclass based on the URI. *)
151578
-let input_libvirt dcpath password libvirt_uri guest =
151578
+let input_libvirt dcpath vddk_options password libvirt_uri guest =
151578
   match libvirt_uri with
151578
   | None ->
151578
     Input_libvirt_other.input_libvirt_other password libvirt_uri guest
151578
@@ -47,10 +47,18 @@ let input_libvirt dcpath password libvirt_uri guest =
151578
     | Some _, Some "" ->
151578
       Input_libvirt_other.input_libvirt_other password libvirt_uri guest
151578
 
151578
-    (* vCenter over https *)
151578
+    (* vCenter over https, or
151578
+     * vCenter or ESXi using nbdkit vddk plugin
151578
+     *)
151578
     | Some server, Some ("esx"|"gsx"|"vpx" as scheme) ->
151578
-      Input_libvirt_vcenter_https.input_libvirt_vcenter_https
151578
-        dcpath password libvirt_uri parsed_uri scheme server guest
151578
+       (match vddk_options with
151578
+        | None ->
151578
+           Input_libvirt_vcenter_https.input_libvirt_vcenter_https
151578
+             dcpath password libvirt_uri parsed_uri scheme server guest
151578
+        | Some vddk_options ->
151578
+           Input_libvirt_vddk.input_libvirt_vddk vddk_options password
151578
+                                                 libvirt_uri parsed_uri guest
151578
+       )
151578
 
151578
     (* Xen over SSH *)
151578
     | Some server, Some ("xen+ssh" as scheme) ->
151578
diff --git a/v2v/input_libvirt.mli b/v2v/input_libvirt.mli
151578
index f5e6314d9..0a6aa3c54 100644
151578
--- a/v2v/input_libvirt.mli
151578
+++ b/v2v/input_libvirt.mli
151578
@@ -18,7 +18,7 @@
151578
 
151578
 (** [-i libvirt] source. *)
151578
 
151578
-val input_libvirt : string option -> string option -> string option -> string -> Types.input
151578
-(** [input_libvirt dcpath password libvirt_uri guest] creates and returns a
151578
-    new {!Types.input} object specialized for reading input from
151578
-    libvirt sources. *)
151578
+val input_libvirt : string option -> Types.vddk_options option -> string option -> string option -> string -> Types.input
151578
+(** [input_libvirt dcpath vddk_options password libvirt_uri guest] creates
151578
+    and returns a new {!Types.input} object specialized for reading input
151578
+    from libvirt sources. *)
151578
diff --git a/v2v/input_libvirt_vddk.ml b/v2v/input_libvirt_vddk.ml
151578
new file mode 100644
151578
index 000000000..89d2552f6
151578
--- /dev/null
151578
+++ b/v2v/input_libvirt_vddk.ml
151578
@@ -0,0 +1,314 @@
151578
+(* virt-v2v
151578
+ * Copyright (C) 2009-2017 Red Hat Inc.
151578
+ *
151578
+ * This program is free software; you can redistribute it and/or modify
151578
+ * it under the terms of the GNU General Public License as published by
151578
+ * the Free Software Foundation; either version 2 of the License, or
151578
+ * (at your option) any later version.
151578
+ *
151578
+ * This program is distributed in the hope that it will be useful,
151578
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
151578
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151578
+ * GNU General Public License for more details.
151578
+ *
151578
+ * You should have received a copy of the GNU General Public License along
151578
+ * with this program; if not, write to the Free Software Foundation, Inc.,
151578
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
151578
+ *)
151578
+
151578
+(** [-i libvirt] when the source is VMware via nbdkit vddk plugin *)
151578
+
151578
+open Unix
151578
+
151578
+open Common_gettext.Gettext
151578
+open Common_utils
151578
+open Unix_utils
151578
+
151578
+open Types
151578
+open Utils
151578
+open Input_libvirt_other
151578
+open Parse_libvirt_xml
151578
+open Xpath_helpers
151578
+
151578
+open Printf
151578
+
151578
+(* Subclass specialized for handling VMware via nbdkit vddk plugin. *)
151578
+class input_libvirt_vddk vddk_options password libvirt_uri parsed_uri guest =
151578
+
151578
+  (* The VDDK path. *)
151578
+  let libdir = vddk_options.vddk_libdir in
151578
+  (* Compute the LD_LIBRARY_PATH that we must pass to nbdkit. *)
151578
+  let library_path = libdir // sprintf "lib%d" Sys.word_size in
151578
+
151578
+  (* Is SELinux enabled and enforcing on the host? *)
151578
+  let have_selinux =
151578
+    0 = Sys.command "getenforce 2>/dev/null | grep -isq Enforcing" in
151578
+
151578
+  (* Check that the VDDK path looks reasonable. *)
151578
+  let error_unless_vddk_libdir () =
151578
+    if not (is_directory libdir) then
151578
+      error (f_"‘--vddk %s’ does not point to a directory.  See \"INPUT FROM VDDK\" in the virt-v2v(1) manual.") libdir;
151578
+
151578
+    if not (is_directory library_path) then
151578
+      error (f_"VDDK library path %s not found or not a directory.  See \"INPUT FROM VDDK\" in the virt-v2v(1) manual.")
151578
+            library_path
151578
+  in
151578
+
151578
+  (* Check that nbdkit is available and new enough. *)
151578
+  let error_unless_nbdkit_working () =
151578
+    if 0 <> Sys.command "nbdkit --version >/dev/null" then
151578
+      error (f_"nbdkit is not installed or not working.  It is required to use ‘--vddk’.  See \"INPUT FROM VDDK\" in the virt-v2v(1) manual.");
151578
+
151578
+    (* Check it's a new enough version.  The latest features we
151578
+     * require are ‘--exit-with-parent’ and ‘--selinux-label’, both
151578
+     * added in 1.1.14.
151578
+     *)
151578
+    let lines = external_command "nbdkit --help" in
151578
+    let lines = String.concat " " lines in
151578
+    if String.find lines "exit-with-parent" == -1 ||
151578
+       String.find lines "selinux-label" == -1 then
151578
+      error (f_"nbdkit is not new enough, you need to upgrade to nbdkit ≥ 1.1.14")
151578
+  in
151578
+
151578
+  (* Check that the VDDK plugin is installed and working *)
151578
+  let error_unless_nbdkit_vddk_working () =
151578
+    let cmd =
151578
+      sprintf "LD_LIBRARY_PATH=%s nbdkit vddk --dump-plugin >/dev/null"
151578
+              (quote library_path) in
151578
+    if Sys.command cmd <> 0 then (
151578
+      (* See if we can diagnose why ... *)
151578
+      let cmd =
151578
+        sprintf "LD_LIBRARY_PATH=%s LANG=C nbdkit vddk --dump-plugin 2>&1 | grep -sq libvixDiskLib.so"
151578
+                (quote library_path) in
151578
+      let needs_library = Sys.command cmd = 0 in
151578
+      if not needs_library then
151578
+        error (f_"nbdkit VDDK plugin is not installed or not working.  It is required if you want to use VDDK.
151578
+
151578
+The VDDK plugin is not enabled by default when you compile nbdkit.  You have to read the instructions in the nbdkit sources under ‘plugins/vddk/README.VDDK’ to find out how to enable the VDDK plugin.
151578
+
151578
+See also \"INPUT FROM VDDK\" in the virt-v2v(1) manual.")
151578
+      else
151578
+        error (f_"nbdkit VDDK plugin is not installed or not working.  It is required if you want to use VDDK.
151578
+
151578
+It looks like you did not set the right path in the ‘--vddk’ option, or your copy of the VDDK directory is incomplete.  There should be a library called ’%s/libvixDiskLib.so.?’.
151578
+
151578
+See also \"INPUT FROM VDDK\" in the virt-v2v(1) manual.") library_path
151578
+    )
151578
+  in
151578
+
151578
+  let error_unless_thumbprint () =
151578
+    if vddk_options.vddk_thumbprint = None then
151578
+      error (f_"You must pass the ‘--vddk-thumbprint’ option with the SSL thumbprint of the VMware server.  To find the thumbprint, see the nbdkit-vddk-plugin(1) manual.  See also \"INPUT FROM VDDK\" in the virt-v2v(1) manual.")
151578
+  in
151578
+
151578
+object
151578
+  inherit input_libvirt password libvirt_uri guest
151578
+
151578
+  method source () =
151578
+    error_unless_vddk_libdir ();
151578
+    error_unless_nbdkit_working ();
151578
+    error_unless_nbdkit_vddk_working ();
151578
+    error_unless_thumbprint ();
151578
+
151578
+    (* Get the libvirt XML.  This also checks (as a side-effect)
151578
+     * that the domain is not running.  (RHBZ#1138586)
151578
+     *)
151578
+    let xml = Libvirt_utils.dumpxml ?password ?conn:libvirt_uri guest in
151578
+    let source, disks = parse_libvirt_xml ?conn:libvirt_uri xml in
151578
+
151578
+    (* Find the <vmware:moref> element from the XML.  This was added
151578
+     * in libvirt >= 3.7 and is required.
151578
+     *)
151578
+    let moref =
151578
+      let doc = Xml.parse_memory xml in
151578
+      let xpathctx = Xml.xpath_new_context doc in
151578
+      Xml.xpath_register_ns xpathctx
151578
+        "vmware" "http://libvirt.org/schemas/domain/vmware/1.0";
151578
+      let xpath_string = xpath_string xpathctx in
151578
+      match xpath_string "/domain/vmware:moref" with
151578
+      | Some moref -> moref
151578
+      | None ->
151578
+         error (f_"<vmware:moref> was not found in the output of ‘virsh dumpxml \"%s\"’.  The most likely reason is that libvirt is too old, try upgrading libvirt to ≥ 3.7.") guest in
151578
+
151578
+    (* Create a temporary directory where we place the sockets and
151578
+     * password file.
151578
+     *)
151578
+    let tmpdir =
151578
+      let base_dir = (open_guestfs ())#get_cachedir () in
151578
+      let t = Mkdtemp.temp_dir ~base_dir "vddk." in
151578
+      (* tmpdir must be readable (but not writable) by "other" so that
151578
+       * qemu can open the sockets.  If we place a password file in
151578
+       * this directory then we'll chmod that to 0600 below.
151578
+       *)
151578
+      chmod t 0o755;
151578
+      rmdir_on_exit t;
151578
+      t in
151578
+
151578
+    (* Start constructing the parts of the incredibly long nbdkit
151578
+     * command line which don't change between disks.
151578
+     *)
151578
+    let args =
151578
+      let add_arg, get_args =
151578
+        let args = ref [] in
151578
+        let add_arg a = push_front a args in
151578
+        let get_args () = List.rev !args in
151578
+        add_arg, get_args in
151578
+
151578
+      (* It probably never happens that the server name can be missing
151578
+       * from the libvirt URI, but we need a server name to pass to
151578
+       * nbdkit, so ...
151578
+       *)
151578
+      let server =
151578
+        match parsed_uri.Xml.uri_server with
151578
+        | Some server -> server
151578
+        | None ->
151578
+           match libvirt_uri with
151578
+           | Some libvirt_uri ->
151578
+              error (f_"‘-ic %s’ URL does not contain a host name field")
151578
+                    libvirt_uri
151578
+           | None ->
151578
+              error (f_"you must use the ‘-ic’ parameter.  See \"INPUT FROM VDDK\" in the virt-v2v(1) manual.") in
151578
+
151578
+      (* Similar to above, we also need a username to pass. *)
151578
+      let user =
151578
+        match parsed_uri.Xml.uri_user with
151578
+        | Some user -> user
151578
+        | None -> "root" (* ? *) in
151578
+
151578
+      add_arg "nbdkit";
151578
+      if verbose () then add_arg "--verbose";
151578
+      add_arg "--readonly";         (* important! readonly mode *)
151578
+      add_arg "--foreground";       (* run in foreground *)
151578
+      add_arg "--exit-with-parent"; (* exit when virt-v2v exits *)
151578
+      add_arg "--newstyle";         (* use newstyle NBD protocol *)
151578
+      add_arg "--exportname"; add_arg "/";
151578
+      if have_selinux then (        (* label the socket so qemu can open it *)
151578
+        add_arg "--selinux-label"; add_arg "system_u:object_r:svirt_t:s0"
151578
+      );
151578
+
151578
+      (* Name of the plugin.  Everything following is a plugin parameter. *)
151578
+      add_arg "vddk";
151578
+
151578
+      let password_param =
151578
+        match password with
151578
+        | None ->
151578
+           (* nbdkit asks for the password interactively *)
151578
+           "password=-"
151578
+        | Some password ->
151578
+           let password_file = tmpdir // "password" in
151578
+           let chan = open_out password_file in
151578
+           chmod password_file 0o600;
151578
+           output_string chan password;
151578
+           close_out chan;
151578
+           (* nbdkit reads the password from the file *)
151578
+           "password=+" ^ password_file in
151578
+      add_arg (sprintf "server=%s" server);
151578
+      add_arg (sprintf "user=%s" user);
151578
+      add_arg password_param;
151578
+      add_arg (sprintf "vm=moref=%s" moref);
151578
+      add_arg (sprintf "libdir=%s" libdir);
151578
+
151578
+      (* The passthrough parameters. *)
151578
+      let pt name = may (fun field -> add_arg (sprintf "%s=%s" name field)) in
151578
+      pt "config" vddk_options.vddk_config;
151578
+      pt "cookie" vddk_options.vddk_cookie;
151578
+      pt "nfchostport" vddk_options.vddk_nfchostport;
151578
+      pt "port" vddk_options.vddk_port;
151578
+      pt "snapshot" vddk_options.vddk_snapshot;
151578
+      pt "thumbprint" vddk_options.vddk_thumbprint;
151578
+      pt "transports" vddk_options.vddk_transports;
151578
+      pt "vimapiver" vddk_options.vddk_vimapiver;
151578
+
151578
+      get_args () in
151578
+
151578
+    (* Create an nbdkit instance for each disk and rewrite the source
151578
+     * paths to point to the NBD socket.
151578
+     *)
151578
+    let disks = List.map (
151578
+      function
151578
+      | { p_source_disk = disk; p_source = P_dont_rewrite } ->
151578
+         disk
151578
+
151578
+      | { p_source = P_source_dev _ } -> (* Should never happen. *)
151578
+         error (f_"source disk has <source dev=...> attribute in XML")
151578
+
151578
+      | { p_source_disk = disk; p_source = P_source_file path } ->
151578
+         (* The <source file=...> attribute returned by the libvirt
151578
+          * VMX driver looks like "[datastore] path".  We can use it
151578
+          * directly as the nbdkit file= parameter, and it is passed
151578
+          * directly in this form to VDDK.
151578
+          *)
151578
+
151578
+         let sock = tmpdir // sprintf "nbdkit%d.sock" disk.s_disk_id in
151578
+         let qemu_uri = sprintf "nbd:unix:%s:exportname=/" sock in
151578
+
151578
+         let pidfile = tmpdir // sprintf "nbdkit%d.pid" disk.s_disk_id in
151578
+
151578
+         (* Construct the final command line with the "static" args
151578
+          * above plus the args which vary for each disk.
151578
+          *)
151578
+         let args =
151578
+           args @ [ "--pidfile"; pidfile;
151578
+                    "--unix"; sock;
151578
+                    sprintf "file=%s" path ] in
151578
+         let args = Array.of_list args in
151578
+
151578
+         (* Start an nbdkit instance in the background.  By using
151578
+          * --exit-with-parent we don't have to worry about cleaning
151578
+          * it up, hopefully.
151578
+          *)
151578
+         let pid = fork () in
151578
+         if pid = 0 then (
151578
+           (* Child process (nbdkit). *)
151578
+           putenv "LD_LIBRARY_PATH" library_path;
151578
+           execvp "nbdkit" args
151578
+         );
151578
+
151578
+         (* Wait for the pidfile to appear so we know that nbdkit
151578
+          * is listening for requests.
151578
+          *)
151578
+         let rec loop i =
151578
+           if i = 0 then false
151578
+           else if Sys.file_exists pidfile then true
151578
+           else (
151578
+             sleep 1;
151578
+             loop (i-1)
151578
+           )
151578
+         in
151578
+         if not (loop 30) then (
151578
+           if verbose () then
151578
+             error (f_"nbdkit did not start up.  See previous debugging messages for problems.")
151578
+           else
151578
+             error (f_"nbdkit did not start up.  There may be errors printed by nbdkit above.
151578
+
151578
+If the messages above are not sufficient to diagnose the problem then add the ‘virt-v2v -v -x’ options and examine the debugging output carefully.")
151578
+         );
151578
+
151578
+         if have_selinux then (
151578
+           (* Note that Unix domain sockets have both a file label and
151578
+            * a socket/process label.  Using --selinux-label above
151578
+            * only set the socket label, but we must also set the file
151578
+            * label.
151578
+            *)
151578
+           ignore (
151578
+               run_command ["chcon"; "system_u:object_r:svirt_image_t:s0";
151578
+                            sock]
151578
+           );
151578
+         );
151578
+         (* ... and the regular Unix permissions, in case qemu is
151578
+          * running as another user.
151578
+          *)
151578
+         chmod sock 0o777;
151578
+
151578
+         { disk with s_qemu_uri = qemu_uri }
151578
+     ) disks in
151578
+
151578
+    if verbose () then (
151578
+      eprintf "vddk: tmpdir %s:\n%!" tmpdir;
151578
+      ignore (Sys.command (sprintf "ls -laZ %s" (quote tmpdir)))
151578
+    );
151578
+
151578
+    { source with s_disks = disks }
151578
+end
151578
+
151578
+let input_libvirt_vddk = new input_libvirt_vddk
151578
diff --git a/v2v/input_libvirt_vddk.mli b/v2v/input_libvirt_vddk.mli
151578
new file mode 100644
151578
index 000000000..19a34c202
151578
--- /dev/null
151578
+++ b/v2v/input_libvirt_vddk.mli
151578
@@ -0,0 +1,24 @@
151578
+(* virt-v2v
151578
+ * Copyright (C) 2017 Red Hat Inc.
151578
+ *
151578
+ * This program is free software; you can redistribute it and/or modify
151578
+ * it under the terms of the GNU General Public License as published by
151578
+ * the Free Software Foundation; either version 2 of the License, or
151578
+ * (at your option) any later version.
151578
+ *
151578
+ * This program is distributed in the hope that it will be useful,
151578
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
151578
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151578
+ * GNU General Public License for more details.
151578
+ *
151578
+ * You should have received a copy of the GNU General Public License along
151578
+ * with this program; if not, write to the Free Software Foundation, Inc.,
151578
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
151578
+ *)
151578
+
151578
+(** [-i libvirt] when the source is VMware via nbdkit vddk plugin *)
151578
+
151578
+val input_libvirt_vddk : Types.vddk_options -> string option -> string option -> Xml.uri -> string -> Types.input
151578
+(** [input_libvirt_vddk vddk_options password libvirt_uri parsed_uri guest]
151578
+    creates and returns a {!Types.input} object specialized for reading
151578
+    the guest disks using the nbdkit vddk plugin. *)
151578
diff --git a/v2v/types.ml b/v2v/types.ml
151578
index 3efb9ff46..4f9205aa0 100644
151578
--- a/v2v/types.ml
151578
+++ b/v2v/types.ml
151578
@@ -472,6 +472,18 @@ type root_choice = AskRoot | SingleRoot | FirstRoot | RootDev of string
151578
 
151578
 type output_allocation = Sparse | Preallocated
151578
 
151578
+type vddk_options = {
151578
+    vddk_libdir : string;
151578
+    vddk_config : string option;
151578
+    vddk_cookie : string option;
151578
+    vddk_nfchostport : string option;
151578
+    vddk_port : string option;
151578
+    vddk_snapshot : string option;
151578
+    vddk_thumbprint : string option;
151578
+    vddk_transports : string option;
151578
+    vddk_vimapiver : string option;
151578
+}
151578
+
151578
 class virtual input = object
151578
   method precheck () = ()
151578
   method virtual as_options : string
151578
diff --git a/v2v/types.mli b/v2v/types.mli
151578
index 2880546ae..087a03702 100644
151578
--- a/v2v/types.mli
151578
+++ b/v2v/types.mli
151578
@@ -328,6 +328,19 @@ type root_choice = AskRoot | SingleRoot | FirstRoot | RootDev of string
151578
 type output_allocation = Sparse | Preallocated
151578
 (** Type of [-oa] (output allocation) option. *)
151578
 
151578
+type vddk_options = {
151578
+    vddk_libdir : string;
151578
+    vddk_config : string option;
151578
+    vddk_cookie : string option;
151578
+    vddk_nfchostport : string option;
151578
+    vddk_port : string option;
151578
+    vddk_snapshot : string option;
151578
+    vddk_thumbprint : string option;
151578
+    vddk_transports : string option;
151578
+    vddk_vimapiver : string option;
151578
+}
151578
+(** Various options passed through to the nbdkit vddk plugin unmodified. *)
151578
+
151578
 (** {2 Input object}
151578
 
151578
     There is one of these used for the [-i] option. *)
151578
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
151578
index 77b6f514f..d713d0b1f 100644
151578
--- a/v2v/virt-v2v.pod
151578
+++ b/v2v/virt-v2v.pod
151578
@@ -514,6 +514,35 @@ boot an operating system from the first VirtIO disk.  Specifically,
151578
 F</boot> must be on the first VirtIO disk, and it cannot chainload an
151578
 OS which is not in the first VirtIO disk.
151578
 
151578
+=item B<--vddk> LIBDIR
151578
+
151578
+Enable VDDK input from VMware vCenter or ESXi.  C<LIBDIR> is the top
151578
+directory of the VDDK library.  This directory should I<contain>
151578
+subdirectories called F<include>, F<lib64> etc., but do not include
151578
+F<lib64> actually in the parameter.
151578
+
151578
+See L</INPUT FROM VDDK> below for details.
151578
+
151578
+=item B<--vddk-config> FILENAME
151578
+
151578
+=item B<--vddk-cookie> COOKIE
151578
+
151578
+=item B<--vddk-nfchostport> PORT
151578
+
151578
+=item B<--vddk-port> PORT
151578
+
151578
+=item B<--vddk-snapshot> SNAPSHOT-MOREF
151578
+
151578
+=item B<--vddk-thumbprint> xx:xx:xx:...
151578
+
151578
+=item B<--vddk-transports> MODE:MODE:...
151578
+
151578
+=item B<--vddk-vimapiver> APIVER
151578
+
151578
+When using VDDK mode, these options are passed unmodified to the
151578
+L<nbdkit(1)> VDDK plugin.  Please refer to L<nbdkit-vddk-plugin(1)>.
151578
+Only I<--vddk-thumbprint> is required, the others are optional.
151578
+
151578
 =item B<--vdsm-compat=0.10>
151578
 
151578
 =item B<--vdsm-compat=1.1>
151578
@@ -1270,6 +1299,130 @@ Perform the conversion of the guest using virt-v2v:
151578
 
151578
 Remove the F<guest.xml> and F<guest-disk*> files.
151578
 
151578
+=head1 INPUT FROM VDDK
151578
+
151578
+Virt-v2v is able to import guests using VMware’s proprietary VDDK
151578
+library (a.k.a. VixDiskLib).
151578
+
151578
+=head2 VDDK: PREREQUISITES
151578
+
151578
+=over 4
151578
+
151578
+=item 1.
151578
+
151578
+As the VDDK library is not open source, and the license of this
151578
+library does not permit redistribution or commercial use, you must
151578
+obtain VDDK yourself and satisfy yourself that your usage of the
151578
+library is permitted by the license.
151578
+
151578
+=item 2.
151578
+
151578
+You must also compile nbdkit, enabling the VDDK plugin.  At least
151578
+nbdkit E<ge> 1.1.14 is required, but it is usually best to compile
151578
+from the git tree.
151578
+
151578
+=over 4
151578
+
151578
+=item *
151578
+
151578
+L<https://github.com/libguestfs/nbdkit>
151578
+
151578
+=item *
151578
+
151578
+L<https://github.com/libguestfs/nbdkit/tree/master/plugins/vddk>
151578
+
151578
+=back
151578
+
151578
+=item 3.
151578
+
151578
+You can run nbdkit from its source directory without needing to
151578
+install it.  Set C<$PATH> to include the nbdkit top build directory
151578
+(the directory containing a shell script called F<nbdkit>):
151578
+
151578
+ export PATH=/path/to/nbdkit:$PATH
151578
+
151578
+=item 4.
151578
+
151578
+You must find the SSL "thumbprint" of your VMware server.  How to do
151578
+this is explained in L<nbdkit-vddk-plugin(1)>, also available at the
151578
+link given in item 2 above.
151578
+
151578
+=item 5.
151578
+
151578
+VDDK imports require a feature added in libvirt E<ge> 3.7.
151578
+
151578
+=back
151578
+
151578
+=head2 VDDK: URI
151578
+
151578
+Construct the correct C<vpx://> (for vCenter) or C<esx://> (for ESXi)
151578
+URL.  It will look something like these:
151578
+
151578
+ vpx://root@vcenter.example.com/Datacenter/esxi
151578
+
151578
+ esx://root@esxi.example.com
151578
+
151578
+To verify that you have the correct URL, use the L<virsh(1)> command
151578
+to list the guests on the server:
151578
+
151578
+ $ virsh -c 'vpx://root@vcenter.example.com/Datacenter/esxi' list --all
151578
+ Enter root's password for vcenter.example.com: ***
151578
+ 
151578
+  Id    Name                           State
151578
+ ----------------------------------------------------
151578
+  -     Fedora 20                      shut off
151578
+  -     Windows 2003                   shut off
151578
+
151578
+If you get an error "Peer certificate cannot be authenticated with
151578
+given CA certificates" or similar, then you can either import the
151578
+vCenter host’s certificate, or bypass signature verification by adding
151578
+the C flag:
151578
+
151578
+ $ virsh -c 'vpx://root@vcenter.example.com/Datacenter/esxi?no_verify=1' list --all
151578
+
151578
+You should also try dumping the metadata from any guest on your
151578
+server, like this:
151578
+
151578
+ $ virsh -c 'vpx://root@vcenter.example.com/Datacenter/esxi' dumpxml "Windows 2003"
151578
+ <domain type='vmware'>
151578
+   <name>Windows 2003</name>
151578
+   [...]
151578
+   <vmware:moref>vm-123</vmware:moref>
151578
+ </domain>
151578
+
151578
+If C<E<lt>vmware:morefE<gt>> does not appear in the metadata, then you
151578
+need to upgrade libvirt.
151578
+
151578
+B
151578
+work either>.  Fix your URI and/or your VMware server before
151578
+continuing.
151578
+
151578
+=head2 VDDK: IMPORTING A GUEST
151578
+
151578
+To import a particular guest from vCenter server or ESXi hypervisor,
151578
+use a command like the following, substituting the URI, guest name and
151578
+SSL thumbprint:
151578
+
151578
+ $ export PATH=/path/to/nbdkit:$PATH
151578
+ $ virt-v2v \
151578
+     -ic 'vpx://root@vcenter.example.com/Datacenter/esxi?no_verify=1' \
151578
+     --vddk /path/to/vmware-vix-disklib-distrib \
151578
+     --vddk-thumbprint xx:xx:xx:... \
151578
+     "Windows 2003" \
151578
+     -o local -os /var/tmp
151578
+
151578
+Other options that you might need to add in rare circumstances include
151578
+I<--vddk-config>, I<--vddk-cookie>, I<--vddk-nfchostport>,
151578
+I<--vddk-port>, I<--vddk-snapshot>, I<--vddk-transports> and
151578
+I<--vddk-vimapiver>, which are all explained in the
151578
+L<nbdkit-vddk-plugin(1)> documentation.
151578
+
151578
+=head2 VDDK: DEBUGGING VDDK FAILURES
151578
+
151578
+The VDDK library can be operated in a verbose mode where it gives
151578
+(very) verbose messages.  Use ‘virt-v2v -v -x’ as usual to enable
151578
+verbose messages.
151578
+
151578
 =head1 INPUT FROM XEN
151578
 
151578
 Virt-v2v is able to import Xen guests from RHEL 5 Xen hosts.
151578
@@ -2032,6 +2185,8 @@ L<virt-v2v-copy-to-local(1)>,
151578
 L<virt-v2v-test-harness(1)>,
151578
 L<engine-image-uploader(8)>,
151578
 L<import-to-ovirt.pl|http://git.annexia.org/?p=import-to-ovirt.git>,
151578
+L<nbdkit(1)>,
151578
+L<nbdkit-vddk-plugin(1)>,
151578
 L<http://libguestfs.org/>.
151578
 
151578
 =head1 AUTHORS
151578
-- 
151578
2.14.3
151578