Http://tech.ccidnet.com/art/741/20070720/1151003_1.html
Author: Sixth
This article is based on the 2.6 kernel. We also recommend that you take a look at "Linux kernel design and implementation (version 2)" as a basic knowledge. Of course, from a practical point of view, as long as you follow the steps below, you should be able to successfully compile the kernel and load the module.
For Linux: Debian GNU/Linux and 2.6.20-1-686.
Step 1: Download the source code of the Linux kernel, that is, build the kernel tree mentioned above in ldd3 (Linux Device Drivers 3rd.
If the source code is already included in the installed Linux system, it should be in the/usr/src directory. If the directory is empty, you need to manually download the source code. There are a lot of methods and links for downloading code, or can it be done on the Cu via http://download.chinaunix.net/search? Key = & Q = Kernel & frmid = 53. However, the downloaded kernel version should be the same as the kernel version of the running Linux system. Of course, it can also be lower than the Linux kernel version, but it should not work if it is higher (I have not practiced it ).
Debian can be easily downloaded from the Debian Source:
First, find the downloadable kernel source code:
# Apt-cache search Linux-Source
The display is: linux-source-2.6.20, not exactly match with my kernel version, but it does not matter, direct download can be:
# Apt-Get install linux-source-2.6.20
After the download is complete, install in/usr/src, the file name is: linux-source-2.6.20.tar.bz2, is a compressed package, extract can get the source code of the entire kernel:
# Tar jxvf linux-source-2.6.20.tar.bz2
Decompress the package and generate a new directory/usr/src/Linux -- source-2.6.20. All source code is in this directory.
Note: This directory varies with the kernel version. You only need to know the specific location of your source code.
Step 2: Configure and compile the kernel.
Go to the/usr/src/Linux -- source-2.6.20 directory, and you can see the MAKEFILE file, which contains the entire kernel tree compilation information. The top four lines of this file are about the kernel version. You do not need to modify the entire makefile. You can use the default value.
Generally, you need to configure the kernel by using commands such as "make menuconfig", "Make xconfig", or "make oldcofig". These commands are used to configure the kernel, but they run in different environments. execute any of these commands to configure the kernel:
Make menuconfig is an interface-Based Kernel configuration method. Make xconfig should be based on the qt library, and make gcofig is also a graphical configuration method. It should be a GTK environment, make oldcofig is the original kernel tree. configure the config file.
In fact, the Kernel configuration is mainly to ensure that the kernel startup module can dynamically load the configuration. The default configuration should already contain such content. Therefore, I use make oldconfig.
Run the following command in the kernel source code directory:
# Make
# Make bzimage
The first make statement can also be executed without making bzimage. This process may take about an hour, so it is re-compiled for the entire kernel. After the execution is complete, a new file named vmlinux is generated in the current directory. Its attribute is-rwxr-XR-X.
Then execute:
# Make modules
# Make modules_install
Compile and install all kernel modules.
After the execution, a new directory/lib/modules/2.6.20/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.
Step 3: Compile the module File and makefile
Take hello. C on ldd3 as an example:
//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); |
The content of makefile is:
obj-m := hello.oKERNELDIR := /lib/modules/2.6.20/buildPWD := $(shell pwd)modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions |
Here, the hello. C and makefile files should be located in the same directory and can be placed in/home. Both of my files are located in/home/David /.
Step 4: Compile and load modules
In the directory where the file is located, run:
Debian:/home/David # Make
Then, view the files generated in the directory:
Debian:/home/David # ls-L total 28drwxr-xr-x 2 David 4096 desktop-RW-r -- 1 David 462 hello. c-RW-r -- 1 Root 2432 hello. ko-RW-r -- 1 Root 607 hello. mod. c-RW-r -- 1 Root 1968 hello. mod. o-RW-r -- 1 Root 1140 hello. o-RW-r -- 1 David 267 makefile-RW-r -- 1 Root 0 module. symvers |
We can see that the module File hello. KO. has been generated.
Then, you can load the module:
Debian:/home/David # insmod hello. Ko
Check whether the module is loaded into the kernel:
Debian:/home/David # lsmod
Module size used
Hello 1344 0
NFS 219468 0
NFSD 202224 17
......
The module named hello is the loaded module.
Uninstall the module:
Debian:/home/David # rmmod hello
You can also use lsmod to check whether the module is uninstalled.
There are two problems: one is the problem of printk () output. ldd3 also said that information is output on the screen when the module is loaded and detached. If terminal simulators (commonly used virtual machines) are used in windows ), no output is displayed on the screen. I have run this module on both the Virtual Machine and physical machine, and none of them see "Hello, world" (printk output when loading the module) or "goodby, cruel world "(printk output when the module is uninstalled ). I don't know whether it is the reason for the release of the operating system or the system configuration problem. Please give me some advice on this issue.
Second, the book says that if no information is displayed on the screen, it may be output in a log file, and it may be in the/var/log/messages file. in addition, many online users also say that the files are output to this file. I don't know if I have found any output in other log files, but I have output this information in/var/log/syslog. after the module is loaded and uninstalled, run the following command:
Debian:/home/David # Cat/var/log/syslog | grep world
We can see that there are two lines of content. Of course, you can also use grep world, it should appear in the last two lines.
Jul 20 14:15:29 localhost kernel: Hello, world
Jul 20 14:15:34 localhost kernel: Goodbye, cruel world
This is the information that printk should output.
Another method is provided to output the printk information on the screen, that is, to change the priority of the printk output. in this example, the priority is kern_alert and the priority is <1>. If you change the priority to kern_emerg, that is, <0>, you can see the output information on the screen.
The modification method is to modify the content of the two printk () Statements in hello. C. The modified hello. C is as follows:
# Include <Linux/init. h> # include <Linux/module. h> module_license ("dual BSD/GPL"); static int hello_init (void) {printk (kern_emerg "Hello, world/N "); /* change part */return 0;} static void hello_exit (void) {printk (kern_emerg "Goodbye, cruel world/N "); /* change part */} module_init (hello_init); module_exit (hello_exit ); |
Compile and generate modules in the same way, and use insmod and rmmod again, the output information displayed on the screen is:
debian:/home/david# insmod hello.kodebian:/home/david#Message from syslogd@localhost at Fri Jul 20 14:27:32 2007 ...localhost kernel: Hello, worlddebian:/home/david# rmmod hellodebian:/home/david#Message from syslogd@localhost at Fri Jul 20 14:27:42 2007 ...localhost kernel: Goodbye, cruel worlddebian:/home/david |
However, whether or not the priority of printk () can be changed to kern_emerg is debatable, because in Linux kernel development, the priority is described as: an emergency condition; the system is probably dead.
The above is the compilation steps of the entire 2.6 kernel and the Dynamic Loading Method of the module. if your understanding and explanation are incorrect, please read this article and share your hopes with friends who are interested in the kernel and driver.