Harald Hoyer 368a0c
From 693b7a32833b7c141f03d802f6bc9a767bdb11f8 Mon Sep 17 00:00:00 2001
Harald Hoyer 368a0c
From: Harald Hoyer <harald@redhat.com>
Harald Hoyer 368a0c
Date: Wed, 22 Jul 2015 11:34:08 +0200
Harald Hoyer 368a0c
Subject: [PATCH] Cleanup compressor handling
Harald Hoyer 368a0c
Harald Hoyer 368a0c
If no compressor is specified, try to find a suitable one.
Harald Hoyer 368a0c
Harald Hoyer 368a0c
Check if kernel modules can be uncompressed.
Harald Hoyer 368a0c
---
Harald Hoyer 368a0c
 dracut.sh   | 78 +++++++++++++++++++++++++++++++++++++++++++++++--------------
Harald Hoyer 368a0c
 dracut.spec |  7 +++---
Harald Hoyer 368a0c
 2 files changed, 64 insertions(+), 21 deletions(-)
Harald Hoyer 368a0c
Harald Hoyer 368a0c
diff --git a/dracut.sh b/dracut.sh
Harald Hoyer 368a0c
index ee0a039..27be54a 100755
Harald Hoyer 368a0c
--- a/dracut.sh
Harald Hoyer 368a0c
+++ b/dracut.sh
Harald Hoyer 368a0c
@@ -751,25 +751,53 @@ if [[ -n "$logfile" ]];then
Harald Hoyer 368a0c
 fi
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
 # handle compression options.
Harald Hoyer 368a0c
-[[ $compress ]] || compress="gzip"
Harald Hoyer 368a0c
+if [[ $_no_compress_l = "cat" ]]; then
Harald Hoyer 368a0c
+    compress="cat"
Harald Hoyer 368a0c
+fi
Harald Hoyer 368a0c
+
Harald Hoyer 368a0c
+if ! [[ $compress ]]; then
Harald Hoyer 368a0c
+    # check all known compressors, if none specified
Harald Hoyer 368a0c
+    for i in pigz gzip lz4 lzop lzma xz lbzip2 bzip2 cat; do
Harald Hoyer 368a0c
+        command -v "$i" &>/dev/null || continue
Harald Hoyer 368a0c
+        compress="$i"
Harald Hoyer 368a0c
+        break
Harald Hoyer 368a0c
+    done
Harald Hoyer 368a0c
+    if [[ $compress = cat ]]; then
Harald Hoyer 368a0c
+            printf "%s\n" "dracut: no compression tool available. Initramfs image is going to be big." >&2
Harald Hoyer 368a0c
+    fi
Harald Hoyer 368a0c
+fi
Harald Hoyer 368a0c
+
Harald Hoyer 368a0c
+# choose the right arguments for the compressor
Harald Hoyer 368a0c
 case $compress in
Harald Hoyer 368a0c
-    bzip2) compress="bzip2 -9";
Harald Hoyer 368a0c
-        command -v lbzip2 > /dev/null 2>&1 && compress="lbzip2 -9";;
Harald Hoyer 368a0c
-    lzma)  compress="lzma -9 -T0";;
Harald Hoyer 368a0c
-    xz)    compress="xz --check=crc32 --lzma2=dict=1MiB -T0";;
Harald Hoyer 368a0c
-    gzip)  compress="gzip -n -9";
Harald Hoyer 368a0c
-        if command -v pigz > /dev/null 2>&1; then
Harald Hoyer 368a0c
+    bzip2|lbzip2)
Harald Hoyer 368a0c
+        if [[ "$compress" =  lbzip2 ]] || command -v lbzip2 &>/dev/null; then
Harald Hoyer 368a0c
+            compress="lbzip2 -9"
Harald Hoyer 368a0c
+        else
Harald Hoyer 368a0c
+            compress="bzip2 -9"
Harald Hoyer 368a0c
+        fi
Harald Hoyer 368a0c
+        ;;
Harald Hoyer 368a0c
+    lzma)
Harald Hoyer 368a0c
+        compress="lzma -9 -T0"
Harald Hoyer 368a0c
+        ;;
Harald Hoyer 368a0c
+    xz)
Harald Hoyer 368a0c
+        compress="xz --check=crc32 --lzma2=dict=1MiB -T0"
Harald Hoyer 368a0c
+        ;;
Harald Hoyer 368a0c
+    gzip|pigz)
Harald Hoyer 368a0c
+        if [[ "$compress" = pigz ]] || command -v pigz &>/dev/null; then
Harald Hoyer 368a0c
             compress="pigz -9 -n -T -R"
Harald Hoyer 368a0c
-        elif command -v gzip --help 2>&1 | grep -q rsyncable; then
Harald Hoyer 368a0c
+        elif command -v gzip &>/dev/null && gzip --help 2>&1 | grep -q rsyncable; then
Harald Hoyer 368a0c
             compress="gzip -n -9 --rsyncable"
Harald Hoyer 368a0c
+        else
Harald Hoyer 368a0c
+            compress="gzip -n -9"
Harald Hoyer 368a0c
         fi
Harald Hoyer 368a0c
         ;;
Harald Hoyer 368a0c
-    lzo)   compress="lzop -9";;
Harald Hoyer 368a0c
-    lz4)   compress="lz4 -l -9";;
Harald Hoyer 368a0c
+    lzo|lzop)
Harald Hoyer 368a0c
+        compress="lzop -9"
Harald Hoyer 368a0c
+        ;;
Harald Hoyer 368a0c
+    lz4)
Harald Hoyer 368a0c
+        compress="lz4 -l -9"
Harald Hoyer 368a0c
+        ;;
Harald Hoyer 368a0c
 esac
Harald Hoyer 368a0c
-if [[ $_no_compress_l = "cat" ]]; then
Harald Hoyer 368a0c
-    compress="cat"
Harald Hoyer 368a0c
-fi
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
 [[ $hostonly = yes ]] && hostonly="-h"
Harald Hoyer 368a0c
 [[ $hostonly != "-h" ]] && unset hostonly
Harald Hoyer 368a0c
@@ -887,10 +915,26 @@ esac
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
-if [[ -d $srcmods ]]; then
Harald Hoyer 368a0c
-    [[ -f $srcmods/modules.dep ]] || {
Harald Hoyer 368a0c
-      dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
Harald Hoyer 368a0c
-    }
Harald Hoyer 368a0c
+if [[ $no_kernel != yes ]] && [[ -d $srcmods ]]; then
Harald Hoyer 368a0c
+    if ! [[ -f $srcmods/modules.dep ]]; then
Harald Hoyer 368a0c
+        dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
Harald Hoyer 368a0c
+    elif ! ( command -v gzip &>/dev/null && command -v xz &>/dev/null); then
Harald Hoyer 368a0c
+        read _mod < $srcmods/modules.dep
Harald Hoyer 368a0c
+        _mod=${_mod%%:*}
Harald Hoyer 368a0c
+        if [[ -f $srcmods/"$_mod" ]]; then
Harald Hoyer 368a0c
+            # Check, if kernel modules are compressed, and if we can uncompress them
Harald Hoyer 368a0c
+            case "$_mod" in
Harald Hoyer 368a0c
+                *.ko.gz) kcompress=gzip;;
Harald Hoyer 368a0c
+                *.ko.xz) kcompress=xz;;
Harald Hoyer 368a0c
+            esac
Harald Hoyer 368a0c
+            if [[ $kcompress ]]; then
Harald Hoyer 368a0c
+                if ! command -v "$kcompress" &>/dev/null; then
Harald Hoyer 368a0c
+                    dfatal "Kernel modules are compressed with $kcompress, but $kcompress is not available."
Harald Hoyer 368a0c
+                    exit 1
Harald Hoyer 368a0c
+                fi
Harald Hoyer 368a0c
+            fi
Harald Hoyer 368a0c
+        fi
Harald Hoyer 368a0c
+    fi
Harald Hoyer 368a0c
 fi
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
 if [[ ! $print_cmdline ]]; then
Harald Hoyer 368a0c
diff --git a/dracut.spec b/dracut.spec
Harald Hoyer 368a0c
index 08b281d..6ef5de7 100644
Harald Hoyer 368a0c
--- a/dracut.spec
Harald Hoyer 368a0c
+++ b/dracut.spec
Harald Hoyer 368a0c
@@ -60,7 +60,6 @@ BuildRequires: docbook-style-xsl docbook-dtds libxslt
Harald Hoyer 368a0c
 BuildRequires: asciidoc
Harald Hoyer 368a0c
 %endif
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
-
Harald Hoyer 368a0c
 %if 0%{?fedora} > 12 || 0%{?rhel}
Harald Hoyer 368a0c
 # no "provides", because dracut does not offer
Harald Hoyer 368a0c
 # all functionality of the obsoleted packages
Harald Hoyer 368a0c
@@ -94,17 +93,17 @@ Requires: findutils
Harald Hoyer 368a0c
 Requires: grep
Harald Hoyer 368a0c
 Requires: kmod
Harald Hoyer 368a0c
 Requires: sed
Harald Hoyer 368a0c
+Requires: xz
Harald Hoyer 368a0c
+Requires: gzip
Harald Hoyer 368a0c
 
Harald Hoyer 368a0c
 %if 0%{?fedora} > 22
Harald Hoyer 368a0c
 Recommends: grubby
Harald Hoyer 368a0c
 Recommends: hardlink
Harald Hoyer 368a0c
-Recommends: gzip
Harald Hoyer 368a0c
-Recommends: xz
Harald Hoyer 368a0c
+Recommends: pigz
Harald Hoyer 368a0c
 Recommends: kpartx
Harald Hoyer 368a0c
 %else
Harald Hoyer 368a0c
 Requires: hardlink
Harald Hoyer 368a0c
 Requires: gzip
Harald Hoyer 368a0c
-Requires: xz
Harald Hoyer 368a0c
 Requires: kpartx
Harald Hoyer 368a0c
 %endif
Harald Hoyer 368a0c