Make missing ZFS config file failures into warnings, bug #438200
[genkernel.git] / gen_initramfs.sh
index c64469453e7d615f399edcf91a5909a8df379943..99bd3f37523a2cfd7f35392f6a2c103255ac37eb 100755 (executable)
@@ -3,6 +3,54 @@
 
 CPIO_ARGS="--quiet -o -H newc"
 
+# The copy_binaries function is explicitly released under the CC0 license to
+# encourage wide adoption and re-use.  That means:
+# - You may use the code of copy_binaries() as CC0 outside of genkernel
+# - Contributions to this function are licensed under CC0 as well.
+# - If you change it outside of genkernel, please consider sending your
+#   modifications back to genkernel@gentoo.org.
+#
+# On a side note: "Both public domain works and the simple license provided by
+#                  CC0 are compatible with the GNU GPL."
+#                 (from https://www.gnu.org/licenses/license-list.html#CC0)
+#
+# Written by: 
+# - Sebastian Pipping <sebastian@pipping.org> (error checking)
+# - Robin H. Johnson <robbat2@gentoo.org> (complete rewrite)
+# - Richard Yao <ryao@cs.stonybrook.edu> (original concept)
+# Usage:
+# copy_binaries DESTDIR BINARIES...
+copy_binaries() {
+       local destdir=$1
+       shift
+
+       for binary in "$@"; do
+               [[ -e "${binary}" ]] \
+                               || gen_die "Binary ${binary} could not be found"
+
+               if LC_ALL=C lddtree "${binary}" 2>&1 | fgrep -q 'not found'; then
+                       gen_die "Binary ${binary} is linked to missing libraries and may need to be re-built"
+               fi
+       done
+       # This must be OUTSIDE the for loop, we only want to run lddtree etc ONCE.
+       lddtree "$@" \
+                       | tr ')(' '\n' \
+                       | awk  '/=>/{ if($3 ~ /^\//){print $3}}' \
+                       | sort \
+                       | uniq \
+                       | cpio -p --make-directories --dereference --quiet "${destdir}" \
+                       || gen_die "Binary ${f} or some of its library dependencies could not be copied"
+}
+
+log_future_cpio_content() {
+       if [[ "${LOGLEVEL}" -gt 1 ]]; then
+               echo =================================================================
+               echo "About to add these files from '${PWD}' to cpio archive:"
+               find . | xargs ls -ald
+               echo =================================================================
+       fi
+}
+
 append_base_layout() {
        if [ -d "${TEMP}/initramfs-base-temp" ]
        then
@@ -13,8 +61,13 @@ append_base_layout() {
        mkdir -p ${TEMP}/initramfs-base-temp/bin
        mkdir -p ${TEMP}/initramfs-base-temp/etc
        mkdir -p ${TEMP}/initramfs-base-temp/usr
+       mkdir -p ${TEMP}/initramfs-base-temp/lib
+       mkdir -p ${TEMP}/initramfs-base-temp/mnt
+       mkdir -p ${TEMP}/initramfs-base-temp/run
+       mkdir -p ${TEMP}/initramfs-base-temp/sbin
        mkdir -p ${TEMP}/initramfs-base-temp/proc
        mkdir -p ${TEMP}/initramfs-base-temp/temp
+       mkdir -p ${TEMP}/initramfs-base-temp/tmp
        mkdir -p ${TEMP}/initramfs-base-temp/sys
        mkdir -p ${TEMP}/initramfs-temp/.initrd
        mkdir -p ${TEMP}/initramfs-base-temp/var/lock/dmraid
@@ -29,11 +82,16 @@ append_base_layout() {
        cd ${TEMP}/initramfs-base-temp/dev
        mknod -m 660 console c 5 1
        mknod -m 660 null c 1 3
+       mknod -m 660 zero c 1 5
+       mknod -m 600 tty0 c 4 0
        mknod -m 600 tty1 c 4 1
+       mknod -m 600 ttyS0 c 4 64
 
-       date '+%Y%m%d' > ${TEMP}/initramfs-base-temp/etc/build_date
+       date -u '+%Y%m%d-%H%M%S' > ${TEMP}/initramfs-base-temp/etc/build_date
+       echo "Genkernel $GK_V" > ${TEMP}/initramfs-base-temp/etc/build_id
 
        cd "${TEMP}/initramfs-base-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing baselayout cpio"
        cd "${TEMP}"
@@ -63,23 +121,44 @@ append_busybox() {
        done
 
        cd "${TEMP}/initramfs-busybox-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing busybox cpio"
        cd "${TEMP}"
        rm -rf "${TEMP}/initramfs-busybox-temp" > /dev/null
 }
 
+append_e2fsprogs(){
+       if [ -d "${TEMP}"/initramfs-e2fsprogs-temp ]
+       then
+               rm -r "${TEMP}"/initramfs-e2fsprogs-temp
+       fi
+
+       cd "${TEMP}" \
+                       || gen_die "cd '${TEMP}' failed"
+       mkdir -p initramfs-e2fsprogs-temp
+       copy_binaries "${TEMP}"/initramfs-e2fsprogs-temp/ /sbin/{e2fsck,mke2fs}
+
+       cd "${TEMP}"/initramfs-e2fsprogs-temp \
+                       || gen_die "cd '${TEMP}/initramfs-e2fsprogs-temp' failed"
+       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
+       rm -rf "${TEMP}"/initramfs-e2fsprogs-temp > /dev/null
+}
+
 append_blkid(){
        if [ -d "${TEMP}/initramfs-blkid-temp" ]
        then
                rm -r "${TEMP}/initramfs-blkid-temp/"
        fi
        cd ${TEMP}
-       mkdir -p "${TEMP}/initramfs-blkid-temp/sbin/"
-       [ "${DISKLABEL}" = '1' ] && { /bin/bzip2 -dc "${BLKID_BINCACHE}" > "${TEMP}/initramfs-blkid-temp/sbin/blkid" ||
-               gen_die "Could not extract blkid binary cache!"; }
-       chmod a+x "${TEMP}/initramfs-blkid-temp/sbin/blkid"
+       mkdir -p "${TEMP}/initramfs-blkid-temp/"
+
+       if [[ "${DISKLABEL}" = "1" ]]; then
+               copy_binaries "${TEMP}"/initramfs-blkid-temp/ /sbin/blkid
+       fi
+
        cd "${TEMP}/initramfs-blkid-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing blkid cpio"
        cd "${TEMP}"
@@ -111,6 +190,7 @@ append_unionfs_fuse() {
                gen_die 'Could not extract unionfs-fuse binary cache!'
        chmod a+x "${TEMP}/initramfs-unionfs-fuse-temp/sbin/unionfs"
        cd "${TEMP}/initramfs-unionfs-fuse-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing unionfs fuse cpio"
        cd "${TEMP}"
@@ -131,6 +211,7 @@ append_unionfs_fuse() {
 #      cp -f /etc/suspend.conf "${TEMP}/initramfs-suspend-temp/etc" ||
 #              gen_die 'Could not copy /etc/suspend.conf'
 #      cd "${TEMP}/initramfs-suspend-temp/"
+#      log_future_cpio_content
 #      find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
 #                      || gen_die "compressing suspend cpio"
 #      rm -r "${TEMP}/initramfs-suspend-temp/"
@@ -142,37 +223,13 @@ append_multipath(){
                rm -r "${TEMP}/initramfs-multipath-temp"
        fi
        print_info 1 '  Multipath support being added'
-       mkdir -p "${TEMP}/initramfs-multipath-temp/bin/"
-       mkdir -p "${TEMP}/initramfs-multipath-temp/etc/" 
-       mkdir -p "${TEMP}/initramfs-multipath-temp/sbin/"
-       mkdir -p "${TEMP}/initramfs-multipath-temp/lib/"
+       mkdir -p "${TEMP}"/initramfs-multipath-temp/{bin,etc,sbin,lib}/
 
-       # Copy files to /lib
-       for i in /lib/{ld-*,libc-*,libc.*,libdl-*,libdl.*,libdevmapper*so*,libpthread*,librt*,libreadline*,libncurses*}
-       do
-               cp -a "${i}" "${TEMP}/initramfs-multipath-temp/lib" \
-                       || gen_die "Could not copy file ${i} for MULTIPATH"
-       done
-
-       for i in /usr/lib/libaio*
-       do
-                cp -a "${i}" "${TEMP}/initramfs-multipath-temp/lib" \
-                       || gen_die "Could not copy file ${i} for MULTIPATH"
-       done
-
-       # Copy files to /sbin
-       for i in /sbin/{multipath,kpartx,mpath_prio_*,devmap_name,dmsetup} /lib64/udev/scsi_id
-       do
-               cp -a "${i}" "${TEMP}/initramfs-multipath-temp/sbin" \
-                       || gen_die "Could not copy file ${i} for MULTIPATH"
-       done
-
-       # Copy files to /bin
-       for i in /bin/mountpoint
-       do
-               cp -a "${i}" "${TEMP}/initramfs-multipath-temp/bin" \
-                       || gen_die "Could not copy file ${i} for MULTIPATH"
-       done
+       # Copy files
+       copy_binaries "${TEMP}/initramfs-multipath-temp" \
+               /bin/mountpoint \
+               /sbin/{multipath,kpartx,mpath_prio_*,devmap_name,dmsetup} \
+               /{lib,lib64}/{udev/scsi_id,multipath/*so} 
 
        if [ -x /sbin/multipath ]
        then
@@ -185,6 +242,7 @@ append_multipath(){
                cp /etc/scsi_id.config "${TEMP}/initramfs-multipath-temp/etc/" || gen_die 'could not copy scsi_id.config'
        fi
        cd "${TEMP}/initramfs-multipath-temp"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing multipath cpio"
        cd "${TEMP}"
@@ -209,6 +267,7 @@ append_dmraid(){
                ln -sf raid456.kp raid45.ko
                cd "${TEMP}/initramfs-dmraid-temp/"
        fi
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing dmraid cpio"
        cd "${TEMP}"
@@ -228,6 +287,7 @@ append_iscsi(){
                gen_die "Could not extract iscsi binary cache!"
        chmod a+x "${TEMP}/initramfs-iscsi-temp/bin/iscsistart"
        cd "${TEMP}/initramfs-iscsi-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing iscsi cpio"
        cd "${TEMP}"
@@ -242,7 +302,7 @@ append_lvm(){
        cd ${TEMP}
        mkdir -p "${TEMP}/initramfs-lvm-temp/bin/"
        mkdir -p "${TEMP}/initramfs-lvm-temp/etc/lvm/"
-       if [ -e '/sbin/lvm.static' ]
+       if false && [ -e '/sbin/lvm.static' ]
        then
                print_info 1 '          LVM: Adding support (using local static binary /sbin/lvm.static)...'
                cp /sbin/lvm.static "${TEMP}/initramfs-lvm-temp/bin/lvm" ||
@@ -252,7 +312,7 @@ append_lvm(){
                then
                        cp /sbin/dmsetup.static "${TEMP}/initramfs-lvm-temp/bin/dmsetup"
                fi
-       elif [ -e '/sbin/lvm' ] && LC_ALL="C" ldd /sbin/lvm|grep -q 'not a dynamic executable'
+       elif false && [ -e '/sbin/lvm' ] && LC_ALL="C" ldd /sbin/lvm|grep -q 'not a dynamic executable'
        then
                print_info 1 '          LVM: Adding support (using local static binary /sbin/lvm)...'
                cp /sbin/lvm "${TEMP}/initramfs-lvm-temp/bin/lvm" ||
@@ -272,8 +332,9 @@ append_lvm(){
                # See bug 382555
                mv ${TEMP}/initramfs-lvm-temp/sbin/dmsetup.static ${TEMP}/initramfs-lvm-temp/bin/dmsetup ||
                        gen_die 'LVM error: Could not move dmsetup.static to dmsetup!'
+               rm -rf ${TEMP}/initramfs-lvm-temp/{lib,share,man,include,sbin/{lvm,dmsetup}}
        fi
-       if [ -x /sbin/lvm ]
+       if [ -x /sbin/lvm -o -x /bin/lvm ]
        then
 #              lvm dumpconfig 2>&1 > /dev/null || gen_die 'Could not copy over lvm.conf!'
 #              ret=$?
@@ -286,6 +347,7 @@ append_lvm(){
 #              fi
        fi
        cd "${TEMP}/initramfs-lvm-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing lvm cpio"
        cd "${TEMP}"
@@ -329,12 +391,39 @@ append_mdadm(){
                fi
        fi
        cd "${TEMP}/initramfs-mdadm-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing mdadm cpio"
        cd "${TEMP}"
        rm -rf "${TEMP}/initramfs-mdadm-temp" > /dev/null
 }
 
+append_zfs(){
+       if [ -d "${TEMP}/initramfs-zfs-temp" ]
+       then
+               rm -r "${TEMP}/initramfs-zfs-temp"
+       fi
+
+       mkdir -p "${TEMP}/initramfs-zfs-temp/etc/zfs/"
+
+       # Copy files to /etc/zfs
+       for i in /etc/zfs/{zdev.conf,zpool.cache}
+       do
+               cp -a "${i}" "${TEMP}/initramfs-zfs-temp/etc/zfs" \
+                       || print_warning 1 "Could not copy file ${i} for ZFS"
+       done
+
+       # Copy binaries
+       copy_binaries "${TEMP}/initramfs-zfs-temp" /sbin/{mount.zfs,zfs,zpool}
+
+       cd "${TEMP}/initramfs-zfs-temp/"
+       log_future_cpio_content
+       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
+                       || gen_die "compressing zfs cpio"
+       cd "${TEMP}"
+       rm -rf "${TEMP}/initramfs-zfs-temp" > /dev/null
+}
+
 append_splash(){
        splash_geninitramfs=`which splash_geninitramfs 2>/dev/null`
        if [ -x "${splash_geninitramfs}" ]
@@ -356,6 +445,7 @@ append_splash(){
                        cp -f "/usr/share/splashutils/initrd.splash" "${TEMP}/initramfs-splash-temp/etc"
                fi
                cd "${TEMP}/initramfs-splash-temp/"
+               log_future_cpio_content
                find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing splash cpio"
                cd "${TEMP}"
@@ -367,11 +457,16 @@ append_splash(){
 
 append_overlay(){
        cd ${INITRAMFS_OVERLAY}
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing overlay cpio"
 }
 
 append_luks() {
+       local _luks_error_format="LUKS support cannot be included: %s.  Please emerge sys-fs/cryptsetup[static]."
+       local _luks_source=/sbin/cryptsetup
+       local _luks_dest=/sbin/cryptsetup
+
        if [ -d "${TEMP}/initramfs-luks-temp" ]
        then
                rm -r "${TEMP}/initramfs-luks-temp/"
@@ -383,22 +478,14 @@ append_luks() {
 
        if isTrue ${LUKS}
        then
-               if is_static /bin/cryptsetup
-               then
-                       print_info 1 "Including LUKS support"
-                       cp /bin/cryptsetup ${TEMP}/initramfs-luks-temp/sbin/cryptsetup
-                       chmod +x "${TEMP}/initramfs-luks-temp/sbin/cryptsetup"
-               elif is_static /sbin/cryptsetup
-               then
-                       print_info 1 "Including LUKS support"
-                       cp /sbin/cryptsetup ${TEMP}/initramfs-luks-temp/sbin/cryptsetup
-                       chmod +x "${TEMP}/initramfs-luks-temp/sbin/cryptsetup"
-               else
-                       print_info 1 "LUKS support requires static cryptsetup at /bin/cryptsetup or /sbin/cryptsetup"
-                       print_info 1 "Not including LUKS support"
-               fi
+               [ -x "${_luks_source}" ] \
+                               || gen_die "$(printf "${_luks_error_format}" "no file ${_luks_source}")"
+
+               print_info 1 "Including LUKS support"
+               copy_binaries "${TEMP}/initramfs-luks-temp/" /sbin/cryptsetup
        fi
 
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                || gen_die "appending cryptsetup to cpio"
 
@@ -429,6 +516,7 @@ append_firmware() {
        else
                cp -a "${FIRMWARE_DIR}"/* ${TEMP}/initramfs-firmware-temp/lib/firmware/
        fi
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                || gen_die "appending firmware to cpio"
        cd "${TEMP}"
@@ -450,6 +538,7 @@ append_gpg() {
                gen_die 'Could not extract gpg binary cache!'
        chmod a+x "${TEMP}/initramfs-gpg-temp/sbin/gpg"
        cd "${TEMP}/initramfs-gpg-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
        rm -rf "${TEMP}/initramfs-gpg-temp" > /dev/null
 }
@@ -502,12 +591,48 @@ append_modules() {
                print_list ${!group_modules} > "${TEMP}/initramfs-modules-${KV}-temp/etc/modules/${group}"
        done
        cd "${TEMP}/initramfs-modules-${KV}-temp/"
+       log_future_cpio_content
        find . | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing modules cpio"
        cd "${TEMP}"
        rm -r "${TEMP}/initramfs-modules-${KV}-temp/"   
 }
 
+append_modprobed() {
+       local TDIR="${TEMP}/initramfs-modprobe.d-temp"
+       if [ -d "${TDIR}" ]
+       then
+               rm -r "${TDIR}"
+       fi
+
+       mkdir -p "${TDIR}/etc/module_options/"
+
+       # Load module parameters
+       for dir in $(find "${MODPROBEDIR}"/*)
+       do
+               while read x
+               do
+                       case "${x}" in
+                               options*)
+                                       module_name="$(echo "$x" | cut -d ' ' -f 2)"
+                                       [ "${module_name}" != "$(echo)" ] || continue
+                                       module_options="$(echo "$x" | cut -d ' ' -f 3-)"
+                                       [ "${module_options}" != "$(echo)" ] || continue
+                                       echo "${module_options}" >> "${TDIR}/etc/module_options/${module_name}.conf"
+                               ;;
+                       esac
+               done < "${dir}"
+       done
+
+       cd "${TDIR}"
+       log_future_cpio_content
+       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
+                       || gen_die "compressing modprobe.d cpio"
+
+       cd "${TEMP}"
+       rm -rf "${TDIR}" > /dev/null
+}
+
 # check for static linked file with objdump
 is_static() {
        LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
@@ -586,8 +711,10 @@ append_auxilary() {
        fi
        if isTrue $CMD_KEYMAP
        then
-               mkdir -p "${TEMP}/initramfs-aux-temp/lib/keymaps"
-               /bin/tar -C "${TEMP}/initramfs-aux-temp/lib/keymaps" -zxf "${GK_SHARE}/defaults/keymaps.tar.gz"
+               print_info 1 "        >> Copying keymaps"
+               mkdir -p "${TEMP}/initramfs-aux-temp/lib/"
+               cp -R "${GK_SHARE}/defaults/keymaps" "${TEMP}/initramfs-aux-temp/lib/" \
+                               || gen_die "Error while copying keymaps"
        fi
 
        cd ${TEMP}/initramfs-aux-temp/sbin && ln -s ../init init
@@ -604,6 +731,7 @@ append_auxilary() {
        fi
 
        cd "${TEMP}/initramfs-aux-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing auxilary cpio"
        cd "${TEMP}"
@@ -623,6 +751,7 @@ append_data() {
 }
 
 create_initramfs() {
+       local compress_ext=""
        print_info 1 "initramfs: >> Initializing..."
 
        # Create empty cpio
@@ -633,6 +762,7 @@ create_initramfs() {
        append_data 'base_layout'
        append_data 'auxilary' "${BUSYBOX}"
        append_data 'busybox' "${BUSYBOX}"
+       isTrue "${CMD_E2FSPROGS}" && append_data 'e2fsprogs'
        append_data 'lvm' "${LVM}"
        append_data 'dmraid' "${DMRAID}"
        append_data 'iscsi' "${ISCSI}"
@@ -648,12 +778,16 @@ create_initramfs() {
                print_info 1 "initramfs: Not copying modules..."
        fi
 
+       append_data 'zfs' "${ZFS}"
+
        append_data 'blkid' "${DISKLABEL}"
 
        append_data 'unionfs_fuse' "${UNIONFS}"
 
        append_data 'splash' "${SPLASH}"
 
+       append_data 'modprobed'
+
        if isTrue "${FIRMWARE}" && [ -n "${FIRMWARE_DIR}" ]
        then
                append_data 'firmware'
@@ -665,16 +799,110 @@ create_initramfs() {
                append_data 'overlay'
        fi
 
-       gzip -9 "${CPIO}"
-       mv -f "${CPIO}.gz" "${CPIO}"
-
        if isTrue "${INTEGRATED_INITRAMFS}"
        then
-#              cp ${TMPDIR}/initramfs-${KV} ${KERNEL_DIR}/usr/initramfs_data.cpio.gz
-               mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio.gz
-#              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.gz\"\nCONFIG_INITRAMFS_ROOT_UID=0\nCONFIG_INITRAMFS_ROOT_GID=0" >> ${KERNEL_DIR}/.config
+               compress_config='INITRAMFS_COMPRESSION_NONE'
+               case ${compress_ext} in
+                       gz)  compress_config='INITRAMFS_COMPRESSION_GZIP' ;;
+                       bz2) compress_config='INITRAMFS_COMPRESSION_BZIP2' ;;
+                       lzma) compress_config='INITRAMFS_COMPRESSION_LZMA' ;;
+                       xz) compress_config='INITRAMFS_COMPRESSION_XZ' ;;
+                       lzo) compress_config='INITRAMFS_COMPRESSION_LZO' ;;
+                       *) compress_config='INITRAMFS_COMPRESSION_NONE' ;;
+               esac
+               # All N default except XZ, so there it gets used if the kernel does
+               # compression on it's own.
+               cat >>${KERNEL_DIR}/.config     <<-EOF
+               CONFIG_INITRAMFS_SOURCE="${TMPDIR}/initramfs-${KV}.cpio${compress_ext}"
+               CONFIG_INITRAMFS_ROOT_UID=0
+               CONFIG_INITRAMFS_ROOT_GID=0
+               CONFIG_INITRAMFS_COMPRESSION_NONE=n
+               CONFIG_INITRAMFS_COMPRESSION_GZIP=n
+               CONFIG_INITRAMFS_COMPRESSION_BZIP2=n
+               CONFIG_INITRAMFS_COMPRESSION_LZMA=n
+               CONFIG_INITRAMFS_COMPRESSION_XZ=y
+               CONFIG_INITRAMFS_COMPRESSION_LZO=n
+               CONFIG_${compress_config}=y
+               EOF
+       else
+               if isTrue "${COMPRESS_INITRD}"
+               then
+                       # NOTE:  We do not work with ${KERNEL_CONFIG} here, since things like
+                       #        "make oldconfig" or --noclean could be in effect.
+                       if [ -f "${KERNEL_DIR}"/.config ]; then
+                               local ACTUAL_KERNEL_CONFIG="${KERNEL_DIR}"/.config
+                       else
+                               local ACTUAL_KERNEL_CONFIG="${KERNEL_CONFIG}"
+                       fi
+
+                       if [[ "$(file --brief --mime-type "${ACTUAL_KERNEL_CONFIG}")" == application/x-gzip ]]; then
+                               # Support --kernel-config=/proc/config.gz, mainly
+                               local CONFGREP=zgrep
+                       else
+                               local CONFGREP=grep
+                       fi
+
+                       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)
+                       pkg_xz='app-arch/xz-utils'
+                       pkg_lzma='app-arch/xz-utils'
+                       pkg_bzip2='app-arch/bzip2'
+                       pkg_gzip='app-arch/gzip'
+                       pkg_lzop='app-arch/lzop'
+                       local compression
+                       case ${COMPRESS_INITRD_TYPE} in
+                               xz|lzma|bzip2|gzip|lzop) compression=${COMPRESS_INITRD_TYPE} ;;
+                               lzo) compression=lzop ;;
+                               best|fastest)
+                                       for tuple in \
+                                                       'CONFIG_RD_XZ    cmd_xz    xz' \
+                                                       'CONFIG_RD_LZMA  cmd_lzma  lzma' \
+                                                       'CONFIG_RD_BZIP2 cmd_bzip2 bzip2' \
+                                                       'CONFIG_RD_GZIP  cmd_gzip  gzip' \
+                                                       'CONFIG_RD_LZO   cmd_lzop  lzop'; do
+                                               set -- ${tuple}
+                                               kernel_option=$1
+                                               cmd_variable_name=$2
+                                               if ${CONFGREP} -q "^${kernel_option}=y" "${ACTUAL_KERNEL_CONFIG}" && test -n "${!cmd_variable_name}" ; then
+                                                       compression=$3
+                                                       [[ ${COMPRESS_INITRD_TYPE} == best ]] && break
+                                               fi
+                                       done
+                                       [[ -z "${compression}" ]] && gen_die "None of the initramfs compression methods we tried are supported by your kernel (config file \"${ACTUAL_KERNEL_CONFIG}\"), strange!?"
+                                       ;;
+                               *)
+                                       gen_die "Compression '${COMPRESS_INITRD_TYPE}' unknown"
+                                       ;;
+                       esac
+
+                       # Check for actual availability
+                       cmd_variable_name=cmd_${compression}
+                       pkg_variable_name=pkg_${compression}
+                       [[ -z "${!cmd_variable_name}" ]] && gen_die "Compression '${compression}' is not available. Please install package '${!pkg_variable_name}'."
+
+                       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" ;;
+                               lzop) 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}"