[Pkg-libvirt-commits] [libguestfs] 18/66: v2v: Move target actual size computation into core v2v.ml code.

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 3 14:47:37 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag debian/1%1.27.54-1
in repository libguestfs.

commit eed49ac8971e1bbf95e089203e14b6fb7c6a731a
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Sat Sep 20 10:11:25 2014 +0100

    v2v: Move target actual size computation into core v2v.ml code.
    
    This was only calculated for RHEV, but might benefit other output
    modes in future, and is generic code, so move it into the core code.
---
 v2v/lib_ovf.ml |  26 ++---------
 v2v/types.ml   |   1 +
 v2v/types.mli  |   6 +++
 v2v/v2v.ml     | 134 ++++++++++++++++++++++++++++++++++-----------------------
 4 files changed, 90 insertions(+), 77 deletions(-)

diff --git a/v2v/lib_ovf.ml b/v2v/lib_ovf.ml
index 6d41c4c..5bb1794 100644
--- a/v2v/lib_ovf.ml
+++ b/v2v/lib_ovf.ml
@@ -337,18 +337,13 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
       in
       let size_gb = bytes_to_gb ov.ov_virtual_size in
       let actual_size_gb =
-        if Sys.file_exists t.target_file then (
-          let actual_size = du t.target_file in
-          if actual_size > 0L then Some (bytes_to_gb actual_size)
-          else None
-        ) else (
+        match t.target_actual_size, t.target_estimated_size with
+        | Some actual_size, _ -> Some (bytes_to_gb actual_size)
           (* In the --no-copy case the target file does not exist.  In
            * that case we use the estimated size.
            *)
-          match t.target_estimated_size with
-          | None -> None
-          | Some size -> Some (bytes_to_gb size)
-        ) in
+        | None, Some estimated_size -> Some (bytes_to_gb estimated_size)
+        | None, None -> None in
 
       let format_for_rhev =
         match t.target_format with
@@ -416,19 +411,6 @@ and add_disks targets guestcaps output_alloc sd_uuid image_uuid vol_uuids ovf =
       append_child item virtualhardware_section;
   ) (List.combine targets vol_uuids)
 
-and du filename =
-  (* There's no OCaml binding for st_blocks, so run coreutils 'du'
-   * to get the used size in bytes.
-   *)
-  let cmd = sprintf "du -b %s | awk '{print $1}'" (quote filename) in
-  let lines = external_command ~prog cmd in
-  (* We really don't want the metadata generation to fail because
-   * of some silly usage information, so ignore errors here.
-   *)
-  match lines with
-  | line::_ -> (try Int64.of_string line with _ -> 0L)
-  | [] -> 0L
-
 (* This modifies the OVF DOM, adding a section for each NIC. *)
 and add_networks nics guestcaps ovf =
   let network_section =
diff --git a/v2v/types.ml b/v2v/types.ml
index 95b56f0..38b6fef 100644
--- a/v2v/types.ml
+++ b/v2v/types.ml
@@ -137,6 +137,7 @@ type target = {
   target_file : string;
   target_format : string;
   target_estimated_size : int64 option;
+  target_actual_size : int64 option;
   target_overlay : overlay;
 }
 
diff --git a/v2v/types.mli b/v2v/types.mli
index 783df18..7d7e6c7 100644
--- a/v2v/types.mli
+++ b/v2v/types.mli
@@ -83,7 +83,13 @@ val string_of_overlay : overlay -> string
 type target = {
   target_file : string;      (** Destination file. *)
   target_format : string;    (** Destination format (eg. -of option). *)
+
+  (* Note that the estimate is filled in by core v2v.ml code before
+   * copying starts, and the actual size is filled in after copying
+   * (but may not be filled in if [--no-copy] so don't rely on it).
+   *)
   target_estimated_size : int64 option; (** Est. max. space taken on target. *)
+  target_actual_size : int64 option; (** Actual size on target. *)
 
   target_overlay : overlay;  (** Link back to the overlay disk. *)
 }
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index 7f0ac9c..bf5295c 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -172,9 +172,11 @@ let rec main () =
 
         (* output#prepare_targets will fill in the target_file field.
          * estimate_target_size will fill in the target_estimated_size field.
+         * actual_target_size will fill in the target_actual_size field.
          *)
         { target_file = ""; target_format = format;
           target_estimated_size = None;
+          target_actual_size = None;
           target_overlay = ov }
     ) overlays in
   let targets = output#prepare_targets source targets in
@@ -253,61 +255,68 @@ let rec main () =
 
   let delete_target_on_exit = ref true in
 
