How to build a Linux kernel:
One of the more complex tasks that many Linux users will have to face is compiling a custom kernel. This is often done to add features that were not enabled in the kernel that came with the distro, or to disable unneeded or unwanted features that were. It's also the only way to take advantage of experimental code, unofficial patches, and other things that you might want.
This procedure will tell you how to compile a kernel with the Preemptibility patch added. This patch will decrease latency quite a bit, which is good if you plan to do multimedia stuff like playing 3D games, rendering, video editing, and the like. The latest stable kernel at the time this was written, and thus the one that will be used in the example, is 2.4.18.
How it's done:
1.
Download the kernel source.
2. Download the
preempt-kernel and
lock-break patches.
3. Open a shell.
4. Become root.
su and enter root password when asked.
5. Move all of the files you downloaded in steps 1 and 2 to /usr/src.
mv linux-2.4.18.tar.bz2 *.patch /usr/src
6. Enter the /usr/src directory.
cd /usr/src
7. Check if there is a linux directory placed here by your distro.
ls linux
If there is, you might need to re-arrange your include directories. See the bottom of the FAQ for info on how this is done, then come back here.
8. Unpack the new source.
tar xjvf linux-2.4.18.tar.bz2
If you get an error about j not being a supported option, replace it with y.
9. Apply the patches.
patch -p0 < preempt-kernel-rml-2.4.18-4.patch && patch -p0 < lock-break-rml-2.4.18-1.patch
10. Enter the Linux source directory.
cd linux
11. Configure the kernel. This is the long and difficult part of the process. If you want to use the graphical config program, run
make xconfig. If you want to use the non-graphical config program, run
make menuconfig. I don't think that either one is better than the other, it's a matter of personal taste. If you like your arrow keys, use menuconfig. If you like your mouse, use xconfig.
Either way, you'll be presented with a menu, with a list of submenus, many of which have submenus of their own. Just go through each one, enabling support for hardware you have and features you want. Most options have detailed descriptions available, just use the help button.
Also note that many features can be built as modules. If you want to be able to disable and enable a feature at will while the OS is running, hit M.
12. Once you have completed your configuration, it's time to actually compile the kernel.
time make dep modules modules_install bzImage
13. Now, copy the bzImage file (the kernel itself) somewhere meaningful.
cp arch/i386/boot/bzImage /boot/mykernel
14. You must now set up your bootloader. Every bootloader does this differently, but since LILO is probably the most common, I will give instructions for it:
1. Open /etc/lilo.conf in a text editor.
kedit /etc/lilo.conf
2. Locate the chunk beginning with
image = whose label matches the boot menu entry you normally select to boot Linux. Copy it and paste it at te end of the file
3. Change the image = line to
image = /boot/mykernel and change the label to something else. It can be almost anything, but it can not contain spaces.
4. Save and exit.
5. Update to boot sector.
lilo
15. Your new kernel is now built and installed. Reboot and select the entry you just created to boot your new kernel.
16. If all goes well, you're done. If not, go back over your kernel config and make absolutely certain that everything is correct. Then repeat steps 12 and 13, then run
lilo again.
Congrats, you're now running your own custom kernel!
If your distro included a /usr/src/linux directory
Many distros seem to symlink the /usr/include/linux and /usr/include/asm directories to locations in /usr/src/linux. This makes the kernel compilation process a bit more difficult, since you can't build it in the /usr/src/linux directory. To fix this:
1. Check to see if the include directories are indeed symlinks.
ls -l /usr/include/asm
If the result line ends like
asm -> /usr/src/linux/include/asm you've got symlinks, so read on to fix this. If it just says
asm or
/usr/include/asm, it's properly set up and you don't need to change anything, so go to step 7.
2. Remove the symlinks.
rm /usr/include/asm /usr/include/linux
3. Make proper directories in their place.
mkdir /usr/include/asm /usr/include/linux
4. Copy the asm headers.
cp -R /usr/src/linux/include/asm/* /usr/include/asm
5. Copy the linux headers.
cp -R /usr/src/linux/include/linux/* /usr/include/linux
6. Make a autoconf.h file.
touch /usr/include/linux/autoconf.h
7. Delete the old linux source, it is no longer needed.
rm -rf linux
8. Go back to the kernel build instructions above.
updated with new info by popular demand