Home >System Tutorial >LINUX >Explore the mysteries of Linux kernel modules

Explore the mysteries of Linux kernel modules

WBOY
WBOYforward
2024-02-14 17:10:17755browse

In the Linux operating system, the kernel is the core part, which controls the interaction between computer hardware and software. The kernel module is an important concept in the kernel. It can be dynamically loaded into the kernel at runtime, thus extending the functionality of the kernel. In this article, we'll take a deep dive into how Linux kernel modules work and are made up of, and explain how to write your own.

Linux kernel modular design

1. Linux kernel design: single kernel, modular (dynamic loading and unloading)

(1) Linux: single-kernel design, but fully draws on the advantages of the microkernel system design; introduces a modular mechanism to the kernel;
(2) Components of the kernel:
kernel: kernel core, usually in bzImage format, usually located in the /boot directory, named vmlinuz-VERSION-release;
This file will no longer be used after the system is started because it has been loaded into the memory and placed under /boot for easy management
Kernel object: Kernel module, generally placed in /lib/modules/VERSION-release/
The kernel module and the kernel core version must strictly match;

2. Kernel module: compile selection mode

[ ]: N, do not compile this part
[M]: Module, compiled in a modular manner, can be temporarily loaded, takes up disk space and does not occupy kernel space
[*]: Y, compiled into the kernel core, can be called directly

3.ramdisk: Auxiliary file, not necessary, depends on whether the kernel can directly drive the device where rootfs is located

ramdisk: A simplified version of the root file system. The available drivers are as follows:
Target device driver, such as SCSI device driver;
Logical device driver, such as LVM device driver;
File system, such as xfs file system;

Kernel module information acquisition and management commands

1.ldd: Print library files that binary applications depend on -print shared library dependencies

Format: ldd [OPTION]… FILE…
show:
\1) Dependent library file name => Dependent library file path (corresponding to the memory loading symbolic link mapping point)
\2) Entrance to the entire system call library

linux-vdso.so.1 => (0x00007fff293fe000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0228073000)
Explore the mysteries of Linux kernel modules

2.uname: Kernel information acquisition -print system information

Format: uname [OPTION]…
uname -a: Display all kernel information
uname -v: The compiled version number of the kernel
uname -r: kernel release number
uname -n: hostname
Explore the mysteries of Linux kernel modules

3.lsmod: List kernel modules

The displayed kernel comes from /proc/modules
Module name, size, number of times it is referenced, and what it is referenced
Explore the mysteries of Linux kernel modules

4.modinfo command: display detailed information of the specified module

Format: modinfo [-F field] [-k kernel] [modulename|filename…] -k kernel: To query module information on another kernel when multiple kernels coexist
-F field: Only display information about the specified field;
-n: Display file path;
Display relevant information by reading the original data of the /lib/modules/
Explore the mysteries of Linux kernel modules#/* file

Explore the mysteries of Linux kernel modules Display content: file name, protocol, description, author, alias, version number applicable to RHEL, dependent modules, signature unit, signature, encryption algorithm

5.modprobe: Implement module loading and unloading, and also mount dependent modules



Format: modprobe [-r] module_name
Dynamic loading of modules: modprobe module_name
Dynamic uninstall: modprobe -r module_nameExplore the mysteries of Linux kernel modules Note: Do not uninstall modules loaded by default

6.depmod:- Generate modules.dep and map files

Tool for generating kernel module dependency files and system information mapping files;

7.insmod, rmmod: loading and unloading of modules cannot automatically resolve module dependencies


insmod [filename] [module options…] filename: the file path of the module file; rmmod [module_name]

Management of ramdisk files

1.mkinitrd(CentOS 5): Remake the ramdisk file for the currently used kernel



# mkinitrd [OPTION…] [] –with=: In addition to the default modules, modules that need to be loaded into initramfs;

–preload=: The modules provided by initramfs require preloaded modules;

Example: ~]# mkinitrd /boot/initramfs-(uname -r)

2.dracut(CentOS 6/7, compatible with 5): – low-level tool for generating an initramfs image

#### dracut [OPTION…] [ []] Example: ~]# dracut /boot/initramfs-(uname -r)###

Kernel information output pseudo file system

1./proc: The output interface for kernel status and statistical information; also provides a configuration interface, /proc/sys

(1) Parameters:
Read-only: information output; for example /proc/#/, process related information
Explore the mysteries of Linux kernel modules
Writable: The user can specify a "new value" to configure a certain function or feature of the kernel; /proc/sys/
Format: /proc/sys: net/ipv4/ip_forward is equivalent to net.ipv4.ip_forward
Explore the mysteries of Linux kernel modules
(2) Modify parameter method
\1) sysctl command
Specially used to view or set the value of parameters in the /proc/sys directory; sysctl [options] [variable[=value]] View: # sysctl -a; # sysctl variable
Modify its value: # sysctl -w variable=value
\2) File system commands (cat, echo)
View: # cat /proc/sys/PATH/TO/SOME_KERNEL_FILE
Setting: # echo “VALUE” > /proc/sys/PATH/TO/SOME_KERNEL_FILE
\3) Configuration files: /etc/sysctl.conf, /etc/sysctl.d/
.conf
Immediately effective method: sysctl -p [/PATH/TO/CONFIG_FILE]

(3)Important kernel parameters
net.ipv4.ip_forward: core forwarding;
vm.drop_caches:
kernel.hostname: host name;
net.ipv4.icmp_echo_ignore_all: Ignore all ping operations;

2./sys directory: introduced after Kernel 2.6 version

sys file system: outputs the relevant attribute information of each hardware device recognized by the kernel, and also has the kernel’s settable parameters for the hardware characteristics;
Modifying these parameters can customize the working characteristics of the hardware device;
Explore the mysteries of Linux kernel modules
udev: Create device files for each hardware device on demand by reading the hardware device information in the /sys directory;
udev is a user space program; special tools: devadmin, hotplug;
When udev creates a device file for a device, it will read its predefined rule file
Generally in the /etc/udev/rules.d/ directory and the /usr/lib/udev/rules.d/ directory;

To summarize, the kernel module is a very important part of the Linux kernel, which can provide the kernel with rich extension functions and help users better adapt to specific hardware or application scenarios. When using kernel modules, you need to be careful not to damage the stability and security of the kernel. It is recommended to use and write kernel modules only when necessary. I believe that the content of this article can help readers better understand and apply Linux kernel modules.

The above is the detailed content of Explore the mysteries of Linux kernel modules. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete