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

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