-  if do_copy then (
-    (* Copy the source to the output. *)
-    at_exit (fun () ->
-      if !delete_target_on_exit then (
-        List.iter (
-          fun t -> try Unix.unlink t.target_file with _ -> ()
-        ) targets
-      )
-    );
-    let nr_disks = List.length targets in
-    iteri (
-      fun i t ->
-        msg (f_"Copying disk %d/%d to %s (%s)")
-          (i+1) nr_disks t.target_file t.target_format;
-        if verbose then printf "%s%!" (string_of_target t);
-
-        (* We noticed that qemu sometimes corrupts the qcow2 file on
-         * exit.  This only seemed to happen with lazy_refcounts was
-         * used.  The symptom was that the header wasn't written back
-         * to the disk correctly and the file appeared to have no
-         * backing file.  Just sanity check this here.
-         *)
-        let overlay_file = t.target_overlay.ov_overlay_file in
-        if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then
-          error (f_"internal error: qemu corrupted the overlay file");
-
-        (* It turns out that libguestfs's disk creation code is
-         * considerably more flexible and easier to use than qemu-img, so
-         * create the disk explicitly using libguestfs then pass the
-         * 'qemu-img convert -n' option so qemu reuses the disk.
-         *)
-        (* What output preallocation mode should we use? *)
-        let preallocation =
-          match t.target_format, output_alloc with
-          | "raw", `Sparse -> Some "sparse"
-          | "raw", `Preallocated -> Some "full"
-          | "qcow2", `Sparse -> Some "off" (* ? *)
-          | "qcow2", `Preallocated -> Some "metadata"
-          | _ -> None (* ignore -oa flag for other formats *) in
-        let compat =
-          match t.target_format with "qcow2" -> Some "1.1" | _ -> None in
-        (new G.guestfs ())#disk_create
-          t.target_file t.target_format t.target_overlay.ov_virtual_size
-          ?preallocation ?compat;
-
-        let cmd =
-          sprintf "qemu-img convert%s -n -f qcow2 -O %s %s %s"
-            (if not quiet then " -p" else "")
-            (quote t.target_format) (quote overlay_file)
-            (quote t.target_file) in
-        if verbose then printf "%s\n%!" cmd;
-        if Sys.command cmd <> 0 then
-          error (f_"qemu-img command failed, see earlier errors");
-    ) targets
-  ) (* do_copy *);
+  let targets =
+    if not do_copy then targets
+    else (
+      (* Copy the source to the output. *)
+      at_exit (fun () ->
+        if !delete_target_on_exit then (
+          List.iter (
+            fun t -> try Unix.unlink t.target_file with _ -> ()
+          ) targets
+        )
+      );
+      let nr_disks = List.length targets in
+      mapi (
+        fun i t ->
+          msg (f_"Copying disk %d/%d to %s (%s)")
+            (i+1) nr_disks t.target_file t.target_format;
+          if verbose then printf "%s%!" (string_of_target t);
+
+          (* We noticed that qemu sometimes corrupts the qcow2 file on
+           * exit.  This only seemed to happen with lazy_refcounts was
+           * used.  The symptom was that the header wasn't written back
+           * to the disk correctly and the file appeared to have no
+           * backing file.  Just sanity check this here.
+           *)
+          let overlay_file = t.target_overlay.ov_overlay_file in
+          if not ((new G.guestfs ())#disk_has_backing_file overlay_file) then
+            error (f_"internal error: qemu corrupted the overlay file");
+
+          (* It turns out that libguestfs's disk creation code is
+           * considerably more flexible and easier to use than qemu-img, so
+           * create the disk explicitly using libguestfs then pass the
+           * 'qemu-img convert -n' option so qemu reuses the disk.
+           *)
+          (* What output preallocation mode should we use? *)
+          let preallocation =
+            match t.target_format, output_alloc with
+            | "raw", `Sparse -> Some "sparse"
+            | "raw", `Preallocated -> Some "full"
+            | "qcow2", `Sparse -> Some "off" (* ? *)
+            | "qcow2", `Preallocated -> Some "metadata"
+            | _ -> None (* ignore -oa flag for other formats *) in
+          let compat =
+            match t.target_format with "qcow2" -> Some "1.1" | _ -> None in
+          (new G.guestfs ())#disk_create
+            t.target_file t.target_format t.target_overlay.ov_virtual_size
+            ?preallocation ?compat;
+
+          let cmd =
+            sprintf "qemu-img convert%s -n -f qcow2 -O %s %s %s"
+              (if not quiet then " -p" else "")
+              (quote t.target_format) (quote overlay_file)
+              (quote t.target_file) in
+          if verbose then printf "%s\n%!" cmd;
+          if Sys.command cmd <> 0 then
+            error (f_"qemu-img command failed, see earlier errors");
+
+          (* Calculate the actual size on the target, returns an updated
+           * target structure.
+           *)
+          actual_target_size t
+      ) targets
+    ) (* do_copy *) in
 
   (* Create output metadata. *)
   msg (f_"Creating output metadata");
@@ -568,4 +577,19 @@ and estimate_target_size ~verbose mpstats targets =
     targets
   )
 
+(* Update the target_actual_size field in the target structure. *)
+and actual_target_size target =
+  { target with target_actual_size = du target.target_file }
+
+(* There's no OCaml binding for st_blocks, so run coreutils 'du'
+ * to get the used size in bytes.
+ *)
+and du filename =
+  let cmd = sprintf "du -b %s | awk '{print $1}'" (quote filename) in
+  let lines = external_command ~prog cmd in
+  (* Ignore errors because we want to avoid failures after copying. *)
+  match lines with
+  | line::_ -> (try Some (Int64.of_string line) with _ -> None)
+  | [] -> None
+
 let () = run_main_and_handle_errors ~prog main

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list