Home > Power Computing > Howto build a custom Linux kernel.

Howto build a custom Linux kernel.

February 8th, 2010 Jimmy Leave a comment Go to comments

In this tutorial I will hopefully teach you how to configure and build a custom kernel. We’ll be using the unmodified generic Linux source code available here. Many Linux distributions sometimes add custom code to their kernel but for this example we’ll be using a pure Linux kernel. Creating a custom kernel can not only be educational but also make your computer run smoother and use less resources such as disk space. You may be required to install some additional software in order to build a custom kernel. We will not be covering boot loaders and initial rams disks in this article. I will however give you a general idea on where to go to test your kernel.

Note: You’ll need to be logged into your system as root in order to follow this tutorial.

1.
First lets make sure you have the necessary tools to build a kernel. Using your package manager or software installer check and make sure you have the following software installed. This version information was pulled directly out of the Change file for the latest 2.6.32 kernel. Usually if you have the update-to-date versions of GNU C and GNU make you should be fine but if you experience problems when doing an actual build come back and check to make sure the version below are installed.

Gnu C 3.2 # gcc –version
Gnu make 3.80 # make –version
binutils 2.12 # ld -v
util-linux 2.10o # fdformat –version
module-init-tools 0.9.10 # depmod -V
e2fsprogs 1.41.4 # e2fsck -V
jfsutils 1.1.3 # fsck.jfs -V
reiserfsprogs 3.6.3 # reiserfsck -V 2>&1|grep reiserfsprogs
xfsprogs 2.6.0 # xfs_db -V
squashfs-tools 4.0 # mksquashfs -version
btrfs-progs 0.18 # btrfsck
pcmciautils 004 # pccardctl -V
quota-tools 3.09 # quota -V
PPP 2.4.0 # pppd –version
isdn4k-utils 3.1pre1 # isdnctrl 2>&1|grep version
nfs-utils 1.0.5 # showmount –version
procps 3.2.0 # ps –version
oprofile 0.9 # oprofiled –version
udev 081 # udevinfo -V
grub 0.93 # grub –version
mcelog 0.6

2.
Download the latest 2.6 kernel into your /usr/src folder and uncompress it with the bzip2 -dc linux-2.6.32.tar.bz2 | tar xvf - command.

3.
Change your directory to the 2.6.32 directory. cd linux-2.6.32

4.
Now that you are in the 2.6.32 folder lets configure your kernel. Type make menuconfig to enter the kernel configuration menu. Once in the menu you can use your arrow keys to navigate up and down, enter key to enter a sub-menu, ESC key to exit out of sub-menus or the kernel menu and space bar to select an option from the menu.
build custom kernel linux make menuconfig

When you select and item with the space bar you’ll notice that sometimes the item is selected as <*> or as a . The <*> indicates to build this option into the kernel and the indicates to build this option as a kernel module. Kernel modules are files located on your computer that generally represent drivers for hardware. Generally modules are loaded after your system boots. If you build everything into your kernel you don’t have to worry about kernel modules loading later on. Certain options and drivers such as file system types and partition types must be built into the kernel and not as a module because they are needed to mount your file system. Compiling other drivers and options such as network devices directly into the kernel and not as a module is generally a good idea too. It’s my opinion that building everything you need into a kernel is best for systems with low resources as kernel modules tend to take up more memory, disk space and can increase boot times.

5.
Once you configure everything you think you need, exit out using the ESC key. When asked, save your kernel configuration.

6.
Now that you have exited the menu it is time to build your kernel. Type make and press enter. Now you get to see a whole bunch of nerdy output. If your build breaks you’ll probably see a bunch of Error and Exiting messages. Once the system is done building the kernel you can check and see if the file bzImage is in /usr/src/linux-2.6.32/arch/x86/boot.

7.
Booting using your new kernel is going to very from system to system. For this tutorial I am going to cover Grub and the very very very basics of adding a new boot option to your boot loader. This is what will be used to boot your system. For more details on Grub, use Google.

7a.
Copy your new kernel to your /boot folder. The kernel is located in /usr/src/linux.6.32/arch/x86/boot/ and is called bzImage. Copy this file to your /boot folder and call it custom-2.6.32.1 (cp the bzImage file to /boot/custom-2.6.32.1).

7b.
Edit the /boot/grub/menu.lst file. Look for a few lines that look like this:
title Ubuntu 9.10, kernel 2.6.31-17-generic
uuid d0ec0e7d-3ec5-4b5b-ace4-111792a2fbc1
kernel /boot/vmlinuz-2.6.31-17-generic root=UUID=d0ec0e7d-3ec5-4b5b-ace4-111792a2fbc1 ro quiet splash
initrd /boot/initrd.img-2.6.31-17-generic

Copy (do not remove, COPY) those lines and change the title to Custom Linux 2.6.32.1. Also make sure and change the kernel line to represent the kernel you copied to /boot. So /boot/custom-2.6.32.1. Don’t worry about the initrd lines, leave those as is.

Now run grub-install, reboot and give it a go. When your system boots select the kernel you just added. If it fails to boot simply reboot and load a previous kernel.

Notes:
If you run into boot issues Google search the errors and problems. You may need to compile in certain drivers to get stuff working. Double check your kernel configuration and make sure you have covered everything for your hardware.

Don’t get frustrated if your boot fails. Creating custom kernels isn’t the easiest thing to do and should generally only be done if you need an optimized system or if you are working with embedded systems.

The boot loader we used in this tutorial is Grub however there are many other boot loaders around such as Lilo, Bootcamp and SYSLINUX.

Categories: Power Computing Tags: ,
Digg: DIGG ME
  1. Sourav Hazra
    March 4th, 2010 at 07:29 | #1

    Hi,
    I have a requirement of creating custom distribution of linux. I want to make a distribution from a configured linux box like what ever is configured inside the linux box e.g Snort, Ntop, Nessus etc. it will compile all together and finally I need to create the distribution so that in future what ever I configured that can be ready for the use say for snort.

    Please help me and advice me is it possible to do in practical.

  2. March 5th, 2010 at 08:17 | #2

    Sure. All you have to do is build your kernel, your initrd and your boot partition. Compile the programs you want and include all necessary libraries and then build your distribution.

  1. No trackbacks yet.