Ubuntu kernel module Compilation

Source: Internet
Author: User
Tags system log dmesg

The kernel module is an external plug-in provided by the Linux kernel. It is called the dynamic loadable kernel module (lkm. The Linux Kernel provides a module mechanism because it is a single kernel ). The biggest advantage of a single kernel is high efficiency, because all the content is integrated, but its disadvantage is the poor scalability and maintainability. The module mechanism aims to make up for this defect.

I. What is a module?

A module is a program with independent functions. It can be compiled but cannot run independently. It is linked to the kernel at runtime and runs in the kernel space as part of the kernel, which is different from the process running in the user space. A module is usually composed of a group of functions and data structures to implement a file system, a driver, or other kernel upper-layer functions.

Comparison between applications and Kernel Modules

To better understand the kernel module, table 1 shows the comparison between applications and kernel module programs.

 

 

Table 1 Comparison between applications and kernel module programs

From table 1, we can see that the kernel module program cannot call functions in the libc library. It runs in the kernel space and can only be run by Super Users. In addition, the module program must use the module_init () and module_exit () functions to tell the kernel "I am coming" and "I am going ".

2. Compile a simple module

Both modules and kernels run in the kernel space. In a sense, module programming is kernel programming. Due to each change in the kernel version, some function names also change accordingly. Therefore, module programming is closely related to the kernel version. The following example is for Ubuntu 11.04 kernel 2.6.37.2.

1. Hello. c

 

# Include "Linux/init. H "<br/> # include" Linux/kernel. H "<br/> # include" Linux/module. H "</P> <p> module_license (" GPL "); </P> <p> static int _ init hello_init (void) {<br/> printk (kern_alert "Hello world! /N "); <br/> return 0; <br/>}</P> <p> static void _ exit hello_exit (void) {<br/> printk (kern_alert "goodbye! /N "); <br/>}</P> <p> module_init (hello_init); <br/> module_exit (hello_exit ); <br/> module_author ("gengjia"); <br/> module_description ("hello ");

Description

The Hello. c file can be placed under any folder.

All modules must use the header file module. H, which must be included.

The header file kernel. h contains common kernel functions.

The header file init. h contains macro _ init and _ exit, which allow you to release the memory occupied by the kernel.

Hello_init is the initialization function of the module. It must contain such content as the code to be compiled and the initialized data structure.

The printk () function is defined by the kernel and similar to the printf () function in the C library. It outputs the information to be printed to the terminal or system log.

Hello_exit is the exit and cleanup function of the module. Here we can do all the cleanup work related to terminating the driver.

Module_init () and module_exit () are the two most basic and necessary functions in module programming.

Module_init () is the entry point for driver initialization. Module_exit () cancels all functions provided by the module.

2. makefile

OBJ-M: = hello. O <br/> kernelbuild: =/lib/modules/$ (shell uname-R)/build <br/> default: <br/> make-C $ (kernelbuild) M = $ (shell PWD) Modules <br/> echo insmod. /Hello. ko to turn it on <br/> clean: <br/> RM-RF *. O *. ko *. mod. c. *. CMD *. markers *. order *. symvers. tmp_versions

(Note the required tab in makefile)

Put the MAKEFILE file and the hello. c file in the same directory.

Kernelbuild: =/lib/modules/$ (shell uname-R)/build is the makefile path required for compiling the kernel module.

/Lib/modules/2.6.37.2/build

Make-C $ (kernelbuild) M = $ (shell PWD) Modules compile the kernel module. -C transfers the working directory to kernelbuild, calls makefile under the directory, and passes the value of the Parameter m to this makefile: $ (shell PWD) modules.

3. Compilation Module

# Sudo make (call the first command default)

At this time, there will be hello. KO in the folder where hello. C is located. This is the kernel module we need.

# Sudo make clean

Clear the compilation garbage. Hello. Ko will also be cleared.

4. Insert a module to make it work. Note that the permission must be root.

# Sudo insmod./Hello. Ko we can use dmesg to see the generated kernel information, hello World!

If "Hello World!" is not output! ", Because if you are running on a character terminal rather than a terminal simulator, the kernel messages will be output to the log file/var/log/Kern in the terminal simulator. log.

# Sudo rmmod./Hello. Ko and dmesg can see goodbye!

Modutils is a software package for managing kernel modules. You can obtain the modutils(modutils-x.y.z.tar.gz) source code from any other source code, and then select the highest level patch. x. y. Z is equal to or less than the current kernel version. After installation, there will be utilities such as insomod, rmmod, ksyms, lsmod, and modprobe in the/sbin directory. Of course, when we load the Linux kernel, The modutils has been loaded.

1. insmod command

Call the insmod program to insert the module to the kernel as the target code. During insertion, insmod automatically calls the module_init () function. Note that only super users can use this command. The command format is: # insmod [path] modulename. Ko

2. rmmod command

Call the rmmod program to remove the module that has been inserted into the kernel from the kernel. rmmod runs the module_exit () function automatically. The command format is # rmmod [path] modulename. Ko.

3. lsmod command

When you call the lsmod program, information about the modules in use in the current system is displayed. In fact, the function of this program is to read the information in/proc/modules in the/proc file system. The command format is: # lsmod

 

The above is the simplest module compilation process in 2.6.xx.

 

This article is reproduced from:

Http://www.xker.com/page/e2011/0214/100154_1.html

And made some modifications to it.

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.