Building the Linux kernel with BTF

Mohan Parthasarathy
2 min readOct 4, 2021

If you are running some older version of Ubuntu (20.04 and before that), the kernel does not have BTF by default and you can’t generate the vmlinux.h to be used by the libbpf programs. Check the /sys/kernel/btf/ directory and a vmlinux file under that. This is absent if BTF information is not available. Though there are some pages with good information about BTF itself (https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html), step by step instructions to get build the linux kernel with BTF is not available.

Preparation

  1. mkdir kernel (directory where we will download and build kernel)
  2. apt-get source linux-image-unsigned-$(uname -r)
  3. cd linux-hwe-<version> (This is the top of the kernel directory where the build is done)
  4. Before you build the kernel, make sure you have the latest pahole utility. This is what I used: wget https://fedorapeople.org/~acme/dwarves/dwarves-1.21.tar.xz
  5. Building and install pahole and the associated libraries in /usr/local/lib/
  6. export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH (in the shell where build is being done). When the make is called below, it requires pahole at the end which converts DWARF into corresponding BTF type information

Configuring the Linux Kernel

  1. Copy the /boot/config-5.8.0–50-generic (check the kernel version on your system) to top of the kernel tree and name it .config
  2. Edit the .config: CONFIG_DEBUG_INFO_BTF=y
  3. Edit the Makefile with the right VERSION (50) and EXTRAVERSION(=-btf or whatever name you want to identify this kernel)

Building the kernel

  1. make oldconfig
  2. make -j 4
  3. sudo make INSTALL_MOD_STRIP=1 modules_install (NOTE: INSTALL_MOD_STRIP=1 strips the symbols without which initramfs gets too big and won’t boot).
  4. sudo make install (This step installs the initramfs, SysMap, bzImage and also updates grub)
  5. Reboot, “Esc”, select the new kernel
  6. Check to see: /sys/kernel/btf/vmlinux is present

--

--