Report on sourced config files
[genkernel.git] / gen_initramfs.sh
index 3dd757bf10291a9cc2e9ec3d9e31bfbbaf18338f..ac90830d200eeea66db4469e830211dfd6d72004 100755 (executable)
@@ -3,12 +3,19 @@
 
 CPIO_ARGS="--quiet -o -H newc"
 
-# The copy_binaries function is explicitly released into the public domain to
+# 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 Public domain outside of genkernel
-# - Past and future changes to copy_binaries() still need to be licensable under
-#   <GPL v2 or later> to maintain license compatibility with genkernel itself
+# - 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:
@@ -16,13 +23,32 @@ CPIO_ARGS="--quiet -o -H newc"
 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
+                       | 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() {
@@ -64,6 +90,7 @@ append_base_layout() {
        date -u '+%Y%m%d-%H%M%S' > ${TEMP}/initramfs-base-temp/etc/build_date
 
        cd "${TEMP}/initramfs-base-temp/"
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing baselayout cpio"
        cd "${TEMP}"
@@ -93,6 +120,7 @@ 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}"
@@ -105,11 +133,14 @@ append_blkid(){
                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}"
@@ -141,6 +172,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}"
@@ -161,6 +193,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/"
@@ -188,6 +221,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}"
@@ -212,6 +246,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}"
@@ -231,6 +266,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}"
@@ -275,7 +311,7 @@ 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,{s,}bin/{lvm,dmsetup}}
+               rm -rf ${TEMP}/initramfs-lvm-temp/{lib,share,man,include,sbin/{lvm,dmsetup}}
        fi
        if [ -x /sbin/lvm -o -x /bin/lvm ]
        then
@@ -290,6 +326,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}"
@@ -333,6 +370,7 @@ 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}"
@@ -358,6 +396,7 @@ append_zfs(){
        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}"
@@ -385,6 +424,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}"
@@ -396,6 +436,7 @@ append_splash(){
 
 append_overlay(){
        cd ${INITRAMFS_OVERLAY}
+       log_future_cpio_content
        find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
                        || gen_die "compressing overlay cpio"
 }
@@ -419,14 +460,11 @@ append_luks() {
                [ -x "${_luks_source}" ] \
                                || gen_die "$(printf "${_luks_error_format}" "no file ${_luks_source}")"
 
-               is_static "${_luks_source}" \
-                               || gen_die "$(printf "${_luks_error_format}" "${_luks_source} not a static binary")"
-
                print_info 1 "Including LUKS support"
-               cp "${_luks_source}" ${TEMP}/initramfs-luks-temp${_luks_dest}
-               chmod +x "${TEMP}/initramfs-luks-temp${_luks_dest}"
+               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"
 
@@ -457,6 +495,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}"
@@ -478,6 +517,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
 }
@@ -530,12 +570,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
@@ -634,6 +710,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}"
@@ -687,6 +764,8 @@ create_initramfs() {
 
        append_data 'splash' "${SPLASH}"
 
+       append_data 'modprobed'
+
        if isTrue "${FIRMWARE}" && [ -n "${FIRMWARE_DIR}" ]
        then
                append_data 'firmware'
@@ -712,6 +791,13 @@ create_initramfs() {
        else
                if isTrue "${COMPRESS_INITRD}"
                then
+                       if [[ "$(file --brief --mime-type "${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)
@@ -730,17 +816,18 @@ create_initramfs() {
                                        for tuple in \
                                                        'CONFIG_RD_XZ    cmd_xz    xz' \
                                                        'CONFIG_RD_LZMA  cmd_lzma  lzma' \
-                                                       'CONFIG_RD_BZIP2 cmd_bzip2 bzip' \
+                                                       '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 grep -sq "^${kernel_option}=y" ${KERNEL_DIR}/.config && test -n "${!cmd_variable_name}" ; then
+                                               if ${CONFGREP} -q "^${kernel_option}=y" "${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 we tried are supported by your kernel (config file \"${KERNEL_CONFIG}\"), strange!?"
                                        ;;
                                *)
                                        gen_die "Compression '${COMPRESS_INITRD_TYPE}' unknown"