Linux is a single kernel. In order to make up for the poor scalability and maintenance of a single kernel, Linux introduces dynamic loadable kernel modules, which can be loaded into or unloaded from the kernel during the running of the system. A module is a stand-alone program that can be compiled but not run independently. It is linked to the kernel at runtime and runs in kernel space as part of the kernel. A module usually consists of a set of functions and data structures used to implement a file system, a driver, or other functions on top of the kernel.
Here’s a simple example: the
(1) module hello.c file
#include <linux/init.h>
//specify the initialization and cleanup functions
#include <linux/module.h>
// Contains definitions for a large number of functions and symbols needed to load the module.
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("WFJ");
// Declare the module's license, for any version of the GNU General Public License
static int hello_init (void)
{
printk(KERN_ALERT "Hello module init/n");
return 0;
}
//The initialization function should be declared as static.
static void hello_exit (void)
{
printk(KERN_ALERT "Hello module exit/n");
}
//Purge function, which logs out the interface and returns all resources to the system before the module is removed.
module_init(hello_init);
//This macro definition adds a special segment to the module object code, indicating where to find the module's initialization function.
//Without this definition, the initialization function will not be called.
module_exit(hello_exit);
A Linux kernel module must contain module initialization and module uninstall functions, which run at insmod and rmMOD, which must be defined before the macros module_init and module_exit are used, otherwise there will be a compilation error.
(2) Makefile file
CONFIG_MODULE_SIG=n
obj-m := hello.o
KERNELBUILD :=/usr/src/linux-source-3.13.0
default:
make -C $(KERNELBUILD) M=$(shell pwd) modules
clean:
rm -rf *.o *.ko *.mod.c .*.cmd .tmp_versions *.order *.symvers
(3) the compilation Module
is compiled with make
command. After the compilation, several new files will be produced: hello.ko, hello.mod. C, hello.mod. O, hello.o, modules. Order, module.symvers. Where Hello.ko is required.
(4) insert module
sudo insmod hello.ko
(5) View module
lsmod
Lsmod is short for list Modules. It lists all the modules.
does not print out the output of hello.c file on the port, but you can use the dmesg command to view it.
dmesg > hello.txt
Save the output of dMESg in the hello.txt file. If the CONFIG_MODULE_SIG=n statement is not included in the Makefile, then the hello.txt file will show: Hello: module verification failed: signature and/or required key missing-tainting kernel
: module verification failed. If this statement is present, the hello.txt file will show:
[ 9753.846136] Hello module init/n
[12601.703040] Hello module exit/n
That is: output the output content of the hello.c file.
(7) unload the module
sudo rmmod hello.ko
Read More:
- Raspberry Pi runs ubuntu-mate and reports Failed to start Load Kernel Modules error countermeasures
- make modules_ Install compiles the kernel driver as a module, and the location of. Ko file in the root file
- When react dynamically prunes components by operating arrays, the state of the remaining components is not preserved. Solution to the problem (method of dynamically setting unique key value)
- [Solved] This dependency was not found: * core-js/modules/es.error.cause.js in ./node_modules/@babel
- Python dynamically imports objects, importlib.import_ Module() uses
- [Solved] gitbook: node_modules\npm\node_modules\graceful-fs\polyfills.js:287
- Python’s json.loads Invalid control character
- uin-app dynamically set the title
- Elasticsearch in Spring uses Spel to dynamically create Index of Documet class
- Error in ogrListLayers(dsn = dsn): Cannot open data source is reported when readOGR loads the shapefile
- Oops error location method in ARM kernel
- Solve the problem of dynamically applying for permission, but still reporting an error of open failed: eacces (permission denied)
- A solution to the problem that the number of nodes does not increase and the name of nodes is unstable after adding nodes dynamically in Hadoop cluster
- ERROR in ./node_modules/css-loader/dist/cjs.js!./node_modules/less-loader/dist/cjs.js!./src/css/spec
- Error Kernel Pattern
- WSL 2 needs to update its kernel components
- Compiling the pi_kernel source code appears: fatal error: openssl/opensslv.h: No such file or directory
- Alpine Linux executable file crash report error / lib / x86 not found_ 64 Linux GNU / libc.so solution
- On the kernel error of jupyter notebook
- Interrupt the kernel in Jupiter does not respond