?
Solved

How to compile linux kernel module

Posted on 2009-03-30
21
Medium Priority
?
2,387 Views
Last Modified: 2013-12-06
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

0
Comment
Question by:jl66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 24031292
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
0
 

Author Comment

by:jl66
ID: 24032033
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?

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24032411
> 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.
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:jl66
ID: 24033618
I copied the Makefile. Do you think it makes sense in Fedora 10? If not, do you have a good example for it?
0
 

Author Comment

by:jl66
ID: 24035479
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?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24036197
try issuing just
make
0
 

Author Comment

by:jl66
ID: 24046854
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'.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24047081
I suppose You are lacking the <tab> char before make in the Makefile, use the attached one.

Makefile
0
 

Author Comment

by:jl66
ID: 24056861
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?
0
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 2000 total points
ID: 24056934
And what directories under /lib/modules/ You can find? The KVERSION variable in the Makefile should point to one that contains valid 'build' symlink.
It is, it should contain the kernel version which jas the kernel-devel package installed.
You can try the original $(shell uname -r) which means try compile the module for currently running kernel.
0
 

Author Comment

by:jl66
ID: 24063501
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.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24063522
Why You downloaded those sources.
You better give me output from those two commands
rpm -qa | grep ^kernel
uname -r
0
 

Author Comment

by:jl66
ID: 24067981
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
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 24068018
It looks like installing kernel-devel-2.6.27.19-170.2.35.fc10.i686 hasn't installed the kernel-2.6.27.19-170.2.35.fc10.i686 itself.
Please run first
yum update kernel kernel-devel

I also noticed, that You running 2.6.27.5-117.fc10.i686 - the build module will not load into that kernel.
To make it happen, reboot after kernel-2.6.27.19-170.2.35.fc10.i686 is installed.
0
 

Author Comment

by:jl66
ID: 24068333
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
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24068345
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
0
 

Author Comment

by:jl66
ID: 24068544
[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?
0
 

Author Comment

by:jl66
ID: 24068551
I did not do anything on blocking the config. file. Can you tell me the commands I can check?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24068586
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.
0
 

Author Comment

by:jl66
ID: 24068660
[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
-------------
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24068668
what about make now (changing the KVERSION line) ?
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order for businesses to be compliant with certain information security laws in some countries, you need to be able to prove that a user (which user it was becomes important to the business to take action against the user after an event has occurr…
Users are often faced with high disk consumption without really knowing where the largest amount of data resides. Disk Usage Analyzer (aka Baobab) is is a graphical, menu-driven application to analyse disk usage in any Gnome environment and can e…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question