psss / rpms / libguestfs

Forked from rpms/libguestfs 5 years ago
Clone

Blame 0001-builder-Don-t-run-virt-resize-when-not-necessary.patch

51fb412
From 33b3a097b4b7720d4d43ebe15e9881e976f70e4b Mon Sep 17 00:00:00 2001
384b531
From: "Richard W.M. Jones" <rjones@redhat.com>
384b531
Date: Tue, 22 Oct 2013 13:52:25 +0100
384b531
Subject: [PATCH] builder: Don't run virt-resize when not necessary.
384b531
384b531
If:
384b531
 - the output is a regular file
384b531
 - the output format is raw
384b531
 - the user didn't specify the --size option
384b531
then we don't need to run virt-resize.  Simply uncompress
384b531
the template directly to the output file.
384b531
384b531
(cherry picked from commit 2f3a3e308af8012501437f723471a66537ecbf64)
384b531
---
2026e90
 builder/builder.ml | 70 +++++++++++++++++++++++++++++++++++++-----------------
2026e90
 1 file changed, 48 insertions(+), 22 deletions(-)
384b531
384b531
diff --git a/builder/builder.ml b/builder/builder.ml
51fb412
index 25ab8c4..16005e1 100644
384b531
--- a/builder/builder.ml
384b531
+++ b/builder/builder.ml
384b531
@@ -232,7 +232,12 @@ let main () =
384b531
 
384b531
       Sigchecker.verify_detached sigchecker template sigfile in
384b531
 
384b531
-  let output, size, format, delete_output_file, resize_sparse =
384b531
+  (* Plan how to create the output.  This depends on:
384b531
+   * - did the user specify --output?
384b531
+   * - is the output a block device?
384b531
+   * - did the user specify --size?
384b531
+   *)
384b531
+  let output, size, format, delete_output_file, do_resize, resize_sparse =
384b531
     let is_block_device file =
384b531
       try (stat file).st_kind = S_BLK
384b531
       with Unix_error _ -> false
384b531
@@ -259,22 +264,22 @@ let main () =
384b531
       (* Dummy: The output file is never deleted in this case. *)
384b531
       let delete_output_file = ref false in
384b531
 
384b531
-      output, None, format, delete_output_file, false
384b531
+      output, None, format, delete_output_file, true, false
384b531
 
384b531
     (* Regular file output.  Note the file gets deleted. *)
384b531
     | _ ->
384b531
       (* Check the --size option. *)
384b531
-      let size =
384b531
+      let size, do_resize =
384b531
         let { Index_parser.size = default_size } = entry in
384b531
         match size with
384b531
-        | None -> default_size +^ headroom
384b531
+        | None -> default_size, false
384b531
         | Some size ->
384b531
           if size < default_size +^ headroom then (
384b531
             eprintf (f_"%s: --size is too small for this disk image, minimum size is %s\n")
384b531
               prog (human_size default_size);
384b531
             exit 1
384b531
           );
384b531
-          size in
384b531
+          size, true in
384b531
 
384b531
       (* Create the output file. *)
384b531
       let output, format =
384b531
@@ -285,6 +290,14 @@ let main () =
384b531
         | Some output, None -> output, "raw"
384b531
         | Some output, Some format -> output, format in
384b531
 
384b531
+      (* If the input format != output format then we must run virt-resize. *)
384b531
+      let do_resize =
384b531
+        let input_format =
384b531
+          match entry with
384b531
+          | { Index_parser.format = Some format } -> format
384b531
+          | { Index_parser.format = None } -> "raw" in
384b531
+        if input_format <> format then true else do_resize in
384b531
+
384b531
       msg (f_"Creating disk image: %s") output;
384b531
       let cmd =
384b531
         sprintf "qemu-img create -f %s%s %s %Ld%s"
2026e90
@@ -308,30 +321,42 @@ let main () =
384b531
       in
384b531
       at_exit delete_file;
384b531
 
384b531
-      output, Some size, format, delete_output_file, true in
384b531
+      output, Some size, format, delete_output_file, do_resize, true in
384b531
 
2026e90
-  if is_char_device output then (
2026e90
-    eprintf (f_"%s: cannot output to a character device or /dev/null\n") prog;
2026e90
-    exit 1
2026e90
-  );
2026e90
-
384b531
-  let source =
384b531
-    (* Uncompress it to a temporary file. *)
384b531
+  if not do_resize then (
384b531
+    (* If the user did not specify --size and the output is a regular
384b531
+     * file and the format is raw, then we just uncompress the template
384b531
+     * directly to the output file.  This is fast but less flexible.
384b531
+     *)
384b531
     let { Index_parser.file_uri = file_uri } = entry in
384b531
-    let tmpfile = Filename.temp_file "vbsrc" ".img" in
384b531
-    let cmd = sprintf "xzcat %s > %s" (quote template) (quote tmpfile) in
384b531
+    let cmd = sprintf "xzcat %s > %s" (quote template) (quote output) in
384b531
     if debug then eprintf "%s\n%!" cmd;
384b531
     msg (f_"Uncompressing: %s") file_uri;
384b531
     let r = Sys.command cmd in
384b531
     if r <> 0 then (
384b531
       eprintf (f_"%s: error: failed to uncompress template\n") prog;
384b531
       exit 1
384b531
-    );
384b531
-    unlink_on_exit tmpfile;
384b531
-    tmpfile in
384b531
+    )
384b531
+  ) else (
384b531
+    (* If none of the above apply, uncompress to a temporary file and
384b531
+     * run virt-resize on the result.
384b531
+     *)
384b531
+    let tmpfile =
384b531
+      (* Uncompress it to a temporary file. *)
384b531
+      let { Index_parser.file_uri = file_uri } = entry in
384b531
+      let tmpfile = Filename.temp_file "vbsrc" ".img" in
384b531
+      let cmd = sprintf "xzcat %s > %s" (quote template) (quote tmpfile) in
384b531
+      if debug then eprintf "%s\n%!" cmd;
384b531
+      msg (f_"Uncompressing: %s") file_uri;
384b531
+      let r = Sys.command cmd in
384b531
+      if r <> 0 then (
384b531
+        eprintf (f_"%s: error: failed to uncompress template\n") prog;
384b531
+        exit 1
384b531
+      );
384b531
+      unlink_on_exit tmpfile;
384b531
+      tmpfile in
384b531
 
384b531
-  (* Resize the source to the output file. *)
384b531
-  let () =
384b531
+    (* Resize the source to the output file. *)
384b531
     (match size with
384b531
     | None ->
384b531
       msg (f_"Running virt-resize to expand the disk")
2026e90
@@ -356,13 +381,14 @@ let main () =
384b531
         (match lvexpand with
384b531
         | None -> ""
384b531
         | Some lvexpand -> sprintf " --lv-expand %s" (quote lvexpand))
384b531
-        (quote source) (quote output) in
384b531
+        (quote tmpfile) (quote output) in
384b531
     if debug then eprintf "%s\n%!" cmd;
384b531
     let r = Sys.command cmd in
384b531
     if r <> 0 then (
384b531
       eprintf (f_"%s: error: virt-resize failed\n") prog;
384b531
       exit 1
384b531
-    ) in
384b531
+    )
384b531
+  );
384b531
 
384b531
   (* Now mount the output disk so we can make changes. *)
384b531
   msg (f_"Opening the new disk");
384b531
-- 
0d2edcd
1.8.4.2
384b531