Creating a initramfs image from scratch

First start off by making the root, and required subdirectories.

mkdir -p root
cd root
mkdir -p bin dev etc lib mnt proc sbin sys tmp var
cd -

We're gonna need some core tools like sh and mount, you can get them from where you want, but for simplicity(haha not really) I'm gonna use busybox.

curl -L 'https://www.busybox.net/downloads/binaries/1.26.2-defconfig-multiarch/busybox-x86_64' >root/bin/busybox
chmod +x root/bin/busybox

We'll also need an init script to mount the required systems.

cat >>root/init << EOF
#!/bin/busybox sh

mount -t devtmpfs  devtmpfs  /dev
mount -t proc      proc      /proc
mount -t sysfs     sysfs     /sys
mount -t tmpfs     tmpfs     /tmp

sh
EOF

Now to create the the initramfs.

cd root
find . | cpio -ov --format=newc | gzip -9 >../initramfz
cd -

and you're done.

Testing it

If you also wanna test it out in qemu, get qemu-system-x86_64 (or whatever other architecture you wanna work on) installed on your system.

You'll also need a linux kernel; if you're running linux already you can just get the one you're currently running, it's probably in /boot/vmlinuz*, or some similar name.

If you're not on linux, you can snatch it up from some random distro, or compile it yourself.

qemu-system-x86_64 -kernel /boot/vmlinuz -initrd initramfz

If you want to run it in the current terminal instead of getting a new qemu window, you can add -nographic and -append 'console=ttyS0 to the qemu-system-* arguments.