Linux dynamically loads kernel modules

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: