mkinitramfs命令详解

它是个脚本文件,位置在/usr/sbin下,被update-initramfs脚本调用。

update-initramfs脚本又会在内核编译最后一步,make install的时候调用。


这个脚本文件要创建一个initrd.img.3.18.4这样的文件和vmlinuz-3.18.4一起启动系统用的。

这个initrd.img文件是个压缩文件可以解压到一个目录里。

这个脚本主要是拷贝一些系统上的文件,包括

/usr/share/initramfs-tools/下的文件和文件夹

/lib/modules/3.18.4/下的文件和文件夹。

还会创建一些文件夹和拷贝一些系统的文件比如/etc文件夹

/sbin文件夹。命令比如wait-for-root命令blkid命令,mrprobe命令。

具体看下面的脚本文件:





#!/bin/sh


umask 0022
export PATH='/usr/bin:/sbin:/bin'

# Defaults
keep="n"
CONFDIR="/etc/initramfs-tools"
verbose="n"
test -e /bin/busybox && BUSYBOXDIR=/bin
test -e /usr/lib/initramfs-tools/bin/busybox && BUSYBOXDIR=/usr/lib/initramfs-tools/bin
export BUSYBOXDIR

OPTIONS=`getopt -o c:d:ko:r:v -n "$0" -- "$@"`

# Check for non-GNU getopt
if [ $? != 0 ] ; then echo "W: non-GNU getopt" >&2 ; exit 1 ; fi

eval set -- "$OPTIONS"

while true; do
    case "$1" in
    -c)
        compress="$2"
        shift 2
        ;;
    -d)
        CONFDIR="$2"
        shift 2
        if [ ! -d "${CONFDIR}" ]; then
            echo "${0}: ${CONFDIR}: Not a directory" >&2
            exit 1
        fi
        ;;
    -o)
        outfile="$2"
        shift 2
        ;;
    -k)
        keep="y"
        shift
        ;;
    -r)
        ROOT="$2"
        shift 2
        ;;
    -v)
        verbose="y"
        shift
        ;;
    --)
        shift
        break
        ;;
    *)
        echo "Internal error!" >&2
        exit 1
        ;;
    esac
done

# For dependency ordered mkinitramfs hook scripts.
. /usr/share/initramfs-tools/scripts/functions
. /usr/share/initramfs-tools/hook-functions

. "${CONFDIR}/initramfs.conf"
EXTRA_CONF=''
for i in /usr/share/initramfs-tools/conf.d/* ${CONFDIR}/conf.d/*; do
    [ -e $i ] && EXTRA_CONF="${EXTRA_CONF} $(basename $i \
        | grep '^[[:alnum:]][[:alnum:]\._-]*$' | grep -v '\.dpkg-.*$')";
done
# FIXME: deprecated those settings on mkinitramfs run
#      these conf dirs are for boot scripts and land on initramfs
for i in ${EXTRA_CONF}; do
    if [ -e  ${CONFDIR}/conf.d/${i} ]; then
        . ${CONFDIR}/conf.d/${i}
    elif [ -e  /usr/share/initramfs-tools/conf.d/${i} ]; then
        . /usr/share/initramfs-tools/conf.d/${i}
    fi
done

# source package confs
for i in /usr/share/initramfs-tools/conf-hooks.d/*; do
    if [ -e "${i}" ]; then
        . "${i}"
    fi
done

if [ -n "${UMASK:-}" ]; then
    umask "${UMASK}"
fi

if [ -z "${outfile}" ]; then
    usage
fi

touch "$outfile"
outfile="$(readlink -f "$outfile")"

# And by "version" we really mean path to kernel modules
# This is braindead, and exists to preserve the interface with mkinitrd
if [ ${#} -ne 1 ]; then
    version="$(uname -r)"
else
    version="${1}"
fi

# Check that we're using a new enough kernel version, first for ourselves,
# then for each of the hooks, which can have a MINKVER variable defined
check_minkver ${version}
check_minkver ${version} /usr/share/initramfs-tools/hooks
check_minkver ${version} ${CONFDIR}/hooks

case "${version}" in
/lib/modules/*/[!/]*)
    ;;
/lib/modules/[!/]*)
    version="${version#/lib/modules/}"
    version="${version%%/*}"
    ;;
esac

