Solved

How to compile linux kernel module

Posted on 2009-03-30
21
2,358 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
  • 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
 

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 500 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Creating a Samba server for a small office. Ubuntu Linux and Samba can breathe new life into a retired PC and save an office money on new hardware/software. Our example server will have two hard disks, one exclusively for storing shared data. …
If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now