3

I'm using CentOS-6.5 and default kernel is 2.6.32-431.11.2.el6.x86_64. I downloaded 2.6.32 and built that kernel (under /lib/modules/2.6.32).

I wrote a module for simple hello world program. This program is working under below case:

Makefile: /lib/modules/$(shell uname -r)/build (default kernel)

$ sudo insmod hello.ko

$ dmesg
module inserted..
hello world..

My program is not working for below case:

Makefile: /lib/modules/3.2.32/build (newly built kernel)

$ sudo insmod hello.ko
insmod: error inserting 'hello.ko': -1 Invalid module format

dmesg: 
hello: disagrees about version of symbol module_layout

I tried modprobe for both cases it is showing below error

FATAL: Module hello not found.

How compile my module with 2.6.32 kernel?

2 Answers 2

2

The Linux kernel contains data structures whose layout varies not only from version to version but also depending on the compilation options. As a consequence, when you compile a kernel module, you need to have not only the header files from the kernel source, but also some header files that are generated during the kernel compilation. Merely unpacking the kernel source isn't enough.

With kernels built with the CONFIG_MODVERSIONS, the version number can differ, but the layout of the data structures must be the same. This option is activated in the Ubuntu kernels. With this option, in addition to the headers, modules need to be compiled against the proper Module.symvers file. CentOS, like most distributions, includes this file in the same package as the kernel headers resulting from the compilation. The CentOS kernel header package is called kernel-headers; make sure to install the version that matches the kernel you're compiling for. See also Overview of kernel packages in the CentOS documentation and You do not need the full kernel source on the wiki.

With kernels built without the CONFIG_MODVERSIONS (which may be the case if you compiled your own kernel), the check when loading a module is a simple version check. You can take the unpacked kernel source, copy the .config that was used during the compilation of your running kernel, and run make modules_prepare. The onus is on you to verify that any patch you've made to the kernel doesn't affect binary compatibility.

1
  • We know all this, but it doesn't solve the problem. The problem is that for many people recompiling the kernel would be catastrophic. Hence why they want to avoid it alll costs. Think problems with undocumented device trees, and all sorts of issues.
    – Owl
    Sep 3, 2018 at 16:54
1

Where did you download the 2.6.32 kernel?

If you use CentOS then just install sources of your kernel 2.6.32-431.11.2.el6.x86_64 from CentOS repository, since RedHat uses patched kernel. Please also make sure that configuration of the kernel source tree you build against is exactly the same as of running kernel.

Usually there are config-* files at /boot/ directory or you could try to get the config of the running kernel from /proc/config.gz.

You could also refer to official CentOS HowTos:

11
  • I downloaded source from www.kernel.org
    – gangadhars
    Apr 22, 2014 at 6:46
  • @SGG As I said, you need the sources from your distribution, because RedHad-like distros usually don't use standard kernel.
    – UVV
    Apr 22, 2014 at 6:48
  • But i want to compile my module with standard kernel
    – gangadhars
    Apr 22, 2014 at 6:50
  • 1
    @SGG Do you want to test it with standard kernel as well? The thing is that you cannot compile it with standard kernel and run with yours; These versions (compiled/running kernel) should match.
    – UVV
    Apr 22, 2014 at 6:55
  • 2
    The first link above explains this. Apr 22, 2014 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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