From:http://hi.baidu.com/hzxing1010/blog/item/efb6bf95c7ab756655fb96bb.html
Just looking at O ' REILLY's "LINUX device Driver". The author has repeatedly emphasized the need to build a kernel tree when writing drivers. The so-called kernel tree, my understanding and online data said consistent is the kernel source of a logical form. Then how to build it. For this the internet "sex" up and the result is "fiasco and return."
It took 4 hours a day (including eating and sleeping, of course), not even a simple hello wrold. (The simplest and most useless driver column of a P22 page in a book)
But Kung fu pays off. Finally figured out what's going on today. Please let me take it slowly below.
First look at the kernel version of your OS
shana@shana:~$ Uname-r
2.6.22-14-generic/* This is the result I have shown.
If the system is installed, the source code is automatically installed. A corresponding version directory is available under the/USR/SRC directory. For example (I am under myself)
shana@shana:/usr/src$ ls
Linux-headers-2.6.22-14
Linux-headers-2.6.22-14-generic
linux-source-2.6.22/* This is the source directory after decompression * *
LINUX-SOURCE-2.6.22.TAR.BZ2/* This is my source code package * *
shana@shana:/usr/src$
If there is no source code. (General Ubuntu is not the bar)
Check the download source package (remember not to use Superuser to use this command otherwise ...) Will be prompted without this command)
shana@shana:/usr/src$ Apt-cache Search Linux-source
Linux-source-linux kernel source with Ubuntu patches
Xen-source-2.6.16-linux kernel source for version 2.6.17 with Ubuntu patches
Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches
shana@shana:/usr/src$
I chose Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then the Install
shana@shana:/usr/src$ sudo apt-get install linux-source-2.6.22
After the download is complete, under/USR/SRC, the file name is: linux-source-2.6.22.tar.bz2, is a compression package, decompression can get the entire kernel source code:
Note that you have switched to Superuser mode
Root@shana:/usr/src#tar JXVF linux-source-2.6.20.tar.bz2
Unzip the/usr/src/linux-source-2.6.22 into a new directory, all the source code is in the directory.
Enter the directory
Start configuring the kernel to select the fastest version of the original configuration (default) mode (I am SO)
root@shana:/usr/src/linux-source-2.6.22 # Make Oldconfig
Of course, you can also use your favorite configuration such as Menuconfig, Xconfig (must have GTK environment bar). It doesn't have to be trimmed anyway, so it's OK to configure it in that way.
When you're done, start make. It's usually 11 hours long here. (to ensure that the space is enough for me to compile after the use of 1.8G) I partition time to/directory 30G space, I did not encounter this problem. But my friend met.
shana@shana:/usr/src/linux-source-2.6.22$ make
shana@shana:/usr/src/linux-source-2.6.22$ make Bzimage
Of course, the first make can also be done without execution, directly to makes Bzimage. After execution, you can see that a new file has been generated in the current directory: Vmlinux, whose properties are-rwxr-xr-x.
And then:
Root@shana:/usr/src/linux-source-2.6.22#make Modules/* Compile module * *
Root@shana:/usr/src/linux-source-2.6.22#make modules_install/* Installation Module * *
After execution completes, a new directory is generated under/lib/modules/lib/modules/2.6.22-14-generic/
。 The build directory under this path is used for subsequent compilation of the module file. This completes the kernel compilation. You can reboot the system.
So the kernel tree is built, it is not very difficult ...
Write one of the simplest and most useless drivers.
I created 2 text files in the/home/shana/linux_q/directory hello.c Makefile
hello.c
#include <linux/init.h>
#include <linux/module.h>
Module_license ("Dual BSD/GPL");
static int hello_init (void)
{
PRINTK (Kern_alert "Hello, world/n");
return 0;
}
static void Hello_exit (void)
{
PRINTK (Kern_alert "Goodbye, cruel world/n");
}
Module_init (Hello_init);
Module_exit (Hello_exit);
Program I don't explain ...
Makefile file
Obj-m: = hello.o
Kerneldir: =/lib/modules/2.6.22-14-generic/build
PWD: = $ (Shell PWD)
Modules
$ (make)-C $ (Kerneldir) m=$ (PWD) modules
Modules_install:
$ (make)-C $ (Kerneldir) m=$ (PWD) Modules_install
If all of the above you have completed the error at make
Shana@shana:~/linux _ Drive Development $ make
Make: Nothing can be done for ' modules '.
The reason is very simple you must be from me this direct copy of it ~ ~ ~ Oh, makefile format wrong ~
The solution is for you to move the $ (make)-C $ (Kerneldir) m=$ (PWD) Modules_install to the beginning of the line and then press the TAB key to automatically align
And then the variables inside are green.
Shana@shana:~/linux _ Drive Development $ make
Make-c/lib/modules/2.6.22-14-generic/build m=/home/shana/linux_ Drive development modules
MAKE[1]: Entering directory '/usr/src/linux-headers-2.6.22-14-generic '
CC [M]/home/shana/linux_ Drive development/hello.o
Building modules, Stage 2.
Modpost 1 Modules
Cc/home/shana/linux_ Drive Development/HELLO.MOD.O
LD [M]/home/shana/linux_ Drive development/hello.ko
MAKE[1]: Leaving directory '/usr/src/linux-headers-2.6.22-14-generic '
Shana@shana:~/linux _ Drive Development $
Shana@shana:~/linux _ Drive Development $ ls-l
Total Dosage 124
-rw-r--r--1 Shana Shana 303 2008-03-16 10:43 hello.c
-rw-r--r--1 Shana Shana 49039 2008-03-16 12:11 Hello.ko
-rw-r--r--1 Shana Shana 687 2008-03-16 12:11 HELLO.MOD.C
-rw-r--r--1 Shana Shana 25840 2008-03-16 12:11 HELLO.MOD.O
-rw-r--r--1 Shana Shana 24360 2008-03-16 12:11 hello.o
-rw-r--r--1 Shana Shana 8344 2008-03-16 09:17 linux_qudong_qu.txt
-rw-r--r--1 Shana Shana 266 2008-03-16 12:09 Makefile
-rw-r--r--1 Shana Shana 0 2008-03-16 12:11 module.symvers
Shana@shana:~/linux _ Drive Development $
Then load the module (superuser)
Root@shana:/home/shana/linux _ Drive Development # Insmod./hello.ko
According to the example in the book will show Hello in the terminal, world but after the run nothing appears (reason puzzled)
Root@shana:/home/shana/linux _ Drive Development # Insmod./hello.ko
Root@shana:/home/shana/linux _ Drive Development #
Viewing load Modules
Root@shana:/home/shana/linux _ Drive Development # Lsmod
Module Size Used by
Hello 2560 0
has been loaded on a slightly ~ ~
Delete Module
Root@shana:/home/shana/linux _ Drive Development # Rmmod Hello
Root@shana:/home/shana/linux _ Drive Development #
The output of that program is there. The book indicates that if it does not appear in the terminal, it will be written into the syslog file
Shana@shana:~/linux _ Drive Development $ cat/var/log/syslog |grep World
Mar 12:14:53 Shana Kernel: [5937.529297] Hello, world
Mar 12:16:05 Shana Kernel: [6009.439036] Goodbye, Cruel world
Shana@shana:~/linux _ Drive Development $
All the work has been done. Is it useful to you.