Ubuntu2.6.22 kernel tree establishment-general Linux technology-Linux programming and kernel information. The following is a detailed description. Author: Fengyun
This article is based on multiple posts on the Internet, but it is not original. The only thing that is gratifying is the implementation on the local machine. Because it failed several times after all. It is also quite difficult for me to learn many simple problems in driver programming. Don't laugh. Finally, I hope this article will provide some help to those who have just learned to drive and haven't started yet.
I just read O 'Reilly's "LINUX Device Driver. The author has repeatedly stressed that the kernel tree must be established when writing drivers. The so-called kernel tree is a logical form of kernel source code. How can we establish it? For this reason, the Internet was "overwhelming" and the result was "a fiasco".
For this reason, after four hours a day (including the time for eating and sleeping, of course), a simple hello wrold is not implemented. (P22 pages in the book are the simplest and most useless driving event columns ). However, it is hard to be honest. Today, I finally figured out what was going on. Let me take it easy.
First, check the kernel version used by your OS.
Shana @ shana :~ $ Uname-r
2.6.22-14-generic/* this is the result of my display */
If the source code is automatically installed during system installation. There is a corresponding version directory under the/usr/src directory. Example)
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 decompressed source directory */
Linux-source-2.6.22.tar.bz2/* this is my source package */
Shana @ shana:/usr/src $
If no source code is available. (Not ubuntu)
Check the downloadable source code package (remember not to use this command by a Super User, otherwise ...... This command is not displayed)
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
After the download is complete, under/usr/src, the file name is: linux-source-2.6.22.tar.bz2, is a compressed package, extract can both get the source code of the entire kernel:
Note that the superuser mode has been switched.
Root @ shana:/usr/src # tar jxvf linux-source-2.6.20.tar.bz2
Decompress the package and generate a new directory/usr/src/linux-source-2.6.22 where all source code is stored.
Enter this directory
Start to configure the kernel and select the fastest original configuration (default) method (I do)
Root @ shana:/usr/src/linux-source-2.6.22 # make oldconfig
Of course, you can also use your favorite configuration methods such as menuconfig and xconfig (you must have a GTK environment ). You don't need to crop anything, so you can configure it in any way.
After that, start to make it. Generally, it takes an hour. (Ensure that the space is sufficient. I used 1.8 GB after compilation.) I gave the/directory 30 GB space for the partition. I did not encounter this problem. My friend met me.
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 statement can also be executed without making bzImage directly. After the execution is complete, a new file named vmlinux is generated in the current directory. Its attribute is-rwxr-xr-x.
Then:
Root @ shana:/usr/src/linux-source-2.6.22 # make modules/* Compilation module */
Root @ shana:/usr/src/linux-source-2.6.22 # make modules_install/* installation module */
After the execution, a new directory/lib/modules/2.6.22-14-generic/is generated under/lib/modules /. The build directory under this path will be used for subsequent compilation of module files. So far, the kernel compilation is complete. You can restart the system.
So far, it is not difficult to establish the kernel tree.
Write the simplest and most useless driver.
I created two text files hello. c Makefile in the/home/shana/linux_q/directory.
// Hello. c
# Include
# Include
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 ");
}
Modules:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Modules_install:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install
If all of the above are complete, this error will occur during make.
Shana @ shana :~ /Linux _ driver development $ make
Make: There is nothing to do for 'modules '.
The reason is very simple. You must have copied it from me. The Makefile format is incorrect.
The solution is to move $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install to the beginning of the line, and then press the Tab key to automatically align
At this time, all the variables in it are green, and then in make
Shana @ shana :~ /Linux _ driver development $ make
Make-C/lib/modules/2.6.22-14-generic/build M =/home/shana/linux _ driver 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 _ driver 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'
Shana @ shana :~ /Linux _ driver development $
Root @ shana:/home/shana/linux _ driver development # insmod./hello. ko
According to the example in the book, hello, world will be displayed on the terminal, but nothing will appear after the operation (the reason is puzzled)
Root @ shana:/home/shana/linux _ driver development # insmod./hello. ko
Root @ shana:/home/shana/linux _ driver development #
View the load Module
Root @ shana:/home/shana/linux _ driver development # lsmod
Module Size Used
Hello 2560 0
Already loaded
Delete A Module
Root @ shana:/home/shana/linux _ driver development # rmmod hello
Root @ shana:/home/shana/linux _ driver development #
What is the output of the program? It indicates that if the terminal does not exist, it will be written into the syslog file.
Shana @ shana :~ /Linux _ driver development $ cat/var/log/syslog | grep world
Mar 16 12:14:53 shana kernel: [5937.529297] Hello, world
Mar 16 12:16:05 shana kernel: [6009.439036] Goodbye, cruel world
Shana @ shana :~ /Linux _ driver development $
Now all the work has been completed. Is it useful to you?
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.