How Compile A Loadable Kernel Module Without Recompiling Kernel - Raspberry Pi Stack Exchange

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

2/26/2017 How compile a loadable kernel module without recompiling kernel - Raspberry Pi Stack Exchange

sign up log in tour help

_
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

How compile a loadable kernel module without recompiling kernel

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

zcat /proc/config.gz >.config


make oldconfig
make modules_prepare
ln -s /root/linux /lib/modules/$(uname -r)/build

The first few lines are from http://lostindetails.com/blog/post/Compiling-a-kernel-module-for-the-raspberry-pi-2

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");

static int __init hello_init(void){


printk(KERN_INFO "Hello, world.\n");
return 0;
}

static void __exit hello_exit(void){


printk(KERN_INFO "Goodbye, world.\n");
}

module_init(hello_init);
module_exit(hello_exit);

Finally, I build the module with this Makefile

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

Finally, I attempt to load the module:

sudo insmod hello.ko

The result, however, is disappointing:

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

Possibly relevant details

I'm using the currently latest jessie version of Raspbian on a Raspberry Pi2.

$ uname --kernel-release --kernel-version


4.1.13-v7+ #826 SMP PREEMPT Fri Nov 13 20:19:03 GMT 2015
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.9/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 4.9.2-10' --with-
bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-
languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9
--enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-
gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --
libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-
libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-
libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-
plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-
1.5.0-gcj-4.9-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-
1.5.0-gcj-4.9-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-
armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --
enable-objc-gc --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --
with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-
gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.9.2 (Raspbian 4.9.2-10)

Unfortunately, I'm not sure how to further troubleshoot this or fix it. Any clues?

kernel modules

edited Dec 23 '15 at 0:32 asked Dec 21 '15 at 0:04


Jacobm001 ♦ Edward
8,358 4 23 44 198 1 12

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.

Note: I'm using Jessie.

UPDATE: Run these as root.

# The usual update routine


apt-get update -y
apt-get upgrade -y

# Update the kernel!


rpi-update

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

# Get the kernel files thingies.


rpi-source

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:

make -C /lib/modules/$(uname -r)/build M=$(pwd) modules


insmod hello.ko

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

edited Jan 2 '16 at 8:50 answered Jan 2 '16 at 1:14


pandalion98
4,822 11 38

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 updated. – pandalion98 Jan 2 '16 at 8:16

@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

in getKernel.sh file add

sudo modprobe configs

before

zcat /proc/config.gz >.config

(now in default rpi image /proc/config.gz not exist)

edited Feb 11 at 7:23 answered Feb 11 at 6:22


Igor Nikolaev
1 1

http://raspberrypi.stackexchange.com/questions/39845/how-compile-a-loadable-kernel-module-without-recompiling-kernel 3/3

You might also like