[Libguestfs] [PATCH] v2v: Add xpath_int64 functions, and use them to read memory values.

Richard W.M. Jones rjones at redhat.com
Thu Oct 8 19:15:03 UTC 2015


On 32 bit platforms, reading the memory values can cause some numbers
to be read as negative numbers.  Fix this by treating memory values as
64 bit integers throughout the parsing and calculation.
---
 v2v/input_libvirtxml.ml |  7 ++++---
 v2v/input_ova.ml        |  7 ++++---
 v2v/utils.ml            | 15 +++++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/v2v/input_libvirtxml.ml b/v2v/input_libvirtxml.ml
index 976848f..d8c6215 100644
--- a/v2v/input_libvirtxml.ml
+++ b/v2v/input_libvirtxml.ml
@@ -54,7 +54,8 @@ let parse_libvirt_xml ?conn xml =
   let xpathctx = Xml.xpath_new_context doc in
   let xpath_string = xpath_string xpathctx
   and xpath_int = xpath_int xpathctx
-  and xpath_int_default = xpath_int_default xpathctx in
+  and xpath_int_default = xpath_int_default xpathctx
+  and xpath_int64_default = xpath_int64_default xpathctx in
 
   let hypervisor =
     match xpath_string "/domain/@type" with
@@ -66,8 +67,8 @@ let parse_libvirt_xml ?conn xml =
     | None | Some "" ->
        error (f_"in the libvirt XML metadata, <name> is missing or empty")
     | Some s -> s in
-  let memory = xpath_int_default "/domain/memory/text()" (1024 * 1024) in
-  let memory = Int64.of_int memory *^ 1024L in
+  let memory = xpath_int64_default "/domain/memory/text()" (1024L *^ 1024L) in
+  let memory = memory *^ 1024L in
   let vcpu = xpath_int_default "/domain/vcpu/text()" 1 in
 
   let features =
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
index a758e94..cd26160 100644
--- a/v2v/input_ova.ml
+++ b/v2v/input_ova.ml
@@ -183,7 +183,8 @@ object
     let xpath_string = xpath_string xpathctx
     and xpath_int = xpath_int xpathctx
     and xpath_string_default = xpath_string_default xpathctx
-    and xpath_int_default = xpath_int_default xpathctx in
+    and xpath_int_default = xpath_int_default xpathctx
+    and xpath_int64_default = xpath_int64_default xpathctx in
 
     (* Search for vm name. *)
     let name =
@@ -193,8 +194,8 @@ object
       | Some name -> name in
 
     (* Search for memory. *)
-    let memory = xpath_int_default "/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=4]/rasd:VirtualQuantity/text()" (1024 * 1024) in
-    let memory = Int64.of_int (memory * 1024 * 1024) in
+    let memory = xpath_int64_default "/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=4]/rasd:VirtualQuantity/text()" (1024L *^ 1024L) in
+    let memory = memory *^ 1024L *^ 1024L in
 
     (* Search for number of vCPUs. *)
     let vcpu = xpath_int_default "/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=3]/rasd:VirtualQuantity/text()" 1 in
diff --git a/v2v/utils.ml b/v2v/utils.ml
index ee956be..2998d90 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -79,6 +79,17 @@ let xpath_int xpathctx expr =
       error (f_"expecting XML expression to return an integer (expression: %s, matching string: %s)")
             expr str
   )
+let xpath_int64 xpathctx expr =
+  let obj = Xml.xpath_eval_expression xpathctx expr in
+  if Xml.xpathobj_nr_nodes obj < 1 then None
+  else (
+    let node = Xml.xpathobj_node obj 0 in
+    let str = Xml.node_as_string node in
+    try Some (Int64.of_string str)
+    with Failure "int_of_string" ->
+      error (f_"expecting XML expression to return an integer (expression: %s, matching string: %s)")
+            expr str
+  )
 
 (* Parse an xpath expression and return a string/int; if the expression
  * doesn't match, return the default.
@@ -91,6 +102,10 @@ let xpath_int_default xpathctx expr default =
   match xpath_int xpathctx expr with
   | None -> default
   | Some i -> i
+let xpath_int64_default xpathctx expr default =
+  match xpath_int64 xpathctx expr with
+  | None -> default
+  | Some i -> i
 
 external drive_name : int -> string = "v2v_utils_drive_name"
 external drive_index : string -> int = "v2v_utils_drive_index"
-- 
2.5.0




More information about the Libguestfs mailing list