Adding kernel modules in Linux

Source: Internet
Author: User

1. Introduction to Kernel Modules

The overall structure of the Linux kernel is very large, it contains a lot of components, how to use the required components?

Method One: All the components are compiled into the kernel file, namely: Zimage or bzimage, but this will cause two problems: first, the generated kernel file is too large, or if you want to add or remove a component, you need to Recompile the entire kernel

Method two: Using kernel modules, the kernel file (zimage or bzimage) does not itself contain a component, but is dynamically added to the running kernel when the component needs to be used, in a modular way


2. Kernel modules have the following features:
? The module itself is not compiled into the kernel file (zimage or Bzimage)
? can be installed or uninstalled dynamically during kernel operation, as required


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. Description of the program structure

Module load function (required): The function that is automatically called by the system when installing the module, specified by Module_init macro, the above example, the specified loaded function is Hello_init

Module unload function (MUST): The function that is automatically called by the system when unloading the module, through the Module_exit macro to specify, the above example, the specified loaded function is Hello_exit


5.PRITK function Description

The function of the PRINTK function and the function of printf is the same, except one running in the kernel state and one running in the user state.

When printing with the PRINTK function, the kernel may print messages to the current console based on the log level, which is usually a character-mode terminal, a serial printer, or a printer with a single port. The normal output of these messages is that the log output level is less than the console log level (the smaller the number in the kernel, the higher the priority). If you do not specify a print level, the default is <4>, which is the kern_warning level, and its customizations can be found in/kernelprintk.c.


There are 8 levels in the log level, and the log level of PRINTK is 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

Read and modify the log level of the console by reading and writing to the/PROC/SYS/KERNEL/PRINTK file

#cat/PROC/SYS/KERNEL/PRINTK

44 1 7

the 4 data shown above correspond to each other Console log level, default message log level, lowest console log level, and default console log level

You can modify the print level of the above data modification console

Note: The Ubuntu graphical interface is not printable to the console, PRINTK Printing is the console, which is/dev/console, the graphical interface of the terminal, in fact, the Stdin,stdout,stderr three files redirected a bit. So PRINTK is no longer displayed in the graphical interface of the terminal, of course, can be/var/log/syslog or DMESG view.


6. Module makefile written, makefile name must be named Makefile, otherwise compile through not, the following makefile will be compiled two times, the first kernelrelease is empty, go to run

Kdir the following makefile, and then re-call their own makefile, compiled successfully, resulting in Hello.o,hello.ko two files

Ifneq ($ (kernelrelease),) obj-m: = hello.o//modifies according to own module Elsekdir: =/lib/modules/2.6.32-21-generic/build//according to itself lib/ Modules/under own kernel repository fill all:        make-c $ (kdir) m=$ (PWD) Modulesclean:        rm-f *.ko *.o *.mod.o *.mod.c *.symversendif

7. Loading the moduleInsmod

Insmod Hello.ko

Available through Lsmod | grep hello to view the kernel modules that you just loaded


8. Uninstalling the module Rmmod

Rmmod Hello





Adding kernel modules in Linux

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.