Customizable initramfs compression based on Pentoo genkernel fork
authorRobin H. Johnson <robbat2@gentoo.org>
Mon, 6 Feb 2012 08:33:10 +0000 (08:33 +0000)
committerRobin H. Johnson <robbat2@gentoo.org>
Mon, 6 Feb 2012 09:31:59 +0000 (09:31 +0000)
This implements customizable initramfs compression, based on the Pentoo
genkernel fork (SVN revs r2142, r2145, by Jens Pranaitis
<jens@chaox.net>).

New configuration option COMPRESS_INITRD_TYPE to allow manual selection,
whereas the Pentoo fork provided auto-selection based on what was
available in the kernel configuration. Auto-selection is used by
default, and is available with the argument of 'best' to the new option.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
ChangeLog
defaults/config.sh
gen_cmdline.sh
gen_determineargs.sh
gen_initramfs.sh
genkernel.conf

index d6e4dcf2663633cf4e72ea0f2541ddfae437f452..e2cc9ab104039a48e84b5f235e7fea134b84b9d8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,10 @@
 # Distributed under the GPL v2
 # $Id$
 
+  06 Feb 2012; Robin H. Johnson <robbat2@gentoo.org> defaults/config.sh,
+  gen_cmdline.sh, gen_determineargs.sh, gen_initramfs.sh, genkernel.conf:
+  Import customizable compression for initramfs, based on Pentoo development.
+
   06 Feb 2012: Richard Yao <ryao@cs.stonybrook.edu> arch/alpha/modules_load,
   arch/arm/modules_load, arch/ia64/modules_load, arch/mips/modules_load,
   arch/parisc/modules_load, arch/parisc64/modules_load, arch/ppc/modules_load,
index d135e0e861a2a17604868d0d81180f3e39f8c606..7ee9ef20ae9d3edbaa1343765e788c280d9482ee 100755 (executable)
@@ -12,7 +12,8 @@ KERNEL_MAKE_DIRECTIVE_2=""
 KERNEL_BINARY="arch/i386/boot/bzImage"
 KERNEL_BINARY_OVERRIDE=""
 
-COMPRESS_INITRD=yes
+DEFAULT_COMPRESS_INITRD=yes
+DEFAULT_COMPRESS_INITRD_TYPE=best
 
 #
 # Arch-specific defaults that can be overridden in the config file or on the
index de56a25393e70466415a381975f1003e9295bded..aabaa7e57da2bc564bb3f6c998d65370fedf5ffd 100755 (executable)
@@ -537,10 +537,14 @@ parse_cmdline() {
                        CMD_INTEGRATED_INITRAMFS=`parse_optbool "$*"`
                        print_info 2 "CMD_INTEGRATED_INITRAMFS=${CMD_INTEGRATED_INITRAMFS}"
                        ;;
-               --compress-initramfs|--no-compress-initramfs)
+               --compress-initramfs|--no-compress-initramfs|--compress-initrd|--no-compress-initrd)
                        CMD_COMPRESS_INITRD=`parse_optbool "$*"`
                        print_info 2 "CMD_COMPRESS_INITRD=${CMD_COMPRESS_INITRD}"
                        ;;
+               --compress-initramfs-type=*|--compress-initrd-type=*)
+                       COMPRESS_INITRD_TYPE=`parse_opt "$*"`
+                       print_info 2 "CMD_COMPRESS_INITRD_TYPE: ${CMD_LINUXRC}"
+                       ;;
                --config=*)
                        print_info 2 "CMD_GK_CONFIG: `parse_opt "$*"`"
                        ;;
