Compilation steps of Linux kernel and dynamic loading of modules

Source: Internet
Author: User
Article title: Linux2.6 kernel compilation steps and dynamic module loading. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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 
      
       #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");}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
     

[1] [2] Next page

Related Article

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.