Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Building Debian Kernel Packages

1. Introduction
While the thought of building your own custom kernel may seem daunting at first, the Debian tools
found in the kernel-package package make the procedure rather painless. Once you figure out the
system’s quirks, modifying your custom kernel becomes as natural as installing a package with apt-get.
If you have never built a kernel, or you have and would like to learn more about the Debian packaging
tools, this article is for you.

2. Install the Software


You will need to install packages for the Debian tools as well as the build essentials. On Debian, the
following command should suffice:

sudo apt-get install make gcc libstdc++6-dev libncurses5 libncurses5-dev kernel-package

3. Download the Source Code


Next, you will need to download the source code for the version of the kernel you would like to build.
You have two options for this. You may either install the Debian source package or download a vanilla
version of the source code from http://www.kernel.org/. The Debian package would be the stabler of the
two, but you will not find packages for the latest releases of the Linux kernel. If you do not need the new
features of the most recent kernel, install the Debian source package. The command apt-cache search
kernel-source should show you which versions of the kernel are available. The Debian source package
will drop a compressed bzip file into /usr/src/. Unpack it with sudo tar -jxvf sourcetree.tar.bz2.
The vanilla source tree will be either a bzip or a tarball depending on your download. To unpack the
tarball, run sudo tar -zxvf sourcetree.tar.gz.

4. Prepare to Customize
Once you have unpacked the kernel source code, cd into the root of the source tree and run the following
commands:

sudo make clean

1
Building Debian Kernel Packages

sudo make mrproper


cp /boot/config-‘uname -r‘ .config

The first two commands remove obsolete files that could corrupt the build process. The third command
copies the configuration of your system’s working kernel to your new kernel’s configuration. This
provides a solid foundation for the customizations that we will be making.1

5. Customize the Kernel


The kernel source code offers several ways to perform customizations. Each method is invoked by
running make method from the root directory of the kernel source tree. The most commonly used
methods are menuconfig and xconfig. For the purposes of this article, I will assume you are using
menuconfig.

make menuconfig

Regardless of which method you choose, each method provides a graphical interface to all configurable
kernel options. These options are organized in a hierarchical structure that is rather intuitive. To add
support for a new file system, navigate to the File Systems section and activate the one you like. Need
support for a SCSI device? Poke around in the Device Drivers -> SCSI Devices section. You will
become comfortable with all of the options available just by browsing through the various categories.

To enable a feature, navigate to it using the arrow keys and press Y. This will build the feature directly
into the kernel. Certain options may be modularized and loaded after the system boots. These options,
designated by < >, may be modularized by pressing M.

Pay close attention to the processor type and device driver options. These options essentially determine
whether the kernel will actually boot. If you are unsure about which processor type to select and are
working on the workstation that you want to update, run uname -m to see what type your current kernel
uses. Consult your system’s documentation to determine what hardware the system has. If there are no
device drivers specific to your hardware, enable support for the corresponding generic driver.

6. Build the Kernel


Now that our kernel is configured, we need to build it. Fortunately, the Debian tool make-kpkg abstracts
all the details of the build process.

To avoid corrupting our build, we need to remove stale files left over from the previous step:

sudo make-kpkg clean

2
Building Debian Kernel Packages

To build a kernel image with an initial ram disk (initrd), run the following command:

sudo make-kpkg --append-to-version "-suffix " --initrd kernel_image

Replace -suffix with a string that will distinguish the kernel you are building from ones you may build
in the future. make-kpkg uses this suffix when naming the Debian package it creates. If you consistently
use the same suffix, you will overwrite previously generated packages each time you build a new image.

Note that the initial ram disk is not needed if you build hardware and file system support for your boot
device directly into the kernel. On my IBM ThinkPad, I boot from a generic IDE hard disk that contains
an ext3 root file system. By choosing to build support for these directly into the kernel, I no longer need
the initial ram disk. Leave off the --initrd option to remove the initial ram disk.

sudo make-kpkg --append-to-version "-suffix " kernel_image

Regardless of which method you choose, make-kpkg will take some time to complete.

7. Install the Kernel


When make-kpkg completes, it drops your new Debian kernel package into the parent directory of your
kernel source tree. Locate this file and pass it to dpkg.

sudo dpkg -i kernel-image*.deb

Your kernel will then be installed just like any other update issued from the Debian repositories.

8. Reboot the Workstation


With the new kernel installed, reboot the workstation. If all goes well, your machine should boot up into
your custom kernel. Confirm that you are using your new kernel by running uname -r. This should
return the version of the kernel you built followed by the -suffix you specified in the previous section.

9. Conclusion
Kernel packages created with make-kpkg drop your custom kernel, as well as its configuration, in
/boot/. When building future kernels, use the configuration of your current kernel as a starting point.
This can save you a lot of headaches when a wrong processor type or neglected device driver prevents
you from booting the new kernel. Also, be sure not to leave out any of the steps in this procedure
whenever you build a new kernel. Though you may be tempted to dismiss make clean and make
mrproper, for instance, doing so may corrupt your build.

3
Building Debian Kernel Packages

With your new kernel up and running, you should now be comfortable using the Debian packaging tools
to build future releases.

Notes
1. While make clean and make mrproper have no effect if you are starting with a newly unpacked
source tree, I have included them here just to provide a consistent procedure.

You might also like