Linux driver development environment configuration [reprinted]

Source: Internet
Author: User
Tags syslog

**************************************** *****************

Note:

1. Compile the driver first;

2. The driver makefile must be correctly specified: kerneldir: =/lib/modules/version of the compiled kernel/build

3. The compiled kernel should be installed into the OS;

4. the OS Kernel running when the driver is loaded must be the same as the kernel version of the compiled driver;

5. The printk output of the driver may be in/var/log/syslog.

(Today, the first hello World Driver in my life is finally realized)

**************************************** ****************

 

From: http://hi.baidu.com/hzxing1010/blog/item/efb6bf95c7ab756655fb96bb.html

I just read the Linux Device Driver written by o'reilly.Program. 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 ~
Then 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 compressed package, extract can get the entire KernelSource code:

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 for all the sourcesCodeAll in this directory.

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/will be generated under/lib/modules/.

 

**************************************** ************************************

There seems to be a problem with the above several red letters,《Compile the 3.0.4 kernel in Ubuntu"This articleArticle【Http://edsionte.com/techblog/archives/3289]

The procedure is as follows:

4. Installation

The installation process is divided into two parts. First, install the kernel module. This process copies the kernel module generated when the kernel module is compiled to the/lib/modules/3.0.4/directory, 3.0.4 indicates the corresponding kernel version. The command used is as follows:
~ Linux-3.0.4 $ sudo make modules_install
Run the following command to install the compiled kernel:
~ Linux-3.0.4 $ sudo make install

The kernel installation process mainly completes the following tasks:
1). Copy the kernel image bzimage generated during kernel compilation to the/boot directory, and name this image as a vmlinuz-3.0.4.

If you use an x86 CPU, the image is located in the arch/x86/boot/directory (under the kernel source code being compiled ).

2). Convert ~ Copy System. Map under the/linux-3.0.4/directory to the/boot/directory and rename it system. Map-3.0.4. This file contains the symbol table of the kernel.
3). Change ~ The. config under the/linux-3.0.4/directory is copied to the/boot/directory and renamed as a config-3.0.4.

5. Create the initrd. imgfile
Initrd. IMG is the initial ramdisk file. It is an image file that packs some basic drivers and command tools into the image file. This image file is used to release the initrd file to the memory before the system has mounted the root partition, as a virtual root partition, execute related scripts and run the insmod command to load the required modules.

The specific creation method is as follows:
~ /Linux-3.0.4 $ sudo mkinitramfs 3.0.4-O/boot/initrd. img-3.0.4
6. Update grub
The last step is to update the grub Startup menu. You can use the following command to automatically update the Startup Menu:
Sudo update-grub2This step does not seem to need to be doneMake install will automatically complete

In this way, the compiled kernel is placed first in the Startup menu. If you need to modify the default system startup sequence in the Startup menu, modify/boot/GRUB/grub. set default = In the cfg file.

 

Another step is very important: Modify the [timeout] item of/boot/GRUB/grub. cfg and specify the wait time. Otherwise, only one kernel is available for you to choose from and the previous kernel cannot be started.

[] "Grub. cfg" indicates the number of grub. cfg entries in the preceding example. For more information about the important documents, refer to the important documents..

<Http://www.ychong.com/ubuntu-boot-grub-config/> (set GRUB boot item order and timeout in Ubuntu)

**************************************** ************************************

 

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 very 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 <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 program ......

Makefile

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 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, the variables inside are all green ~ Then go to 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 $

Shana @ Shana :~ /Linux _ driver development $ LS-l
Total usage 124
-RW-r -- 1 Shana 303 2008-03-16 hello. c
-RW-r -- 1 Shana 49039 hello. Ko
-RW-r -- 1 Shana 687 hello. Mod. c
-RW-r -- 1 Shana 25840 hello. Mod. o
-RW-r -- 1 Shana 24360 hello. o
-RW-r -- 1 Shana 8344 2008-03-16 linux_qudong_qu.txt
-RW-r -- 1 Shana 266 makefile
-RW-r -- 1 Shana 0 2008-03-16 12:11 module. symvers
Shana @ Shana :~ /Linux _ driver development $

Then load the module (Super User)

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.[] When too many syslog records are generated, the output is too large.

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?

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.