dev-guides

Kernel Compilation in Debian Linux

Overview

In Debian Linux, there are two ways to compile custom kernels:

  1. Using Debian packages

  2. The traditional method

We’ll use the traditional method because it applies to all Linux distributions (in fact, the guide we linked is from the ArchWiki).

Preparation

  1. First and foremost, please take a snapshot of your VM before you get started.

  2. Make sure you have the following kernel configuration/compilation dependencies installed:

    build-essential bc python bison flex
    libelf-dev libssl-dev libncurses-dev
    
  3. Reboot and make sure that your system is all good.

  4. Run uname -r to make sure that you are running the stock kernel that ships with Debian. At this time of writing, our system reports 4.19.0-6-amd64.

Obtain kernel source

In the Spring 2020 semester, you will be working Linux kernel version 4.19.50. But instead of fetching the Linux source code from kernel.org, you will be compiling the kernel source code provided in each kernel assignment’s skeleton repo. You will find the pristine, mainline Linux kernel source tree in the linux/ directory in your assignment repo.

You should verify the version of the kernel. The first 6 lines of Linux’s top-level Makefile will show you the version:

$ head -n 6 Makefile
# SPDX-License-Identifier: GPL-2.0
VERSION = 4
PATCHLEVEL = 19
SUBLEVEL = 50
EXTRAVERSION =
NAME = "People's Front"

It is standard practice to run make mrproper in a freshly cloned repo before proceeding. This removes any configuration files that might have been accidentally left over from previous builds.

Configuring the kernel build

The Linux kernel build system can be configured using the .config, which must be located at the root of the Linux source tree. This file is used to specify a truly astounding number of options with which you can build your kernel.

You can start with the .config file of your running kernel (e.g. the stock Debian kernel). The .config file that was used to build the stock Debian kernel is located in the /boot/ directory. The following command copies over that .config file, and updates any missing options with default values:

$ make olddefconfig

Depending on how your stock kernel is configured, this may report some warnings. For example:

/boot/config-4.19.0-6-amd64:6954:warning: symbol value 'm' invalid for ASHMEM
/boot/config-4.19.0-6-amd64:7583:warning: symbol value 'm' invalid for ANDROID_BINDER_IPC

You should be able to ignore these without running into any trouble.

Now, run the following command to edit your .config:

$ make menuconfig

This will open up an interactive menu that organizes configuration options in a more user-friendly manner. If you make any configuration changes, it will also create a backup of your original .config file, named .config.old.

Make the following changes, either using menuconfig, or by directly editing the .config file:

After setting your local version, take a moment to inspect the contents of the .config file. Make sure that the options you configured are set to what you expect them to be.

Linux also provides the scripts/diffconfig utility, which can be used to compare different .config files. For example, you would see:

$ scripts/diffconfig .config.old .config
 BLK_DEV_LOOP m -> y
 LOCALVERSION "" -> "-cs4118"
 SYSTEM_TRUSTED_KEYS "debian/certs/debian-uefi-certs.pem" -> ""

Build and Install a New Kernel

Compiling a kernel takes a long time – it can take over an hour on a slow machine. You can speed up this process by running compile jobs in parallel – if your computer has multiple CPU cores, you should configure your VM to use them. Run make as a non-root user:

$ make -jN

where N is the number of parallel jobs you would like make to spawn. Use the number of CPUs for N. So for a 8-core (virtual) machine, you would run make -j8.

Then, install the kernel modules and the kernel itself:

# make modules_install
# make install

Verify that you have the following 3 files in /boot/:

vmlinuz-4.19.50-cs4118
initrd.img-4.19.50-cs4118
System.map-4.19.50-cs4118

Boot to the New Kernel

Reboot by running:

# reboot

Make sure to pick your custom kernel from the boot loader menu.

Now verify that you’re running your own custom kernel by running:

$ uname -a

Instead of 4.19.0-6-amd64, you should now see your kernel version string, 4.19.50-cs4118!


Last updated: 2020-01-31