Howto build a custom Linux kernel.

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.



Digg: DIGG ME

2 Comments »

  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.

    Comment by Sourav Hazra — March 4, 2010 @ 7:29 am

  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.

    Comment by Jimmy — March 5, 2010 @ 8:17 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Sponsor & Advertise
Tech Buzz

Computer for SALE!

Howdy everyone, Jimmy B here trying to sell my computer. Basically I have two computers at home right now. A nice machine with Windows 7, good for gaming, tinkering, Linux, programming and that sort of stuff. My other computer is just a normal Dell desktop which I plan to use as my main machine now [...]

Read More »

Zero-Day VBScript plagues Windows XP / 2000.

Apparently there is a new zero-day flaw that affects Windows XP and 2000 computers utilizing VBScript. An attacker can trick someone into visiting a website that binds the F1 key to a VBScript event which ultimately installs malicious code on your machine. Microsoft’s fix: Don’t press the F1 key when windows pop up. LOL. Ok [...]

Read More »

Has Verizon been hacked? Security certificates revoked!

Has Verizon been hacked? Google Chrome seems to think so. Just a few minutes ago I tried to log into Verizon to see why my phone isn’t making any calls and to also see why I can’t make any text messages. I’m going to have to probably assume they haven’t been hacked, but how does [...]

Read More »

My first blocked number in Google Voice.

Today I received my first piece of spam in Google Voice. At first I was really PISSED-OFF but then a feeling of serenity passed over my whole body as I noticed the “block” button. Slowly and cautiously I clicked it, making my day THAT MUCH better. Just knowing that I will no longer be getting [...]

Read More »

3500 Netflix on Linux petitions.

Currently you can not watch Netflix if you are a Linux user and all those new Ubuntu Netbook owners will not be watching Netflix anytime soon either. Watching movies online through Netflix is an awesome service, but worthless to Linux users. Netflix has chosen to only allow Windows and MAC users access to their online [...]

Read More »

Apple bans “android” from apps store.

Apples waving the ban stick around again, this time rejecting an educational iPhone app because it contained the word “Android”. The application? Flash of Genius: SAT Vocab 2.2, an iPhone app developed by Tim Novikof. The app did really well in the Android Developer Challenge that Google puts on and decided to mention that [...]

Read More »