mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0165-v2v-Add-a-C-function-to-fetch-libvirt-hypervisor-cap.patch

ffd6ed
From fe36f57d5f5c19d17c29e04c68526eb4b0118db6 Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Tue, 5 May 2015 15:55:53 +0100
ffd6ed
Subject: [PATCH] v2v: Add a C function to fetch libvirt hypervisor
ffd6ed
 capabilities.
ffd6ed
ffd6ed
Instead of having to run external 'virsh capabilities' command and
ffd6ed
parsing the output.
ffd6ed
ffd6ed
(cherry picked from commit e7833a5eea350ca3579d1f9e6702e116b45572fc)
ffd6ed
---
ffd6ed
 v2v/domainxml-c.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++-
ffd6ed
 v2v/domainxml.ml      |  5 +++-
ffd6ed
 v2v/domainxml.mli     |  7 +++++-
ffd6ed
 v2v/output_libvirt.ml |  9 +-------
ffd6ed
 4 files changed, 74 insertions(+), 11 deletions(-)
ffd6ed
ffd6ed
diff --git a/v2v/domainxml-c.c b/v2v/domainxml-c.c
ffd6ed
index 60157e1..2aebc5c 100644
ffd6ed
--- a/v2v/domainxml-c.c
ffd6ed
+++ b/v2v/domainxml-c.c
ffd6ed
@@ -16,7 +16,9 @@
ffd6ed
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
  */
ffd6ed
 
ffd6ed
-/* [virsh dumpxml] but with non-broken authentication handling. */
ffd6ed
+/* This module implements various [virsh]-like commands, but with
ffd6ed
+ * non-broken authentication handling.
ffd6ed
+ */
ffd6ed
 
ffd6ed
 #include <config.h>
ffd6ed
 
ffd6ed
@@ -360,6 +362,60 @@ v2v_vol_dumpxml (value connv, value poolnamev, value volnamev)
ffd6ed
   CAMLreturn (retv);
ffd6ed
 }
ffd6ed
 
ffd6ed
+value
ffd6ed
+v2v_capabilities (value connv, value unitv)
ffd6ed
+{
ffd6ed
+  CAMLparam2 (connv, unitv);
ffd6ed
+  CAMLlocal1 (capabilitiesv);
ffd6ed
+  const char *conn_uri = NULL;
ffd6ed
+  char *capabilities;
ffd6ed
+  /* We have to assemble the error on the stack because a dynamic
ffd6ed
+   * string couldn't be freed.
ffd6ed
+   */
ffd6ed
+  char errmsg[256];
ffd6ed
+  virErrorPtr err;
ffd6ed
+  virConnectPtr conn;
ffd6ed
+
ffd6ed
+  if (connv != Val_int (0))
ffd6ed
+    conn_uri = String_val (Field (connv, 0)); /* Some conn */
ffd6ed
+
ffd6ed
+  /* We have to call the default authentication handler, not least
ffd6ed
+   * since it handles all the PolicyKit crap.  However it also makes
ffd6ed
+   * coding this simpler.
ffd6ed
+   */
ffd6ed
+  conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
ffd6ed
+                             VIR_CONNECT_RO);
ffd6ed
+  if (conn == NULL) {
ffd6ed
+    if (conn_uri)
ffd6ed
+      snprintf (errmsg, sizeof errmsg,
ffd6ed
+                _("cannot open libvirt connection '%s'"), conn_uri);
ffd6ed
+    else
ffd6ed
+      snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
ffd6ed
+    caml_invalid_argument (errmsg);
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  /* Suppress default behaviour of printing errors to stderr.  Note
ffd6ed
+   * you can't set this to NULL to ignore errors; setting it to NULL
ffd6ed
+   * restores the default error handler ...
ffd6ed
+   */
ffd6ed
+  virConnSetErrorFunc (conn, NULL, ignore_errors);
ffd6ed
+
ffd6ed
+  capabilities = virConnectGetCapabilities (conn);
ffd6ed
+  if (!capabilities) {
ffd6ed
+    err = virGetLastError ();
ffd6ed
+    snprintf (errmsg, sizeof errmsg,
ffd6ed
+              _("cannot get libvirt hypervisor capabilities: %s"),
ffd6ed
+              err->message);
ffd6ed
+    virConnectClose (conn);
ffd6ed
+    caml_invalid_argument (errmsg);
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  capabilitiesv = caml_copy_string (capabilities);
ffd6ed
+  free (capabilities);
ffd6ed
+
ffd6ed
+  CAMLreturn (capabilitiesv);
ffd6ed
+}
ffd6ed
+
ffd6ed
 #else /* !HAVE_LIBVIRT */
ffd6ed
 
ffd6ed
 value
ffd6ed
@@ -380,4 +436,10 @@ v2v_vol_dumpxml (value connv, value poolnamev, value volnamev)
ffd6ed
   caml_invalid_argument ("virt-v2v was compiled without libvirt support");
ffd6ed
 }
