The first kernel module compiled under Ubuntu

Source: Internet
Author: User
Tags data structures system log dmesg

The kernel module is a socket provided externally by the Linux kernel, which is known as the dynamic loadable kernel module (Loadable Kernel module,lkm), and is referred to as a module . The Linux kernel provides a modular mechanism because it itself is a single kernel (monolithic kernel). One of the biggest advantages of a single kernel is the high efficiency, because all the content is integrated, but its disadvantage is that scalability and maintainability is relatively poor, the modular mechanism is to make up for this flaw.

One, what is a module

A module is a stand-alone program that can be compiled separately, but not run independently. It is linked to the kernel as part of the kernel running in the kernel space at runtime, which is different from the process running in user space. A module is usually composed of a set of functions and data structures to implement a file system, a driver, or other functions on the top of the kernel.

Comparison of application and kernel modules
To deepen understanding of the kernel modules, table one gives a comparison between the application and the kernel module program.
Table one application vs Kernel module program comparison

C Language Application Kernel Module Program
Working with functions LIBC Library Kernel functions
Running space User space Kernel space
Run permissions Ordinary users Super User
Entry function Main () Module_init ()
Export function Exit () Module_exit ()
Compile Gcc–c Makefile
Connection Gcc Insmod
Run Run directly Insmod
Debugging Gdb Kdbug, KDB,KGDB, etc.

As we can see from table one, the kernel module program cannot call a function in the LIBC library, which runs in kernel space and only the superuser can run on it. In addition, the module program must use the Module_init () and Module-exit () functions to tell the kernel "I'm Here" and "I'm Gone".

Second, the preparation of a simple module

Both the module and the kernel are running in the kernel space, and the module programming is the kernel programming in a sense. Because of each change in the kernel version, some of the function names change accordingly, so module programming is closely related to the kernel version. The following example is for the Ubuntu 9.10 kernel 2.6.31-14-generic

1. Program examples

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> module_license ("GPL"); static int __init lkp_init (void) {PRINTK (kern_alert "Hello world!/n"); return 0;} static void __exit lkp_cleanup (void) { PRINTK (kern_alert "Bye world!/n"); } module_init (Lkp_init); Module_exit (Lkp_cleanup); Module_author ("Heyutao"); Module_description ("Hello");


Description

All modules are to be module.h with the header file, which must be included in the file. The
header file kernel.h contains the common kernel functions. The
header file Init.h contains macros _init and _exit that allow you to free the memory that the kernel occupies.
Lkp_init is a module initialization function that must contain content such as code to compile, initialization data structure, and so on.
The PRINTK () function, which is defined by the kernel, is similar to printf () in the C library, which outputs the information to be printed to the terminal or System log.
Lkp_cleanup is the exit and cleanup function of a module. Here you can do all the cleanup work associated with terminating the driver.
Module_init () and Cleanup_exit () are the most basic and necessary two functions in modular programming.
Module_init () is the entry point for driver initialization. Cleanup_exit () logs out all the functionality provided by the module.

2 Write Makefile files, and hello.c in the same directory

Obj-m: = hello.o kernelbuild: =/lib/modules/$ (Shell uname-r)/build default:make-c $ (kernelbuild) M=$ (shell pwd) modules CLEAN:RM-RF *.o *.ko *.mod.c *.cmd *.markers *.order *.symvers. tmp_versions

(Note the tab required in makefile)

Kernelbuild: =/lib/modules/$ (Shell uname-r)/build is the makefile path needed to compile the kernel module, and Ubuntu is

/lib/modules/2.6.31-14-generic/build

Make-c $ (kernelbuild) m=$ (shell pwd) modules compile kernel modules. -C Transfer the working directory to Kernelbuild, call the makefile in the directory, and pass the value of parameter m to this makefile is $ (shell pwd) modules.

3. Compile module

#sudo make (Invoke first command default)

At this point, there will be Hello.ko in the Hello.c folder, which is the kernel module we need.

#sudo Make Clean

Clean up the garbage, Hello.ko will also be cleaned up.

4. Insert the module and let it work. Note must be root permission

#sudo Insmod./hello.ko
We can see the generated kernel information with DMESG, Hello world!

If you do not output "Hello from Hello World", it will output if you are running at a character terminal instead of a terminal emulator, because the kernel message is exported to the log file/var/log/kern.log under the terminal emulator.


#sudo Rmmod./hello
And then you can see Bye world! with DMESG.

Modutils is a software package that manages kernel modules. You can get the Modutils (modutils-x.y.z.tar.gz) source code wherever you get the kernel source code, and then choose the highest level patch.x.y.z equal to or less than the current kernel version, which will have insomod in the/sbin directory after installation. Rmmod, ksyms, Lsmod, modprobe and other utility programs. Of course, normally when we load the Linux kernel, modutils is already loaded.
1. Insmod command
Call the Insmod program to insert the module you want to insert into the kernel in the form of the target code. When inserting, Insmod automatically invokes the Init_module () function to run. Note that only Superuser can use this command, and the command format is:
# insmod [path] Modulename.ko
2. Rmmod command
Call the RMMOD program to move the module that is already plugged into the kernel out of the kernel, Rmmod automatically runs the Cleanup_module () function with the command format:
#rmmod [path] Modulename.ko
3. Lsmod command
Calling the Lsmod program displays the module information that is being used in the current system. In fact, the function of this program is to read the information in the file/proc/modules in the/proc file system, the command format is:
#lsmod
4. ksyms command

Ksyms This program is used to display information about kernel symbols and module symbol tables. Similar to Lsmod, its function is to read another file/proc/kallsyms in the/proc file system.

This is the next simplest module writing process in 2.6.xx.

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.