您的位置:首页 > 其它

解决insmod: error inserting 'helloworld.ko': -1 Invalid module format

2010-09-28 13:54 513 查看
我的环境 :VM ware5.5虚拟机 Cent OS系统 linux版本2.6





在使用命令ismod helloworld.ko 加载编译成功的模块helloworld.ko时出现错误 insmod: error inserting 'helloworld.ko': -1 Invalid module format

一般出错信息被记录在文件/var/log/messages中
[root@hailiang linux-2.6.15.5]# cat /var/log/messages |tail

Sep 14 09:20:19 localhost kernel: hello: disagrees about version of symbol struct_module

通过命令看一下模块的相关信息



[gyb@localhost hello]$ /sbin/modinfo hello.ko
filename: hello.ko
license: GPL
srcversion: 44D26D2A30D730996A843CE
depends:
vermagic: 2.6.27.48 SMP mod_unload modversions 686 4KSTACKS

内核无法加载模块的原因是因为记载版本号的字符串和当前正在运行的内核模块的不一样,这个版本印戳作为一个静态的字符串存在于内核模块中,叫vermagic,可以从编译模块中间生成的文件helloworld.moc.h中







#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

MODULE_INFO(vermagic, VERMAGIC_STRING);找到这个符号.





其实,真正的原因是由于当前的内核源码和系统版本不相匹配。例如:我的系统版本是2.6.18-194.e15,但是找不到该版本对应的内核,于是下载了2.6.27.48这个版本的内核代码。在建立内核树的时候,也是用了该代码。于是,在加载驱动模块的时候出现了上述错误。





解决方法:



在编译2.6内核代码的时候,是按照如下步骤进行的:

# tar zxvf linux-2.6.27.48.tar.gz -C /usr/src
# cd /usr/src/linux-2.6.27.48
# make menuconfig
# make (要很长时间)
# make modules_install

(以上是编译内核模块)

# cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.27.48-root (注意:目录i386是根据你的系统类型, 如果是64位系统, 那就很可能是x86_64)
# cp System.map /boot/System.map-2.6.27.48-root

# mkinitrd /boot/initrd-2.6.27-root.img 2.6.27.48
(设置启动项)

# vi /etc/grub.conf

default=0

timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-194.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
initrd /initrd-2.6.18-194.el5.img


将启动版本改为:

title CentOS (2.6.27.48)
root (hd0,0)
kernel /vmlinuz-2.6.27.48-root ro root=/dev/VolGroup00/LogVol00 rhgb quiet
initrd /initrd-2.6.27.48-root.img

并将上述相应的内容注释掉。
reboot

于是,再次启动机器,就会发现系统的版本已经升级到了2.6.27.48.

ok。再次ismod helloworld.ko ,已经成功了!

查看输出信息:

vim /var/log/messagesSep 14 21:32:56 localhost kernel:

Sep 14 21:32:56 localhost kernel: Hello, world

后记:之所出现上述问题,主要还是在于编译内核源码的时候没有彻底解决版本的问题。
如果提示没有mkinitrd 这个命令,那么试下/sbin/mkinitrd
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