小编典典

insmod错误:插入'./hello.ko':-1无效的模块格式”

linux

我刚刚制作了我的第一个驱动程序模块,即LDD3之后的hello world模块。但是不幸的是遇到了这个错误:

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

我正在Ubuntu 11.04和我的环境上执行此操作:

$ uname -r
2.6.38-8-generic

我得到这样的内核源代码:

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

我的/ 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

然后我编译内核

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

然后编译我的内核模块

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

使用Makefile:

obj-m := hello.o

最后,当我插入模块时:

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

我在dmesg中发现了什么:

hello: disagrees about version of symbol module_layout

所以有什么问题?

我还注意到,linux-header is -2.26.38-generic源代码版本是-2.26.38,这是问题吗?但是我真的没有linux- source-2.26.38-generic在网络上找到一个软件包。

状态更新:我发现文件/ lib / moduels / $(name -r)/ build / Makefile指示我正在运行的内核版本:

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

因此,我下载了linux-2.6.38.2并进行了编译,但是仍然出现相同的错误。

我还发现/ boot / config-$(uname -r)中有一行:

CONFIG_VERSION_SIGNATURE="Ubuntu 2.6.38-8.42-generic 2.6.38.2"

有人知道这是什么意思吗?我在构建的内核的配置文件中没有看到它。


阅读 863

收藏
2020-06-07

共1个答案

小编典典

从其构建内核模块和向其插入模块的内核应具有相同的版本。如果您不想照顾这件事,可以使用以下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

现在,您可以构建并尝试插入模块。

我建议您尽可能在此行之前成为root

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

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

另外,您也可以使用以下make文件

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
2020-06-07