Thursday, February 25, 2010

Steps to Compile a Kernel

First download and install the kernel source to your machine. Then move to that directory. In redhat linux the source is usually installed in a directory named linux-2.x.xx.xxx in the /usr/src directory. And a soft link to that directory is created by the name linux-2.4 (Assuming it is the source of the linux 2.4 kernel). So the kernel source is installed in /usr/src/linux-2.4 directory.
Now move to this directory.
# cd /usr/src/linux-2.4
The next step is creating the configuration file (.config). This can be done in three ways.
# make config - brings up a command-line console mode interface.
# make menuconfig - brings up a ncurses based GUI Interface.
# make xconfig - brings up a X GUI based user friendly interface.
You may use any one of the above three commands.Depending upon the command that was executed, you will get a relevent interface using which you can configure your kernel before it is compiled.For example, you can select the proper processor type in the configuration dialog. Another thing is you can decide whether to build a functionality directly in the kernel or load it as a module when the kernel needs it.This will optimise your kernel for your computer and will help decrease the size of your kernel. The end result is your linux machine starts much faster.
After you have made the changes to the settings, you have to save and exit.Then all the changes you made to the configuration file has been saved at /usr/src/linux-2.4/.config
Now the next step is to make the dependencies. For that execute the following commands.
# make dep
# make clean
The first of these commands builds the tree of interdependencies in the kernel sources. These dependencies may have been affected by the options you have choosen in the configure step. The 'make clean' purges any now-unwanted files left from previous builds of the kernel.
The next step is the actual compilation of the kernel. Here you can opt to create a compressed image by executing the command
# make bzImage
Or if you opt for a non compressed image then you can execute the command
#make zImage
The time taken for compilation of the kernel depends on the speed of your machine. In my machine (Celeron 333MHz) it took around 15-20 mins.
After the compilation is complete, you can find your newly compiled kernel here : /usr/src/linux-2.4/arch/i386/boot/bzImage .
If you have enabled loadable modules support in the kernel during configuring, then you have to now execute the commands
# make modules
# make modules_install
Loadable modules are installed in the /lib/modules directory.
Now to start using your newly compiled kernel, you have to copy the kernel into the /boot directory.
# cp /usr/src/linux-2.4/arch/i386/boot/bzImage /boot/bzImage-mykernel-sept2004
Now open your boot configuration file (I used lilo) in your favourate editor and insert the following lines to boot linux using your new kernel.
# vi /etc/lilo.conf
//Inside lilo.conf file
image=/boot/bzImage-mykernel-sept2004
label=myker
root=/dev/hda3
read-only
My root is located at /dev/hda3 which I found out using the command
# df /
Now don't forget to execute the 'lilo' command to update the changes in the boot loader. Reboot your machine and in the lilo prompt, select 'myker' and press enter to start loading your newly compiled kernel.

Links:
 http://bobcares.com/blog/?p=162
 http://linuxhelp.blogspot.com/2004/08/steps-to-compile-kernel.html#axzz0gcGYpoAR

No comments:

Post a Comment

 Linux Interview  Linux booting process EXT4 XFS file system runlevel scan disk hba driver systool -c fc_host lspci -nn | grep -i hba single...