4.1. Building the initramfs image

[Note]

Note

This chapter is mandatory for both the livecd and the livekey.

To boot our live system, we will need an initramfs image, which will contain a couple of utilities and kernel modules, necessary to find our rootfs and mount it.

Create a directory structure:

mkdir -p initramfs/{bin,sbin,dev/{pts,shm},etc,proc,sys,rtmnt,lib}

Copy busybox which we created in the chapter busybox 1.19.2 to the bin directory

cp <path-to-busybox-builddir>/busybox initramfs/bin 

Busybox can install symlinks for the desired utilities itself, however this can be done until the init script is running. So for some utilities that we need before this stage, we need to create the symlinks our selfs.

ln -s /bin/busybox initramfs/bin/ash 
ln -s /bin/busybox initramfs/bin/mount 

We need access to some devicenodes before udev starts:

mknod initramfs/dev/null c 1 3 
mknod initramfs/dev/tty c 5 0 
mknod initramfs/dev/console c 5 1 

Add the necessary kernel modules:

for filename in sd_mod usbcore ehci-hcd usb-storage usb-libusual uhci-hcd ext3 mbcache jbd 
do
	find /lib/modules/`uname -r` -name "$filename"* -exec cp -r --parents {} initramfs \;
done

This will search for the given modules, and copy them to the initramfs

Now we need to create the initscript:

cat > initramfs/init << "EOF" 
#!/bin/ash

PATH=/bin:/sbin

# Mount proc and sysfs

mount -t proc none /proc 
mount -t sysfs none /sys 
mount -t devtmpfs none /dev

# Unpack the busybox

/bin/busybox --install -s

# Add modules

if [ -z $(ls /lib/modules/) ]; then

	depmod -a

	modprobe sd_mod
	modprobe usbcore
	modprobe ehci-hcd
	modprobe usb-storage
	modprobe ext3

fi

# Check the command line 

initfile="/sbin/init"
root=""

# Check the parameterlist for rootdelay 

for arg in $(cat /proc/cmdline); do
	case $arg in

	rootdelay=*)
		dt=$(echo $arg | cut -d= -f2)
		sleep $dt
		;;
	esac
done

# Check the parameterlist for UUID= and LABEL=

for arg in $(cat /proc/cmdline); do
	case $arg in

	 root=*)
		option=$(echo $arg | cut -d= -f2)

		if [ $option == "LABEL" ] ; then
			root=$(findfs LABEL=$(echo $arg | cut -d= -f3))
		fi

		if [ $option == "UUID" ] ; then
			root=$(findfs UUID=$(echo $arg | cut -d= -f3))
		fi

		;;

	init=*)
		initfile=$(echo "$@" | cut -d "=" -f 2 )

		;;

	esac
done

if [ $root != "" ] ; then

	mount "${root}" /rtmnt

	if [[ -x "/rtmnt/${initfile}" ]] ; then

		umount /dev
		umount /sys 
		umount /proc

		exec switch_root /rtmnt "${initfile}"
	fi
fi

# We shouldn't come here

exec ash
EOF
chmod 0755 initramfs/init

Pack our stuff:

cd initramfs 
find ./ | cpio -H newc -o > ../initrd 
cd .. 
gzip initrd

Keep our created initrd.gz for later use.