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.

  • What distribution? And how did you install the kernel? Was a local install or a distribution binary package? – Faheem Mitha Apr 7 '16 at 3:30
  • This is CentOS 6.7 minimal. Distribution binary. Followed this guide: 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: vault.centos.org/6.7/updates/Source/SPackages – Kenci Apr 7 '16 at 7:58
  • Ok. Make sure your running kernel and the source you are working with are an exact match. Why aren't you using modprobe? That's smarter and more standard. – Faheem Mitha Apr 7 '16 at 8:31
  • I didnt know about modprobe. Will try it out. I could change the running kernel to match the source, and that would probably work, but I thought that I would not have to do this with the EXTRAVERSION setting. What if I want to build a module for a kernel that I cant find the sources for? – Kenci Apr 7 '16 at 9:05
  • In general you don't need the kernel source to build a kernel module, just the headers. And if you have the kernel binary image installed, you should have the headers installed as well. And I don't know whether your running kernel matches what you are trying to compile against, but just make sure that is the case. – Faheem Mitha Apr 7 '16 at 9:17

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.