Linux Experiment 2: Test the Linux kernel module

Source: Internet
Author: User
I. Tutorial Purpose

PairLinuxKernel module.

Ii. experiment content

Create a kernel module and print related information in the kernel when loading and uninstalling the module.

Iii. Background Knowledge:

1. What is a kernel module?

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 (monolithickernel ). 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.

The module has independent functions.ProgramIt can be compiled separately, but cannot be 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.

In short, a module provides usage functions for the kernel (in a sense, the kernel is also a module) or other kernel modules.CodeBlock.

 

2. Write the kernel module

When writing a kernel module, you must have two functions:
_ Init and _ exit are macro definitions in the Linux kernel. After initialization, the system releases the function and releases the memory occupied by it. Therefore, its advantages are obvious. We recommend that you use the following method when writing the entry function and exit function.
Also, the library functions used in kernel programming are different from those in user mode. For example, the printk function used in the module program corresponds to the printf function in the user State. printk is the kernel state information printing function. The function is similar to printf, but printk also has the information printing level.

Load and uninstall modules:
1> module_init (hello_init)
A. Tell the kernel where you can write module programs for execution.
B. the parameter in the module_init () function is the name of the registered function.
2> module_exit (hello_exit)
A. Tell the kernel that you write module programs to exit from there.

B. The parameter name in module_exit () is the name of the function to uninstall.

 

Iii. kernel module Compilation:

Step 1: ModuleSource codeIf you use VI, simply save and exit WQ!

Step 2: Write a makefile file.

Makefile is used to compile and generate. O,. Ko, and other modules!

Iv. Experiment steps:

I. Compile the module codeHelloworld. c

2. WriteMakefileFile

Iii. Compilation Module

Iv. Load And UnloadHelloworldModule

5. View module information

6. UninstallHelloworldModule

VII. View module information

 

V. experiment demonstration:

EnterLinuxTerminal console, createHelloworld. cFile, availableVI helloworld. cCreate and write content.

// Helloworld. c:

Figure2-1ModuleHelloworldCode

CreateMakefileFile(Note that file names are case sensitive.)

Figure2-2ModuleHelloworldOfMakefileConfiguration File

Note:

"Make-C/lib ......"Statement

The space before the make statement is a tab (press the tab key), and C in-C is in upper case, and cannot be in lower case.

Save and run the command in the current directory of the terminal.MakeOperations to compile, such:

Figure2-3ModuleHelloworldRunMakeOperation

After compilation, many files are generated:

Figure2-4ModuleHelloworldCompiled content

 

Finally, load and uninstall the module:

Enter the command in the current directory.Insmod./helloworld. Ko;

Enter the commandLsmod, you can find the module named helloworld, indicating that the module has been loaded;

Figure2-5ModuleHelloworldAlready loaded to the kernel

 

Enter the commandDmesgTo view the last row, there will be function output called during module loading;

Enter the commandRmmod helloworld, Uninstall the module (note that it is different from loading), and then enterLsmod, Cannot be foundHelloworldModule, indicating that the module has been uninstalled;

Enter the commandDmesg. Check that the module uninstallation is the function output called.

 

Figure 2-6 kernel module record during helloworld loading and uninstallation

Vi. Experiment summary:

L Makefile is a script mainly used for multi-file compilation.

L The make program can maintain mutually dependent source files, but when some files change, it can automatically identify and only automatically compile the corresponding files.

In the above simple example, the content of the MAKEFILE file is: obj-M: = helloworld. o. I would like to remind you that there are many MAKEFILE file writing methods on the Internet, but it is too troublesome. If you are writing the kernel module, obj-M: = *. O is enough (* is your module file name, such as the above Hellworld. c file ). later use

Make-wall-C/lib/modules/'uname-R'/build M = 'pwd' modules

Command to generate helloworld. O, helloworld. Ko and other files, some people directly use

Make-C/lib/modules/'uname-R'/build M = 'pwd' modules

-Wall is omitted in the middle. wall can be regarded as W + ALL, while W represents warning. Therefore, using-wall is to display all warnings!

 

L Module loading command: insmod helloworld. Ko

L Command for uninstalling the module: rmmod helloworld. Ko

View module information:Dmesg

 

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.