Ubuntu2.6.22 kernel tree Creation

Source: Internet
Author: User
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. Just look at O 'Reilly written "LINUX Device Driver" [Download In http://www.linuxidc.net/thread-2029-1-1.htm

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.

Just read O 'Reilly's LINUX Device Driver [downloaded at http://www.linuxidc.net/thread-2029-1-1.html. 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, it took another four hours a day (including the time for eating and sleeping, of course), not even a simple hello wrold. (P22 pages in the book are the simplest and most useless driver examples)

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.

Linuxidc@www.linuxidc.com :~ $ Uname -R
2.6.22- 14-Generic /*This is my display result */

If the source code is automatically installed during system installation. There is a corresponding version directory under the/usr/src directory. Example)

Linuxidc @Www.linuxidc.com: /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 code directory. */
Linux-source-2.6.22.tar.bz2 /*This is my source code package. */
Linuxidc @Www.linuxidc.com: /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 ...... Will prompt that this command is not available )(PS: No problem now. jarson 2011.06.13 note)

Linuxidc @Www.linuxidc.com: /Usr /Src $ Apt-cache searchLinux-source
Linux-source-Linux kernel SourceWith Ubuntu patches
Xen-source-2.6.16-Linux kernel Source ForVersion 2.6.17 with Ubuntu patches
Linux-source-2.6.22-Linux kernel Source ForVersion 2.6.22 with Ubuntu patches
Linuxidc @Www.linuxidc.com: /Usr /Src $

I chose linux-source-2.6.22-Linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install

Linuxidc@www.linuxidc.com:/usr/src $ Sudo Apt-get installLinux-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, extract can both get the source code of the entire kernel:

Note that the superuser mode has been switched.

Root@www.linuxidc.com:/usr/src # TarJxvf 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@www.linuxidc.com:/usr/src/linux-source-2.6.22 # MakeOldconfig

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.

Linuxidc @Www.linuxidc.com: /Usr /Src /Linux-source-2.6.22 $ Make
Linuxidc @Www.linuxidc.com: /Usr /Src /Linux-source-2.6.22 $ MakeBzImage

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 @Www.linuxidc.com: /Usr /Src /Linux-source-2.6.22 # Make modules/* Compilation module */
Root @Www.linuxidc.com: /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/will be generated under/lib/modules/
. The build directory (PS: build is actually a symbolic link file, which is linked to the corresponding kernel header file directory under the/usr/src directory jarson 2011.06.13 note). So far, the kernel compilation is complete. You can restart the system.

So far, it is not very difficult to establish the kernel tree .....
========================================================== ========
Write the simplest and most useless driver.
I created two text files hello. c and Makefile in the/home/shana/linux_q/directory.

// Hello. c
# Include
# Include
MODULE_LICENSE ( "Dual BSD/GPL" ) ;

Static IntHello_init ( Void )
{
Printk (KERN_ALERT "Hello, world\ N" ) ;
Return 0 ;
}

Static VoidHello_exit ( Void )
{
Printk (KERN_ALERT "Goodbye, cruel world\ N" ) ;
}

Module_init (Hello_init ) ;
Module_exit (Hello_exit ) ;

I will not explain the program ......

Makefile

