Kernel and Initramfs Construction

Updated

October 30, 2026

Kernel and Initramfs Construction

In this stage, we configure and compile the Linux kernel and construct an early-userspace initramfs designed to boot the live ISO.

Step 1: Kernel Configuration

Navigate to the kernel source directory:

$ cd "$BYOL_ROOT/build/linux-6.10.2"
$ make defconfig

Enable critical options using kernel configuration scripts:

# Enable devtmpfs automatic dev nodes
scripts/config --enable CONFIG_DEVTMPFS
scripts/config --enable CONFIG_DEVTMPFS_MOUNT

# Enable OverlayFS and SquashFS for live ISO
scripts/config --enable CONFIG_OVERLAY_FS
scripts/config --enable CONFIG_SQUASHFS
scripts/config --enable CONFIG_SQUASHFS_ZSTD
scripts/config --enable CONFIG_SQUASHFS_XZ

# Enable ISO 9660 filesystem
scripts/config --enable CONFIG_ISO9660_FS

# Enable QEMU VirtIO drivers for testing
scripts/config --enable CONFIG_VIRTIO
scripts/config --enable CONFIG_VIRTIO_PCI
scripts/config --enable CONFIG_VIRTIO_BLK
scripts/config --enable CONFIG_VIRTIO_NET
scripts/config --enable CONFIG_VIRTIO_CONSOLE

# Custom distro release identifier
scripts/config --set-str CONFIG_LOCALVERSION "-byol-capstone"

Update .config with defaults for newly selected options:

$ make olddefconfig

Step 2: Kernel Compilation

Compile the compressed kernel image (bzImage):

$ make $MAKEFLAGS bzImage
$ make $MAKEFLAGS modules

# Stage kernel binary into ISO staging tree
$ mkdir -p "$BYOL_ROOT/iso-staging/boot"
$ cp arch/x86/boot/bzImage "$BYOL_ROOT/iso-staging/boot/vmlinuz"

Step 3: Constructing the Live Initramfs

The initramfs discovers the live USB/CD media, mounts the SquashFS rootfs, creates a writable OverlayFS RAM layer, and invokes /sbin/init.

$ mkdir -p "$BYOL_ROOT/build/initramfs"/{bin,sbin,dev,proc,sys,mnt,newroot}
$ cd "$BYOL_ROOT/build/initramfs"

# Install static BusyBox binary into initramfs
$ cp "$SYSROOT/bin/busybox" bin/
$ chmod +s bin/busybox
$ cd bin
$ ln -s busybox sh
$ ln -s busybox mount
$ ln -s busybox blkid
$ ln -s busybox switch_root
$ ln -s busybox sleep
$ cd ..

Write the master /init script:

#!/bin/sh
# /init — BYOL Live ISO Early Userspace
PATH=/bin:/sbin

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

echo "=========================================="
echo "    BYOL Linux 1.0 (Live Environment)     "
echo "=========================================="

echo "[initramfs] Locating installation media..."
CDROM=""
for i in 1 2 3 4 5; do
    CDROM=$(blkid -L "BYOL_LIVE" || true)
    [ -n "$CDROM" ] && break
    sleep 1
done

if [ -z "$CDROM" ]; then
    echo "[initramfs] FATAL: Media with label BYOL_LIVE not found!"
    exec /bin/sh
fi

echo "[initramfs] Found media at $CDROM. Mounting..."
mkdir -p /mnt/cdrom /mnt/squashfs /mnt/overlay/upper /mnt/overlay/work
mount -t iso9660 -o ro "$CDROM" /mnt/cdrom

echo "[initramfs] Mounting compressed OS payload..."
mount -t squashfs -o loop /mnt/cdrom/byol/rootfs.squashfs /mnt/squashfs

echo "[initramfs] Establishing writable OverlayFS in RAM..."
mount -t tmpfs -o size=1G none /mnt/overlay
mount -t overlay overlay -o lowerdir=/mnt/squashfs,upperdir=/mnt/overlay/upper,workdir=/mnt/overlay/work /newroot

# Clean up mounts before switch_root
umount /proc
umount /sys
umount /dev

echo "[initramfs] Pivoting to live root filesystem..."
exec switch_root /newroot /sbin/init

Ensure /init is executable:

$ chmod +x "$BYOL_ROOT/build/initramfs/init"

Pack into a compressed CPIO archive:

$ cd "$BYOL_ROOT/build/initramfs"
$ find . -print0 | cpio --null --create --format=newc | gzip -9 > "$BYOL_ROOT/iso-staging/boot/initramfs.img"