How to compile a kernel module for Raspberry pi?
When compiling a module the -C
parameter should point to the source tree where the kernel was built (don't clean it up!). If you built it on the pi its likely in a directory under your home directory.
The build
directory under /lib/modules/<version>
is a Debian-ism, where a cut-down version of the source tree is provided with just enough context to build modules against. The kernels from the Raspberry Pi Foundation kernels don't ship with a build
directory.
They may be a bit out of date, but raspbian provides a kernel as a Debian-style package, which should include the build
directory you could use to build kernel modules against.
sudo aptitude install linux-image-rpi-rpfv linux-headers-rpi-rpfv
Here are the steps I used to build the Hello World kernel module on Raspbian.
Perform
sudo rpi-update
See https://github.com/Hexxeh/rpi-update for details on
rpi-update
. You have to be on the latest firmware and associated kernel to be able to perform the next step.Install and run
rpi-source
to install the source code that built the latest kernel that you are running. This will create the correct entry in/lib/modules
for the kernel that you are running. Note: you don't need to be root to run this, however the script will perform certain tasks usingsudo
and the root password will be requested during the script execution.Instructions to install
rpi-source
can be found at https://github.com/notro/rpi-source/wiki
Once those steps are performed you should be able to make
the Hello World kernel module.
johnma@raspberrypi ~/HelloWorld $ makemake -C /lib/modules/3.12.19+/build M=/home/johnma/HelloWorld modulesmake[1]: Entering directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5' CC [M] /home/johnma/HelloWorld/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/johnma/HelloWorld/hello.mod.o LD [M] /home/johnma/HelloWorld/hello.komake[1]: Leaving directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'johnma@raspberrypi ~/HelloWorld $ sudo insmod hello.kojohnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslogMay 15 13:45:39 raspberrypi kernel: [59789.169461] Hello World :)johnma@raspberrypi ~/HelloWorld $ sudo rmmod hello.kojohnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslogMay 15 13:46:10 raspberrypi kernel: [59819.503503] Goodbye World!
You first need kernel headers (and the corresponding kernel binary) to build your module.
Like Greg said, the raspbian distribution provides the packages :
sudo apt-get install linux-image-rpi-rpfv linux-headers-rpi-rpfv
Then, tell raspbian to boot your newly installed kernel (3.10-3-rpi for me).
Append this at end of /boot/config.txt
and reboot your Pi :
# Parameters to boot on raspbian kernel (linux-image-rpi-rpfv package)kernel=vmlinuz-3.10-3-rpiinitramfs initrd.img-3.10-3-rpi followkernel
Then, modify your Makefile to point the freshly installed kernel headers :
KERNEL_HEADERS=/lib/modules/$(shell uname -r)/buildobj-m := hello-1.oall: @$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) modulesclean: @$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) clean