Richard W.M. Jones
2017-May-08  09:07 UTC
[Libguestfs] [PATCH 0/3] v2v: -i ova: Prefer pigz or pxz for uncompressing OVA
https://bugzilla.redhat.com/show_bug.cgi?id=1448739
Richard W.M. Jones
2017-May-08  09:07 UTC
[Libguestfs] [PATCH 1/3] v2v: -i ova: Refactor untar function.
---
 v2v/input_ova.ml | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
index 55ea71fb0..c85cca508 100644
--- a/v2v/input_ova.ml
+++ b/v2v/input_ova.ml
@@ -43,10 +43,20 @@ let libvirt_supports_json_raw_driver ()  (* Untar part or
all files from tar archive. If [paths] is specified it is
  * a list of paths in the tar archive.
  *)
-let untar ?(format = "") ?paths file outdir -  let cmd = [
"tar"; sprintf "-x%sf" format; file; "-C"; outdir
]
-            @ match paths with None -> [] | Some p -> p in
-  if run_command cmd <> 0 then
+let untar ?format ?(paths = []) file outdir +  let paths = String.concat "
" (List.map quote paths) in
+  let cmd +    match format with
+    | None ->
+       sprintf "tar -xf %s -C %s %s"
+               (quote file) (quote outdir) paths
+    | Some `GZip ->
+       sprintf "gzip -c -d %s | tar -xf - -C %s %s"
+               (quote file) (quote outdir) paths
+    | Some `XZ ->
+       sprintf "xz -c -d %s | tar -xf - -C %s %s"
+               (quote file) (quote outdir) paths in
+  if shell_command cmd <> 0 then
     error (f_"error unpacking %s, see earlier error messages") file
 
 (* Untar only ovf and manifest from the archive *)
@@ -151,7 +161,6 @@ object
         | (`GZip|`XZ) as format ->
           (match uncompressed_type format ova with
           | `Tar ->
-             let format = match format with `GZip -> "z" | `XZ
-> "J" in
              untar ~format ova tmpdir;
              tmpdir, false
           | `Zip | `GZip | `XZ | `Unknown ->
-- 
2.12.0
Richard W.M. Jones
2017-May-08  09:07 UTC
[Libguestfs] [PATCH 2/3] v2v: -i ova: Refactor constructing zcat command.
---
 v2v/input_ova.ml | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
index c85cca508..e21de35fb 100644
--- a/v2v/input_ova.ml
+++ b/v2v/input_ova.ml
@@ -40,6 +40,10 @@ let libvirt_supports_json_raw_driver ()    else
     true
 
+let zcat_command_of_format = function
+  | `GZip -> "gzip -c -d"
+  | `XZ -> "xz -c -d"
+
 (* Untar part or all files from tar archive. If [paths] is specified it is
  * a list of paths in the tar archive.
  *)
@@ -50,12 +54,10 @@ let untar ?format ?(paths = []) file outdir      | None
->
        sprintf "tar -xf %s -C %s %s"
                (quote file) (quote outdir) paths
-    | Some `GZip ->
-       sprintf "gzip -c -d %s | tar -xf - -C %s %s"
-               (quote file) (quote outdir) paths
-    | Some `XZ ->
-       sprintf "xz -c -d %s | tar -xf - -C %s %s"
-               (quote file) (quote outdir) paths in
+    | Some ((`GZip|`XZ) as format) ->
+       sprintf "%s %s | tar -xf - -C %s %s"
+               (zcat_command_of_format format) (quote file)
+               (quote outdir) paths in
   if shell_command cmd <> 0 then
     error (f_"error unpacking %s, see earlier error messages") file
 
@@ -72,10 +74,10 @@ let untar_metadata file outdir    untar ~paths:files file
outdir
 
 (* Uncompress the first few bytes of [file] and return it as
- * [(bytes, len)].  [zcat] is the command to use (eg. zcat or xzcat).
+ * [(bytes, len)].
  *)
-let uncompress_head zcat file -  let cmd = sprintf "%s %s" zcat
(quote file) in
+let uncompress_head format file +  let cmd = sprintf "%s %s"
(zcat_command_of_format format) (quote file) in
   let chan_out, chan_in, chan_err = Unix.open_process_full cmd [||] in
   let b = Bytes.create 512 in
   let len = input chan_out b 0 (Bytes.length b) in
@@ -89,8 +91,7 @@ let uncompress_head zcat file   * type of the uncompressed
content (if known).
  *)
 let uncompressed_type format file -  let zcat = match format with `GZip ->
"zcat" | `XZ -> "xzcat" in
-  let head, headlen = uncompress_head zcat file in
+  let head, headlen = uncompress_head format file in
   let tmpfile, chan      Filename.open_temp_file "ova.file."
"" in
   output chan head 0 headlen;
-- 
2.12.0
Richard W.M. Jones
2017-May-08  09:07 UTC
[Libguestfs] [PATCH 3/3] v2v: -i ova: Prefer pigz or pxz for uncompressing OVA files (RHBZ#1448739).
If the parallel tools pigz or pxz are available, prefer them for
uncompressing gz- and xz-compressed OVA files respectively.  If not
available then gzip or xz are used as normal.
---
 v2v/input_ova.ml | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
index e21de35fb..b509326dd 100644
--- a/v2v/input_ova.ml
+++ b/v2v/input_ova.ml
@@ -40,9 +40,19 @@ let libvirt_supports_json_raw_driver ()    else
     true
 
+let pigz_available +  let test = lazy (shell_command "pigz --help
>/dev/null 2>&1" = 0) in
+  fun () -> Lazy.force test
+
+let pxz_available +  let test = lazy (shell_command "pxz --help
>/dev/null 2>&1" = 0) in
+  fun () -> Lazy.force test
+
 let zcat_command_of_format = function
-  | `GZip -> "gzip -c -d"
-  | `XZ -> "xz -c -d"
+  | `GZip ->
+     if pigz_available () then "pigz -c -d" else "gzip -c
-d"
+  | `XZ ->
+     if pxz_available () then "pxz -c -d" else "xz -c -d"
 
 (* Untar part or all files from tar archive. If [paths] is specified it is
  * a list of paths in the tar archive.
-- 
2.12.0
Seemingly Similar Threads
- [PATCH v2 0/9] v2v: -i ova: Handle OVAs containing snapshots.
- [v2v PATCH 1/2] v2v: nbdkit: change base dir for nbdkit sockets/pidfiles
- [PATCH 0/2] v2v: -i ova: A couple of cleanup patches.
- [PATCH v2 0/3] Fix OVA import with libvirt backend
- [PATCH virt-v2v v2 0/2] v2v: Large temporary directory handling.