Harald Hoyer afa7aa
From 7c9e64272ef250a4b8804beaba24a3da7c157ca4 Mon Sep 17 00:00:00 2001
Harald Hoyer afa7aa
From: Harald Hoyer <harald@hoyer.xyz>
Harald Hoyer afa7aa
Date: Mon, 9 Oct 2017 11:39:10 +0200
Harald Hoyer afa7aa
Subject: [PATCH] Merge pull request #280 from tpgxyz/zstd
Harald Hoyer afa7aa
Harald Hoyer afa7aa
add support for Zstandard
Harald Hoyer afa7aa
---
Harald Hoyer afa7aa
 dracut-bash-completion.sh           |  2 +-
Harald Hoyer afa7aa
 dracut-initramfs-restore.sh         |  2 ++
Harald Hoyer afa7aa
 dracut.8.asc                        |  8 ++++++++
Harald Hoyer afa7aa
 dracut.conf.5.asc                   |  2 +-
Harald Hoyer afa7aa
 dracut.sh                           | 10 +++++++++-
Harald Hoyer afa7aa
 lsinitrd.sh                         |  3 +++
Harald Hoyer afa7aa
 modules.d/99img-lib/img-lib.sh      |  5 +++--
Harald Hoyer afa7aa
 modules.d/99img-lib/module-setup.sh |  2 +-
Harald Hoyer afa7aa
 8 files changed, 28 insertions(+), 6 deletions(-)
Harald Hoyer afa7aa
Harald Hoyer afa7aa
diff --git a/dracut-bash-completion.sh b/dracut-bash-completion.sh
Harald Hoyer afa7aa
index 09330a67..9e5806cc 100644
Harald Hoyer afa7aa
--- a/dracut-bash-completion.sh
Harald Hoyer afa7aa
+++ b/dracut-bash-completion.sh
Harald Hoyer afa7aa
@@ -29,7 +29,7 @@ _dracut() {
Harald Hoyer afa7aa
                               --hardlink --nohardlink --noprefix --mdadmconf --nomdadmconf
Harald Hoyer afa7aa
                               --lvmconf --nolvmconf --debug --profile --verbose --quiet
Harald Hoyer afa7aa
                               --local --hostonly --no-hostonly --fstab --help --bzip2 --lzma
Harald Hoyer afa7aa
-                              --xz --no-compress --gzip --list-modules --show-modules --keep
Harald Hoyer afa7aa
+                              --xz --zstd --no-compress --gzip --list-modules --show-modules --keep
Harald Hoyer afa7aa
                               --printsize --regenerate-all --noimageifnotneeded --early-microcode
Harald Hoyer afa7aa
                               --no-early-microcode --print-cmdline --prelink --noprelink --reproducible
Harald Hoyer afa7aa
                               --uefi
Harald Hoyer afa7aa
diff --git a/dracut-initramfs-restore.sh b/dracut-initramfs-restore.sh
Harald Hoyer afa7aa
index 0c41a59c..94794804 100644
Harald Hoyer afa7aa
--- a/dracut-initramfs-restore.sh
Harald Hoyer afa7aa
+++ b/dracut-initramfs-restore.sh
Harald Hoyer afa7aa
@@ -31,6 +31,8 @@ elif $SKIP "$IMG" | xzcat | cpio -id --no-absolute-filenames --quiet >/dev/null;
Harald Hoyer afa7aa
     rm -f -- .need_shutdown
Harald Hoyer afa7aa
 elif $SKIP "$IMG" | lz4 -d -c | cpio -id --no-absolute-filenames --quiet >/dev/null; then
Harald Hoyer afa7aa
     rm -f -- .need_shutdown
Harald Hoyer afa7aa
+elif $SKIP "$IMG" | zstd -d -c | cpio -id --no-absolute-filenames --quiet >/dev/null; then
Harald Hoyer afa7aa
+    rm -f -- .need_shutdown
Harald Hoyer afa7aa
 else
Harald Hoyer afa7aa
     # something failed, so we clean up
Harald Hoyer afa7aa
     echo "Unpacking of $IMG to /run/initramfs failed" >&2
Harald Hoyer afa7aa
diff --git a/dracut.8.asc b/dracut.8.asc
Harald Hoyer afa7aa
index 23c53cc4..64c0207f 100644
Harald Hoyer afa7aa
--- a/dracut.8.asc
Harald Hoyer afa7aa
+++ b/dracut.8.asc
Harald Hoyer afa7aa
@@ -432,6 +432,14 @@ Make sure your kernel has lz4 decompression support compiled in, otherwise you
Harald Hoyer afa7aa
 will not be able to boot.
Harald Hoyer afa7aa
 ====
Harald Hoyer afa7aa
 
Harald Hoyer afa7aa
+**--zstd**::
Harald Hoyer afa7aa
+    Compress the generated initramfs using Zstandard.
Harald Hoyer afa7aa
+[WARNING]
Harald Hoyer afa7aa
+====
Harald Hoyer afa7aa
+Make sure your kernel has zstd decompression support compiled in, otherwise you
Harald Hoyer afa7aa
+will not be able to boot.
Harald Hoyer afa7aa
+====
Harald Hoyer afa7aa
+
Harald Hoyer afa7aa
 **--compress** _<compressor>_::
Harald Hoyer afa7aa
     Compress the generated initramfs using the passed compression program. If
Harald Hoyer afa7aa
     you pass it just the name of a compression program, it will call that
Harald Hoyer afa7aa
diff --git a/dracut.conf.5.asc b/dracut.conf.5.asc
Harald Hoyer afa7aa
index 907a2fd4..6e8f3eba 100644
Harald Hoyer afa7aa
--- a/dracut.conf.5.asc
Harald Hoyer afa7aa
+++ b/dracut.conf.5.asc
Harald Hoyer afa7aa
@@ -79,7 +79,7 @@ Configuration files must have the extension .conf; other extensions are ignored.
Harald Hoyer afa7aa
     Specify additional files to include in the initramfs, separated by spaces,
Harald Hoyer afa7aa
     if they exist.
Harald Hoyer afa7aa
 
Harald Hoyer afa7aa
-*compress=*"__{bzip2|lzma|xz|gzip|lzo|lz4|<compressor [args ...]>}__"::
Harald Hoyer afa7aa
+*compress=*"__{bzip2|lzma|xz|gzip|lzo|lz4|zstd|<compressor [args ...]>}__"::
Harald Hoyer afa7aa
     Compress the generated initramfs using the passed compression program. If
Harald Hoyer afa7aa
     you pass it just the name of a compression program, it will call that
Harald Hoyer afa7aa
     program with known-working arguments. If you pass arguments, it will be called
Harald Hoyer afa7aa
diff --git a/dracut.sh b/dracut.sh
Harald Hoyer afa7aa
index 06127ca4..5ad0f464 100755
Harald Hoyer afa7aa
--- a/dracut.sh
Harald Hoyer afa7aa
+++ b/dracut.sh
Harald Hoyer afa7aa
@@ -197,6 +197,9 @@ Creates initial ramdisk images for preloading modules
Harald Hoyer afa7aa
   --lz4                 Compress the generated initramfs using lz4.
Harald Hoyer afa7aa
                          Make sure that your kernel has lz4 support compiled
Harald Hoyer afa7aa
                          in, otherwise you will not be able to boot.
Harald Hoyer afa7aa
+  --zstd                Compress the generated initramfs using Zstandard.
Harald Hoyer afa7aa
+                         Make sure that your kernel has zstd support compiled
Harald Hoyer afa7aa
+                         in, otherwise you will not be able to boot.
Harald Hoyer afa7aa
   --compress [COMPRESSION] Compress the generated initramfs with the
Harald Hoyer afa7aa
                          passed compression program.  Make sure your kernel
Harald Hoyer afa7aa
                          knows how to decompress the generated initramfs,
Harald Hoyer afa7aa
@@ -358,6 +361,7 @@ rearrange_params()
Harald Hoyer afa7aa
         --long xz \
Harald Hoyer afa7aa
         --long lzo \
Harald Hoyer afa7aa
         --long lz4 \
Harald Hoyer afa7aa
+        --long zstd \
Harald Hoyer afa7aa
         --long no-compress \
Harald Hoyer afa7aa
         --long gzip \
Harald Hoyer afa7aa
         --long list-modules \
Harald Hoyer afa7aa
@@ -557,6 +561,7 @@ while :; do
Harald Hoyer afa7aa
         --xz)          compress_l="xz";;
