Modify, compile and execute a Linux Kernel in QEMU
How to modify a Linux Kernel and execute it in 5 minutes on Ubuntu.
I wanted to modify the Linux Kernel and execute it in QEMU. After multiple hours of searching the web, reading poor guides, tracking debian package bugs and hacking I finally found a way to do that. In the end it is quite simple…
Things you need
- a Linux Kernel of our choice
- for the make menuconfig you need ncurses dev package, for ubuntu it is libncurses5-dev
- a installed QEMU for the emulation
- a GCC version that is able to compile your kernel version (the newest gcc should work for the newer kernel versions, however I needed gcc 4.9 for kernel 3.6.2)
- mkinitramfs generates a initial filesystem on Ubuntu which can be used by QEMU
Get the kernel files
Go to kernel.org, download a kernel and extract it.
Optional: modify / patch the Kernel
This applies a patch to the kernel. You can check the detailed changes in the file if you want to.
Switch to the linux directory and execute menuconfig. There you can enable and disable all the modules you want.
Compile the kernel
I need to compile with the older gcc version, so I typed:
The -j 4 executes 4 parallel make recipes at once. So it runs quicker. My PC needed about 3 minutes to compile. This command generated a bzimage file in linux-3.6.2/arch/x86/boot. This file is a compiled image of the kernel.
Create initial file system
Switch to the location of the bzimage and call mkinitramfs there. The initial filesystem is needed to execute the kernel.
This generates a initrd.img-3.6.2 file in that directory.
Now we can start QEMU with our custom kernel and the initial file system on 1GB of RAM.
This should open a window with the running BusyBox Linux: