[Libguestfs] [PATCH v5 5/7] v2v: -o libvirt: switch away from virsh

Pino Toscano ptoscano at redhat.com
Mon May 20 11:56:16 UTC 2019


Now that we have a proper libvirt connection object, use it directly to
refresh the storage pool, and define the final guest.  This avoids
spawning a new virsh process twice, with no possibility to even share a
possible authentication required.
---
 v2v/output_libvirt.ml | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/v2v/output_libvirt.ml b/v2v/output_libvirt.ml
index 712b21f5e..abd1c8443 100644
--- a/v2v/output_libvirt.ml
+++ b/v2v/output_libvirt.ml
@@ -160,14 +160,14 @@ object (self)
     (* We copied directly into the final pool directory.  However we
      * have to tell libvirt.
      *)
-    let cmd = [ "virsh" ] @
-      (if quiet () then [ "-q" ] else []) @
-      (match oc with
-      | None -> []
-      | Some uri -> [ "-c"; uri; ]) @
-      [ "pool-refresh"; output_pool ] in
-    if run_command cmd <> 0 then
-      warning (f_"could not refresh libvirt pool %s") output_pool;
+    (try
+      let pool = Libvirt_utils.get_pool self#conn output_pool in
+      Libvirt.Pool.refresh (Libvirt.Pool.const pool)
+    with
+      Libvirt.Virterror { message } ->
+        warning (f_"could not refresh libvirt pool ‘%s’: %s")
+          output_pool (Option.default "" message)
+    );
 
     let pool_name =
       match pool_name with
@@ -198,17 +198,14 @@ object (self)
     );
 
     (* Define the domain in libvirt. *)
-    let cmd = [ "virsh" ] @
-      (if quiet () then [ "-q" ] else []) @
-      (match oc with
-      | None -> []
-      | Some uri -> [ "-c"; uri; ]) @
-      [ "define"; tmpfile ] in
-    if run_command cmd = 0 then (
-      try Unix.unlink tmpfile with _ -> ()
-    ) else (
-      warning (f_"could not define libvirt domain.  The libvirt XML is still available in ‘%s’.  Try running ‘virsh define %s’ yourself instead.")
-        tmpfile tmpfile
+    (try
+      ignore (Libvirt.Domain.define_xml self#conn (DOM.doc_to_string doc));
+      (try Unix.unlink tmpfile with _ -> ())
+    with
+      Libvirt.Virterror { message } ->
+        warning (f_"could not define libvirt domain: %s.\nThe libvirt XML is still available in ‘%s’.  Try running ‘virsh -c %s define %s’ yourself instead.")
+          (Option.default "" message) tmpfile
+          (Libvirt.Connect.get_uri self#conn) tmpfile
     );
 end
 
-- 
2.21.0




More information about the Libguestfs mailing list