Blame SOURCES/0019-o-disk-o-libvirt-o-qemu-Implement-of-qcow2-oo-compre.patch

696189
From ca3643d06eed2de22cb81ad2eb13ba7f75c0487e Mon Sep 17 00:00:00 2001
696189
From: "Richard W.M. Jones" <rjones@redhat.com>
696189
Date: Fri, 28 Jan 2022 09:30:58 +0000
696189
Subject: [PATCH] -o disk, -o libvirt, -o qemu: Implement -of qcow2 -oo
696189
 compressed
696189
696189
For various output modes, implement -oo compressed which can be used
696189
to generate compressed qcow2 files.  This option was dropped when
696189
modularizing virt-v2v, and required changes to nbdcopy which are
696189
finally upstream in libnbd >= 1.13.5.
696189
696189
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2047660
696189
Fixes: commit 255722cbf39afc0b012e2ac00d16fa6ba2f8c21f
696189
Reported-by: Xiaodai Wang
696189
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
696189
(cherry picked from commit 53690a0c602a4286fdb9408fdf6a01cc352697ec)
696189
---
696189
 TODO                     | 14 --------------
696189
 output/output_disk.ml    | 29 +++++++++++++++++++++--------
696189
 output/output_libvirt.ml | 31 ++++++++++++++++++++++---------
696189
 output/output_qemu.ml    | 38 +++++++++++++++++++++-----------------
696189
 4 files changed, 64 insertions(+), 48 deletions(-)
696189
696189
diff --git a/TODO b/TODO
696189
index f578d506..04b1dd20 100644
696189
--- a/TODO
696189
+++ b/TODO
696189
@@ -1,17 +1,3 @@
696189
-virt-v2v -o disk|qemu -oo compressed
696189
-------------------------------------
696189
-
696189
-This was temporarily dropped when I modularized virt-v2v.  It would
696189
-not be too difficult to add it back.  The following is the qemu-nbd
696189
-command required (to be run as the output helper) which creates a
696189
-compressed qcow2 disk image:
696189
-
696189
-$ qemu-nbd --image-opts driver=compress,file.driver=qcow2,file.file.driver=file,file.file.filename=new.qcow2
696189
-
696189
-Note this requires fixes in nbdcopy so it obeys the advertised block
696189
-alignment:
696189
-https://lists.gnu.org/archive/html/qemu-block/2022-01/threads.html#00729
696189
-
696189
 virt-v2v -o rhv-upload
696189
 ----------------------
696189
 
696189
diff --git a/output/output_disk.ml b/output/output_disk.ml
696189
index bc5b4e1c..abcfcdc0 100644
696189
--- a/output/output_disk.ml
696189
+++ b/output/output_disk.ml
696189
@@ -30,7 +30,7 @@ open Create_libvirt_xml
696189
 open Output
696189
 
696189
 module Disk = struct
696189
-  type poptions = Types.output_allocation * string * string * string
696189
+  type poptions = bool * Types.output_allocation * string * string * string
696189
 
696189
   type t = unit
696189
 
696189
@@ -41,11 +41,21 @@ module Disk = struct
696189
       | None -> ""
696189
 
696189
   let query_output_options () =
