Setting up the kernel development environment - Basic

This article describes how to do the basic setup for your kernel development environment.

Kernel development requires a number of Linux packages to be installed. This series documents the setup process for an arch-based linux system. The general requirements are defined in minimal requirements to compile the kernel.

To install the base packages execute the following commands:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo pacman -y -S neovim
sudo pacman -y -S git
sudo pacman -y -S gcc
sudo pacman -y -S clang
sudo pacman -y -S make
sudo pacman -y -S binutils
sudo pacman -y -S util-linux
sudo pacman -y -S module-init-tools
sudo pacman -y -S e2fsprogs
sudo pacman -y -S jfsutils
sudo pacman -y -S xfsprogs
sudo pacman -y -S btrfs-progs
sudo pacman -y -S procps
sudo pacman -y -S bc
sudo pacman -y -S bison
sudo pacman -y -S flex
sudo pacman -y -S quota-tools
sudo pacman -y -S grub
sudo pacman -y -S ncurses
sudo pacman -y -S openssl
sudo pacman -y -S cscope
sudo pacman -y -S ctags
sudo pacman -y -S gdb

The next step is to get the current kernel source to the development machine. There are several ways to do this, but the best way is to use git clone. Using git clone we can fetch the next tree or one of the other development trees. In this example the next tree is cloned. Cloning the next tree is described here.

1
2
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux

Add the remote so we can fetch the changes and the new tags.

1
2
3
4
5
git remote add linux-next https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
git fetch linux-next
git fetch --tags linux-next
git checkout master
git remote update

Query which branches are available to be checked out.

1
git tag -l "next-*" | tail

To work on some changes it is best to checkout a new branch. The following commands achieve this. The two commands are only examples on how to checkout a branch. Both commands will check out a branch.

1
2
git checkout -b first-patch next-.....
git checkout -b feature v6.2-rc1

The first step in compiling a kernel is to create a kernel configuration. There are various ways to create a kernel configuration. In this guide we use the machine configuration as the starting point for our config:

1
2
cp /boot/config-$(uname -r) .config
zcat /proc.config.gz > .config

Both of the above commands create a kernel config file. It is possible that one of the commands does not work. It depends which features are configured in the current kernel. The kernel config file is called .config.

Afterwards invoke menuconfig to see that we have a valid configuration. In addition it is possible that new configuration options have been added and you will get query prompts for them. If unsure answer the question with ’n’ (no).

1
make menuconfig

The last step is to compile the kernel. The kernel can be compiled with the following command. The command invokes the make command with the -j option. The -j option specifies the parallelism (how many workers work in parallel on the compilation).

1
make -j$(nproc) 

or if you want to compile with clang, you can use:

1
make CC=clang -j$(nproc) 

To make development easier tools like cscope and tags are used to query where source code symbols are used or defined. cscope and tags are the older tools. They require a symbol database. The symbol database for these commands can be created with the make command.

1
2
make cscope
make ctags

The cscope command has the advantage that it also has a terminal program to run queries against the database. Otherwise the queries can be run from an editor like vim or emacs. cscope is described here and ctags is described here.

A more modern way to query source code symbols is by using the compiler databases. Instead of parsing the source code, they use the AST of the compiler. Most editors have the ability to integrate and query that information. The disadvantage to cscope is that only the symbols that are currently compiled in can be queried. Kernel features that are not compiled in will not report source code symbols. This can be a problem when kernel code changes are made. However compiler databases have the advantage that they are more accurate.