Professional Documents
Culture Documents
How Compile A Loadable Kernel Module Without Recompiling Kernel - Raspberry Pi Stack Exchange
How Compile A Loadable Kernel Module Without Recompiling Kernel - Raspberry Pi Stack Exchange
How Compile A Loadable Kernel Module Without Recompiling Kernel - Raspberry Pi Stack Exchange
_
Raspberry Pi Stack Exchange is a Here's how it works:
question and answer site for users and
developers of hardware and software
for Raspberry Pi. Join them; it only
takes a minute:
Anybody can ask Anybody can The best answers are voted
Sign up a question answer up and rise to the top
I have read quite a bit about how to compile a kernel module on (and for) the Raspberry Pi, but I'm still not quite able to figure out why it's not
working. I have been able to build the module, but it reports Invalid module format when I try to insmod the result. Here is the process I
followed. First, as root under /root I executed the following shell script:
getKernel.sh
#! /usr/bin/bash
FIRMWARE_HASH=$(zgrep "* firmware as of" /usr/share/doc/raspberrypi-
bootloader/changelog.Debian.gz | head -1 | awk '{ print $5 }')
KERNEL_HASH=$(wget
https://raw.githubusercontent.com/raspberrypi/firmware/$FIRMWARE_HASH/extra/git_hash
-O -)
git clone https://github.com/raspberrypi/linux
cd linux
git checkout $KERNEL_HASH
wget
https://raw.githubusercontent.com/raspberrypi/firmware/$FIRMWARE_HASH/extra/Module.symvers
The rest I wrote to automate more of the process. Once all of that runs successfully, I have the source that should exactly match the running
kernel, the configuration to match and a symlink. There were some redirects from the github web location (apparently it's now
https://raw.githubusercontent.com/ ) but no actual errors.
Then I become the default pi user and in a directory named /home/pi/projects/lkm I have this source code for a very simple toy module:
hello.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Do-nothing test driver");
MODULE_VERSION("0.1");
module_init(hello_init);
module_exit(hello_exit);
Makefile
MODSRC=/home/pi/projects/lkm
obj-m+=hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=${MODSRC} modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=${MODSRC} clean
http://raspberrypi.stackexchange.com/questions/39845/how-compile-a-loadable-kernel-module-without-recompiling-kernel 1/3
2/26/2017 How compile a loadable kernel module without recompiling kernel - Raspberry Pi Stack Exchange
insmod: ERROR: could not insert module hello.ko: Invalid module format
I'm using the currently latest jessie version of Raspbian on a Raspberry Pi2.
Unfortunately, I'm not sure how to further troubleshoot this or fix it. Any clues?
kernel modules
I compiled all my findings and experiences into a script, see github.com/x29a/kernel/blob/master/rpi/prepare.sh and
the related blogpost blog.chris007.de/… – x29a May 21 '16 at 21:48
2 Answers
First of all, make sure you use the proper kernel headers. I assume that your kernel headers
and source code are more updated than the kernel you're running.
Try to do an apt-get update && apt-get upgrade then reinstall the module. If the problem
persists, triple check that your kernel headers match your current kernel, recompile again then
try to install.
You may need to reboot. After that, proceed with the commands below, still using the root
account.
# Get rpi-source
sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O
/usr/bin/rpi-source
# Make it executable
sudo chmod +x /usr/bin/rpi-source
# Tell the update mechanism that this is the latest version of the script
/usr/bin/rpi-source -q --tag-update
If rpi-source throws a GCC error (something about a version mismatch), it's okay as long as
your current GCC version is higher. Run rpi-source --skip-gcc instead of rpi-source
Then, proceed with your Hello World example. Create the folder and cd into it. Then, create
the files.
mkdir hello
cd hello
http://raspberrypi.stackexchange.com/questions/39845/how-compile-a-loadable-kernel-module-without-recompiling-kernel 2/3
2/26/2017 How compile a loadable kernel module without recompiling kernel - Raspberry Pi Stack Exchange
Files:
hello.c
#include <linux/module.h>
#include <linux/kernel.h>
int hello_init(void)
{
pr_alert("Hello World :)\n");
return 0;
}
void hello_exit(void)
{
pr_alert("Goodbye World!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile (case-sensitive?)
obj-m := hello.o
Now that you have your files, you can go ahead and run the usual Hello World build
commands:
You should now check dmesg . The last line should print Hello World :) highlighted in red.
If you do, congratulations. You just made and installed a kernel module.
Now remove it using rmmod hello . dmesg should now print Goodbye World! highlighted in
red.
Sources: 1 2 3
When you say to "check that your kernel headers match your current kernel" how exactly do you mean I should do
that? – Edward Jan 2 '16 at 1:54
@Edward Take note that this is the hello world example. I built your module, but I realized it's the same. The only
difference is that your code does not have the red highlight. – pandalion98 Jan 2 '16 at 8:47
@Edward In your case, I think following the instructions up until the rpi-source part is enough. You can try
building yours from that point. – pandalion98 Jan 2 '16 at 8:51
before
http://raspberrypi.stackexchange.com/questions/39845/how-compile-a-loadable-kernel-module-without-recompiling-kernel 3/3