Build an external module for specified kernel.

Sometimes, we need to build a module for specified kernel. Following is an example:


ENV:

[root@atest-guest ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@atest-guest ~]# uname -a
Linux atest-guest 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

(1), Problem:

[root@atest-guest linux_btrfs]# make -j 4 fs/btrfs/

[root@atest-guest linux_btrfs]# insmod fs/btrfs/btrfs.ko
insmod: ERROR: could not insert module fs/btrfs/btrfs.ko: Invalid module format
[root@atest-guest linux_btrfs]# dmesg
[ 2976.500283] btrfs: disagrees about version of symbol module_layout                    

                                   <------------------------- It means the version of module_layout in this module is not same with the current kernel.


(2), More information:

[root@atest-guest linux_btrfs]# cat /lib/modules/3.10.0-123.el7.x86_64/build/Module.symvers |grep module_layout
0x28950ef1    module_layout    vmlinux    EXPORT_SYMBOL

        |<-------- That means what required by the running kernel is 0x28950ef1.

[root@atest-guest linux_btrfs]# cat Module.symvers |grep module_lay
0x8798da3e    module_layout    vmlinux    EXPORT_SYMBOL
[root@atest-guest linux_btrfs]# modprobe --dump-modversions fs/btrfs/btrfs.ko |grep module_lay
0x8798da3e    module_layout

        |<-------- But what we get in our module is 0x8798da3e


(3), Reason

There is a function named module_layout in kernel only for modversions, we can only insmod a module

with the modversions all matched.


(4), Solution

We need to reuse the Module.symvers of running kernel to build our module. This file store the all version

of each functions. we need to use these to build our module, then we will link the all related symbols with

the correct address.

[root@atest-guest linux_btrfs]# make -j 4 -C /lib/modules/3.10.0-123.el7.x86_64/build/ M=/data/linux_btrfs/drivers/mtd/

[root@atest-guest linux_btrfs]# modprobe --dump-modversions drivers/mtd/mtd.ko |grep module_lay
0x28950ef1    module_layout
[root@atest-guest linux_btrfs]# cat /lib/modules/3.10.0-123.el7.x86_64/build/Module.symvers |grep module_lay
0x28950ef1    module_layout    vmlinux    EXPORT_SYMBOL

>>>>>>>>>>>>>>>>>>>>>currently, the version of module_layout in our module of mtd.ko is same with running kernel.

[root@atest-guest linux_btrfs]# cat Module.symvers |grep module_lay
0x8798da3e    module_layout    vmlinux    EXPORT_SYMBOL
>>>>>>>>>>>>>>>>>>>>>We use -C in make command to use the environment of the specified path, I guess it's chroot but not sure.

[root@atest-guest linux_btrfs]# insmod drivers/mtd/mtd.ko
[root@atest-guest linux_btrfs]# echo $?
0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值