Skip to content

Instantly share code, notes, and snippets.

@Josua-SR
Created June 17, 2020 12:24
Show Gist options
  • Select an option

  • Save Josua-SR/3ee497179b75e8e164e508f98b12d810 to your computer and use it in GitHub Desktop.

Select an option

Save Josua-SR/3ee497179b75e8e164e508f98b12d810 to your computer and use it in GitHub Desktop.
How to build out of tree Kernel Module on Debian

How to build out of tree Kernel Module on Debian

Install Kernel Development files

First get the general build tools for compiling kernel modules: sudo apt install build-essential

Also install the kernel development headers for the currently running kernel:

debian@sr-imx8:~$ uname -a
Linux sr-imx8 4.19.72-imx8-sr #1 SMP PREEMPT Thu Apr 30 03:44:47 UTC 2020 aarch64 GNU/Linux
debian@sr-imx8:~$ apt-cache search imx8-sr
linux-4.19.y-imx8-sr-dev - kernel API development files
linux-headers-4.19.y-imx8-sr - kernel development files
linux-headers-4.19.y-imx8-sr-dbgsym - debug symbols for linux-headers-4.19.y-imx8-sr
linux-image-4.19.y-imx8-sr - LONGNAME kernel
debian@sr-imx8:~$ sudo apt install linux-headers-4.19.y-imx8-sr

Compile the Driver

Unpack sources:

sudo apt install unzip
unzip ~/MMA8652FC-ccaeb5d75dc2.zip
cd MMA8X5X

It appears this driver comes without a Makefile ...

debian@sr-imx8:~/MMA8X5X$ ls
mma8x5x.c  readme.txt

and without a Kbuild file :(

As per https://www.kernel.org/doc/html/latest/kbuild/modules.html the simplified steps are:

  1. Create simple Kbuild file:

    obj-m := mma8x5x.o
    
  2. invoke system-wide kernel Makefile

    debian@sr-imx8:~/MMA8X5X$ uname -r
    4.19.72-imx8-sr
    make -C /lib/modules/4.19.72-imx8-sr/source M=$PWD
    
  3. have fun fixing all the build errors 😅

  4. load the final module

    insmod mma8x5x.ko
    
@JonathonReinhart

Copy link
Copy Markdown

Per https://www.kernel.org/doc/html/latest/kbuild/modules.html it should be the build directory, not soruce:

$ make -C /lib/modules/`uname -r`/build M=$PWD

@casept

casept commented Sep 25, 2024

Copy link
Copy Markdown

Debian actually installs the kernel build system in a non-standard location, you need to pass e.g. /usr/src/linux-headers-6.1.0-25-common/ as the argument to -C.

The build may also complain about missing build scripts like so:

make: Entering directory '/usr/src/linux-headers-6.1.0-25-common'
Makefile:1103: scripts/Makefile.extrawarn: No such file or directory
make: *** No rule to make target 'scripts/Makefile.extrawarn'.  Stop.
make: Leaving directory '/usr/src/linux-headers-6.1.0-25-common'

In that case, you need to install the respective linux-kbuild package (e.g. linux-kbuild-6.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment