Skip to main content

How to Build Linux Kernel

ResearchC/C++ClangLLVMLinux KernelAbout 3 minAbout 1036 words

EnvironmentVersion
Host ProcessorIntel® Core™ i7-10710U CPU @ 1.10GHz 1.61 GHz
Host OSWindows 11 Pro Education 21H2
VMware® Workstation 16 Pro16.2.2 build-19200509
Virtual Machine OSUbuntu 20.04.3 (amd64, Desktop LiveDVD) (tsinghua.edu.cn)open in new window

Download the Source Code

Visit the official kernel websiteopen in new window and download the linux-5.13.tar.gz (kernel.org)open in new window. The downloaded file contains a compressed source code.

Extract the Source Code

tar --extract --gzip --file linux-5.13.tar.gz

Install Required Packages

Install additional packages before building a kernel. To do so, run this command:[1][2]

sudo apt install build-essential flex bison libelf-dev libssl-dev dwarves zstd

The command we used above installs the following packages:

PackagePackage description
gitTracks and makes a record of all changes during development in the source code. It also allows reverting the changes.
fakerootPackaging tool that makes the fake root environment.
build-essentialInstalls development tools such as C, C++, gcc, and g++.
ncurses-devProgramming library that provides API for the text-based terminals.
xz-utilsProvides fast file compression and decompression.
libssl-devSupports SSL and TSL that encrypt data and make the internet connection secure.
bc (Basic Calculator)A mathematical scripting language that supports the interactive execution of statements.
flex (Fast Lexical Analyzer Generator)Generates lexical analyzers that convert characters into tokens.
libelf-devIssues a shared library for managing ELF files (executable files, core dumps and object code)
bisonGNU parser generator that converts grammar description to a C program.

Configure Kernel

The Linux kernel source code comes with the default configuration. However, you can adjust it to your needs. To do so, follow the steps below:

Navigate to the linux-5.13.0. directory using the cd command:

cd linux-5.13.0

To make changes to the configuration file, run the make command:

make menuconfig

If no .config file exists, defaults found in /boot/config-$(uname -r) will be used. The command launches several scripts, which then open the configuration menu.

The configuration menu includes options such as firmware, file system, network, and memory settings. Use the arrows to make a selection or choose Help to learn more about the options. When you finish making the changes, select Save, and then exit the menu.

Note: Changing settings for some options can lead to a non-functional kernel. If you are unsure what to change, leave the default settings.

In your kernel configuration file you will find this line:[3]

CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"

Change it to this:

CONFIG_SYSTEM_TRUSTED_KEYS=""

Depending on your source structure you might be able to do it via command line. Examples:

scripts/config --disable SYSTEM_TRUSTED_KEYS

or

scripts/config --set-str SYSTEM_TRUSTED_KEYS ""

Another key has been added to the default Canonical kernel configuration since this answer was posted:

CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"

So, it also needs to be dealt with for user kernel compiles to complete:

scripts/config --disable SYSTEM_REVOCATION_KEYS

Build the Kernel

Start building the kernel by running the following command:

make --jobs=$(nproc --all)

LLVM has substitutes for GNU binutils utilities. Kbuild supports LLVM=1 to enable them.[4]

make LLVM=1

They can be enabled individually. The full list of the parameters:

make CC=clang-12 LD=ld.lld-12 AR=llvm-ar-12 NM=llvm-nm-12 STRIP=llvm-strip-12 \
  OBJCOPY=llvm-objcopy-12 OBJDUMP=llvm-objdump-12 READELF=llvm-readelf-12 \
  HOSTCC=clang-12 HOSTCXX=clang++-12 HOSTAR=llvm-ar-12 HOSTLD=ld.lld-12

Note: The version of kernel and compiler matters!!! If you cannot pass compilation, try another release of kernel or compiler.

The process of building and compiling the Linux kernel takes some time to complete.

The terminal lists all Linux kernel components: memory management, hardware device drivers, filesystem drivers, network drivers, and process management.

Install the required modules with this command:

sudo make modules_install

Finally, install the kernel by typing:

sudo make install

Change the Default Boot Kernel

You may press Esc to enter the boot menu when staring up, but that’s a bit troublesome. Instead, use the following commands to list GRUB’s menu entries:

awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg

For example, I got the output:

0 : Ubuntu
1 : Advanced options for Ubuntu
        1>0 : Ubuntu, with Linux 5.13.0-30-generic
        1>1 : Ubuntu, with Linux 5.13.0-30-generic (recovery mode)
        1>2 : Ubuntu, with Linux 5.13.0
        1>3 : Ubuntu, with Linux 5.13.0 (recovery mode)
        1>4 : Ubuntu, with Linux 5.11.0-27-generic
        1>5 : Ubuntu, with Linux 5.11.0-27-generic (recovery mode)
2 : Memory test (memtest86+)
3 : Memory test (memtest86+, serial console 115200)

For me, the desired default kernel to boot from is 1>2 : Ubuntu, with Linux 5.13.0. Open the /etc/default/grub file and change the value of GRUB_DEFAULT to "1>2", like this:[5]

GRUB_DEFAULT="1>2"
  • 1 in 1>2 indicates the second entry of the main menu.
  • 2 in 1>2 indicates the third entry of the submenu.
  • There is no space before and after the greater-than sign (>) in 1>2.
  • Use a set of quotation marks to enclose 1>2.

Run update-grub afterwards to update /boot/grub/grub.cfg:

sudo update-grub

If you stuck on Loading inital ramdisk... after reboot, try adding IUCODE_TOOL_INITRAMFS=no to /etc/default/intel-microcode and then re-running update-initramfs -u to remove the microcode bits from the initramfs image.[6]

Reference


  1. How To Build Linux Kernel {Step-By-Step} | phoenixNAP KBopen in new window ↩︎

  2. compilation - BTF: .tmp_vmlinux.btf: pahole (pahole) is not available - Stack Overflowopen in new window ↩︎

  3. Compiling the kernel 5.11.11 - Ask Ubuntuopen in new window ↩︎

  4. Building Linux with Clang/LLVM — The Linux Kernel documentationopen in new window ↩︎

  5. How Do I Change the Default Boot Kernel in Ubuntu?_Elastic Cloud Server_Troubleshooting_Linux Issues_HUAWEI CLOUDopen in new window ↩︎

  6. boot - Stuck on “Loading initial ramdisk…” after upgrading - Ask Ubuntuopen in new window ↩︎