|
| 1 | +--- |
| 2 | +title: Booting Linux Kernel with a custom init program on QEMU Ubuntu 24 |
| 3 | +image: /assets/img/default-banner.jpg |
| 4 | +author: jack |
| 5 | +date: 2025-07-13 20:55:00 +0800 |
| 6 | +categories: [blog, linux] |
| 7 | +tags: [linux] |
| 8 | +math: false |
| 9 | +pin: false |
| 10 | +--- |
| 11 | + |
| 12 | +This post documents how I wrote and compiled a minimal `init` program in C, built a simple `initramfs`, and successfully booted it using a custom Linux kernel v6.12 on Ubuntu 24.04 via QEMU. Before you begin, make sure you’ve already built your own Linux kernel as shown in [this post](/posts/boot-tiiny-linux). |
| 13 | + |
| 14 | +## Create a Minimal C Project |
| 15 | + |
| 16 | +```bash |
| 17 | +mkdir -p my-init/bin |
| 18 | +cd my-init |
| 19 | +touch init.c |
| 20 | +``` |
| 21 | + |
| 22 | +Edit `init.c`: |
| 23 | + |
| 24 | +```c |
| 25 | +// init.c |
| 26 | +#include <stdio.h> |
| 27 | +#include <unistd.h> |
| 28 | + |
| 29 | +int main() { |
| 30 | + while (1) { |
| 31 | + printf("$ "); |
| 32 | + fflush(stdout); |
| 33 | + |
| 34 | + char input[256]; |
| 35 | + if (fgets(input, sizeof(input), stdin) == NULL) break; |
| 36 | + |
| 37 | + printf("Sorry, I don't know how to do that.\n"); |
| 38 | + } |
| 39 | + return 0; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +Now compile this as a statically linked binary. This ensures the kernel doesn’t rely on any external shared libraries or runtime: |
| 44 | + |
| 45 | +```bash |
| 46 | +gcc init.c -static -o bin/init |
| 47 | +``` |
| 48 | + |
| 49 | +## Build an initramfs Image |
| 50 | + |
| 51 | +Before the kernel can execute the `init` program, we must place it inside an initial RAM-based file system (initramfs). The kernel will extract this archive into memory at boot and use it as the root filesystem. |
| 52 | + |
| 53 | +```bash |
| 54 | +cd bin |
| 55 | +chmod +x init |
| 56 | +cd .. |
| 57 | +find . -print0 | cpio --null --create --verbose --format=newc | gzip -9 > ../initrd |
| 58 | +``` |
| 59 | + |
| 60 | +This generates an `initrd` file — a gzipped `cpio` archive — which contains your custom `init` binary. |
| 61 | + |
| 62 | +## Enable Kernel Features |
| 63 | + |
| 64 | +Make sure your kernel supports `initramfs`, ELF binaries, and 64-bit execution. Run: |
| 65 | + |
| 66 | +```bash |
| 67 | +make menuconfig |
| 68 | +``` |
| 69 | + |
| 70 | +Then enable these options: |
| 71 | + |
| 72 | +1. General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support |
| 73 | +2. Executable file formats ---> Kernel support for ELF binaries |
| 74 | +3. Processor type and features ---> 64-bit kernel |
| 75 | + |
| 76 | +Recompile the kernel afterward if needed. |
| 77 | + |
| 78 | +## Boot with QEMU |
| 79 | + |
| 80 | +Now boot your kernel and initramfs using QEMU: |
| 81 | + |
| 82 | +```bash |
| 83 | +qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd initrd -nographic -append "console=ttyS0" |
| 84 | +``` |
| 85 | + |
| 86 | +Expected output: |
| 87 | + |
| 88 | +```text |
| 89 | +$ Sorry, I don't know how to do that. |
| 90 | +$ Sorry, I don't know how to do that. |
| 91 | +... |
| 92 | +``` |
| 93 | + |
| 94 | +Each time you type a line and press Enter, it responds with a fixed message — just like the minimal shell we designed! |
| 95 | + |
| 96 | + |
0 commit comments