[Libguestfs] [PATCH 4/4] v2v: Refactor the map_source_to_uri function.

Richard W.M. Jones rjones at redhat.com
Fri Oct 9 11:53:25 UTC 2015


Move this function to the VCenter module.  This is easier since it
doesn't have to do dcPath calculation (see previous commit).

The readhead parameter is no longer labelled.
---
 v2v/input_libvirt_vcenter_https.ml | 94 ++------------------------------------
 v2v/vCenter.ml                     | 74 ++++++++++++++++++++++++++++++
 v2v/vCenter.mli                    | 10 ++++
 3 files changed, 89 insertions(+), 89 deletions(-)

diff --git a/v2v/input_libvirt_vcenter_https.ml b/v2v/input_libvirt_vcenter_https.ml
index a5b3c8b..8310dd5 100644
--- a/v2v/input_libvirt_vcenter_https.ml
+++ b/v2v/input_libvirt_vcenter_https.ml
@@ -33,89 +33,6 @@ open Printf
 let readahead_for_conversion = None
 let readahead_for_copying = Some (64 * 1024 * 1024)
 
-(* Map the <source/> string to a qemu URI using the cURL driver
- * in qemu.  The 'path' will be something like
- *
- *   "[datastore1] Fedora 20/Fedora 20.vmdk"
- *
- * including those literal spaces in the string.
- *
- * 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
- * place of "-flat.vmdk".
- *)
-let source_re = Str.regexp "^\\[\\(.*\\)\\] \\(.*\\)\\.vmdk$"
-
-let map_source_to_uri ?readahead dcPath password uri scheme server path =
-  if not (Str.string_match source_re path 0) then
-    path
-  else (
-    let datastore = Str.matched_group 1 path
-    and path = Str.matched_group 2 path in
-
-    let port =
-      match uri.uri_port with
-      | 443 -> ""
-      | n when n >= 1 -> ":" ^ string_of_int n
-      | _ -> "" in
-
-    let url =
-      sprintf
-        "https://%s%s/folder/%s-flat.vmdk?dcPath=%s&dsName=%s"
-        server port
-        (uri_quote path) (uri_quote dcPath) (uri_quote datastore) in
-
-    (* If no_verify=1 was passed in the libvirt URI, then we have to
-     * turn off certificate verification here too.
-     *)
-    let sslverify =
-      match uri.uri_query_raw with
-      | None -> true
-      | Some query ->
-        (* XXX only works if the query string is not URI-quoted *)
-        String.find query "no_verify=1" = -1 in
-
-    (* Now we have to query the server to get the session cookie. *)
-    let session_cookie =
-      VCenter.get_session_cookie password scheme uri sslverify url in
-
-    (* Construct the JSON parameters. *)
-    let json_params = [
-      "file.driver", JSON.String "https";
-      "file.url", JSON.String url;
-      (* https://bugzilla.redhat.com/show_bug.cgi?id=1146007#c10 *)
-      "file.timeout", JSON.Int 2000;
-    ] in
-
-    let json_params =
-      match readahead with
-      | None -> json_params
-      | Some readahead ->
-        ("file.readahead", JSON.Int readahead) :: json_params in
-
-    let json_params =
-      if sslverify then json_params
-      else ("file.sslverify", JSON.String "off") :: json_params in
-
-    let json_params =
-      match session_cookie with
-      | None -> json_params
-      | Some cookie -> ("file.cookie", JSON.String cookie) :: json_params in
-
-    if verbose () then
-      printf "vcenter: json parameters: %s\n" (JSON.string_of_doc json_params);
-
-    (* Turn the JSON parameters into a 'json:' protocol string.
-     * Note this requires qemu-img >= 2.1.0.
-     *)
-    let qemu_uri = "json: " ^ JSON.string_of_doc json_params in
-
-    qemu_uri
-  )
-
 (* Subclass specialized for handling VMware vCenter over https. *)
 class input_libvirt_vcenter_https
   cmdline_dcPath password libvirt_uri parsed_uri scheme server guest =
@@ -190,9 +107,9 @@ object
       | { p_source = P_source_dev _ } -> assert false
       | { p_source_disk = disk; p_source = P_dont_rewrite } -> disk
       | { p_source_disk = disk; p_source = P_source_file path } ->
-        let qemu_uri = map_source_to_uri ?readahead
-	                                 dcPath password
-                                         parsed_uri scheme server path in
+        let qemu_uri =
+          VCenter.map_source_to_uri readahead dcPath password
+                                    parsed_uri scheme server path in
 
         (* The libvirt ESX driver doesn't normally specify a format, but
          * the format of the -flat file is *always* raw, so force it here.
@@ -212,9 +129,8 @@ object
     | Some orig_path ->
       let readahead = readahead_for_copying in
       let backing_qemu_uri =
-        map_source_to_uri ?readahead
-                          dcPath password
-                          parsed_uri scheme server orig_path in
+        VCenter.map_source_to_uri readahead dcPath password
+                                  parsed_uri scheme server orig_path in
 
       (* Rebase the qcow2 overlay to adjust the readahead parameter. *)
       let cmd =
diff --git a/v2v/vCenter.ml b/v2v/vCenter.ml
index 9ff9415..49b3f32 100644
--- a/v2v/vCenter.ml
+++ b/v2v/vCenter.ml
@@ -21,6 +21,7 @@ open Printf
 open Common_utils
 open Common_gettext.Gettext
 
+open Utils
 open Xml
 
 (* Memoized session cookie. *)
@@ -156,3 +157,76 @@ let guess_dcPath uri = function
      default_dc
   | _ ->     (* Don't know, so guess. *)
      default_dc
+
+let source_re = Str.regexp "^\\[\\(.*\\)\\] \\(.*\\)\\.vmdk$"
+
+let map_source_to_uri readahead dcPath password uri scheme server path =
+  if not (Str.string_match source_re path 0) then
+    path
+  else (
+    let datastore = Str.matched_group 1 path
+    and path = Str.matched_group 2 path in
+
+    let port =
+      match uri.uri_port with
+      | 443 -> ""
+      | 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
+     * place of "-flat.vmdk".
+     *)
+    let url =
+      sprintf
+        "https://%s%s/folder/%s-flat.vmdk?dcPath=%s&dsName=%s"
+        server port
+        (uri_quote path) (uri_quote dcPath) (uri_quote datastore) in
+
+    (* If no_verify=1 was passed in the libvirt URI, then we have to
+     * turn off certificate verification here too.
+     *)
+    let sslverify =
+      match uri.uri_query_raw with
+      | None -> true
+      | Some query ->
+        (* XXX only works if the query string is not URI-quoted *)
+        String.find query "no_verify=1" = -1 in
+
+    (* Now we have to query the server to get the session cookie. *)
+    let session_cookie = get_session_cookie password scheme uri sslverify url in
+
+    (* Construct the JSON parameters. *)
+    let json_params = [
+      "file.driver", JSON.String "https";
+      "file.url", JSON.String url;
+      (* https://bugzilla.redhat.com/show_bug.cgi?id=1146007#c10 *)
+      "file.timeout", JSON.Int 2000;
+    ] in
+
+    let json_params =
+      match readahead with
+      | None -> json_params
+      | Some readahead ->
+        ("file.readahead", JSON.Int readahead) :: json_params in
+
+    let json_params =
+      if sslverify then json_params
+      else ("file.sslverify", JSON.String "off") :: json_params in
+
+    let json_params =
+      match session_cookie with
+      | None -> json_params
+      | Some cookie -> ("file.cookie", JSON.String cookie) :: json_params in
+
+    if verbose () then
+      printf "vcenter: json parameters: %s\n" (JSON.string_of_doc json_params);
+
+    (* Turn the JSON parameters into a 'json:' protocol string.
+     * Note this requires qemu-img >= 2.1.0.
+     *)
+    let qemu_uri = "json: " ^ JSON.string_of_doc json_params in
+
+    qemu_uri
+  )
diff --git a/v2v/vCenter.mli b/v2v/vCenter.mli
index 87583c0..15b5143 100644
--- a/v2v/vCenter.mli
+++ b/v2v/vCenter.mli
@@ -42,3 +42,13 @@ val guess_dcPath : Xml.uri -> string -> string
     This function is only used with [libvirt < 1.2.20] because later
     versions of libvirt provide the dcPath (see
     https://bugzilla.redhat.com/1263574). *)
+
+val map_source_to_uri : int option -> string -> string option -> Xml.uri -> string -> string -> string -> string
+(** [map_source_to_uri readahead dcPath password uri scheme server path]
+    maps the [<source path=...>] string to a qemu URI.
+
+    The [path] will be something like:
+
+    ["[datastore1] Fedora 20/Fedora 20.vmdk"]
+
+    including those literal spaces in the string. *)
-- 
2.5.0




More information about the Libguestfs mailing list