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
Requirments
The nesseary tools required for compiling the kernel are
- gcc
- 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.
$mkdir linux
$cd linux
The kernel can be downloaded using wget
$wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.59.tar.bz2
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-2.6.32.59.tar.bz2
$cd linux-2.6.32.59
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-2.6.32.59. An another method is to use make to generate a default config file using the command
$make defconfig
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
$make menuconfig
4.Make the kernel
The next is to make the kernel, it will take several minutes to complete. Use the following commands
$make all
$make modules
when it is finised , a file bzImage will be created in linux-2.6.32.59/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
$cd ~/linux/linux-2.6.32.59/arch/i386/boot
$mkinitramfs -o initrd.img-2.6.32.59
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-2.6.32.59 -m 512M
Tada.. you gets your running kernel
Reblogged this on Gigable – Tech Blog.
You are amazing! post in your blog are very helpfull and i will read your news more times. Cheers for this info. Your blog and info are very important. Thanks again for your time for us 🙂
thaks hostlng 🙂
what does we do next step?
Post is very usefill newbies like me, it is amazing.