in the first part We've compiled busybox and the linux kernel. In this section, we will create the initramfs image.

Creating initramfs

Initramfs is the first file loaded into memory at system boot. contained in this file /init The boot process is started by running the file by our kernel. Let's create an initramfs image using the busybox file that we compiled as static in the previous example.

For this, let's open a new directory and copy our busybox file into it. next init Let's write our file as follows.

#!/busybox ash /busybox mkdir -p /bin /busybox --install -s /bin export PATH=/bin exec /bin/ash

Here the first line is the shebang line and specifies what the file should be run with.

second line / bin Used to create the directory.

The third line is used to install the busybox file by symlinking to /bin.

on the fourth line PATH The environmental variable is set. This way we specify where to look for busybox commands.

In the last line we wrote to replace the current process with a new ash shell.

Initramfs packaging

init Let's make our file executable and generate our initramfs image with the following command.

find ./ | cpio -H newc -o > ../initrd.img

If we want, we can compress it to reduce the size of our initramfs image.

gzip -9 initrd.img

Testing initramfs and kernel

We can test our kernel and initramfs image using qemu with the following command.

qemu-system-x86_64 --enable-kvm -kernel bzImage -initrd initrd.img -append "quiet" -m 1024 

If there is no problem, the terminal screen will appear and you can use busybox commands.

Testing the minimal distribution

Next in the section It will be explained how to create our own minimal rootfs system from simple C code.