In this article I will demonstrate how to compile a Linux kernel from source step by step, let's get busy ;)
1.The most recent Linux kernel source can be obtained from:http://www.kernel.org/.
The file comes as a .bz2 compressed file, so after you finish the download extract it with:
tar xvjf linux-126.96.36.199.tar.bz2 -C /usr/src
*important notice:before you begin compiling the kernel make sure your partition has plenty of free space allocated, the procedure may consume couple of gigs, so if you run out of space during the compiling procedure - the procedure will fail ,so if your /usr/src/x will be on your root partition please check it with df -h.
2. Make sure you have most recent gcc & ncurses develpoment tools installed, if you are using Debian based distro:
apt-get gcc install
apt-get install libncurses5-dev
Red Hat/Fedora users:
yum install gcc
yum install ncurses-devel
yast -i gcc
yast -i ncurses-devel
3. Switch to the extracted kernel's directory:
I suggest running:
This will remove temporary files & leftovers from the source tree.
It's time to configure the features of your kernel, with the following 3 tools you can include/exclude certain modules from you kernel and build it from scratch, the tools are:
* make config -This is a text-based tool that you step through to configure kernel options one by one. It is no longer recommended because the kernel provies so many options, and the interface involves simply stepping one by one through them tediously. However, make config is a standard tool and is provided with all Linux distributions
* make menuconfig - The second type of make method is make menuconfig. It gives you a graphical menu-based display without requiring you to use the X Window System. When you use make menuconfig, you are presented with a directory tree menu system. Following each selection, you are presented with the available options. To include an option, use the y key; to exclude it, use the n key; and to include it as a module, use the m key. Letters that are highlighted are considered hotkeys and allow you to quickly maneuver through the menu options. To exit a window, select the Exit option, or press the Esc key twice.
* make xconfig - The third type of make method is make xconfig. This method is very popular among new Linux users and users who are accustomed to graphical interface tools. The make xconfig tool requires X Window System support. When starting make xconfig, you are presented with a window with buttons for each class of configuration. Pressing a configuration button, you are prompted, in a tree style, the options available. You can then select, deselect, or modularize each option by pressing the corresponding button next to the item. One of the benefits of using this configuration method over the standard make config method is the backward mobility of the configuration. This means that if you change your mind, or make a mistake, you can move back and change the option in make xconfig.
When you're done setting the configuration of your brand new kernel the configuration will be stored inside .config
4. It's time to compile the kernel but before that, we want to remove any irrelevant dependencies:
Ok, you are ready for the compilation, issue the next command:
This will build the kernel boot image ,it may take pretty long time, so be patient . The bzImage kernel image is not restricted to 520 KB or even 640 KB (unlike zImage). The bzImage is now the preferred boot image.
The resulting kernel image will be under "arch/x86/boot/bzImage"
5.When the build of the boot image is done run:
Modules are parts of the kernel that are loaded on the fly, as they are needed. They are stored in individual files (e.g. ext3.o). The more modules you have, the longer this will take to compile, once again be patient.
6.It's time to install the modules:
This will copy all the modules to a new directory, "/lib/modules/a.b.c" where a.b.c is the kernel version.
7. now, you're left with 2 options - you can either run:
make install (this will automatically copy the new image to /boot partition,create initrd image there and update your boot loader).
or you can set this manually (less recommended, as there is a good chance you will miss something):
*copy your bzImage file to the /boot partition and give it a uniqe name such as:
cp arch/i386/boot/bzImage /boot/vmlinuz-188.8.131.52
cp System.map /boot/System.map-184.108.40.206
*create and initrd image with: mkinitrd -v /boot/initrd-220.127.116.11
*update the boot loader configuration file, a good chance you're using GRUB with your distro as a default boot loader, in that case you will need to edit /boot/grub/menu.lst
and add another instance for the new kernel, this will look something like this:
title Test Kernel (18.104.22.168)
root (hd0,1) kernel /boot/bzImage-22.214.171.124 ro root=LABEL=/
That's it, you're done - now reboot your machine and when the GRUB menu comes up try booting up to the new kernel image.