jl66
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.
$ 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
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 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.
In the makefile You provided.
say
# rpm -q kernel-devel # says
kernel-devel-2.6.27.19-170
then edit the Makefile and change the KVERSION. Of course module compiled for kernel other than running will not load to this kernel.
ASKER
I copied the Makefile. Do you think it makes sense in Fedora 10? If not, do you have a good example for it?
ASKER
It does not seem working:
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i6 86
all:
make -C /lib/modules/$(KVERSION)/b uild M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/b uild 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?
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i6
all:
make -C /lib/modules/$(KVERSION)/b
clean:
make -C /lib/modules/$(KVERSION)/b
[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
make
ASKER
Still can't make it.
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i6 86
all:
make -C /lib/modules/$(KVERSION)/b uild M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/b uild M=$(PWD) clean
[jeff@localhost demo]$ make
make: Nothing to be done for `all'.
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i6
all:
make -C /lib/modules/$(KVERSION)/b
clean:
make -C /lib/modules/$(KVERSION)/b
[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
Makefile
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/buil d M=/home/jeff/demo modules
make: *** /lib/modules/2.6.27.19-170 .2.35.fc10 .i686/buil d: 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?
[jeff@localhost demo]$ make
make -C /lib/modules/2.6.27.19-170
make: *** /lib/modules/2.6.27.19-170
make: *** [all] Error 2
I checked I could not find the directory /lib/modules/2.6.27.19-170
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
You better give me output from those two commands
rpm -qa | grep ^kernel
uname -r
ASKER
Here they are:
[jeff@localhost ~]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i 686
kernel-devel-2.6.27.19-170 .2.35.fc10 .i686
kerneloops-0.12-1.fc10.i38 6
kernel-firmware-2.6.27.5-1 17.fc10.no arch
kernel-headers-2.6.27.19-1 70.2.35.fc 10.i386
[jeff@localhost ~]$ uname -r
2.6.27.5-117.fc10.i686
[jeff@localhost ~]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i
kernel-devel-2.6.27.19-170
kerneloops-0.12-1.fc10.i38
kernel-firmware-2.6.27.5-1
kernel-headers-2.6.27.19-1
[jeff@localhost ~]$ uname -r
2.6.27.5-117.fc10.i686
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.i 686
kernel-devel-2.6.27.19-170 .2.35.fc10 .i686
kerneloops-0.12-1.fc10.i38 6
kernel-devel-2.6.27.21-170 .2.56.fc10 .i686
kernel-headers-2.6.27.19-1 70.2.35.fc 10.i386
kernel-2.6.27.21-170.2.56. fc10.i686
kernel-firmware-2.6.27.21- 170.2.56.f c10.noarch
[jeff@localhost Documents]$ uname -r
2.6.27.21-170.2.56.fc10.i6 86
[jeff@localhost Documents]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i
kernel-devel-2.6.27.19-170
kerneloops-0.12-1.fc10.i38
kernel-devel-2.6.27.21-170
kernel-headers-2.6.27.19-1
kernel-2.6.27.21-170.2.56.
kernel-firmware-2.6.27.21-
[jeff@localhost Documents]$ uname -r
2.6.27.21-170.2.56.fc10.i6
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
Tyr installing: yum install kernel-2.6.27.19-170.2.35.
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?
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.
Nothing to do
----------------------
How do you think?
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.
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.
reboot
change the Makefile line back to
KVERSION = $(shell uname -r)
and build the module - it worked for me.
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.i 686
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
-------------
Loaded plugins: refresh-packagekit
Setting up Local Package Process
Examining kernel-2.6.27.21-170.2.56.
Marking kernel-2.6.27.21-170.2.56.
kernel-2.6.27.21-170.2.56.
Examining kernel-devel-2.6.27.21-170
Marking kernel-devel-2.6.27.21-170
kernel-devel-2.6.27.21-170
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.
kernel-devel
i686 2.6.27.21-170.2.56.fc10 kernel-devel-2.6.27.21-170
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.
package kernel-devel-2.6.27.21-170
Error Summary
-------------
what about make now (changing the KVERSION line) ?
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.i6