index e6ed0a3cdbb1ff388f120c915bb7ef5e2f86cb5c..4a215304fd2691fd45747c37e2e0042ba6f1f825 100755 (executable)
@@ -76,6 +76,7 @@ determine_real_args() {
        set_config_with_override STRING KNAME                CMD_KERNNAME             "genkernel"
 
        set_config_with_override STRING COMPRESS_INITRD      CMD_COMPRESS_INITRD      "$DEFAULT_COMPRESS_INITRD"
+       set_config_with_override STRING COMPRESS_INITRD_TYPE CMD_COMPRESS_INITRD_TYPE "$DEFAULT_COMPRESS_INITRD_TYPE"
        set_config_with_override STRING MAKEOPTS             CMD_MAKEOPTS             "$DEFAULT_MAKEOPTS"
        set_config_with_override STRING KERNEL_MAKE          CMD_KERNEL_MAKE          "$DEFAULT_KERNEL_MAKE"
        set_config_with_override STRING UTILS_MAKE           CMD_UTILS_MAKE           "$DEFAULT_UTILS_MAKE"
index 170361bc5a90ec53209089c16708a639e597555d..3aab15dbba9b9514686fbfde1f146d476c19ee4f 100755 (executable)
@@ -686,23 +686,57 @@ create_initramfs() {
                append_data 'overlay'
        fi
 
-       # Implement support for disabling compression
-       if isTrue "${COMPRESS_INITRD}"
-       then
-               compress_ext=".gz"
-               print_info 1 "        >> Compressing cpio data..."
-               gzip -9 "${CPIO}" || gen_die "Compression failed"
-               mv -f "${CPIO}.gz" "${CPIO}" || gen_die "Rename failed"
-       fi
-
-
        if isTrue "${INTEGRATED_INITRAMFS}"
        then
-#              cp ${TMPDIR}/initramfs-${KV} ${KERNEL_DIR}/usr/initramfs_data.cpio.gz
-               mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio${compress_ext}
-#              sed -i "s|^.*CONFIG_INITRAMFS_SOURCE=.*$|CONFIG_INITRAMFS_SOURCE=\"${TMPDIR}/initramfs-${KV}.cpio.gz\"|" ${KERNEL_DIR}/.config
+               # Explicitly do not compress if we are integrating into the kernel.
+               # The kernel will do a better job of it than us.
+               mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio
                sed -i '/^.*CONFIG_INITRAMFS_SOURCE=.*$/d' ${KERNEL_DIR}/.config
-               echo -e "CONFIG_INITRAMFS_SOURCE=\"${TMPDIR}/initramfs-${KV}.cpio${compress_ext}\"\nCONFIG_INITRAMFS_ROOT_UID=0\nCONFIG_INITRAMFS_ROOT_GID=0" >> ${KERNEL_DIR}/.config
+               cat >>${KERNEL_DIR}/.config     <<-EOF
+               CONFIG_INITRAMFS_SOURCE="${TMPDIR}/initramfs-${KV}.cpio${compress_ext}"
+               CONFIG_INITRAMFS_ROOT_UID=0
+               CONFIG_INITRAMFS_ROOT_GID=0
+               EOF
+       else
+               if isTrue "${COMPRESS_INITRD}"
+               then
+                       cmd_xz=$(type -p xz)
+                       cmd_lzma=$(type -p lzma)
+                       cmd_bzip2=$(type -p bzip2)
+                       cmd_gzip=$(type -p gzip)
+                       cmd_lzop=$(type -p lzop)
+                       local compression
+                       case ${COMPRESS_INITRD_TYPE} in
+                               xz|lzma|bzip2|gzip2|lzo) compression=${COMPRESS_INITRD_TYPE} ;;
+                               best)
+                                       if grep -sq '^CONFIG_RD_XZ=y' ${KERNEL_DIR}/.config && test -n "${cmd_xz}" ;
+                                               compression=xz
+                                       elif grep -sq '^CONFIG_RD_LZMA=y' ${KERNEL_DIR}/.config && test -n "${cmd_lzma}" ;
+                                               compression=lzma
+                                       elif grep -sq '^CONFIG_RD_BZIP2=y' ${KERNEL_DIR}/.config  && test -n "${cmd_bzip2}" ;
+                                               compression=bzip2
+                                       elif grep -sq '^CONFIG_RD_GZIP=y' ${KERNEL_DIR}/.config && test -n "${cmd_gzip}" ;
+                                               compression=gzip
+                                       elif grep -sq '^CONFIG_RD_LZO=y' ${KERNEL_DIR}/.config && test -n "${cmd_lzop}" ;
+                                               compression=lzo
+                                       fi ;;
+                       esac
+                       case $compression in
+                               xz) compress_ext='.xz' compress_cmd="${cmd_xz} -e --check=none -z -f -9" ;;
+                               lzma) compress_ext='.lzma' compress_cmd="${cmd_lzma} -z -f -9" ;;
+                               bzip2) compress_ext='.bz2' compress_cmd="${cmd_bzip2} -z -f -9"
+                               gzip) compress_ext='.gz' compress_cmd="${cmd_gzip} -f -9" ;;
+                               lzo) compress_ext='.lzo' compress_cmd="${cmd_lzop} -f -9" ;;
+                       esac
+       
+                       if [ -n "${compression}" ]; then
+                               print_info 1 "        >> Compressing cpio data (${compress_ext})..."
+                               ${compress_cmd} "${CPIO}" || gen_die "Compression (${compress_cmd}) failed"
+                               mv -f "${CPIO}${compress_ext}" "${CPIO}" || gen_die "Rename failed"
+                       else
+                               print_info 1 "        >> Not compressing cpio data ..."
+                       fi
+               fi
        fi
 
        if isTrue "${CMD_INSTALL}"
index 7032ac718cebb96a11893b6674cb8c32ae57deab..22b18d73356cad4a79eb0d2b0b33cddc26880540 100644 (file)
@@ -319,6 +319,9 @@ GPG_BINCACHE="%%CACHE%%/gnupg-${GPG_VER}-%%ARCH%%.bz2"
 
 # Compress generated initramfs
 #COMPRESS_INITRD="yes"
+# Types of compression: best, xz, lzma, bzip2, gzip, lzop
+# "best" selects the best available compression method
+#COMPRESS_INITRD_TYPE="best"
 
 
 # Create a self-contained env in the initramfs