• 2011/12/12 - 17:28

    News

    The section "Nas/Router" has been added

Current page: Home > Tech

Building the RAM disk

The RAM disk is the base for almost everything that happens before the original boot process.

The kernel has a nice feature for packing and autoloading of an initial RAM disk. The first step is to make sure that it's activated.

su
cd /usr/src/linux
make menuconfig

General setup
+--> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
+-->  (/usr/src/initramfs) Initramfs source file(s)

The kernel will now automatically put every file under /usr/src/initramfs in the initial RAM disk on compilation. This directory has to be created and filled with life.

mkdir -p /usr/src/initramfs
cd /usr/src/initramfs
mkdir bin dev etc lib mnt proc root sbin sys tmp usr var
cp -a /dev/console /dev/null /dev/tty /dev/sd* dev
ln -s lib lib64
touch init

The kernel will load this initial RAM disk on boot and execute its init-script. The init-script has to handle everything that has to be done before the orininal boot process (e.g. hardware decryption, loading of special modules, assembling raid devices, asking for passwords, ...). A basic init-script is shown below. It only mounts the root partition and continues with the original boot process.

cat /usr/src/initramfs/init
#!/bin/busybox sh

shell() {
    /bin/busybox --install -s
    exec /bin/sh
}

# mounting proc and sys
/bin/mount -t proc none /proc || shell
/bin/mount -t sysfs none /sys || shell

# mounting root
/bin/mount -o ro /dev/sda1 /mnt/root || shell

# unmounting proc and sys
/bin/umount /proc || shell
/bin/umount /sys || shell

# continuing with boot
exec switch_root /mnt/root /sbin/init || shell

The init-script makes use of these commands: sh, mount, umount. These can all be provided by busybox (a toolset for most basic commands). All that is needed is to install busybox on the system and then copy the busybox executable to the inital RAM disk. If additional libraries are needed for a command, these have also to be copied into the RAM disk.

cd /usr/src/initramfs/bin
cp `which busybox` .
ln -s busybox sh
ln -s busybox mount
ln -s busybox umount
cd ..
ldd busybox
        linux-vdso.so.1 =>  (0x00007fff88148000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fd9562ab000)
        libpam_misc.so.0 => /lib64/libpam_misc.so.0 (0x00007fd9560a7000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd955d41000)
        libpam.so.0 => /lib64/libpam.so.0 (0x00007fd955b33000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd95652c000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fd95592f000)
cp -L /lib64/libm.so.6 lib
cp -L /lib64/libpam_misc.so.0 lib
cp -L /lib64/libc.so.6 lib
cp -L /lib64/libpam.so.0 lib
cp -L /lib64/libdl.so.2 lib

Now the initial RAM disk is prepared for the first boot. Of course there is nothing special yet about this init-script so feel free to extend it for whatever you want. To test your RAM disk compile and install the new kernel and reboot the computer.

cd /usr/src/linux
make
make install
reboot

last updated: May 4, 2020 17:36:28