Harald Hoyer afa7aa
         --lzo)         compress_l="lzo";;
Harald Hoyer afa7aa
         --lz4)         compress_l="lz4";;
Harald Hoyer afa7aa
+        --zstd)        compress_l="zstd";;
Harald Hoyer afa7aa
         --no-compress) _no_compress_l="cat";;
Harald Hoyer afa7aa
         --gzip)        compress_l="gzip";;
Harald Hoyer afa7aa
         --list-modules) do_list="yes";;
Harald Hoyer afa7aa
@@ -797,7 +802,7 @@ fi
Harald Hoyer afa7aa
 
Harald Hoyer afa7aa
 if ! [[ $compress ]]; then
Harald Hoyer afa7aa
     # check all known compressors, if none specified
Harald Hoyer afa7aa
-    for i in pigz gzip lz4 lzop lzma xz lbzip2 bzip2 cat; do
Harald Hoyer afa7aa
+    for i in pigz gzip lz4 lzop zstd lzma xz lbzip2 bzip2 cat; do
Harald Hoyer afa7aa
         command -v "$i" &>/dev/null || continue
Harald Hoyer afa7aa
         compress="$i"
Harald Hoyer afa7aa
         break
Harald Hoyer afa7aa
@@ -837,6 +842,9 @@ case $compress in
Harald Hoyer afa7aa
     lz4)
Harald Hoyer afa7aa
         compress="lz4 -l -9"
Harald Hoyer afa7aa
         ;;
Harald Hoyer afa7aa
+    zstd)
Harald Hoyer afa7aa
+       compress="zstd -15 -q -T0"
Harald Hoyer afa7aa
+       ;;
Harald Hoyer afa7aa
 esac
Harald Hoyer afa7aa
 
Harald Hoyer afa7aa
 [[ $hostonly = yes ]] && hostonly="-h"
Harald Hoyer afa7aa
diff --git a/lsinitrd.sh b/lsinitrd.sh
Harald Hoyer afa7aa
index b19a66a0..f4088453 100755
Harald Hoyer afa7aa
--- a/lsinitrd.sh
Harald Hoyer afa7aa
+++ b/lsinitrd.sh
Harald Hoyer afa7aa
@@ -219,6 +219,9 @@ case $bin in
Harald Hoyer afa7aa
     $'\x89'LZO$'\0'*)
Harald Hoyer afa7aa
         CAT="lzop -d -c"
Harald Hoyer afa7aa
         ;;
Harald Hoyer afa7aa
+    $'0xFD2FB528'*)
Harald Hoyer afa7aa
+        CAT="zstd -d -c"
Harald Hoyer afa7aa
+        ;;
Harald Hoyer afa7aa
     *)
Harald Hoyer afa7aa
         if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
Harald Hoyer afa7aa
             CAT="xzcat --single-stream --"
Harald Hoyer afa7aa
diff --git a/modules.d/99img-lib/img-lib.sh b/modules.d/99img-lib/img-lib.sh
Harald Hoyer afa7aa
index 48e56ce7..43785812 100755
Harald Hoyer afa7aa
--- a/modules.d/99img-lib/img-lib.sh
Harald Hoyer afa7aa
+++ b/modules.d/99img-lib/img-lib.sh
Harald Hoyer afa7aa
@@ -8,12 +8,13 @@
Harald Hoyer afa7aa
 # works with stdin if $1 is not set.
Harald Hoyer afa7aa
 det_archive() {
Harald Hoyer afa7aa
     # NOTE: echo -e works in ash and bash, but not dash
Harald Hoyer afa7aa
-    local bz="BZh" xz="$(echo -e '\xfd7zXZ')" gz="$(echo -e '\x1f\x8b')"
Harald Hoyer afa7aa
+    local bz="BZh" xz="$(echo -e '\xfd7zXZ')" gz="$(echo -e '\x1f\x8b')" zs="$(echo -e '0xFD2FB528')"
Harald Hoyer afa7aa
     local headerblock="$(dd ${1:+if=$1} bs=262 count=1 2>/dev/null)"
Harald Hoyer afa7aa
     case "$headerblock" in
Harald Hoyer afa7aa
         $xz*) echo "xz" ;;
Harald Hoyer afa7aa
         $gz*) echo "gzip" ;;
Harald Hoyer afa7aa
         $bz*) echo "bzip2" ;;
Harald Hoyer afa7aa
+        $zs*) echo "zstd" ;;
Harald Hoyer afa7aa
         07070*) echo "cpio" ;;
Harald Hoyer afa7aa
         *ustar) echo "tar" ;;
Harald Hoyer afa7aa
     esac
Harald Hoyer afa7aa
@@ -33,7 +34,7 @@ unpack_archive() {
Harald Hoyer afa7aa
     local img="$1" outdir="$2" archiver="" decompr=""
Harald Hoyer afa7aa
     local ft="$(det_archive $img)"
Harald Hoyer afa7aa
     case "$ft" in
Harald Hoyer afa7aa
-        xz|gzip|bzip2) decompr="$ft -dc" ;;
Harald Hoyer afa7aa
+        xz|gzip|bzip2|zstd) decompr="$ft -dc" ;;
Harald Hoyer afa7aa
         cpio|tar) decompr="cat";;
Harald Hoyer afa7aa
         *) return 1 ;;
Harald Hoyer afa7aa
     esac
Harald Hoyer afa7aa
diff --git a/modules.d/99img-lib/module-setup.sh b/modules.d/99img-lib/module-setup.sh
Harald Hoyer afa7aa
index 69346b2f..fe1eca1a 100755
Harald Hoyer afa7aa
--- a/modules.d/99img-lib/module-setup.sh
Harald Hoyer afa7aa
+++ b/modules.d/99img-lib/module-setup.sh
Harald Hoyer afa7aa
@@ -16,7 +16,7 @@ depends() {
Harald Hoyer afa7aa
 install() {
Harald Hoyer afa7aa
     inst_multiple tar gzip dd bash
Harald Hoyer afa7aa
     # TODO: make this conditional on a cmdline flag / config option
Harald Hoyer afa7aa
-    inst_multiple -o cpio xz bzip2
Harald Hoyer afa7aa
+    inst_multiple -o cpio xz bzip2 zstd
Harald Hoyer afa7aa
     inst_simple "$moddir/img-lib.sh" "/lib/img-lib.sh"
Harald Hoyer afa7aa
 }
Harald Hoyer afa7aa
 
Harald Hoyer afa7aa