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

时间:2021-11-20 16:54:23

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

我刚刚制作了我的第一个驱动模块,跟随LDD3的hello world模块。但遗憾的是遇到了这个错误:

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

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

我在Ubuntu 11.04和我的环境中这样做:

$ 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:

我在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.

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

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

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

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

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

所以我下载linux-2.6.38.2并编译,但仍然是同样的错误。

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

我还发现/ 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.

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

3 个解决方案

#1


4  

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.

构建内核模块的内核以及要插入模块的内核应该是相同的版本。如果您不想处理这件事,可以使用以下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

我建议你在这一行之前成为root用户

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

$ 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

或者,您也可以使用以下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

#2


0  

Try using cross compile. Please look at the code below for the make file. Be mindful of the indentation else you may end up with error such as missing separator. Stop

尝试使用交叉编译。请查看以下代码以获取make文件。请注意缩进,否则您可能会遇到错误,例如缺少分隔符。停止

obj-m += hello_.o #this name should be the name of your .c file. I am just using hello for example

obj-m + = hello_.o#这个名称应该是.c文件的名称。我只是用hello为例

I suggest the best approach is via cross compilation

我建议最好的方法是通过交叉编译

Create a variable to hold the directory name where the linux kernel directory resides In my example, change the value "PATH_TO_LINUX_KERNEL_DIRECTORY" to a real path value Example ~/linux You really need to do this so that the make file will know where to find arm-linux-gnueabi- Without this, you are likely to run into issues arm-linux-gnueabi-

创建一个变量来保存linux内核目录所在的目录名称在我的示例中,将值“PATH_TO_LINUX_KERNEL_DIRECTORY”更改为实际路径值示例〜/ linux您真的需要这样做,以便make文件知道在哪里找到arm -linux-gnueabi-没有这个,你可能会遇到问题arm​​-linux-gnueabi-

KDIR := PATH_TO_LINUX_KERNEL_DIRECTORY

all:
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -C $(KDIR) M=$(shell pwd) modules

clean:
    make -C $(KDIR) M=$(shell pwd) clean

#3


-2  

You did everything correctly but did not booted your system with the kernel you compiled so the the first step is you should boot with it. If you are using Ubuntu you can hold shift button at the time of booting and you will be given a list of compiled kernel in your system from there select linux-source-2.6.38 and then try to build your module and install it with your way ,than you won't find any problem. GoodLuck.

您已正确完成所有操作,但未使用您编译的内核启动系统,因此第一步是您应该使用它启动。如果您正在使用Ubuntu,您可以在启动时按住shift按钮,系统会从您的系统中选择linux-source-2.6.38,然后尝试构建您的模块方式,比你不会发现任何问题。祝你好运。

#1


4  

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.

构建内核模块的内核以及要插入模块的内核应该是相同的版本。如果您不想处理这件事,可以使用以下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

我建议你在这一行之前成为root用户

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

$ 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

或者,您也可以使用以下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

#2


0  

Try using cross compile. Please look at the code below for the make file. Be mindful of the indentation else you may end up with error such as missing separator. Stop

尝试使用交叉编译。请查看以下代码以获取make文件。请注意缩进,否则您可能会遇到错误,例如缺少分隔符。停止

obj-m += hello_.o #this name should be the name of your .c file. I am just using hello for example

obj-m + = hello_.o#这个名称应该是.c文件的名称。我只是用hello为例

I suggest the best approach is via cross compilation

我建议最好的方法是通过交叉编译

Create a variable to hold the directory name where the linux kernel directory resides In my example, change the value "PATH_TO_LINUX_KERNEL_DIRECTORY" to a real path value Example ~/linux You really need to do this so that the make file will know where to find arm-linux-gnueabi- Without this, you are likely to run into issues arm-linux-gnueabi-

创建一个变量来保存linux内核目录所在的目录名称在我的示例中,将值“PATH_TO_LINUX_KERNEL_DIRECTORY”更改为实际路径值示例〜/ linux您真的需要这样做,以便make文件知道在哪里找到arm -linux-gnueabi-没有这个,你可能会遇到问题arm​​-linux-gnueabi-

KDIR := PATH_TO_LINUX_KERNEL_DIRECTORY

all:
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -C $(KDIR) M=$(shell pwd) modules

clean:
    make -C $(KDIR) M=$(shell pwd) clean

#3


-2  

You did everything correctly but did not booted your system with the kernel you compiled so the the first step is you should boot with it. If you are using Ubuntu you can hold shift button at the time of booting and you will be given a list of compiled kernel in your system from there select linux-source-2.6.38 and then try to build your module and install it with your way ,than you won't find any problem. GoodLuck.

您已正确完成所有操作,但未使用您编译的内核启动系统,因此第一步是您应该使用它启动。如果您正在使用Ubuntu,您可以在启动时按住shift按钮,系统会从您的系统中选择linux-source-2.6.38,然后尝试构建您的模块方式,比你不会发现任何问题。祝你好运。