【发布时间】:2016-03-26 13:02:54
【问题描述】:
我刚刚制作了我的第一个驱动模块,即 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"
有人知道这是什么意思吗?我在我正在构建的内核的配置文件中没有看到它。
【问题讨论】:
-
insmod中的错误表明该模块插入到与编译它的内核不同的内核中。确保您启动的内核与您一直在编译的内核完全相同... -
@dragosht,您能否指出如何找到我的系统正在运行的确切内核源代码?上面的步骤是我在google上找到的,但是还是不行。
-
uname -r 显示启动的内核并选择特定的内核在启动时按住 shift 键。
-
@roMoon 尝试使用
make -C /usr/src/linux-headers-$(uname -r ) M=pwd` modules` 编译你的模块 -
感谢@Rusty,使用 -C /usr/src/linux-headers-$(uname -r) 确实有效。另一个问题,ldd3说我需要一个内核源代码树来构建模块,头目录“/usr/src/linux-headers-$(uname -r)”有同样的效果吗?或者我仍然需要构建我的内核源代码树?
标签: linux linux-kernel linux-device-driver embedded-linux kernel-module