ffd6ed
 
ffd6ed
+value
ffd6ed
+v2v_capabilities (value connv, value unitv)
ffd6ed
+{
ffd6ed
+  caml_invalid_argument ("virt-v2v was compiled without libvirt support");
ffd6ed
+}
ffd6ed
+
ffd6ed
 #endif /* !HAVE_LIBVIRT */
ffd6ed
diff --git a/v2v/domainxml.ml b/v2v/domainxml.ml
ffd6ed
index d8b9ed4..3357856 100644
ffd6ed
--- a/v2v/domainxml.ml
ffd6ed
+++ b/v2v/domainxml.ml
ffd6ed
@@ -16,8 +16,11 @@
ffd6ed
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
  *)
ffd6ed
 
ffd6ed
-(* [virsh dumpxml] but with non-broken authentication handling. *)
ffd6ed
+(* This module implements various [virsh]-like commands, but with
ffd6ed
+    non-broken authentication handling. *)
ffd6ed
 
ffd6ed
 external dumpxml : ?password:string -> ?conn:string -> string -> string = "v2v_dumpxml"
ffd6ed
 external pool_dumpxml : ?conn:string -> string -> string = "v2v_pool_dumpxml"
ffd6ed
 external vol_dumpxml : ?conn:string -> string -> string -> string = "v2v_vol_dumpxml"
ffd6ed
+
ffd6ed
+external capabilities : ?conn:string -> unit -> string = "v2v_capabilities"
ffd6ed
diff --git a/v2v/domainxml.mli b/v2v/domainxml.mli
ffd6ed
index 98690fe..089b793 100644
ffd6ed
--- a/v2v/domainxml.mli
ffd6ed
+++ b/v2v/domainxml.mli
ffd6ed
@@ -16,7 +16,8 @@
ffd6ed
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
  *)
ffd6ed
 
ffd6ed
-(** [virsh dumpxml] but with non-broken authentication handling.
ffd6ed
+(** This module implements various [virsh]-like commands, but with
ffd6ed
+    non-broken authentication handling.
ffd6ed
 
ffd6ed
     If you do [virsh dumpxml foo] and if the libvirt source (eg. ESX)
ffd6ed
     requires an interactive password, then virsh unhelpfully sends the
ffd6ed
@@ -38,3 +39,7 @@ val vol_dumpxml : ?conn:string -> string -> string -> string
ffd6ed
     which is part of the pool [pool].
ffd6ed
     The optional [?conn] parameter is the libvirt connection URI.
ffd6ed
     [pool] may be a pool name or UUID. *)
ffd6ed
+
ffd6ed
+val capabilities : ?conn:string -> unit -> string
ffd6ed
+(** [capabilities ?conn ()] returns the libvirt capabilities XML.
ffd6ed
+    The optional [?conn] parameter is the libvirt connection URI. *)
ffd6ed
diff --git a/v2v/output_libvirt.ml b/v2v/output_libvirt.ml
ffd6ed
index 48d39e2..b516d51 100644
ffd6ed
--- a/v2v/output_libvirt.ml
ffd6ed
+++ b/v2v/output_libvirt.ml
ffd6ed
@@ -325,14 +325,7 @@ class output_libvirt verbose oc output_pool = object
ffd6ed
 
ffd6ed
   method prepare_targets source targets =
ffd6ed
     (* Get the capabilities from libvirt. *)
ffd6ed
-    let cmd =
ffd6ed
-      match oc with
ffd6ed
-      | None -> "virsh capabilities"
ffd6ed
-      | Some uri -> sprintf "virsh -c %s capabilities" (quote uri) in
ffd6ed
-    if verbose then printf "%s\n%!" cmd;
ffd6ed
-    let xml = external_command ~prog cmd in
ffd6ed
-    let xml = String.concat "\n" xml in
ffd6ed
-
ffd6ed
+    let xml = Domainxml.capabilities ?conn:oc () in
ffd6ed
     if verbose then printf "libvirt capabilities XML:\n%s\n%!" xml;
ffd6ed
 
ffd6ed
     (* This just checks that the capabilities XML is well-formed,
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed