mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From bdeb7926bea75bb62a5165eb94ee4084d678f7fe Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Fri, 13 Oct 2017 17:14:07 +0100
Subject: [PATCH] v2v: vCenter: Handle disks with snapshots (RHBZ#1172425).

This implements a missing feature from old virt-v2v, namely being able
to cope with a guest with snapshots.  Note this only converts the top
(latest) snapshot.  As in old virt-v2v it does NOT convert the whole
chain of snapshots.

(cherry picked from commit c380920034612290eef4476ab4e731c492c68869)
---
 v2v/vCenter.ml | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/v2v/vCenter.ml b/v2v/vCenter.ml
index f53affb48..a1aaf7b7e 100644
--- a/v2v/vCenter.ml
+++ b/v2v/vCenter.ml
@@ -46,6 +46,7 @@ type remote_resource = {
 }
 
 let source_re = Str.regexp "^\\[\\(.*\\)\\] \\(.*\\)\\.vmdk$"
+let snapshot_re = Str.regexp "^\\(.*\\)-[0-9][0-9][0-9][0-9][0-9][0-9]\\(\\.vmdk\\)$"
 
 let rec map_source ?readahead ?password dcPath uri scheme server path =
   (* If no_verify=1 was passed in the libvirt URI, then we have to
@@ -58,7 +59,28 @@ let rec map_source ?readahead ?password dcPath uri scheme server path =
        (* XXX only works if the query string is not URI-quoted *)
        String.find query "no_verify=1" = -1 in
 
-  let https_url = get_https_url dcPath uri server path in
+  let https_url =
+    let https_url = get_https_url dcPath uri server path in
+    (* Check the URL exists. *)
+    let status, _, _ =
+      fetch_headers_from_url password scheme uri sslverify https_url in
+    (* If a disk is actually a snapshot image it will have '-00000n'
+     * appended to its name, e.g.:
+     *   [yellow:storage1] RHEL4-X/RHEL4-X-000003.vmdk
+     * The flat storage is still called RHEL4-X-flat, however. If we got
+     * a 404 and the vmdk name looks like it might be a snapshot, try
+     * again without the snapshot suffix.
+     *)
+    if status = "404" && Str.string_match snapshot_re path 0 then (
+      let path = Str.matched_group 1 path ^ Str.matched_group 2 path in
+      get_https_url dcPath uri server path
+    )
+    else
+      (* Note that other non-200 status errors will be handled
+       * in get_session_cookie below, so we don't have to worry
+       * about them here.
+       *)
+      https_url in
 
   let session_cookie =
     get_session_cookie password scheme uri sslverify https_url in
@@ -115,9 +137,7 @@ and get_https_url dcPath uri server path =
       | n when n >= 1 -> ":" ^ string_of_int n
       | _ -> "" in
 
-    (* XXX Old virt-v2v could also handle snapshots, ie:
-     * "[datastore1] Fedora 20/Fedora 20-NNNNNN.vmdk"
-     * XXX Need to handle templates.  The file is called "-delta.vmdk" in
+    (* XXX Need to handle templates.  The file is called "-delta.vmdk" in
      * place of "-flat.vmdk".
      *)
     sprintf "https://%s%s/folder/%s-flat.vmdk?dcPath=%s&dsName=%s"
-- 
2.14.3