[Libguestfs] [PATCH] v2v: -o null: Use the qemu null device driver.

Richard W.M. Jones rjones at redhat.com
Thu Dec 7 16:21:14 UTC 2017


Instead of writing to a temporary file and deleting it, use the null
block driver in qemu to throw it away.
---
 v2v/output_null.ml | 47 +++++++++++++++++++++++++++++++++--------------
 v2v/virt-v2v.pod   |  4 ----
 2 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/v2v/output_null.ml b/v2v/output_null.ml
index d01f45654..4d06aa0de 100644
--- a/v2v/output_null.ml
+++ b/v2v/output_null.ml
@@ -26,16 +26,23 @@ open Common_gettext.Gettext
 open Types
 open Utils
 
+(* Notes:
+ *
+ * This only happens to work because we run qemu-img convert
+ * with the -n [no create output] option, since null-co doesn't
+ * support creation.  If -n is removed in the main program then
+ * the tests will break very obviously.
+ *
+ * The null-co device is not zero-sized.  It actually has a fixed
+ * size (defaults to 2^30 I believe).
+ *
+ * qemu-img convert checks the output size and will fail if it's
+ * too small, so we have to set the size.  We could set it to
+ * match the input size but it's easier to set it to some huge
+ * size instead.
+ *)
+
 class output_null =
-  (* It would be nice to be able to write to /dev/null.
-   * Unfortunately qemu-img convert cannot do that.  Instead create a
-   * temporary directory which is always deleted at exit.
-   *)
-  let tmpdir =
-    let base_dir = (open_guestfs ())#get_cachedir () in
-    let t = Mkdtemp.temp_dir ~base_dir "null." in
-    rmdir_on_exit t;
-    t in
 object
   inherit output
 
@@ -44,13 +51,25 @@ object
   method supported_firmware = [ TargetBIOS; TargetUEFI ]
 
   method prepare_targets source targets =
-    List.map (
-      fun t ->
-        let target_file = tmpdir // t.target_overlay.ov_sd in
-        { t with target_file = target_file }
-    ) targets
+    let json_params = [
+      "file.driver", JSON.String "null-co";
+      "file.size", JSON.String "1E";
+    ] in
+    let target_file = "json:" ^ JSON.string_of_doc json_params in
+    (* XXX It's not really right to set target_format here, but
+     * it works.
+     *)
+    let target_format = "raw" in
+    List.map (fun t -> { t with target_file; target_format }) targets
 
   method create_metadata _ _ _ _ _ _ = ()
+
+  (* Stops the main program from calling g#disk_create to try to create
+   * the null device.
+   *)
+  method disk_create ?backingfile ?backingformat ?preallocation ?compat
+                     ?clustersize path format size =
+    ()
 end
 
 let output_null () = new output_null
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
index aad06ead0..4ac44988e 100644
--- a/v2v/virt-v2v.pod
+++ b/v2v/virt-v2v.pod
@@ -1901,10 +1901,6 @@ This temporarily places a full copy of the output disks in C<$TMPDIR>.
 You must ensure there is sufficient space in the output directory for
 the converted guest.
 
-=item I<-o null>
-
-This temporarily places a full copy of the output disks in C<$TMPDIR>.
-
 =back
 
 See also L</Minimum free space check in the host> below.
-- 
2.13.2




More information about the Libguestfs mailing list