Centos – How to compile kernel module for different kernel version

centoscompilingkernel-moduleslinux-kernel

I have the following kernel installed:

[root@localhost ~]# uname -r
2.6.32-573.el6.x86_64

And the following kernel source:

linux-2.6.32-573.1.1.el6.x86_64

I am trying to compile a kernel module with that kernel source for my own kernel.

In the Makefile of the kernel source, I edited the EXTRAVERSION setting:

[root@localhost linux-2.6.32-573.1.1.el6.x86_64]# cat Makefile | grep "EXTRAVERSION ="
EXTRAVERSION = 2.6.32-573.el6.x86_64

And used the following command to compile the module:

[root@localhost linux-2.6.32-573.1.1.el6.x86_64]# make M=~/hw/
  LD      /root/hw/built-in.o
  CC [M]  /root/hw/hello_world.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/hw/hello_world.mod.o
  LD [M]  /root/hw/hello_world.ko.unsigned
  NO SIGN [M] /root/hw/hello_world.ko

But when I try to install it I recieve the following error:

[root@localhost linux-2.6.32-573.1.1.el6.x86_64]# insmod ~/hw/hello_world.ko
insmod: error inserting '/root/hw/hello_world.ko': -1 Invalid module format

And the output of modinfo is:

[root@localhost linux-2.6.32-573.1.1.el6.x86_64]# modinfo ~/hw/hello_world.ko
filename:       /root/hw/hello_world.ko
license:        GPL
srcversion:     A8BB878B88F36D3A046026C
depends:
vermagic:       2.6.32-573.el6.x86_64 SMP mod_unload modversions

Can I compile the module for my current kernel without having to install its kernel sources?

Additional info:

OS: CentOS 6.7 minimal. Distribution binary.

Followed this guide: http://wiki.centos.org/HowTos/I_need_the_Kernel_Source

and downloaded the sources for linux-2.6.32-573.1.1.el6.x86_64 from here:

http://vault.centos.org/6.7/updates/Source/SPackages

I know that I could just download the source for my particular kernel, but in the end I actually want to build the module for a kernel version that I can't find the sources for (still 2.6.32), so that's why I want to do it this way.

Best Answer

Actually here two things are there to care about:

Is the running kernel version is same are of the source we are using. As previously compiled kernel may not be having all the dependencies which may be used in latest version, while compilation of external module with latest kernel source may be dependent on any part of the code, which is only present in latest version. So it is recommended to use the latest version kernel with which we are using the external module.

Second, the .config file for kernel compilation You can copy it from /boot/config(current-version)* of your file system to .config in kernel source top directory. Current version we can get it from uname -r

So procedure will be if running version (check with command uname -r) is not equal to downloaded source, then you need to compile and use new kernel or otherwise download same version of kernel as of running kernel. For kernel compilation use the config file present in boot directory, copy it as stated above.

Then you can proceed with a normal way of compiling the external module and load it with running kernel without any issue.

Related Question