Obj-m: = hello. o
KERNELDIR: =/lib/modules/2.6.20/Build (PS: 2.6.20 here depends on the actual situation. jarson 2011.06.13 note)
PWD: = $ (shell pwd)

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.

Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $ Make
Make: nothing can be done `Modules '.

The reason is very simple. You must have copied it from me ~~~ Haha,Incorrect Makefile formatLa ~
The solution is to move $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install to the first line (PS: If it is the first line alignment, you do not have to move it. ^_^ other target rules are also adjusted accordingly. jarson 1.01.06.13 note), And then press the Tab key to automatically align
At this time, the variables inside are all green ~ Then go to make.

Linuxidc @Www.linuxidc.com :~ /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'

Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $

Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $ Ls -L
Total usage 124
-Rw-r -- 1Shana 303 2008-03- 16 10: 43Hello. c
-Rw-r -- 1Shana 49039 2008-03- 16 12: 11Hello. ko
-Rw-r -- 1Shana 687 2008-03- 16 12: 11Hello. mod. c
-Rw-r -- 1Shana 25840 2008-03- 16 12: 11Hello. mod. o
-Rw-r -- 1Shana 24360 2008-03- 16 12: 11Hello. o
-Rw-r -- 1Shana 8344 2008-03- 1609: 17Linux_qudong_qu.txt
-Rw-r -- 1Shana 266 2008-03- 16 12: 09 Makefile
-Rw-r -- 1Shana 0 2008-03- 16 12: 11Module. symvers
Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $

Then load the module (Super User)

Root @Www.linuxidc.com: /Home /Shana /Linux _ driver development # Insmod./hello. ko

According to the examples in the book, hello, world will be displayed on the terminal, but nothing will appear after the operation (the reason is puzzled )(PS: Later I have explained jarson 2011.06.13 note)

Root @Www.linuxidc.com: /Home /Shana /Linux _ driver development # Insmod./hello. ko
Root @Www.linuxidc.com: /Home /Shana /Linux _ driver development #

View the load Module

Root @Www.linuxidc.com: /Home /Shana /Linux _ driver development # Lsmod
Module Size Used
Hello 2560 0

Already loaded ~~

Delete A Module

Root @Www.linuxidc.com: /Home /Shana /Linux _ driver development # Rmmod hello
Root @Www.linuxidc.com: /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.

Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $ Cat /Var /Log /Syslog | GrepWorld
Mar 16 12: 14: 53Shana kernel: [ 5937.529297 ]Hello, world
Mar 16 12: 16: 05 shana kernel: [ 6009.439036 ]Goodbye, cruel world
Linuxidc @Www.linuxidc.com :~ /Linux _ driver development $

Now all the work has been completed. Is it useful to you?

Added:

Mxd271966940:

Printk is the kernel calling interface. It writes messages to the console before the system init, but once the system init is completed, it is rewritten to the system log.

The advantage of doing so is self-evident. If the kernel can write information to the console at any time, the console will be overwhelmed by such information and cannot work. The implementation of printk is very simple, that is, after a log message is received, first apply for the console semaphore. If the application is received, call the console write method and write it to the console. When linux Initialization is complete, it will open the console before init. After that, printk will not be able to apply for a semaphore, and the message is organized as skb and sent to a specific netlink buffer, which is read by klogd, write to the/var/log/messages file.

Yoafer:

The reason why messages cannot be output on the terminal can be viewed here:

The original Article "Linux Device Drivers" ["Linux Device Drivers" PDF version is downloaded at http://www.linuxidc.com/linux/2011-06/37778.htm.pdf ]:

According to the mechanic your system uses to deliver the message lines, your out-
Put may be different. In participates, the previous screen dump was taken from a text
Console; if you are running insmod and rmmod from a terminal emulator running
Under the window system, you won't see anything on your screen. The message goes
To one of the system log files, such as/var/log/messages.

Gxustudent:

Thanks to the landlord, I have compiled this method successfully, but note the following:
1. The article has been updated for a long time in some places;
2. It is said that in kernel compilation of version 2.6, make includes the make bzImage and make modules processes. Therefore, the kernel compilation steps should be:
1.1. make oldconfig
1.2. make
1.3. make modules_install
1.4. make install
3. after compilation, the source code directory occupies too much space. I don't know if there is a problem with my process. The size of my source code file reaches 6 GB, in this case, you can use make mrproper to delete some space.
4. When creating a makefile file, the file header should be modified according to its own version and Path:
For example, my path is KERNELDIR: =/lib/modules/2.6.32-24-generic/build
Finally, thank you!

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.