Compiling Linux kernel and running it using QEMU
As a part of my final year project i had to compile and test the linux kernel. This post is how i did it.
Kernel Compilation is a task whose difficulty varies according to the way you want to mould the kernel. If a simple kernel with a default setting is required then the steps are simple. But if the kernel has to be customized in a perticulat way then compilation can be a head ache.OK, lets get into bussiness
The nesseary tools required for compiling the kernel are
- GNU make
They can be installed using apt-get in ubuntu. To test the compiled kernel we need QEMU.It can be downloaded using the command
$ sudo apt-get install qemu-system-i386
1.Downloading the linux kernel
First step of getting the kernel compiled is to get the kernel itself.It can be downloaded from Kernel.org. I am using the long term stable kernel version 2.6.32 which was the kernel of ubuntu 10.04.
Lets begin by creating a working directory.
The kernel can be downloaded using wget
2.Extracting the kernel
The downloaded kernel is in compressed state, it is extracted using the tar command and cd in to the kernel directory
$tar xjf linux-22.214.171.124.tar.bz2
3.Configuring the Kernel
The next step is to configure the kernel. To configure the kernel , the .config file is needed to be generated. A simple method to generate the .config file is to copy the configure file of the working Linux operating system.. For Ubuntu, it will be in the /boot/ directory. Copy this file to ~/linux/linux-126.96.36.199. An another method is to use make to generate a default config file using the command
After generating the .config file, it has to be configured according to the setting we need on the compiled kernel. To make kernel with no windows, without any GUI, we can use menuconfig. Run the following command and a blue screen will appear. Select the settings you need on the kernel and unmark the things which are not required.Then save the config file
4.Make the kernel
The next is to make the kernel, it will take several minutes to complete. Use the following commands
when it is finised , a file bzImage will be created in linux-188.8.131.52/arch/i386/boot/ if you are using the i386 architecure. This file is the binary image of the compiled kernel and it is used to run the kernel.
5.Create the initial file system
Even though the file sytems will be compiled alone the kernel, an initial file sytem has to be created which will be present in the RAM to run the kernel. To make this inital ram filesystem, mkinitrd is used. In ubuntu mkinitramfs is used instead of that
$mkinitramfs -o initrd.img-184.108.40.206
6.Run the kernel in QEMU
QEMU which stands for ‘Quick EMUlater’ is an excellent emulator. It can emulate different processer architecture with high speed on the PC itself. To run the kerenl use the following command,-kernel denotes the kernel image which is going to execute, -initrd reprresents the initial ram filesystem and -m shows how much memory in RAM is allocated for running qemu.
$qemu-system-i386 -kernel bzImage -initrd initrd.img-220.127.116.11 -m 512M
Tada.. you gets your running kernel