Just read the "LINUX device Driver" written by O ' REILLY. The author has repeatedly emphasized that the kernel tree must be established when writing drivers. The previous kernel only needs a set of kernel header files, but because 2.6 of the kernel module calls the kernel source tree of the target file connection, in this way, can get a more robust module loader, but also need these target files exist in the kernel directory tree. The so-called kernel tree, my understanding and online information said the same is a logical form of kernel source code. How do you build it? For this, the Internet "to" get up and the result is "fiasco and return."
It took a day and 4 hours (including the time to eat and sleep, of course), and even a simple hello Wrold didn't come true. (The simplest and most useless driver of the P22 page in the book)
But Kung fu is not a conscientious. Finally figured out what's going on today. Please let me take a slow path.
Check the kernel version used by your OS first
[Email protected]:~$ uname-r
2.6.22-14-generic
If the system is installed, the source code is installed automatically. In the/USR/SRC directory there is a corresponding version of the directory to use. For example (I am under my own)
[Email protected]:/usr/src$ ls
Linux-headers-2.6.22-14
Linux-headers-2.6.22-14-generic
linux-source-2.6.22
linux-source-2.6.22.tar.bz2
[Email protected]:/usr/src$
If there is no source code. (There is no general Ubuntu.)
Check out the source package that you can download (remember not to use this command with Superuser otherwise ...). Will be prompted without this command)
[Email protected]:/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
[Email protected]:/usr/src$
I chose Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install the
[Email protected]:/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 compressed package, decompression can get the entire kernel source code:
Note that you have switched to Super User mode
[Email Protected]:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2
Unzip into a new directory/usr/src/linux-source-2.6.22, all the source code is in this directory.
Enter this directory
Start configuring the kernel select the fastest original configuration (default) mode (I am)
[Email protected]:/usr/src/linux-source-2.6.22# make Oldconfig
Of course you can also use your preferred 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 a long time to be here for 11 hours. (Make sure the space is enough for me to compile and use 1.8G) I partition time to give/directory 30G space, I do not encounter this problem. It was my friend who met.
[Email protected]:/usr/src/linux-source-2.6.22$ make
[Email protected]:/usr/src/linux-source-2.6.22$ make bzimage
Of course, the first make can also be done without execution, directly makes Bzimage. After execution, you can see that a new file has been generated under the current directory: Vmlinux, whose property is-rwxr-xr-x.
And then:
[Email protected]:/usr/src/linux-source-2.6.22#make modules
[Email Protected]:/usr/src/linux-source-2.6.22#make Modules_install
After execution finishes, 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 restart the system.
At this point the kernel tree has been established is not very difficult .....
Write one of the simplest and most useless drivers.
I created 2 text files under 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);
I will not explain the procedure ...
Makefile file
Obj-m: = hello.o
Kerneldir: =/lib/modules/2.6.20/build
PWD: = $ (shell pwd)
Modules
$ (make)-C $ (Kerneldir) m=$ (PWD) modules
Modules_install:
$ (make)-C $ (Kerneldir) m=$ (PWD) Modules_install
If you have completed the above error in make
[Email protected]:~/linux_ driver Development $ make
Make: There's nothing you can do for ' modules '.
The reason is very simple you must be from my this direct copy of it ~ ~ ~ Oh, makefile format is wrong ~
The solution is for you to move the Modules_install (make)-C $ (Kerneldir) m=$ (PWD) to the beginning of the line and then press the TAB key to automatically align
Then the variables inside are turned green ~ then make it
[Email protected]:~/linux_ driver 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_ Driver Development/hello.o
Building modules, Stage 2.
Modpost 1 Modules
Cc/home/shana/linux_ Drive Development/HELLO.MOD.O
LD [M]/home/shana/linux_ Driver Development/hello.ko
MAKE[1]: Leaving directory '/usr/src/linux-headers-2.6.22-14-generic '
[Email protected]:~/linux_ Driver Development $
[Email protected]:~/linux_ driver 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
[Email protected]:~/linux_ Driver Development $
Then load the module (super User)
[Email protected]:/home/shana/linux_ Driver Development # Insmod./hello.ko
According to the example in the book will show Hello at the terminal, world but nothing appears after running (reason puzzled)
[Email protected]:/home/shana/linux_ Driver Development # Insmod./hello.ko
[Email protected]:/home/shana/linux_ Driver Development #
View load Modules
[Email protected]:/home/shana/linux_ Driver Development # Lsmod
Module Size used by
Hello 2560 0
Already loaded on the ~ ~
Deleting a module
[Email protected]:/home/shana/linux_ Driver Development # Rmmod Hello
[Email protected]:/home/shana/linux_ Driver Development #
What about the output of that program? The book says that if it does not appear in the terminal it will be written into the syslog file
[Email protected]:~/linux_ driver 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
[Email protected]:~/linux_ Driver Development $
Build Linux kernel tree-based on Ubuntu system