696189
-    printf (f_"No output options can be used in this mode.\n")
696189
+    printf (f_"Output options that can be used with -o disk:
696189
+
696189
+  -oo compressed      Compress the output file (used only with -of qcow2)
696189
+")
696189
 
696189
   let parse_options options source =
696189
-    if options.output_options <> [] then
696189
-      error (f_"no -oo (output options) are allowed here");
696189
+    let compressed = ref false in
696189
+    List.iter (
696189
+      function
696189
+      | "compressed", "" -> compressed := true
696189
+      | "compressed", v -> compressed := bool_of_string v
696189
+      | k, _ ->
696189
+         error (f_"-o disk: unknown output option ‘-oo %s’") k
696189
+    ) options.output_options;
696189
+
696189
     if options.output_password <> None then
696189
       error_option_cannot_be_used_in_output_mode "local" "-op";
696189
 
696189
@@ -60,11 +70,13 @@ module Disk = struct
696189
 
696189
     let output_name = Option.default source.s_name options.output_name in
696189
 
696189
-    options.output_alloc, options.output_format, output_name, output_storage
696189
+    !compressed, options.output_alloc, options.output_format,
696189
+    output_name, output_storage
696189
 
696189
   let setup dir options source =
696189
     let disks = get_disks dir in
696189
-    let output_alloc, output_format, output_name, output_storage = options in
696189
+    let compressed, output_alloc, output_format, output_name, output_storage =
696189
+      options in
696189
 
696189
     List.iter (
696189
       fun (i, size) ->
696189
@@ -73,11 +85,12 @@ module Disk = struct
696189
 
696189
         (* Create the actual output disk. *)
696189
         let outdisk = disk_path output_storage output_name i in
696189
-        output_to_local_file output_alloc output_format outdisk size socket
696189
+        output_to_local_file ~compressed output_alloc output_format
696189
+          outdisk size socket
696189
     ) disks
696189
 
696189
   let finalize dir options () source inspect target_meta =
696189
-    let output_alloc, output_format, output_name, output_storage = options in
696189
+    let _, output_alloc, output_format, output_name, output_storage = options in
696189
 
696189
     (* Convert metadata to libvirt XML. *)
696189
     (match target_meta.target_firmware with
696189
diff --git a/output/output_libvirt.ml b/output/output_libvirt.ml
696189
index e0d3432d..04b4c5f8 100644
696189
--- a/output/output_libvirt.ml
696189
+++ b/output/output_libvirt.ml
696189
@@ -32,7 +32,7 @@ open Create_libvirt_xml
696189
 open Output
696189
 
696189
 module Libvirt_ = struct
696189
-  type poptions = Libvirt.rw Libvirt.Connect.t Lazy.t *
696189
+  type poptions = Libvirt.rw Libvirt.Connect.t Lazy.t * bool *
696189
                   Types.output_allocation * string * string * string
696189
 
696189
   type t = string * string
696189
@@ -44,11 +44,21 @@ module Libvirt_ = struct
696189
       | None -> ""
696189
 
696189
   let query_output_options () =
696189
-    printf (f_"No output options can be used in this mode.\n")
696189
+    printf (f_"Output options that can be used with -o libvirt:
696189
+
696189
+  -oo compressed      Compress the output file (used only with -of qcow2)
696189
+")
696189
 
696189
   let parse_options options source =
696189
-    if options.output_options <> [] then
696189
-      error (f_"no -oo (output options) are allowed here");
696189
+    let compressed = ref false in
696189
+    List.iter (
696189
+      function
696189
+      | "compressed", "" -> compressed := true
696189
+      | "compressed", v -> compressed := bool_of_string v
696189
+      | k, _ ->
696189
+         error (f_"-o disk: unknown output option ‘-oo %s’") k
696189
+    ) options.output_options;
696189
+
696189
     if options.output_password <> None then
696189
       error_option_cannot_be_used_in_output_mode "libvirt" "-op";
696189
 
696189
@@ -59,12 +69,13 @@ module Libvirt_ = struct
696189
 
696189
     let output_name = Option.default source.s_name options.output_name in
696189
 
696189
-    (conn, options.output_alloc, options.output_format, output_name,
696189
-     output_pool)
696189
+    (conn, !compressed, options.output_alloc, options.output_format,
696189
+     output_name, output_pool)
696189
 
696189
   let setup dir options source =
696189
     let disks = get_disks dir in
696189
-    let conn, output_alloc, output_format, output_name, output_pool = options in
696189
+    let conn, compressed, output_alloc, output_format,
696189
+        output_name, output_pool = options in
696189
     let conn = Lazy.force conn in
696189
 
696189
     (* Get the capabilities from libvirt. *)
696189
@@ -119,13 +130,15 @@ module Libvirt_ = struct
696189
 
696189
         (* Create the actual output disk. *)
696189
         let outdisk = target_path // output_name ^ "-sd" ^ (drive_name i) in
696189
-        output_to_local_file output_alloc output_format outdisk size socket
696189
+        output_to_local_file ~compressed output_alloc output_format
696189
+          outdisk size socket
696189
     ) disks;
696189
 
696189
     (capabilities_xml, pool_name)
696189
 
696189
   let rec finalize dir options t source inspect target_meta =
696189
-    let conn, output_alloc, output_format, output_name, output_pool = options in
696189
+    let conn, _, output_alloc, output_format, output_name, output_pool =
696189
+      options in
696189
     let capabilities_xml, pool_name = t in
696189
 
696189
     (match target_meta.target_firmware with
696189
diff --git a/output/output_qemu.ml b/output/output_qemu.ml
696189
index 527d3c5e..e7efbb73 100644
696189
--- a/output/output_qemu.ml
696189
+++ b/output/output_qemu.ml
696189
@@ -29,7 +29,8 @@ open Utils
696189
 open Output
696189
 
696189
 module QEMU = struct
696189
-  type poptions = bool * Types.output_allocation * string * string * string
696189
+  type poptions = bool * bool *
696189
+                  Types.output_allocation * string * string * string
696189
 
696189
   type t = unit
696189
 
696189
@@ -42,6 +43,7 @@ module QEMU = struct
696189
   let query_output_options () =
696189
     printf (f_"Output options (-oo) which can be used with -o qemu:
696189
 
696189
+  -oo compressed      Compress the output file (used only with -of qcow2)
696189
   -oo qemu-boot       Boot the guest in qemu after conversion
696189
 ")
696189
 
696189
@@ -49,19 +51,19 @@ module QEMU = struct
696189
     if options.output_password <> None then
696189
       error_option_cannot_be_used_in_output_mode "qemu" "-op";
696189
 
696189
-    let qemu_boot = ref false in
696189
+    let compressed = ref false
696189
+    and qemu_boot = ref false in
696189
     List.iter (
696189
-      fun (k, v) ->
696189
-        match k with
696189
-        | "qemu-boot" ->
696189
-           if v = "" || v = "true" then qemu_boot := true
696189
-           else if v = "false" then qemu_boot := false
696189
-           else
696189
-             error (f_"-o qemu: use -oo qemu-boot[=true|false]")
696189
-        | k ->
696189
-           error (f_"-o qemu: unknown output option ‘-oo %s’") k
696189
-      ) options.output_options;
696189
-    let qemu_boot = !qemu_boot in
696189
+      function
696189
+      | "compressed", "" -> compressed := true
696189
+      | "compressed", v -> compressed := bool_of_string v
696189
+      | "qemu-boot", "" -> qemu_boot := true
696189
+      | "qemu-boot", v -> qemu_boot := bool_of_string v
696189
+      | k, _ ->
696189
+         error (f_"-o qemu: unknown output option ‘-oo %s’") k
696189
+    ) options.output_options;
696189
+    let compressed = !compressed
696189
+    and qemu_boot = !qemu_boot in
696189
 
696189
     if qemu_boot then
696189
       error (f_"-o qemu: the -oo qemu-boot option cannot be used in RHEL");
696189
@@ -77,12 +79,13 @@ module QEMU = struct
696189
 
696189
     let output_name = Option.default source.s_name options.output_name in
696189
 
696189
-    (qemu_boot, options.output_alloc, options.output_format,
696189
+    (compressed, qemu_boot, options.output_alloc, options.output_format,
696189
      output_name, output_storage)
696189
 
696189
   let setup dir options source =
696189
     let disks = get_disks dir in
696189
-    let _, output_alloc, output_format, output_name, output_storage = options in
696189
+    let compressed, _, output_alloc, output_format,
696189
+        output_name, output_storage = options in
696189
 
696189
     List.iter (
696189
       fun (i, size) ->
696189
@@ -91,11 +94,12 @@ module QEMU = struct
696189
 
696189
         (* Create the actual output disk. *)
696189
         let outdisk = disk_path output_storage output_name i in
696189
-        output_to_local_file output_alloc output_format outdisk size socket
696189
+        output_to_local_file ~compressed output_alloc output_format
696189
+          outdisk size socket
696189
     ) disks
696189
 
696189
   let finalize dir options () source inspect target_meta =
696189
-    let qemu_boot, output_alloc, output_format,
696189
+    let _, qemu_boot, output_alloc, output_format,
696189
         output_name, output_storage = options in
696189
 
696189
     let { guestcaps; target_buses; target_firmware } = target_meta in