cat > /sbin/mkinitramfs << "EOF" #!/bin/bash # This file based in part on the mkinitramfs script for the LFS LiveCD # written by Alexander E. Patrakov and Jeremy Huntwork.
copy() { local file
if [ "$2" == "lib" ]; then file=$(PATH=/lib:/usr/lib type -p $1) else file=$(type -p $1) fi
if [ -n $file ] ; then cp $file $WDIR/$2 else echo "Missing required file: $1 for directory $2" rm -rf $WDIR exit 1 fi }
if [ -z $1 ] ; then # 传递内核版本 INITRAMFS_FILE=initrd.img-no-kmods else KERNEL_VERSION=$1 INITRAMFS_FILE=initrd.img-$KERNEL_VERSION fi
if [ -n "$KERNEL_VERSION" ] && [ ! -d "/lib/modules/$1" ] ; then echo "No modules directory named $1" exit 1 fi
printf "Creating $INITRAMFS_FILE... "
binfiles="sh cat cp dd killall ls mkdir mknod mount " #需要复制的二进制文件 binfiles="$binfiles umount sed sleep ln rm uname"
# Systemd installs udevadm in /bin. Other udev implementations have it in /sbin if [ -x /bin/udevadm ] ; then binfiles="$binfiles udevadm"; fi
sbinfiles="modprobe blkid switch_root"
#Optional files and locations for f in mdadm mdmon udevd udevadm; do if [ -x /sbin/$f ] ; then sbinfiles="$sbinfiles $f"; fi done
if [ -n "$KERNEL_VERSION" ] ; then if [ -x /bin/kmod ] ; then binfiles="$binfiles kmod" else binfiles="$binfiles lsmod" sbinfiles="$sbinfiles insmod" fi fi
# Install basic binaries for f in $binfiles ; do ldd /bin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted copy $f bin done
# Add lvm if present if [ -x /sbin/lvm ] ; then sbinfiles="$sbinfiles lvm dmsetup"; fi
for f in $sbinfiles ; do ldd /sbin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted copy $f sbin done
# Add udevd libraries if not in /sbin if [ -x /lib/udev/udevd ] ; then ldd /lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted elif [ -x /lib/systemd/systemd-udevd ] ; then ldd /lib/systemd/systemd-udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted fi
# Add module symlinks if appropriate if [ -n "$KERNEL_VERSION" ] && [ -x /bin/kmod ] ; then ln -s kmod $WDIR/bin/lsmod ln -s kmod $WDIR/bin/insmod fi
# Add lvm symlinks if appropriate # Also copy the lvm.conf file if [ -x /sbin/lvm ] ; then ln -s lvm $WDIR/sbin/lvchange ln -s lvm $WDIR/sbin/lvrename ln -s lvm $WDIR/sbin/lvextend ln -s lvm $WDIR/sbin/lvcreate ln -s lvm $WDIR/sbin/lvdisplay ln -s lvm $WDIR/sbin/lvscan
problem() { printf "Encountered a problem!\n\nDropping you to a shell.\n\n" sh }
no_device() { printf "The device %s, which is supposed to contain the\n" $1 printf "root file system, does not exist.\n" printf "Please fix this problem and exit this shell.\n\n" }
no_mount() { printf "Could not mount device %s\n" $1 printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n" printf "Maybe the device is formatted with an unsupported file system?\n\n" printf "Or maybe filesystem type autodetection went wrong, in which case\n" printf "you should add the rootfstype=... parameter to the kernel command line.\n\n" printf "Available partitions:\n" }
mount -n -t devtmpfs devtmpfs /dev mount -n -t proc proc /proc mount -n -t sysfs sysfs /sys mount -n -t tmpfs tmpfs /run
read -r cmdline < /proc/cmdline
for param in $cmdline ; do case $param in init=* ) init=${param#init=} ;; root=* ) root=${param#root=} ;; rootdelay=* ) rootdelay=${param#rootdelay=} ;; rootfstype=*) rootfstype=${param#rootfstype=} ;; rootflags=* ) rootflags=${param#rootflags=} ;; ro ) ro="ro" ;; rw ) ro="rw" ;; esac done
# udevd location depends on version if [ -x /sbin/udevd ]; then UDEVD=/sbin/udevd elif [ -x /lib/udev/udevd ]; then UDEVD=/lib/udev/udevd elif [ -x /lib/systemd/systemd-udevd ]; then UDEVD=/lib/systemd/systemd-udevd else echo "Cannot find udevd nor systemd-udevd" problem fi
if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y > /dev/null ; fi if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi