insmod error: inserting './hello.ko': -1 Invalid module format"

roMoon picture roMoon · Dec 20, 2015 · Viewed 53k times · Source

I have just made my first driver module, the hello world module following LDD3. However unfortunately encountered this error:

insmod: error inserting './hello.ko': -1 Invalid module format.

I am doing this on Ubuntu 11.04, and my environment:

$ uname -r
2.6.38-8-generic

I get the kernel source like this:

sudo apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
linux-source-2.6.38 - Linux kernel source for version 2.6.38 with Ubuntu patches
$sudo apt-get install linux-source-2.6.38

my /usr/src:

$ls /usr/src/
linux-headers-2.6.38-8          linux-source-2.6.38          vboxguest-5.0.10
linux-headers-2.6.38-8-generic  linux-source-2.6.38.tar.bz2

and then I compile the kernel

$sudo cp /boot/config-2.6.38-8-generic ./.config
$sudo make menuconfig -- load the .config file
$make
$make modules

and then I compile my kernel module

$make -C /usr/src/linux-source-2.6.38/linux-source-2.6.38 M=`pwd` modules

with Makefile:

obj-m := hello.o

and then finally when I insert the module:

$sudo insmod hello_world.ko
insmod: error inserting 'hello_world.ko': -1 Invalid module format

what I found in dmesg:

hello: disagrees about version of symbol module_layout

So what's the problem?

I have also noticed that the linux-header is -2.26.38-generic and source code version is -2.26.38, is this the problem? but I have really not found a linux-source-2.26.38-generic package on web.

status update: I have found that the file /lib/moduels/$(name -r)/build/Makefile indicate my running kernel version:

VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 38
EXTRAVERSION = .2

So I download the linux-2.6.38.2 and compile, but still the same error.

I have also found that there is a line in /boot/config-$(uname -r):

CONFIG_VERSION_SIGNATURE="Ubuntu 2.6.38-8.42-generic 2.6.38.2"

Does any one know what is this mean? I don't see it in the config file of the kernel i am building.

Answer

Punit Vara picture Punit Vara · Dec 21, 2015

Kernel from which you build your kernel module and to which you are inserting module should be of same version. If you do not want to take care of this thing you can use following Makefile.

obj−m += hello−world.o

all:
 make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules
clean:
 make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean

Now you can build and try to insert module.

I suggest you to become root if possible before this line

$sudo cp /boot/config-2.6.38-8-generic ./.config

$su
#cp /boot/config-2.6.38-8-generic ./.config
#insmod hello_world.ko

Alternatively you can also use following make file

TARGET  := hello-world
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := gcc-3.0

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
    rm -rf ${TARGET}.o