Search code examples
clinux-kernelkernelkernel-module

How to compile a custom kernel and have the headers and sources be installed in the correct place?


So, I have configured a kernel, and installed it by using make install and make module_install

Now, I want to compile out-of-tree kernel modules but obviously the linux-header-$(uname -r) package doesn't exist because it is a custom kernel.

So, I want the kernel source and the header files and all to be properly installed in the right place so that I will be able to compile the kernel.

How do I make that happen?


Solution

  • Assuming you're using Debian kernel source since you mentioned linux-headers-$(uname -r)

    Please refer to the Building the kernel section in this Kernel/BuildYourOwnKernel wiki

    After a successful build, several .deb kernel package files will be generated. You can find linux-headers-$(uname -r).deb too which you can install on the target to compile out-of-tree kernel modules.

    You can also refer to this answer to compile out-of-tree kernel module against a compiled kernel source-code path.

    You can also compile modules without installing linux-headers-$(uname -r).deb by making use of cross-compilation. This involves extracting the linux-headers-$(uname -r).deb to a directory with dpkg-deb and modifying out-of-tree module Makefile to use KDIR as extracted header directory and use compatible GCC compiler version intended for same architecture (if you're compiling for ARM64, you've to use gcc-aarch64-linux-gnu) or you can also pass it via commandline make KDIR=/path/to/extracted/headers CROSS_COMPILE=<optional> to compile out-of-tree modules.