insmod驱动模块 出现 Invalid module format

在使用命令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 

  May  8 16:41:45 hailiang kernel: helloworld: version magic '2.6.27.5-117.fc10.i686 SMP mod_unload modversions 686 4KSTACKS ' should be '2.6.27.5-117.fc10.i686 SMP mod_unload 686 4KSTACKS '

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

[root@hailiang tmp]# modinfo helloworld.ko
filename:       helloworld.ko
alias:          a simplest module
description:    A simple helloworld module
author:         zhanghailiang
depends:       
vermagic:       2.6.27.5-117.fc10.i686 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);找到这个符号,

打开文件/usr/src/kernels/2.6.27.5-117.fc10.i686/include/linux/vermagic.h (注意在fedroa 10 中源码树是在/usr/src/下)

#include <linux/utsrelease.h>
#include <linux/module.h>

/* Simply sanity version stamp for modules. */
#ifdef CONFIG_SMP
#define MODULE_VERMAGIC_SMP "SMP "
#else
#define MODULE_VERMAGIC_SMP ""
#endif
。。。。。。。。。。。。。。。。。。。
#ifdef CONFIG_MODVERSIONS
#define MODULE_VERMAGIC_MO

DVERSIONS "modversions "
#else
#define MODULE_VERMAGIC_MODVERSIONS ""
#endif
#ifndef MODULE_ARCH_VERMAGIC
#define MODULE_ARCH_VERMAGIC ""
#endif

#define VERMAGIC_STRING                                                 /
        UTS_RELEASE " "                                                 /
        MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT                     /
        MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS       /
        MODULE_ARCH_VERMAGIC

从这里看出vermagic中多出来的字符modversions是由于编译内核时选上了 “”选项的原因,(其实这是因为在编译helloworld模块前,自己曾试图重新编译内核所致:cd /usr/src/kernel..

[root@hailiang 2.6.27.5-117.fc10.i686]# make menuconfig
scripts/kconfig/mconf arch/x86/Kconfig
#
# configuration written to .config
#


*** End of Linux kernel configuration.
*** Execute 'make' to build the kernel or try 'make help'.

[root@hailiang 2.6.27.5-117.fc10.i686]# make
scripts/kconfig/conf -s arch/x86/Kconfig
  CHK     include/linux/version.h
  CHK     include/linux/utsrelease.h
make[1]: *** No rule to make target `missing-syscalls'.  Stop.
make: *** [prepare0] Error 2

 
)见下图

 

然后再重新编译helloworld.ko模块,然后再加载helloworld 成功。

  一点别的:在网上对这个问题有另一个解决方法就是modprobe --force-vermagic helloworld强制加载内核,在这里我试了一下问题还是原来invalid module format

 注意:(1)modprobe   模块名(不要带后缀.ko)   注:挂载一个模块

(2)在这里直接modprobe ./helloworld 错误 
FATAL: Module helloworld not found.
这是因为

使用man modprobe看

DESCRIPTION

      modprobe intelligently adds or removes a module from the Linux kernel: note that  for  conve-
       nience, there is no difference between _ and - in module names.  modprobe looks in the module
       directory /lib/modules/‘uname -r‘ for all  the  modules  and  other  files,
  except  for  the
       optional  /etc/modprobe.conf  configuration  file  and  /etc/modprobe.d  directory  (see mod-
       probe.conf(5)). modprobe will also use module options specified on the kernel command line i
modprobe会自动在/lib/modules/'uname -r'下寻找模块加载,将helloworld.ko拷到/lib/modules 下然后再执命令 modprobe helloworld 发现还是找不到,从man modprobe

  modprobe  expects  an  up-to-date  modules.dep  file, as generated by depmod (see depmod(8))

看出还需要依赖命令:depmod生成的module.dep 使用这个命令后 在modprobe helloworld 成功


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: b'insmod invalid module format' 的意思是“insmod无效的模块格式”。这可能是由于尝试安装不兼容内核的模块或不完全编译的模块导致的。需要检查模块的完整性,并确保其与当前内核兼容。 ### 回答2: insmod invalid module format,是指在Linux操作系统中,使用insmod命令加载内核模块时,系统显示无效的模块格式错误。这种错误一般是由于内核模块文件格式不正确或者版本不匹配所引起的。 针对这种错误,需要进行以下排查和解决措施: 1.确认内核模块文件是否正确,检查内核模块文件的版本和系统内核版本是否一致。 2.检查内核模块文件的权限,确保文件有可执行权限,并且可以被当前用户访问。 3.检查内核模块文件是否完整,特别是在文件传输或拷贝过程中是否损坏或缺失部分文件。 4.检查系统内核支持的文件格式,确保使用的内核模块格式与系统内核支持的模块格式相同。 5.尝试重新编译内核模块文件,确保其版本与系统内核版本匹配,重新编译时需要注意内核模块编译的过程,以及编译后内核模块的文件格式。 综上所述,当系统显示insmod invalid module format错误时,需要根据具体情况进行排查和解决,以确保内核模块能够正确加载并使用。 ### 回答3: insmod invalid module format是Linux系统中一个常见的错误信息。该错误通常发生在我们尝试加载一个内核模块时,提示模块格式无效。该错误信息的出现原因可能与以下几个方面有关。 1. 内核版本不兼容 内核模块是与特定内核版本兼容的。如果我们试图在当前的内核版本下加载此模块,但该模块是在另一个内核版本下编译的,那么就可能会出现该错误。解决方法是编译新的内核模块源代码以与当前内核版本兼容。 2. 缺少依赖项 内核模块可能依赖于其他模块或库文件。如果我们缺少这些依赖项,就会出现该错误。解决方法是安装相应的依赖项,或检查系统库路径是否正确。 3. 模块文件已损坏 内核模块文件可能已损坏或不完整,这也可能导致该错误。解决方法是重新下载正确的模块文件,并重新编译和安装。 4. 编译时出错 编译内核模块时出现错误也会导致该错误信息。检查编译器和编译选项是否正确,并检查代码是否正确编写。 总之,当出现insmod invalid module format错误时,我们应该仔细检查模块文件的来源、依赖关系和编译方式,以确定原因并解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值