10.3 Getting the Source Code

10.3 Getting the Source Code

Linux kernel versions have three components. Suppose you have kernel release 2.6.3. Here's what the numbers mean:

  • 2 is the major release number.

  • 6 is the minor release number.

  • 3 is the patchlevel. Kernels with the same major and minor numbers but with different patchlevels belong to a release series.

There are two different kinds of kernel releases:

  • Production releases have minor version numbers that are even (for example, 1.2.x, 2.0.x , 2.2.x, 2.4.x, 2.6.x, and so on). These versions are meant to be as stable as possible. Within a single production release series, there are no radical feature or interface changes.

  • Development releases have minor version numbers that are odd (such as 2.5.). Don't use a developer release if you aren't willing to take risks. Kernel developers intentionally break code, introduce new features, and may inadvertently make the kernel unstable in a development series. Stay away from development releases unless you know exactly what you're doing.

You can get the latest Linux kernel source code at http://www.kernel.org/. The releases have names like linux-version.tar.bz2 for bzip2 compression or linux-version.tar.gz for GNU Zip compression. Bzip2 archives are smaller.

In addition to the full releases available in the kernel archives, you will also find patches containing just the changes between two consecutive kernel patchlevel releases. Unless you are obsessed with upgrading your kernel with every single release, you probably won't be able to do much with a patch.

10.3.1 Unpacking the Source Archive

You can unpack kernels with zcat/bzip2 and tar. To get started, go to /usr/src and make your permissions less restrictive if necessary:

cd /usr/src
umask 022

Then run one of these commands to extract the source files, depending on whether you are using bzip2 or GNU Zip:

bzip2 -dc linux-version.tar.bz2 | tar xvf -
zcat linux-version.tar.gz | tar xvf -


If your kernel release is 2.4.18 or lower, the kernel unpacks into a directory named linux rather than linux-version. Rename any existing linux directory before unpacking one of these kernels (the new name does not matter, as long as it is different than the old one).

10.3.2 A Look Around the Kernel Source

If you've never seen the Linux kernel source code before, it helps to take a look around first. The top-level directory includes several subdirectories and files, including these:

  • README This file is a general introduction to the Linux kernel source and what you need to get started. This document explains the version of the C compiler that you need to compile the kernel.

  • Documentation This directory contains a wealth of documents. Most of the documents here are in plain-text format, and they may describe anything from user-level programs to low-level programming interfaces. One of the most important files in this directory is Changes, which describes recent changes to the kernel and identifies utility programs you may need to upgrade to get full functionality.

  • include You'll find the kernel header files in this directory. If you feel comfortable with your kernel, you can use the header files in include/linux as your system /usr/include/linux header file set.

  • arch This directory contains architecture-specific kernel build files and targets. After a kernel build on a PC system, the final kernel image is in arch/i386/boot/bzImage.

10.3.3 Distribution Kernels

Linux distributions come with generic kernels intended to run on almost any processor type. These kernels tend to be a little larger than custom kernels, and they rely heavily on loadable modules.

However, many of the Linux kernels that come with the various Linux distributions omit complete source code in their default installations, and furthermore, distribution kernels often differ from the official standard kernels at kernel.org. For example, Red Hat adds another component to the patchlevel to distinguish their kernels (for example, 2.4.20-20). If you wish to install a distribution kernel from source code, you need to use the distribution's kernel source package. In Red Hat Linux, the package name is kernel-source-version.rpm.

If your distribution has a /usr/src/linux directory, this does not mean that you have the entire kernel source. It is possible that you only have the header files. The kernel source takes up dozens of megabytes; run du -s /usr/src/linux for a quick check on what you have.