LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compiling kernel modules (https://www.linuxquestions.org/questions/programming-9/compiling-kernel-modules-386537/)

axr0284 11-25-2005 05:52 PM

compiling kernel modules
 
Hi,
I have been trying to compile and insert a simple kernel module but without luck. This is what I did.
Since the freshly installed debian sarge 3.1 distro did not have any source files under /usr/src, I di uname -a to make sure of the kernel version that is installed:
Linux test 2.4.27-2-386 #1 Mon May 16 16:47:51 JST 2005 i686 GNU/Linux

and then I downloaded the kernel-source-2.4.27.tar.bz2, unziped and untarred it. I then copied this program from a book into example.c:
Code:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
static char __initdata hellomessage[] = KERN_NOTICE "Hello, world!\n";
static char __exitdata byemessage[] = KERN_NOTICE "Goodbye, cruel world.\n";
static int __init start_hello_world(void)
{
  printk(hellomessage);
  return 0;
}
static void __exit go_away(void)
{
  printk(byemessage);
}
module_init(start_hello_world);
module_exit(go_away);

I then compiled it using
gcc -DMODULE -D__KERNEL__ -I/usr/src/kernel-source-2.4.27/include -c example.c


I tried inserting it into the kernel using
/sbin/insmod example.o

but this is the message I got back:

example.o: kernel-module version mismatch
example.o was compiled for kernel version 2.6.0
while this kernel is version 2.4.27-2-386.
example.o: cannot create /var/log/ksymoops/20051125172050.ksyms Permission denied

I don't understand how it could have been compiled for a version of the kernel that I did not use. Thanks in advance.
Amish

goldennuggets 11-26-2005 10:17 AM

just a thought, perhaps you a running the 2.6 kernel. and when you attempt to compile this kernel module, it looks into your existing and running kernel headers?

axr0284 11-26-2005 03:08 PM

Hi,
Well Almut Behrens on the debian mailing list was good enough to enlighten me. Here is his answer. Hopefully it will be helpful to someone else:

If you want to build kernel modules, you need to use the kernel headers
> _as configured for your current kernel_. The generic header files which
> come with the original kernel sources won't work...
>
> For a stock debian kernel such as 2.4.27-2-386, it's probably easiest
> to just install the respective packages
>
> * kernel-headers-2.4.27-2-386 (or kernel-headers-2.4-386 for that
> matter, which depends on kernel-headers-2.4.27-2-386), and
>
> * kernel-headers-2.4.27-2 (containing the header files common to all
> architectures, referenced via symlinks from within the -386 package).
>
> Then set your include path
> to -I/usr/src/kernel-headers-2.4.27-2-386/include.
>
> I'm not entirely sure how you got that 2.6.0 version into your module,
> but I guess the following happened: as there's no "version.h" in the
> unconfigured kernel sources, the file /usr/include/linux/version.h
> probably got pulled in instead (because it's on the standard include
> path)... However, these include files (though they're kernel headers,
> too) belong to libc, and must not necessarily match the current kernel
> version (in fact, I believe those in sarge are version 2.6.0 -- btw,
> this is the package linux-kernel-headers).
>
> If you're interested in what went wrong in your original attempt, you
> could run just the preprocessor (-E), and grep for version.h in its output
>
> gcc -DMODULE -D__KERNEL__ -I/usr/src/kernel-source-2.4.27/include -E
> example.c | grep version.h
>
> I'd think you see something like "# 1 "/usr/include/linux/version.h" 1
> 3"...
>
> Cheers,
> Almut


All times are GMT -5. The time now is 09:56 PM.