[Libguestfs] [PATCH v2 4/4] v2v: reuse main guestfs for virtio win drivers iso

Roman Kagan rkagan at virtuozzo.com
Tue Oct 13 12:50:54 UTC 2015


If we're given an ISO image as the source of virtio windows drivers, and
the backend supports hot-adding drives, reuse the same guestfs handle
that is open for accessing the guest being inspected, rather than
creating a new one (which would run another virtual machine instance and
is fairly resource-intensive).

Signed-off-by: Roman Kagan <rkagan at virtuozzo.com>
---
changes since v1:
 - updated usage of string functions
 - add missing and remove excessive parentheses
 - preserve support for backends with no hot-add capability

 v2v/convert_windows.ml | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/v2v/convert_windows.ml b/v2v/convert_windows.ml
index cfa5474..5fd8bb6 100644
--- a/v2v/convert_windows.ml
+++ b/v2v/convert_windows.ml
@@ -294,13 +294,25 @@ echo uninstalling Xen PV driver
     )
     else if is_regular_file virtio_win then (
       try
-        let g2 = new Guestfs.guestfs () in
-        if trace () then g2#set_trace true;
-        if verbose () then g2#set_verbose true;
-        g2#add_drive_opts virtio_win ~readonly:true;
-        g2#launch ();
-        let vio_root = "/" in
-        g2#mount_ro "/dev/sda" vio_root;
+        let label = "viowin" in
+        let vio_root = "/virtio-win-iso" in
+        let g2 =
+          (* not all backends support hot-adding drives; create an extra
+           * guestfs handle in that case *)
+          try
+            g#add_drive_opts virtio_win ~readonly:true ~label:label;
+            g
+          with Guestfs.Error msg -> (
+            let g2 = new Guestfs.guestfs () in
+            if trace () then g2#set_trace true;
+            if verbose () then g2#set_verbose true;
+            g2#add_drive_opts virtio_win ~readonly:true ~label:label;
+            g2#launch ();
+            g2
+          ) in
+
+        g2#mkmountpoint vio_root;
+        g2#mount_ro ("/dev/disk/guestfs" // label) vio_root;
         let paths = g2#find vio_root in
         Array.iter (
           fun path ->
@@ -315,10 +327,18 @@ echo uninstalling Xen PV driver
                 printf "Copying virtio driver bits: '%s:%s' -> '%s'\n"
                   virtio_win path target;
 
-              g#write target (g2#read_file source)
+              if (g2 == g) then
+                g#cp source target
+              else
+                g#write target (g2#read_file source)
             )
         ) paths;
-        g2#close()
+        g2#umount vio_root;
+        g2#rmmountpoint vio_root;
+        if (g2 == g) then
+          g2#remove_drive label
+        else
+          g2#close()
       with Guestfs.Error msg ->
         error (f_"%s: cannot open virtio-win ISO file: %s") virtio_win msg
     )
-- 
2.4.3




More information about the Libguestfs mailing list