Linux Kernel: Build & Run

1. Build Kernel

Set up a docker container with essential packages.

  1. Dockerfile.
FROM ubuntu:22.04

RUN apt update
RUN apt install -y libncurses-dev make git exuberant-ctags bc libssl-dev
RUN apt install -y flex bison libelf-dev build-essential
RUN apt install -y cpio unzip rsync vim wget file xz-utils

CMD ["/bin/bash"]
  1. Build docker image.
$ docker build --tag kernel_builder .
  1. Run docker container.
$ mkdir /root/kernel
$ docker run -it -v /root/kernel:/root/kernel --name kbuilder kernel_builder
  1. Download kernel source code.
$ cd /root/kernel
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.186.tar.xz
$ tar xf linux-5.15.186.tar.xz
  1. Build kernel.
$ docker exec -it kbuilder bash

$ cd /root/kernel/linux-5.15.186
$ make defconfig
$ make -j `nproc`

Output.

BUILD   arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready  (#1)
  1. Run kernel with QEMU.
$ qemu-system-x86_64 -boot order=c -m 2048M \
    -kernel /root/kernel/linux-5.15.186/arch/x86/boot/bzImage \    
    -append "root=/dev/sda rw console=ttyS0,115200 nokaslr" \
    -nographic

2. Build Filesystem Image

  1. Download buildroot.
$ cd /root/kernel
$ wget https://buildroot.org/downloads/buildroot-2025.05.tar.gz
$ tar xf buildroot-2025.05.tar.gz
  1. Build buildroot in the same docker container as kernel.
$ cd buildroot-2025.05
$ make defconfig
$ make menuconfig

In the menu config, select below options:

  1. Build.
$ export FORCE_UNSAFE_CONFIGURE=1
$ make -j `nproc`
  1. Run the kernel on QEMU, with the filesystem image.
$ qemu-system-x86_64 -boot order=c -m 2048M \
    -kernel /root/kernel/linux-5.15.186/arch/x86/boot/bzImage \
    -hda /root/kernel/buildroot-2025.05/output/images/rootfs.ext2 \
    -append "root=/dev/sda rw console=ttyS0,115200 nokaslr" \
    -nographic

To exit QEMU: Ctrl + A, X.

3. Hello World Program

  1. Build the program on the host, then copy it to the QEMU VM and run it there.
#include <stdio.h>
int main() {
    printf("Hello, Linux kernel on QEMU!\n");
    return 0;
}
$ gcc -static -o hello hello.c
  1. Copy the program from host into the filesystem of the QEMU VM.
$ mkdir /mnt/rootfs
$ mount /root/kernel/buildroot-2025.05/output/images/rootfs.ext2 /mnt/rootfs/

$ cp hello /mnt/rootfs/opt
  1. Restart QEMU VM.
$ qemu-system-x86_64 -boot order=c -m 2048M \
    -kernel /root/kernel/linux-5.15.186/arch/x86/boot/bzImage \
    -hda /root/kernel/buildroot-2025.05/output/images/rootfs.ext2 \
    -append "root=/dev/sda rw console=ttyS0,115200 nokaslr" \
    -nographic
  1. Run the program on QEMU VM.
$ /opt/demo
Hello, Linux kernel on QEMU!