Add a kernel module in linux and a kernel module in linux

Source: Internet
Author: User

Add a kernel module in linux and a kernel module in linux

1. Introduction to Kernel Modules

The Linux kernel has a very large integer structure, and many components are contained in the package. How can I use the components required?

Method 1: compile all the components into the kernel file, that is, zImage or bzImage. However, this will lead to two questions: first, the generated Kernel File is too large. Second, if you want to add or delete a group file, you need to re-compile the entire kernel.

Method 2: using the kernel module, the kernel file (zImage or bzImage) does not include a group of parts, but is used when the group is needed, dynamically add modules to the running kernel.


2. the kernel module has the following features:
• The module itself is not compiled into the Kernel File (zImage or bzImage)
• It can be installed or unloaded dynamically during kernel runtime based on data requirements


3. Example (hello world)

#include <linux/init.h>#include <linux/module.h>static int hello_init(void){printk(KERN_WARNING"Hello, world !\n");return 0;}static void hello_exit(void){printk(KERN_INFO "Goodbye, world\n");}module_init(hello_init);module_exit(hello_exit);

4. Program Structure Description

Module Loading Function (required): number of functions automatically called by the system during module installation, which is specified through the module_init macro. In the preceding example, the specified loading function is hello_init.

Module uninstallation function (required): the number of functions automatically called by the system during module uninstallation, which is specified through the module_exit macro. In the preceding example, the specified function is hello_exit.


5. pritk Function Description

The functions of printk functions and printf functions are the same, except that one runs in the kernel state and the other runs in the user State.

When printing with the printk function, the kernel may print messages to the current Console Based on the log level. This console is usually a character-mode terminal, a serial printer, or a parallel printer. The premise for normal output of these messages is that the log output level is lower than the console log level (the smaller the number in the kernel, the higher the priority ). If no print level is specified, the default value is <4>, that is, the KERN_WARNING level, which can be customized and found in/kernelprintk. c.


There are a total of eight log levels. The log levels of printk are defined as follows (in include/linux/kernel. h ):

# Define KERN_EMERG 0

# Define KERN_ALERT 1
# Define KERN_CRIT 2
# Define KERN_ERR 3
# Define KERN_WARNING 4
# Define KERN_NOTICE 5
# Define KERN_INFO 6
# Define KERN_DEBUG 7

You can read and write the/proc/sys/kernel/printk file to read and modify the Log Level of the console.

# Cat/proc/sys/kernel/printk

44 1 7

The four data items shown above correspond to the console Log Level, Default Message Log Level, minimum console Log Level, and default console log level respectively.

You can modify the data above to modify the printing level of the console.

Note: The ubuntu graphical interface cannot be printed to the console. printk prints the console, that is,/dev/console. The terminal in the graphic interface actually uses stdin, stdout, stderr redirected the three files. Therefore, printk cannot be displayed on the terminal in the graphic interface. Of course, you can view it in/var/log/syslog or dmesg.


6. Compile the module makefile. The makefile name must be named Makefile. Otherwise, the compilation fails. The makefile will be compiled twice. The first time KERNELRELEASE is empty, run it.

The makefile under KDIR is then re-called to compile the Makefile. Two files hello. o and hello. ko are generated.

Ifneq ($ (KERNELRELEASE),) obj-m: = hello. o // modify elseKDIR: =/lib/modules/2.6.32-21-generic/build // enter all according to your own lib/modules/kernel version library below: make-C $ (KDIR) M = $ (PWD) modulesclean: rm-f *. ko *. o *. mod. o *. mod. c *. symversendif

7. Loading module insmod

Insmod hello. ko

You can use lsmod | grep hello to view the kernel module you just loaded.


8. Uninstall the rmmod module.

Rmmod hello





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.