Skip to content

Kernel Module Build Workflow

This page summarizes the common out-of-tree kernel module build workflow used throughout the labs.

Build Model

Most labs use the standard kernel build system from outside the kernel source tree:

module source directory
make -C <kernel-build-dir> M=<module-dir> modules
*.ko

Minimal Makefile Pattern

obj-m += mydriver.o

KDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
    $(MAKE) -C $(KDIR) M=$(PWD) clean

Important Files

File Purpose
*.ko Loadable kernel module
*.mod.c Generated module metadata source
Module.symvers Symbol version information
modules.order Module build order

Native Build on Raspberry Pi

make
sudo insmod mydriver.ko
lsmod | grep mydriver
sudo rmmod mydriver

Cross Build from WSL

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- KDIR=/path/to/pi/kernel/headers

Useful Inspection Commands

modinfo mydriver.ko
modinfo -F vermagic mydriver.ko
uname -r
file mydriver.ko

Common Pitfalls

  • KDIR points to the wrong kernel headers
  • uname -r is evaluated on the host instead of the target
  • generated module has a different vermagic from the target kernel
  • copied headers are incomplete
  • CONFIG_LOCALVERSION or Raspberry Pi kernel suffix does not match