commit:     9d16c25da9935b1b0beb38357fe3c8b4ee2e236b
Author:     Dmitriy Baranov <reagentoo <AT> gmail <DOT> com>
AuthorDate: Tue Mar  5 20:28:49 2024 +0000
Commit:     Ben Kohler <bkohler <AT> gentoo <DOT> org>
CommitDate: Mon May 20 12:58:59 2024 +0000
URL:        https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=9d16c25d

gen_initramfs.sh: unpack compressed modules/firmwares to reduce image size

Signed-off-by: Dmitriy Baranov <reagentoo <AT> gmail.com>
Signed-off-by: Ben Kohler <bkohler <AT> gentoo.org>

 gen_funcs.sh     | 34 ++++++++++++++++++++++++++++++++++
 gen_initramfs.sh |  8 ++++++++
 2 files changed, 42 insertions(+)

diff --git a/gen_funcs.sh b/gen_funcs.sh
index c31e15a..70a4969 100755
--- a/gen_funcs.sh
+++ b/gen_funcs.sh
@@ -2054,6 +2054,40 @@ expand_file() {
        echo "${expanded_file}"
 }
 
+find_and_unpack() {
+       local flist
+
+       local fmt
+       for fmt in "$@"
+       do
+               case "${fmt}" in
+               "gz"|"xz"|"zstd")
+                       flist=( $(find -type f -name "*.${fmt}") )
+                       ;;
+               *)
+                       gen_die "unknown compression format: ${fmt}"
+                       ;;
+               esac
+
+               if [ ${#flist[@]} -lt 1 ]
+               then
+                       continue
+               fi
+
+               case "${fmt}" in
+               "gz")
+                       gunzip "${flist[@]}"
+                       ;;
+               "xz")
+                       unxz "${flist[@]}"
+                       ;;
+               "zstd")
+                       unzstd "${flist[@]}"
+                       ;;
+               esac
+       done
+}
+
 find_kernel_binary() {
        local kernel_binary=${*}
        local kernel_binary_found=

diff --git a/gen_initramfs.sh b/gen_initramfs.sh
index 486f0e6..aaf108e 100755
--- a/gen_initramfs.sh
+++ b/gen_initramfs.sh
@@ -1806,6 +1806,10 @@ append_firmware() {
                cp -rL --parents --target-directory="${TDIR}/lib/firmware" 
"${fwlist[@]}" 2>/dev/null \
                        || gen_die "Failed to copy firmware files to 
'${TDIR}/lib/firmware'!"
                popd &>/dev/null || gen_die "Failed to chdir!"
+
+               pushd "${TDIR}/lib/firmware" &>/dev/null || gen_die "Failed to 
chdir to '${TDIR}/lib/firmware'!"
+               find_and_unpack xz zstd
+               popd &>/dev/null || gen_die "Failed to chdir!"
        fi
 
        cd "${TDIR}" || gen_die "Failed to chdir to '${TDIR}'!"
@@ -1940,6 +1944,10 @@ append_modules() {
        cp -ax --parents --target-directory "${modules_dstdir}" modules* 
2>/dev/null \
                || gen_die "Failed to copy '${modules_srcdir}/modules*' to 
'${modules_dstdir}'!"
 
+       pushd "${modules_dstdir}" &>/dev/null || gen_die "Failed to chdir to 
'${modules_dstdir}'!"
+       find_and_unpack gz xz zstd
+       popd &>/dev/null || gen_die "Failed to chdir!"
+
        print_info 2 "$(get_indent 2)modules: Updating modules.dep ..."
        local depmod_cmd=( depmod -a -b "${TDIR}" ${KV} )
        print_info 3 "COMMAND: ${depmod_cmd[*]}" 1 0 1

Reply via email to