case "${version}" in
*/*)
    echo "$PROG: ${version} is not a valid kernel version" >&2
    exit 1
    ;;
esac

# Check userspace and kernel support for compressed initramfs images
if [ -z "${compress:-}" ]; then
    compress=${COMPRESS}
else
    COMPRESS=${compress}
fi

if ! command -v "${compress}" >/dev/null 2>&1; then
    compress=gzip
    [ "${verbose}" = y ] && \
        echo "No ${COMPRESS} in ${PATH}, using gzip"
    COMPRESS=gzip
fi

if ! `grep -q -i ^config_rd_${COMPRESS%p} /boot/config-${version}` ; then
    compress=gzip
    [ "${verbose}" = y ] && \
        echo "linux-2.6 misses ${COMPRESS} support, using gzip"
fi

[ "${compress}" = lzop ] && compress="lzop -9"
[ "${compress}" = xz ] && compress="xz -8 --check=crc32"

if [ -d "${outfile}" ]; then
    echo "${outfile} is a directory" >&2
    exit 1
fi

MODULESDIR="/lib/modules/${version}"

这里的version是你下载的要重新编译内核的版本。

这样MODULESDIR就表示了/lib/modules/3.18.4


if [ ! -e "${MODULESDIR}" ]; then
    echo "WARNING: missing ${MODULESDIR}"
    echo "Device driver support needs thus be built-in linux image!"
fi
if [ ! -e "${MODULESDIR}/modules.dep" ]; then
    depmod ${version}
fi

DESTDIR="$(mktemp -d ${TMPDIR:-/tmp}/mkinitramfs_XXXXXX)" || exit 1
chmod 755 "${DESTDIR}"

# do not execute cache_run_scripts() if mounted with noexec
NOEXEC=""
fs=$(df -P $DESTDIR | tail -1 | awk '{print $6}')
if [ -n "$fs" ] && mount | grep -q "on $fs .*noexec" ; then
    NOEXEC=1
fi

__TMPCPIOGZ="$(mktemp ${TMPDIR:-/tmp}/mkinitramfs-OL_XXXXXX)" || exit 1
__TMPEARLYCPIO="$(mktemp ${TMPDIR:-/var/tmp}/mkinitramfs-FW_XXXXXX)" || exit 1

DPKG_ARCH=`dpkg --print-architecture`

# Export environment for hook scripts.
#
export MODULESDIR
export version
export CONFDIR
export DESTDIR
export DPKG_ARCH
export verbose
export MODULES
export BUSYBOX
export COMPCACHE_SIZE

# Private, used by 'catenate_cpiogz'.
export __TMPCPIOGZ

# Private, used by 'prepend_earlyinitramfs'.
export __TMPEARLYCPIO

for d in bin conf/conf.d etc lib/modules run sbin scripts ${MODULESDIR}; do
    mkdir -p "${DESTDIR}/${d}"
done

这里会创建大部分的必要的文件夹。

这里会创建${MODULESDIR}文件夹。

比如我的是/lib/modules/3.18.4。

如果你不能启动系统进入busybox,这也是一个常见的错误。

你会发现里面也有一个/lib/modules/3.18.4目录,里面的文件就是拷贝的原系统里面

刚刚编译除的模块。


# Copy in modules.builtin and modules.order (not generated by depmod)
for x in modules.builtin modules.order; do
    if [ -f "${MODULESDIR}/${x}" ]; then
        cp -p "${MODULESDIR}/${x}" "${DESTDIR}${MODULESDIR}/${x}"
    fi

done

这里是把/lib/modules/3.18.4/下的模块,包括modules.builtin

和modules.order,都拷贝到busybox里面。




# MODULES=list case.  Always honour.
for x in "${CONFDIR}/modules" /usr/share/initramfs-tools/modules.d/*; do
    if [ -f "${x}" ]; then
        add_modules_from_file "${x}"
    fi

done

add_modules_from_file定义在/usr/share/initramfs-tools/hook-functions里面。

/etc/initramfs-tools/modules文件内容为:

# List of modules that you want to include in your initramfs.
# They will be loaded at boot time in the order below.
#
# Syntax:  module_name [args ...]
#
# You must run update-initramfs(8) to effect this change.
#
# Examples:
#
# raid1
# sd_mod

可见如果你想在启动的时候就加载一个模块,这是一个不错的时机。


# MODULES=most is default
case "${MODULES}" in
dep)
    dep_add_modules
    ;;
most)
    auto_add_modules
    ;;
netboot)
    auto_add_modules base
    auto_add_modules net
    ;;
list)
    # nothing to add
    ;;
*)
    echo "W: mkinitramfs: unsupported MODULES setting: ${MODULES}."
    echo "W: mkinitramfs: Falling back to MODULES=most."
    auto_add_modules
    ;;
esac

# Resolve hidden dependencies
hidden_dep_add_modules

# First file executed by linux-2.6
cp -p /usr/share/initramfs-tools/init ${DESTDIR}/init

这个就是initrd.img文件根目录里面的init脚本文件。

/usr/share/initramfs-tools/init是一模一样的。

有兴趣的可以把这个init文件换成/bin/sh试试。然后在make install之后重启,就能看到结果了。


# add existant boot scripts
for b in $(cd /usr/share/initramfs-tools/scripts/ && find . \
    -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f); do
    option=$(sed '/^OPTION=/!d;$d;s/^OPTION=//;s/[[:space:]]*$//' "/usr/share/initramfs-tools/scripts/${b}")
    [ -z "${option}" ] || eval test -n \"\${$option}\" -a \"\${$option}\" != \"n\" || continue

    [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
        || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
    cp -p "/usr/share/initramfs-tools/scripts/${b}" \
        "${DESTDIR}/scripts/$(dirname "${b}")/"
done
for b in $(cd "${CONFDIR}/scripts" && find . \
    -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f); do
    option=$(sed '/^OPTION=/!d;$d;s/^OPTION=//;s/[[:space:]]*$//' "${CONFDIR}/scripts/${b}")
    [ -z "${option}" ] || eval test -n \"\${$option}\" -a \"\${$option}\" != \"n\" || continue

    [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
        || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
    cp -p "${CONFDIR}/scripts/${b}" "${DESTDIR}/scripts/$(dirname "${b}")/"
done

echo "DPKG_ARCH=${DPKG_ARCH}" > ${DESTDIR}/conf/arch.conf
cp -p "${CONFDIR}/initramfs.conf" ${DESTDIR}/conf
for i in ${EXTRA_CONF}; do
    if [ -e "${CONFDIR}/conf.d/${i}" ]; then
        copy_exec "${CONFDIR}/conf.d/${i}" /conf/conf.d
    elif [ -e "/usr/share/initramfs-tools/conf.d/${i}" ]; then
        copy_exec "/usr/share/initramfs-tools/conf.d/${i}" /conf/conf.d
    fi
done

# ROOT hardcoding
if [ -n "${ROOT:-}" ]; then
    echo "ROOT=${ROOT}" > ${DESTDIR}/conf/conf.d/root
fi

if ! command -v ldd >/dev/null 2>&1 ; then
    echo "WARNING: no ldd around - install libc-bin" >&2
    exit 1
fi

copy_exec /usr/lib/initramfs-tools/bin/wait-for-root /sbin

# module-init-tools
copy_exec /sbin/modprobe /sbin
copy_exec /sbin/rmmod /sbin
mkdir -p "${DESTDIR}/etc/modprobe.d"
cp -a /etc/modprobe.d/* "${DESTDIR}/etc/modprobe.d/"

上面的是拷贝了两个mod相关的命令和配置文件。

cheng@chpc:~$ ls /etc/modprobe.d/*
/etc/modprobe.d/alsa-base.conf
/etc/modprobe.d/blacklist-ath_pci.conf
/etc/modprobe.d/blacklist.conf
/etc/modprobe.d/blacklist-firewire.conf
/etc/modprobe.d/blacklist-framebuffer.conf
/etc/modprobe.d/blacklist-modem.conf
/etc/modprobe.d/blacklist-oss.conf
/etc/modprobe.d/blacklist-rare-network.conf
/etc/modprobe.d/blacklist-watchdog.conf
/etc/modprobe.d/fbdev-blacklist.conf
/etc/modprobe.d/iwlwifi.conf
/etc/modprobe.d/mlx4.conf
/etc/modprobe.d/vmwgfx-fbdev.conf

可以看出里面就是一些.conf配置文件。

注意里面有一个黑名单文件blacklist.conf。init脚本会调用到。



# util-linux
copy_exec /sbin/blkid /sbin

这里会把blkid文件拷贝到/sbin目录,你可以看看是否有这个文件。

这个文件是看文件系统的类型的。


# workaround: libgcc always needed on old-abi arm
if [ "$DPKG_ARCH" = arm ] || [ "$DPKG_ARCH" = armeb ]; then
    cp -a /lib/libgcc_s.so.1 "${DESTDIR}/lib/"
fi

run_scripts /usr/share/initramfs-tools/hooks optional
run_scripts "${CONFDIR}"/hooks optional

:比如hooks文件夹里的udev脚本文件里面有拷贝systemd-udevd的一条

mkdir -p $DESTDIR/lib/systemd

copy_exec /lib/systemd/systemd-udevd      /lib/systemd
if [ -x /sbin/udevadm.upgrade ]; then
    copy_exec /bin/udevadm.upgrade /bin/udevadm
else
    copy_exec /bin/udevadm

fi



# cache boot run order
if [ -n "$NOEXEC" ]; then
    echo "W: TMPDIR is mounted noexec, will not cache run scripts."
else
    for b in $(cd "${DESTDIR}/scripts" && find . -mindepth 1 -type d); do
        cache_run_scripts "${DESTDIR}" "/scripts/${b#./}"
    done
fi

# generate module deps
depmod -a -b "${DESTDIR}" ${version}
rm -f "${DESTDIR}/lib/modules/${version}"/modules.*map

# make sure that library search path is up to date
cp -ar /etc/ld.so.conf* "$DESTDIR"/etc/
if ! ldconfig -r "$DESTDIR" ; then
    [ $(id -u) != "0" ] \
    && echo "ldconfig might need uid=0 (root) for chroot()" >&2
fi

# Apply DSDT to initramfs
if [ -e "${CONFDIR}/DSDT.aml" ]; then
    copy_exec "${CONFDIR}/DSDT.aml" /
fi

# Remove any looping or broken symbolic links, since they break cpio.
[ "${verbose}" = y ] && xargs_verbose="-t"
(cd "${DESTDIR}" && find . -type l -printf '%p %Y\n' | sed -n 's/ [LN]$//p' \
    | xargs ${xargs_verbose:-} -rL1 rm -f)

# dirty hack for armhf's double-linker situation; if we have one of
# the two known eglibc linkers, nuke both and re-create sanity
if [ "$DPKG_ARCH" = armhf ]; then
    if [ -e "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3" ] || \
       [ -e "${DESTDIR}/lib/ld-linux-armhf.so.3" ]; then
        rm -f "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
        rm -f "${DESTDIR}/lib/ld-linux-armhf.so.3"
        cp -aL /lib/ld-linux-armhf.so.3 "${DESTDIR}/lib/"
        ln -sf /lib/ld-linux-armhf.so.3 "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
    fi
fi

[ "${verbose}" = y ] && echo "Building cpio ${outfile} initramfs"

if [ -s "${__TMPEARLYCPIO}" ]; then
    cat "${__TMPEARLYCPIO}" >"${outfile}" || exit 1
else
    # truncate
    > "${outfile}"
fi

(
# work around lack of "set -o pipefail" for the following pipe:
# cd "${DESTDIR}" && find . | cpio --quiet -R 0:0 -o -H newc | gzip >>"${outfile}" || exit 1
exec 3>&1
eval `
    # http://cfaj.freeshell.org/shell/cus-faq-2.html
    exec 4>&1 >&3 3>&-
    cd  "${DESTDIR}"
    {
        find . 4>&-; echo "ec1=$?;" >&4
    } | {
        cpio --quiet -R 0:0 -o -H newc 4>&-; echo "ec2=$?;" >&4
    } | ${compress} >>"${outfile}"
    echo "ec3=$?;" >&4
`
if [ "$ec1" -ne 0 ]; then
    echo "E: mkinitramfs failure find $ec1 cpio $ec2 $compress $ec3"
    exit "$ec1"
fi
if [ "$ec2" -ne 0 ]; then
    echo "E: mkinitramfs failure cpio $ec2 $compress $ec3"
    exit "$ec2"
fi
if [ "$ec3" -ne 0 ]; then
    echo "E: mkinitramfs failure $compress $ec3"
    exit "$ec3"
fi
) || exit 1

if [ -s "${__TMPCPIOGZ}" ]; then
    cat "${__TMPCPIOGZ}" >>"${outfile}" || exit 1
fi

if [ "${keep}" = "y" ]; then
    echo "Working files in ${DESTDIR}, early initramfs in ${__TMPEARLYCPIO} and overlay in ${__TMPCPIOGZ}"
else
    rm -rf "${DESTDIR}"
    rm -rf "${__TMPCPIOGZ}"
    rm -rf "${__TMPEARLYCPIO}"
fi

exit 0


你可能感兴趣的:(内核重新编译,ubuntu)