Link to home
Start Free TrialLog in
Avatar of jl66
jl66Flag for United States of America

asked on

How to compile linux kernel module

Want to compile a linux kernel module. Start to work on it from a simple hello example, here is error info:
$ make hello
cc     hello.c   -o hello
hello.c:1:60: error: linux/module.h: No such file or directory
hello.c:3:60: error: linux/init.h: No such file or directory
hello.c:5: error: expected =, ,, ;, asm or __attribute__ before hello_start
hello.c:12: error: expected =, ,, ;, asm or __attribute__ before hello_end
hello.c:17: warning: data definition has no type or storage class
hello.c:17: warning: parameter names (without types) in function declaration
hello.c:18: warning: data definition has no type or storage class
hello.c:18: warning: parameter names (without types) in function declaration
make: *** [hello] Error 1
--------------------------------
What 's wrong with it? Looking forwards to hear from experts.
hello.c
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
 
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
 
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
 
module_init(hello_start);
module_exit(hello_end);
 
Makefile:
obj-m = hello.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Open in new window

Avatar of ravenpl
ravenpl
Flag of Poland image

Do You have installed "kernel-devel" package?
yum install kernel-devel

also, it may happen, that the running kernel and kernel-devel version differ - boot to proper kernel then, or change the
KVERSION = $(shell uname -r)
to
KVERSION = 2.6.27.19-170.2.35.fc10.i686 # change to kernel-devel package version
Avatar of jl66

ASKER

I installed the kernel-devel package, whose version is different from the fedora 10 binary version. Could you please elaborate your solution in detail?
In which file can I change the KVERSION?

> In which file can I change the KVERSION?
In the makefile You provided.
say
# rpm -q kernel-devel # says
kernel-devel-2.6.27.19-170.2.35.fc10.i686
then edit the Makefile and change the KVERSION. Of course module compiled for kernel other than running will not load to this kernel.
Avatar of jl66

ASKER

I copied the Makefile. Do you think it makes sense in Fedora 10? If not, do you have a good example for it?
Avatar of jl66

ASKER

It does not seem working:
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i686
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
[jeff@localhost demo]$ make hello
cc     hello.c   -o hello
hello.c:1:60: error: linux/module.h: No such file or directory
hello.c:3:60: error: linux/init.h: No such file or directory
hello.c:5: error: expected =, ,, ;, asm or __attribute__ before hello_start
hello.c:12: error: expected =, ,, ;, asm or __attribute__ before hello_end
hello.c:17: warning: data definition has no type or storage class
hello.c:17: warning: parameter names (without types) in function declaration
hello.c:18: warning: data definition has no type or storage class
hello.c:18: warning: parameter names (without types) in function declaration
make: *** [hello] Error 1

What else can I try?
try issuing just
make
Avatar of jl66

ASKER

Still can't make it.
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i686
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
[jeff@localhost demo]$ make
make: Nothing to be done for `all'.
I suppose You are lacking the <tab> char before make in the Makefile, use the attached one.

Makefile
Avatar of jl66

ASKER

I used your makefile to make hello module, but it still failed.
[jeff@localhost demo]$ make
make -C /lib/modules/2.6.27.19-170.2.35.fc10.i686/build M=/home/jeff/demo modules
make: *** /lib/modules/2.6.27.19-170.2.35.fc10.i686/build: No such file or directory.  Stop.
make: *** [all] Error 2

I checked I could not find the directory /lib/modules/2.6.27.19-170.2.35.fc10.i686. Should we have to make a link? BTW, did you test the Makefile in your system? How did it go?
SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jl66

ASKER

I downloaded the source code linux-2.6.29.tar.bz2. Could you point me to a link on how to load the new source in or list the steps I can follow for it? I found so many links, but lack knowledge on how to select it.
Why You downloaded those sources.
You better give me output from those two commands
rpm -qa | grep ^kernel
uname -r
Avatar of jl66

ASKER

Here they are:

[jeff@localhost ~]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i686
kernel-devel-2.6.27.19-170.2.35.fc10.i686
kerneloops-0.12-1.fc10.i386
kernel-firmware-2.6.27.5-117.fc10.noarch
kernel-headers-2.6.27.19-170.2.35.fc10.i386

[jeff@localhost ~]$ uname -r
2.6.27.5-117.fc10.i686
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jl66

ASKER

After issuing that command and rebooting the server, re-issue the commands:

[jeff@localhost Documents]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i686
kernel-devel-2.6.27.19-170.2.35.fc10.i686
kerneloops-0.12-1.fc10.i386
kernel-devel-2.6.27.21-170.2.56.fc10.i686
kernel-headers-2.6.27.19-170.2.35.fc10.i386
kernel-2.6.27.21-170.2.56.fc10.i686
kernel-firmware-2.6.27.21-170.2.56.fc10.noarch

[jeff@localhost Documents]$ uname -r
2.6.27.21-170.2.56.fc10.i686
This is very strange, have You blocked the kernel updates in yum.conf?
Tyr installing: yum install kernel-2.6.27.19-170.2.35.fc10.i686
Avatar of jl66

ASKER

[root@localhost ~]# yum install kernel-2.6.27.19-170.2.35.fc10.i686
Loaded plugins: refresh-packagekit
updates                                                  | 2.3 kB     00:00    
fedora                                                   | 2.8 kB     00:00    
Setting up Install Process
Parsing package install arguments
No package kernel-2.6.27.19-170.2.35.fc10.i686 available.
Nothing to do
----------------------
How do you think?
Avatar of jl66

ASKER

I did not do anything on blocking the config. file. Can you tell me the commands I can check?
Sure, there's already newer one
Please download
ftp://download.fedora.redhat.com/pub/fedora/linux/updates/10/i386/kernel-2.6.27.21-170.2.56.fc10.i686.rpm
ftp://download.fedora.redhat.com/pub/fedora/linux/updates/10/i386/kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
install them with
yum localinstall kernel-2.6.27.21-170.2.56.fc10.i686.rpm kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
reboot
change the Makefile line back to
KVERSION = $(shell uname -r)

and build the module - it worked for me.
Avatar of jl66

ASKER

[root@localhost Download]# yum localinstall kernel-2.6.27.21-170.2.56.fc10.i686.rpm kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
Loaded plugins: refresh-packagekit
Setting up Local Package Process
Examining kernel-2.6.27.21-170.2.56.fc10.i686.rpm: kernel-2.6.27.21-170.2.56.fc10.i686
Marking kernel-2.6.27.21-170.2.56.fc10.i686.rpm as an update to kernel-2.6.27.5-117.fc10.i686
kernel-2.6.27.21-170.2.56.fc10.i686.rpm: does not update installed package.
Examining kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm: kernel-devel-2.6.27.21-170.2.56.fc10.i686
Marking kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm as an update to kernel-devel-2.6.27.19-170.2.35.fc10.i686
kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm: does not update installed package.
Resolving Dependencies
--> Running transaction check
---> Package kernel-devel.i686 0:2.6.27.21-170.2.56.fc10 set to be installed
---> Package kernel.i686 0:2.6.27.21-170.2.56.fc10 set to be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package
   Arch Version                   Repository                               Size
================================================================================
Installing:
 kernel
   i686 2.6.27.21-170.2.56.fc10   kernel-2.6.27.21-170.2.56.fc10.i686.rpm  50 M
 kernel-devel
   i686 2.6.27.21-170.2.56.fc10   kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
                                                                           18 M

Transaction Summary
================================================================================
Install      2 Package(s)        
Update       0 Package(s)        
Remove       0 Package(s)        

Total download size: 68 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test


Transaction Check Error:
  package kernel-2.6.27.21-170.2.56.fc10.i686 is already installed
  package kernel-devel-2.6.27.21-170.2.56.fc10.i686 is already installed

Error Summary
-------------
what about make now (changing the KVERSION line) ?