当前位置: 首页 > article >正文

Linux 内核模块 | 加载 / 添加 / 删除 / 优先级

注:本文为 “Linux 内核模块加载 / 添加 / 删除 / 优先级” 相关文章合辑

机翻,未校。

未整理去重。


How Linux Kernel Boots?

Linux 内核如何启动?

Last Updated: 26 Apr, 2023

Many processes are running in the background when we press the system’s power button. It is very important to learn the booting process to understand the working of any operating system. Knowing how the kernel boots is a must to solve the booting error. It is a very interesting topic to learn, let us start with the basics. A simplified read of the boot method is given below:
当我们按下系统的电源按钮时,许多进程都在后台运行。了解任何作系统的工作情况,了解引导过程是非常重要的。了解内核如何引导是解决引导错误的必要条件。这是一个非常有趣的学习话题,让我们从基础开始。下面给出了 boot 方法的简化阅读:

Stages of Linux Boot Process:

Linux 启动过程的阶段:

  1. The machine’s BIOS (Basic Input/Output System) or boot microcode hundreds and runs a boot loader.
    机器的 BIOS(基本输入/输出系统)或引导微码数百并运行引导加载程序。
  2. Boot loader finds the kernel image on the disk and loads it into memory, to start the system.
    引导加载程序在磁盘上找到内核映像并将其加载到内存中,以启动系统。
  3. The kernel initializes the devices and their drivers.
    内核初始化设备及其驱动程序。
  4. The kernel mounts the basis filesystem.
    内核挂载基础文件系统。
  5. The kernel starts a program referred to as init with a method ID zero
    内核启动一个称为 init 的程序,其方法 ID 为零
  6. init sets the remainder of the system processes in motion.
    init 设置系统进程的其余部分。
  7. For some purpose, init starts a method permitting you to log in, typically at the top or close to the top of the boot sequence.
    出于某种目的,init 会启动一个允许您登录的方法,通常在引导序列的顶部或靠近顶部。

How Linux Kernel Boots?

Booting Proces

启动过程

Startup Message:

启动消息:

Traditional UNIX operating system manufactures several diagnostic messages upon boot that tell you regarding the boot method. The messages come back initially from the kernel and so from processes and low-level formatting procedures that init starts. However, these messages aren’t pretty or consistent, and in some cases, they aren’t even terribly informative. Additionally, hardware enhancements have caused the kernel to start a lot quicker than before the messages flash by therefore quickly, it may be tough to check what’s happening. As a result, most current Linux distributions do their best to cover boot medical specialty with splash screens and different varieties of filler to distract you whereas the system starts.
传统的 UNIX作系统在引导时会产生几条诊断消息,告诉您有关引导方法的信息。消息最初来自内核,因此来自 init 启动的进程和低级格式化过程。然而,这些信息并不漂亮或一致,在某些情况下,它们甚至没有提供太多信息。此外,硬件增强导致内核的启动速度比消息闪过之前快得多,因此可能很难检查发生了什么。因此,大多数当前的 Linux 发行版都尽最大努力通过启动画面和不同种类的填充物来覆盖启动医疗专业,以在系统启动时分散您的注意力。

Kernel Initialization and Boot Options:

内核初始化和启动选项:

  1. CPU examination CPU 检查
  2. Memory examination 记忆检查
  3. Device bus discovery 设备总线发现
  4. Device discovery 设备发现
  5. Auxiliary kernel system setup 辅助内核系统设置
  6. Root filesystem mount 根文件系统挂载
  7. User-space begin. 用户空间开始

The first 2 steps aren’t too exceptional, however, once the kernel gets to devices, the question of dependencies arises. As an example, the disk device drivers might rely on bus support and SCSI system support. In general, you won’t need to worry regarding the dependencies, except that some necessary parts are also loadable kernel modules instead of a part of the most kernel.
前 2 个步骤并不是太特殊,但是,一旦内核到达设备,依赖关系问题就出现了。例如,磁盘设备驱动程序可能依赖于总线支持和 SCSI 系统支持。一般来说,你不需要担心依赖关系,除了一些必要的部分也是可加载的内核模块,而不是大多数内核的一部分。

Kernel Parameters:

内核参数:

When the Linux kernel starts, it receives a group of text-based kernel parameters containing some further system details. The parameters specify many alternative kinds of behavior, like the number of diagnostic outputs the kernel ought to manufacture and device driver–specific choices. The Ro parameter instructs the kernel to mount the basis filesystem in read-only mode upon user area begins. This normal read-only mode ensures that fsck can safely check the basis filesystem before attempting to do something serious. When the check, the bootup method remounts the basis filesystem in read-write mode.
当 Linux 内核启动时,它会收到一组基于文本的内核参数,其中包含一些进一步的系统详细信息。这些参数指定了许多替代行为类型,例如内核应该制造的诊断输出的数量和特定于设备驱动程序的选择。Ro 参数指示内核在用户区开始时以只读模式挂载基础文件系统。这种正常的只读模式确保 fsck 可以在尝试执行严重作之前安全地检查基础文件系统。检查时,bootup 方法以读写模式重新挂载基础文件系统。

Boot Loader Tasks:

引导加载程序任务:

  • Select from multiple kernels.
    从多个内核中进行选择。
  • Switch between sets of kernel parameters.
    在内核参数集之间切换。
  • Provide support for booting different operating systems.
    为引导不同的作系统提供支持。

Bootloaders have become significantly advanced since the origin of the Linux kernel, with options like command-line history and menu systems, however, a basic want has perpetually been flexibility in kernel image and parameter choice. One stunning development is that some wants to have really diminished. As an example, as a result of being able to perform associate emergency or recovery boot from a USB device, you seldom need to worry about manually getting into kernel parameters or going into single-user mode. Current bootloaders supply a lot of power than ever, which may be notably handy if you’re building custom kernels or simply wish to tweak parameters.
自 Linux 内核诞生以来,引导加载程序已经变得非常先进,具有命令行历史记录和菜单系统等选项,但是,一个基本需求一直是内核映像和参数选择的灵活性。一个令人震惊的发展是,有些人希望真正减少。例如,由于能够从 USB 设备执行关联的紧急或恢复启动,您很少需要担心手动进入内核参数或进入单用户模式。当前的 bootloader 提供了比以往更多的功能,如果您正在构建自定义内核或只是希望调整参数,这可能非常方便。

Boot Loader Overview:

引导加载程序概述

  • GRUB – A near-universal normal on Linux systems, with BIOS/MBR and UEFI versions.
    GRUB – Linux 系统上几乎通用的正常现象,具有 BIOS/MBR 和 UEFI 版本。
  • LILO – One of the primary Linux bootloaders. ELILO could be a UEFI version.
    LILO – 主要的 Linux 引导加载程序之一。ELILO 可以是 UEFI 版本。
  • SYSLINUX – It may be organized to run from many alternative styles of filesystems.
    SYSLINUX – 它可以被组织为从许多替代样式的文件系统运行。
  • LOADLIN – Boots a kernel from DOS.
    LOADLIN – 从 DOS 引导内核。
  • System-boot a straightforward UEFI boot manager.
    系统引导 一个简单的 UEFI 引导管理器。
  • coreboot – A superior replacement for the computer BIOS which will embody a kernel.
    coreboot – 计算机 BIOS 的卓越替代品,它将体现内核。
  • Linux Kernel EFISTUB A kernel plug-in for loading the kernel directly from associate EFI/UEFI System Partition.
    Linux 内核 EFISTUB 用于直接从关联的 EFI/UEFI 系统分区加载内核的内核插件。
  • EFI Linux – A UEFI boot loader meant to function as a model and reference for different UEFI boot loaders.
    EFI Linux – 一种 UEFI 引导加载程序,旨在用作不同 UEFI 引导加载程序的模型和参考。

Introduction and Work of Few Bootloaders mentioned above:

上面提到的几个 Bootloader 的介绍和工作:

1) GRUB:

GRUB stands for Grand Unified Boot Loader. One of GRUB’s most vital capabilities is filesystem navigation that enables straightforward kernel image and configuration choice.
GRUB 代表 Grand Unified Boot Loader。GRUB 最重要的功能之一是文件系统导航,它支持直接的内核映像和配置选择。

  • Upon finding the boot code, the BIOS hundreds and executes it. This is often wherever GRUB begins.
    找到引导代码后,BIOS 百人并执行它。这通常是 GRUB 的起点。
  • The GRUB core hundreds.
    GRUB 核心数百个。
  • The core initializes. At now, GRUB will currently access disks and filesystems.
    核心初始化。目前,GRUB 目前将访问磁盘和文件系统。
  • GRUB identifies its boot partition and hundreds of configurations there.
    GRUB 标识其引导分区和其中的数百个配置。
  • GRUB offers the user an opportunity to vary the configuration.
    GRUB 为用户提供了更改配置的机会。
  • GRUB executes the configuration after a timeout or user action.
    GRUB 在超时或用户作后执行配置。
  • In the course of execution of the configuration, GRUB might load further code within the boot partition. a number of these modules are also preloaded.
    在执行配置的过程中,GRUB 可能会在 boot 分区中加载更多代码。其中许多模块也已预加载。
  • To load and execute the kernel GRUB executes boot commands.
    为了加载和执行内核,GRUB 会执行引导命令。

2) LILO (LInux LOader):

It was the first bootloader developed for Linux, still some users use it today. If we compare it to the GRUB bootloader it is quite simple and primarily used with BIOS system. To install it on Linux system one can install it using package manager like apt or yum. After installation it can be easily configured by editing its configuration file located at /etc/lolo.conf.
它是第一个为 Linux 开发的引导加载程序,今天仍然有一些用户使用它。如果我们将它与 GRUB 引导加载程序进行比较,它非常简单,主要用于 BIOS 系统。要在 Linux 系统上安装它,可以使用 apt 或 yum 等包管理器进行安装。安装后,可以通过编辑位于 ‘/etc/lolo.conf’ 的配置文件来轻松配置它。

3) SYSLINUX:

It is lightest among other bootloaders; it can perform booting from removable media such as CDs or USB drives. Its key feature is that it supports different filesystems, a command-line interface and the ability to boot from multiple devices. For example, to install SYSLINUX on a USB drive we can configure SYSLINUX setting by editing its configuration file located in /syslinux/syslinux.cfg.
它是其他引导加载程序中最轻的;它可以从 CD 或 USB 驱动器等可移动媒体执行引导。它的主要特点是它支持不同的文件系统、命令行界面以及从多个设备启动的能力。 例如,要在 USB 驱动器上安装 SYSLINUX,我们可以通过编辑位于 ‘/syslinux/syslinux.cfg’ 的配置文件来配置 SYSLINUX 设置。

Conclusion:

结论:

It is very important to know how Linux kernel boots and to know that one must know how to solve boot problems. The Linux boot process has several stages that include BIOS, boot loader, kernel initialization, device and driver initialization. While kernel is responsible for specifying various behaviors and device drivers-specific option whereas bootloader is responsible for providing flexible kernel image and parameter selection. We discussed the most common boot loader and overall, we can conclude that it is important for system administrators, developers and single users using Linux operating systems.
了解 Linux 内核如何引导以及必须知道如何解决引导问题非常重要。Linux 启动过程有几个阶段,包括 BIOS、启动加载程序、内核初始化、设备和驱动程序初始化。内核负责指定各种行为和设备驱动程序特定的选项,而 bootloader 负责提供灵活的内核映像和参数选择。我们讨论了最常见的引导加载程序,总的来说,我们可以得出结论,它对于使用 Linux作系统的系统管理员、开发人员和单个用户很重要。


Linux: How to load a kernel module automatically at boot time

Linux:如何在启动时自动加载内核模块

Author: Vivek Gite

Last updated: February 10, 2022

How do I load a Linux kernel module automatically at boot time to recognize my hardware during the system boot sequence?

如何在引导时自动加载 Linux 内核模块,以便在系统引导顺序中识别我的硬件?

The Linux kernel follows modular kernel design. Loadable Kernel Modules (LKM) are object files that contain code to extend the running kernel or so-called base kernel. LKM’s are typically used to add support for new hardware, filesystems, NICs and more. Most of the time, they are device drivers for hardware, but LKM’s are common for file systems, firewalls, and other purposes. This page explains how to load a Linux kernel module (driver) at boot time using configuration options.

Linux 内核遵循模块化内核设计。可加载内核模块(LKM)是对象文件,其中包含扩展正在运行的内核或所谓的基本内核的代码。LKM 通常用于添加对新硬件、文件系统、nic 等的支持。大多数情况下,它们是硬件的设备驱动程序,但 LKM 通常用于文件系统、防火墙和其他用途。本页解释了如何在启动时使用配置选项加载 Linux 内核模块(驱动程序)。

Tutorial details
Difficulty levelIntermediate
Root privilegesYes
RequirementsLinux
Est. reading time5 minutes

Linux config file to load a kernel module

用于加载内核模块的 Linux 配置文件

Loading a Linux kernel module is an essential task for sysadmins. You need to edit the file named /etc/modules or put a new config file in /etc/modules-load.d/ directory. Use any one of the methods for loading kernel modules. The configuration file consists of a set of lines. All empty lines, and all text on a line after a #, will be ignored. The file /etc/modules (or other files in /etc/modules-load.d/) is used if new hardware is added after installation and the hardware requires a kernel module, the system must be configured to load the proper kernel module for the new hardware.
加载 Linux 内核模块是系统管理员的一项基本任务。您需要编辑名为 /etc/modules 的文件,或在 /etc/modules-load.d/ 目录中放置一个新的配置文件。使用任何一种方法来加载内核模块。配置文件由一组行组成。所有空行以及 # 之后的行上的所有文本都将被忽略。如果在安装后添加了新硬件,并且硬件需要内核模块,则必须将系统配置为为新硬件加载正确的内核模块,则使用 /etc/modules 文件(或 /etc/modules-load.d/ 中的其他文件)。

Examples

例子

For example, if a system included an IDE CD-ROM, the module configuration file contains the following 3 lines in the /etc/modules file. You can use the vim command or cat command to display it:

例如,如果系统包含 IDE CD-ROM,则模块配置文件在 /etc/modules 文件中包含以下 3 行。您可以使用 vim 命令或 cat 命令来显示它:

# vi /etc/modules # cat /etc/modules
Now you can append following lines:

现在您可以附加以下行:

ide-cd ide-core cdrom

Save and close the file. Reboot the Linux system for testing purpose using the reboot command or shutdown command:

保存并关闭文件。使用 reboot 命令或 shutdown 命令重新启动 Linux 系统以进行测试:

# reboot

How do I find out location of my Linux kernel device drivers/modules

如何找到我的 Linux 内核设备驱动程序/模块的位置

Use the ls command along with the uname command as follows:
将 ls 命令与 uname 命令一起使用,如下所示:

ls /usr/lib/modules/$(uname -r)/kernel/
ls -l /usr/lib/modules/$(uname -r)/kernel/

We use the uname to find out Linux kernel version and device drivers on the system:

我们使用 uname 来查找系统上的 Linux 内核版本和设备驱动程序:

Listing Linux kernel module on Linux CLI

Listing Linux kernel module on Linux CLI
在 Linux CLI 上列出 Linux 内核模块

Loading a device driver without rebooting the Linux machine

在不重新启动 Linux 计算机的情况下加载设备驱动程序

Another option is to load drivers without rebooting the system. Use the modprobe command. For instance:

另一种选择是在不重新启动系统的情况下加载驱动程序。使用 modprobe 命令。例如:

# modprobe {driver-name}
# modprobe {driver-name} *variable=value*
# modprobe ide-cd
# modprobe ide-cd cdrom
# pass the start_ro=1 option to md_mod module
# modprobe md_mod start_ro=1

Please note that if you are using an older version of Debian Linux or Ubuntu Linux use the file /etc/modules file instead of /etc/modules.conf which works on on an older version of Red Hat/Fedora/CentOS Linux too. These days it is better to use the directory /etc/modules-load.d/ on all Linux distros.
请注意,如果您使用的是旧版本的 Debian Linux 或 Ubuntu Linux,请使用文件 /etc/modules 文件而不是 /etc/modules.conf,它也适用于旧版本的 Red Hat/Fedora/CentOS Linux。如今,最好在所有 Linux 发行版上使用 /etc/modules-load.d/ 目录。

Configuration directory for modprobe

modprobe 的配置目录

The modprobe command can add or remove more than one module at a time. Many modules have dependencies. Hence Linux provides a method of specifying what options are to be used with those modules. Therefore, use the /etc/modprobe.d/ directory with commands to build your logic. For example, here is my logic for Intel Wifi driver:

modprobe 命令可以一次添加或删除多个模块。许多模块都有依赖项。因此,Linux 提供了一种指定要与这些模块一起使用的选项的方法。因此,请使用带有命令的 /etc/modprobe.d/ 目录来构建逻辑。例如,以下是我的 Intel Wifi 驱动程序的逻辑:

$ cat /etc/modprobe.d/iwlwifi.conf

# /etc/modprobe.d/iwlwifi.conf
# iwlwifi will dyamically load either iwldvm or iwlmvm depending on the
# microcode file installed on the system. When removing iwlwifi, first
# remove the iwl?vm module and then iwlwifi.
remove iwlwifi \
(/sbin/lsmod | grep -o -e ^iwlmvm -e ^iwldvm -e ^iwlwifi | xargs /sbin/rmmod) \
&& /sbin/modprobe -r mac80211

You can see list of those commands using the man command:

您可以使用 man 命令查看这些命令的列表:

$ man modprobe.d

A note about modern Linux kernels

关于现代 Linux 内核的说明

These days udev is used for for automatic module handling. There is no need to put modules in any configuration file as udev takes care of it. However, sometimes you still need add an extra module during the boot process, or blacklist another one for your Linux laptop or server to function correctly. For example, kernel modules can be loaded during boot a boot in files under /etc/modules-load.d/. For instance:
如今,udev 用于自动模块处理。无需将模块放在任何配置文件中,因为 udev 会处理它。但是,有时您仍然需要在启动过程中添加一个额外的模块,或者将另一个模块列入黑名单,以便您的 Linux 笔记本电脑或服务器正常运行。例如,内核模块可以在启动期间加载到 /etc/modules-load.d/ 下的文件中。例如:

# cat /etc/modules-load.d/kvm.conf
kvm
kvm_intel

Task: See what Linux kernel modules are currently loaded
任务:查看当前加载的 Linux 内核模块

Type the lsmod command as follows:
键入 lsmod 命令,如下所示:

$ lsmod

OR

$ lsmod | more

Sample outputs:
输出示例:

Fig.01: Obtaining information about currently loaded modules/drivers

图片 01: 获取有关当前加载的模块/驱动程序的信息

To get information about a module, run modinfo command

要获取有关模块的信息,请运行 modinfo 命令

You need to use the modinfo command as follows:

您需要按如下方式使用 modinfo 命令:

$ modinfo {module_name}

For example get info about a module named igb:

例如,获取有关名为 igb 的模块的信息:

$ modinfo igb

Sample outputs:

输出示例:

Fig.02: Find information about a module

图片 02: 查找有关模块的信息

One can also use the systool command:

也可以使用systool命令:

$ systool -v -m igb
$ systool -v -m nvme

How to unload/remove a module

如何卸载/删除模块

The syntax is as follows for the rmmod command

命令的语法如下

$ sudo rmmod {module_name}

OR

$ sudo modprobe -r {module_name}

In some cases, the Linux kernel modules cannot be removed by sysadmin at runtime to maintain system stability. You need to delete the configuration option from the /etc/modules file in such cases. Also
delete the config file in /etc/modules-load.d/ directory using the rm command and cp command:

在某些情况下,sysadmin 无法在运行时删除 Linux 内核模块以保持系统稳定性。在这种情况下,您需要从 /etc/modules
文件中删除配置选项。还要使用 rm 命令和 cp 命令删除 /etc/modules-load.d/ 目录中的配置文件:

\# ls -l /etc/modules-load.d/

First, make a backup

要先做好备份

\# cp /etc/modules-load.d/my-config.conf /root/my-config.conf-backup
\# rm -i /etc/modules-load.d/my-config.confrmmod 

Summing up

总结
You learned about Linux kernel module configuration file locations on your disk and related commands. I would recommend reading the documentation using the help command or man command:

您了解了 Linux 内核模块配置文件在磁盘上的位置和相关命令。我建议使用 help 命令或 man 命令阅读文档:

man lsmod
man rmmod
man modinfo

Kernel Module Loading at Boot and modprobe Automation

启动时的内核模块加载和 modprobe 自动化

Last updated: August 31, 2023

Written by: Hiks Gerganov

Reviewed by: Michal Aibin

  • Boot Process 启动过程

  • Installation 安装

  • kernel 内核

  • modprobe 探测器

  • systemd 系统

1. Introduction

1.引言

In Linux, kernel modules are a method for extending the functionality of the kernel without replacing, configuring, or recompiling code. Although they are meant to be dynamically pluggable, knowing how to ensure a given module loads on startup can help avoid repetition and manual setup.
在 Linux 中,内核模块是一种无需替换、配置或重新编译代码即可扩展内核功能的方法。尽管它们是动态可插拔的,但知道如何确保在启动时加载给定的模块有助于避免重复和手动设置。

In this tutorial, we explain how modules can get loaded during the boot procedures and how we can automate that process. First, we go over ways to load modules automatically when the system starts. After that, we explore how to perform additional operations and use more features of the module loading system during boot.
在本教程中,我们将解释如何在引导过程中加载模块,以及如何自动执行该过程。首先,我们介绍一下在系统启动时自动加载模块的方法。之后,我们将探讨如何在启动过程中执行额外的作并使用 module loading system 的更多功能。

We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments unless otherwise specified.
我们在 Debian 12 (Bookworm) 上使用 GNU Bash 5.1.4 测试了本教程中的代码。除非另有说明,否则它应该可以在大多数 POSIX 兼容环境中工作。

2. Kernel Module Loading at Boot

2.启动时加载内核模块

Many Linux distributions have configuration options that enable the automatic loading of kernel modules when the system boots.
许多 Linux 发行版具有配置选项,这些选项允许在系统引导时自动加载内核模块。

2.1. /etc/modules

Before Linux kernel version 2.6, the */etc/modules* file contained a newline-separated list of module names to load during boot:
在 Linux 内核版本 2.6 之前,/etc/modules 文件包含一个以换行符分隔的模块名称列表,该列表将在引导期间加载:

$ cat /etc/modules
modulename1
modulename2

Here, the precedence is by line.
此处,优先级为按行。

However, since kernel version 2.6, many distributions have added more options for module loading.
但是,从内核版本 2.6 开始,许多发行版都添加了更多模块加载选项。

2.2. systemd-modules-load

2.2. systemd 模块加载

In fact, a standard and fairly universal method to ensure a given kernel module loads on its own involves the *systemd-modules-load* service as part of the systemd initialization management. After ensuring the service is started with systemctl, we can perform the necessary setup:
事实上,确保给定内核模块自行加载的标准且相当通用的方法涉及 systemd-modules-load 服务,作为 systemd 初始化管理的一部分。在确保使用 systemctl 启动服务后,我们可以执行必要的设置:

$ systmctl start systemd-modules-load

Two kernel boot parameter relate to this service: modules_load and rd.modules_load. Both take a comma-separated list of modules to load, but the latter only acts with initrd
两个内核引导参数与此服务相关:modules_load 和 rd.modules_load。两者都接受一个逗号分隔的模块列表来加载,但后者只与 initrd 一起作用

In addition, *systemd-modules-load* provides several directories for [*.conf*]iguration files:
此外,systemd-modules-load 为 [.conf] iguration 文件提供了几个目录:

  • /etc/modules-load.d/
  • /run/modules-load.d/
  • /usr/lib/modules-load.d/
  • /usr/local/lib/modules-load.d/

A .conf file usually accounts for a single module, in which case the convention is to use the name of that module as the filename. On the other hand, groups of modules can be loaded with a single configuration file as well.
一个 .conf 文件通常代表一个模块,在这种情况下,约定是使用该模块的名称作为文件名。另一方面,模块组也可以使用单个配置文件加载。

Regardless, a *.conf* file is just a list of kernel module names, one per line, with line comments using an # octothorp or ; semicolon:
无论如何,.conf 文件只是一个内核模块名称列表,每行一个,带有使用 # octothorp 或 ;分号:

$ cat /etc/modules-load.d/cups-filters.conf
# Parallel printer driver modules loading for cups
# LOAD_LP_MODULE was 'yes' in /etc/default/cups
lp
ppdev
parport_pc

Here, we use cat to output the contents of /etc/modules-load.d/cups-filters.conf, responsible for loading three kernel modules, associated with the Common Unix Printing System (CUPS).
这里,我们使用 cat 输出 /etc/modules-load.d/cups-filters.conf 的内容,负责加载三个内核模块,与通用 Unix 打印系统 (CUPS) 相关联。

Notably, both of the solutions above only enable module loading without any additional options or extra features.
值得注意的是,上述两种解决方案都只启用模块加载,没有任何额外的选项或额外的功能。

3. Kernel Module Boot Operations Automation and modprobe.d

3.内核模块启动作自动化和 modprobe.d

To load and unload modules, Linux systems use the *modprobe* tool from the kmod package:
为了加载和卸载模块,Linux 系统使用 kmod 包中的 modprobe 工具:

$ modprobe modulename

Yet, we can also employ the modprobe.d directory available at several possible locations:
然而,我们也可以使用在几个可能的位置可用的 modprobe.d 目录:

  • /etc/modprobe.d/
  • /run/modprobe.d/
  • /lib/modprobe.d/
  • /usr/lib/modprobe.d/
  • /usr/local/lib/modprobe.d/

The *modprobe.d* directories should contain *.conf* files with *modprobe* command parameters and arguments on separate lines. Thus, unlike the contents of files in modules-load.d, modprobe.d configurations can perform most actions that modprobe can. Adding the necessary commands to any *modprobe.d* *.conf* file ensures their execution on boot.
modprobe.d 目录应包含 .conf 文件,其中 modprobe 命令参数和参数位于单独的行中。因此,与 modules-load.d 中的文件内容不同,modprobe.d 配置可以执行 modprobe 可以执行的大多数作。将必要的命令添加到任何 modprobe.d .conf 文件中可以确保它们在启动时执行。

Officially, the /etc/modprobe.conf file can still combine all of these configurations in one place. However, /etc/modprobe.conf is due for deprecation.
正式地,/etc/modprobe.conf 文件仍然可以将所有这些配置合并到一个地方。但是,/etc/modprobe.conf 即将被弃用。

Finally, problems with modules might arise due to an incorrect configuration or environment.
最后,由于配置或环境不正确,可能会出现模块问题。

3.1. Priority

3.1. 优先级

Since the order of execution for some commands and options is important, files in modprobe.d can have filenames that begin with their priority, as we see via ls:
由于某些命令和选项的执行顺序很重要,因此 modprobe.d 中的文件可以具有以优先级开头的文件名,正如我们通过 ls 看到的那样:

$ ls /etc/modprobe.d/
50-alsa-base.conf
10-modem.conf

However, that’s not a strict rule. In fact, only some Linux distributions follow the convention. Yet, the loading order among files in *modprobe.d* can be assumed to follow the regular sorting of filenames.
但是,这并不是一个严格的规则。事实上,只有一些 Linux 发行版遵循该约定。然而,可以假设 modprobe.d 中文件之间的加载顺序遵循文件名的常规排序。

Still, we can group commands within the same configuration:
不过,我们可以在同一配置中对命令进行分组:

$ cat /etc/modprobe.d/51-audio.conf
softdep sound pre: alsa-base post: 
install xostaudio

Here, we combine two commands that relate to audio within the same file.
在这里,我们将与同一文件中的音频相关的两个命令组合在一起。

3.2. Commands

3.2. 命令

Further, each *modprobe.d* file command has its precedence. In fact, we can list all commands in the order of their processing:
此外,每个 modprobe.d 文件命令都有其优先级。事实上,我们可以按照命令的处理顺序列出所有命令:

  1. alias 别名
  2. blacklist 黑名单
  3. softdep 软件
  4. install 安装
  5. remove 删除
  6. options 选项

To show the current configuration, we use the –showconfig or -c flag of modprobe:
为了显示当前配置,我们使用 modprobe 的 –showconfig 或 -c 标志:

$ modprobe --showconfig
blacklist amodule
blacklist bmodule
[...]
install module1 modprobe module1; sleep 5; echo "Loaded module1"
remove module2
options module1 opt=val arg=vae
softdep module1 pre: module2 post: module3
[...]
# End of configuration files. Dumping indexes now:

alias amodule amo
alias bmodule bmo
alias module1 mod1
alias module2 mod2
[...]

Let’s continue with the possible commands within modprobe.d configuration files.
让我们继续 modprobe.d 配置文件中可能的命令。

3.3. alias

3.3. 别名

To begin with, we use alias to introduce module aliases for convenience:
首先,为了方便起见,我们使用 alias 来引入模块别名:

$ cat /etc/modprobe.d/aliases.conf
alias modx module_of_x

At this point, we can refer to module_of_x as just modx. So, any other commands that need to specify module_of_x, can use either name.
在这一点上,我们可以将 module_of_x 简称为 modx。因此,需要指定 module_of_x 的任何其他命令都可以使用任一名称。

3.4. blacklist

3.4. 黑名单

True to its name, the *blacklist* command prevents the loading of a given module and its aliases. Since it takes precedence over all other commands, we can be sure that its presence blocks related calls:
正如其名称一样,blacklist 命令会阻止加载给定的模块及其别名。由于它优先于所有其他命令,因此我们可以确保它的存在会阻止相关调用:

$ cat /etc/modprobe.d/denied.conf
blacklist module1
blacklist module2

Moreover, we can instead use module_blacklist or the more recent modprobe.blacklist kernel boot parameter and assign it a comma-separated list of module names:
此外,我们可以改用 module_blacklist 或更新的 modprobe.blacklist 内核引导参数,并为其分配一个以逗号分隔的模块名称列表:

$ cat /proc/cmdline
[...] modprobe.blacklist=module1,module2

This way, we prevent each module in the list from being loaded even before the kernel boot stage, when the bootloader is in control.
这样,我们可以防止列表中的每个模块在内核启动阶段之前就被加载,此时 bootloader 处于控制之中。

3.5. install and remove

3.5. 安装和删除

The function of both *install* and *remove* is to invoke a specific command or shell script instead of performing the respective load or unload operation:
install 和 remove 的功能都是调用特定的命令或 shell 脚本,而不是执行相应的 load 或 unload作:

$ cat /etc/modprobe.d/module1.conf
install module1 modprobe module1; sleep 5; echo "Loaded module1." > /var/log/module1.log
$ cat /var/log/module1.log
cat: /var/log/module1.log: No such file or directory
$ modprobe module1
$ cat /var/log/module1.log
Loaded module1.

In this case, we see several semicolon-separated shell commands following the install keyword and the module name module1 in /etc/modprobe.d/module1.conf. The last command rewrites the /var/log/module1.log file with the content Loaded module1. After using modprobe to (still) manually load the module, we see the previously non-existent /var/log/module1.log appears. So, we only introduce a small 5-second delay with sleep before declaring the module as loaded.
在这种情况下,我们在 /etc/modprobe.d/module1.conf 中的 install 关键字和模块名称 module1 后面看到几个以分号分隔的 shell 命令。最后一个命令使用内容 Loaded module1 重写 /var/log/module1.log 文件。在使用 modprobe (仍然)手动加载模块后,我们看到以前不存在的 /var/log/module1.log 出现。因此,我们只在声明模块为 loaded 之前引入 sleep 的 5 秒小延迟。

Had we used remove, the trigger would have been modprobe -r module1, i.e., the unloading of module1.
如果我们使用 remove,触发器将是 modprobe -r module1,即 module1 的卸载。

3.6. options

3.6. 选项

The options command enables us to add options or parameters to a module:
options 命令使我们能够向模块添加选项或参数:

$ cat /etc/modprobe.d/module1.conf
options module1 var=val

In this case, we add the var parameter and assign it the value val. Thus, we can include necessary customization according to our needs and what the module supports.
在本例中,我们添加 var 参数并为其分配值 val。因此,我们可以根据自己的需要和模块支持的内容进行必要的定制。

3.7. softdep

3.7. 软 Dep

Finally, we can add optional dependencies that a module might request using softdep:
最后,我们可以添加模块可能使用 softdep 请求的可选依赖项:

$ cat /etc/modprobe.d/module1.conf
softdep module1 pre: module2 post: module3

Here, we try to load module2 before module1 using pre:. On the other hand, post: attempts to ensure module3 is loaded after loading module1. While they’re not hard requirements, lacking any soft dependencies might result in reduced functionality.
在这里,我们尝试使用 pre: 在 module1 之前加载 module2。另一方面,post: 尝试确保在加载 module1 后加载 module3。虽然它们不是硬性要求,但缺少任何软依赖项可能会导致功能减少。

4. Summary

4.总结

In this article, we looked at ways to load kernel modules on boot and automate modprobe.
在本文中,我们研究了在启动时加载内核模块和自动化 modprobe 的方法。

In conclusion, due to its flexibility and versatility, Linux usually loads modules as they are needed instead of preemptively, so automating this process at boot should rarely be needed. Still, should we need to do so, there are several automatic kernel module loading options at our disposal.
总之,由于其灵活性和多功能性,Linux 通常根据需要加载模块,而不是抢占式,因此很少需要在启动时自动执行此过程。尽管如此,如果我们需要这样做,我们可以使用几个自动内核模块加载选项。


How to Add, Remove, and Manage Linux Kernel Modules (Drivers)

如何添加、删除和管理 Linux 内核模块(驱动程序)

Author: Vivek Gite
Last updated: May 11, 2024

How do I add or remove hardware device driver (module) from running Linux kernel?

如何从运行Linux内核中添加或删除硬件设备驱动程序(模块)?

Linux systems comes with the modprobe command, to add and remove device drivers (modules) from the Linux Kernel. Please note that, Under MS-Windows you use term device driver for modules and under Linux you use term modules for device drivers. At boot time, only a minimal resident kernel is loaded into memory. If you add new hardware you need to add driver i.e. modules. The modprobe command intelligently adds or removes a module from the Linux kernel. Let us see how to easily add or remove Linux kernel modules from the CLI.

Linux 系统附带了 modprobe 命令,用于从 Linux 内核中添加和删除设备驱动程序(模块)。请注意,在 MS-Windows 下,模块使用术语设备驱动程序,在 Linux 下,设备驱动程序使用术语模块。在引导时,只有最小的常驻内核被加载到内存中。如果你添加新的硬件,你需要添加驱动程序,即模块。modprobe 命令可以智能地在 Linux 内核中添加或删除模块。让我们看看如何在 CLI 中轻松地添加或删除 Linux 内核模块。

Tutorial details
Difficulty levelEasy
Root privilegesYes
RequirementsLinux terminal
CategorySystem Management
OS compatibilityAlmaLinux • Alpine • Amazon Linux • Arch • CentOS • Debian • Fedora • Linux • Mint • Mint • openSUSE • Pop!_OS • RHEL • Rocky • Slackware • Stream • SUSE • Ubuntu
Est. reading time3 minutes

Listing Linux kernel modules (drivers)

列出 Linux 内核模块(驱动程序)

Usually, all Linux kernel modules (drivers) are stored in the module directory located that /lib/modules/$(uname -r) directory. To see current modules, type the following ls command:
通常,所有 Linux 内核模块(驱动程序)都存储在位于 /lib/modules/$(uname -r) 目录下的模块目录中。要查看当前模块,请键入以下 ls 命令:

ls /lib/modules/$(uname -r)

Output:
输出:

build        modules.ccwmap    modules.order
initrd        modules.dep     modules.pcimap
kernel        modules.dep.bin   modules.seriomap
misc         modules.devname   modules.softdep
modules.alias    modules.ieee1394map modules.symbols
modules.alias.bin  modules.inputmap   modules.symbols.bin
modules.builtin   modules.isapnpmap  modules.usbmap
modules.builtin.bin modules.ofmap    updates

The entire command becomes ls /lib/modules/your_linux_kernel_version_here because of the uname command, effectively listing the kernel modules available for your currently running kernel version. Use the following command to list all drivers for various devices:
由于使用了 uname 命令,整个命令变成了 ls /lib/modules/your_linux_kernel_version_here,有效地列出了当前运行的内核版本可用的内核模块。使用以下命令列出各种设备的所有驱动程序:

ls /lib/modules/$(uname -r)/kernel/drivers/

Sample outputs:
输出示例:

Fig.01: Device drivers on my Linux based system

Fig.01: Device drivers on my Linux based system
图 01: 基于 Linux 的系统上的设备驱动程序

Task: Add a Module (driver) Called foo

任务:添加名为 foo 的模块 (驱动程序)

Type the following command as root user:
以 root 用户身份键入以下命令:
modprobe foo

In this example, I am loading a module called i8k, enter:
在这个例子中,我加载了一个叫做 i8k 的模块,输入:
modprobe -v i8k

Sample outputs:
输出示例:

insmod /lib/modules/3.5.0-30-generic/kernel/drivers/char/i8k.ko 

Find out info about loaded module

了解有关 loaded module 的信息

You need to use the modinfo command to see information about a Linux Kernel module. The syntax is:
您需要使用 modinfo 命令来查看有关 Linux Kernel 模块的信息。语法为:

modinfo -v {module-name-here} ``modinfo i8k

Fig.02: Displaying information about a Linux Kernel module called i8k
Fig.02: Displaying information about a Linux Kernel module called i8k
图 .02: 显示有关名为 i8k 的 Linux 内核模块的信息

Task: List all loaded modules

任务:列出所有加载的模块

Use the lsmod command to show the status of modules in the Linux Kernel:
使用 lsmod 命令显示 Linux 内核中模块的状态:

lsmod

Sample outputs:
输出示例:

Module         Size Used by
smbfs         75465 0
md5           5953 1
ipv6         284193 10
ipt_TOS         4033 2
iptable_mangle     4545 1
ip_conntrack_ftp    74801 0
ip_conntrack_irc    74033 0
ipt_REJECT       8897 43
ipt_LOG         8513 2
ipt_limit        4033 6
iptable_filter     4673 1
ipt_multiport      3521 4
ipt_state        3393 16
ip_conntrack      54297 3 ip_conntrack_ftp,ip_conntrack_irc,ipt_state
ip_tables       21825 8 ipt_TOS,iptable_mangle,ipt_REJECT,ipt_LOG,ipt_limit,iptable_filter,ipt_multiport,ipt_state
i2c_dev        13889 0
i2c_core        28865 1 i2c_dev
dm_mirror       32721 0
dm_mod         68609 1 dm_mirror
button         9313 0
battery        11465 0
ac           6985 0
ohci_hcd        24529 0
ehci_hcd        33989 0
tg3          109381 0
floppy         66065 0
ext3         137937 2
jbd          69105 1 ext3
sata_svw        10053 3
libata         78345 1 sata_svw
sd_mod         19393 4
scsi_mod       141457 2 libata,sd_mod

Task: Remove a module called foo

任务:删除名为 foo 的模块

Pass the -r option to modprobe command to remove a module, type:
将 -r 选项传递给 modprobe 命令以删除模块,键入:

modprobe -r foo

You can also use the rmmod command, which is simple program to remove a module from the Linux Kernel:
您还可以使用 rmmod 命令,这是一个从 Linux 内核中删除模块的简单程序:

rmmod foo

Configuring Linux drivers to automatically load at boot time

将 Linux 驱动程序配置为在引导时自动加载

You must create a new config file in the /etc/modules-load.d/ directory. First, Cd into that folder using the cd command:
您必须在 /etc/modules-load.d/ 目录中创建一个新的配置文件。首先,使用 cd 命令将 cd 放入该文件夹:

cd /etc/modules-load.d/

Next, create a .conf file in this directory. For example, foo-driver.conf:
接下来,在此目录中创建一个 .conf 文件。例如,foo-driver.conf:

sudo vim foo-driver.conf

Now add the names of the kernel modules you want to autoload, one module name per line:
现在添加要自动加载的内核模块的名称,每行一个模块名称:

\# /etc/modules-load.d/foo-driver.conf
\# Load my custom complied driver:
foo1
bar2

Please note that the /etc/modules is also the Linux kernel modules config file that load modules at boot time. However, we do not edit this file these days. Instead you need to make a new file in the /etc/modules-load.d/ directory. Please note that most modern Linux systems, using systemd as init, load Linux modules during boot via configuration files stored into the /etc/modules-load.d/ directory. If needed, you can still load modules manually using the modprobe command as explained earlier.
请注意,/etc/modules 也是 Linux 内核模块配置文件,用于在启动时加载模块。但是,我们现在不编辑此文件。相反,您需要在 /etc/modules-load.d/ 目录中创建一个新文件。请注意,大多数现代 Linux 系统使用 systemd 作为 init,在启动期间通过存储在 /etc/modules-load.d/ 目录中的配置文件加载 Linux 模块。如果需要,您仍然可以使用 modprobe 命令手动加载模块,如前所述。

Summing up

总结

You learned how to use various Linux command to list, add, and remove Linux kernel modules in the current Linux kernel. For more information see the following manual pages using the man command/help command:

您学习了如何使用各种 Linux 命令列出、添加和删除当前 Linux 内核中的 Linux 内核模块。有关更多信息,请参阅以下使用 man 命令/help 命令的手册页:

man modinfo
man lsmod
man insmod
man modprobe

篇外 一些讨论

What is the sequence loading linux kernel module on startup? How priority is set to them?

启动时加载 linux 内核模块的顺序是什么?如何为他们设置优先级?

edited Sep 10, 2013 at 21:12 Gilles ‘SO- stop beingevil’
asked Sep 10, 2013 at 6:24 ganes ganeshredcobra

1. 内核模块启动时的加载方式

They are not loaded automatically at start-up or any other time, although a lot of them do end up being loaded during boot. There are three different mechanisms by which this happens: Userspace request: Which covers everything from init services to udev to the command-line. Init or udev are probably the most straightforward means if you want to load a specific module at boot time. Hotplugged device: When you connect something to, e.g., USB or PCI, the kernel detects this and requests an appropriate module based on how the device identifies itself. Needed protocol or other implementation: When the kernel needs to do something, such as read a filesystem, and discovers it lacks the knowledge to do so, it will request a module.

它们不会在启动时或其他任何时候自动加载,尽管很多模块最终确实会在启动过程中被加载。这一过程有三种不同的机制:用户空间请求:涵盖从初始化服务到 udev 再到命令行的所有操作。如果你想在启动时加载特定模块,init 或 udev 可能是最直接的方式。热插拔设备:当你将设备连接到(例如)USB 或 PCI 接口时,内核会检测到这一情况,并根据设备的自我识别信息请求合适的模块。所需协议或其他功能实现:当内核需要执行某项操作(如读取文件系统),却发现自身不具备相关功能时,它会请求加载模块。

2. 内核加载模块的实际执行者

Notice that for the last two I used the phrase “request a module” – this is because the kernel actually loads via a userspace daemon, kmod which executes /sbin/modprobe. According to Wolfgang Mauerer in Linux Kernel Architecture, there are only ~100 different points in the 2.6 kernel where it calls an internal request_module () function.

请注意,对于后两种情况我使用了 “请求加载模块” 这一表述 —— 这是因为内核实际上是通过用户空间守护进程 kmod 来加载模块的,kmod 会执行 /sbin/modprobe。根据 Wolfgang Mauerer 所著的《Linux 内核架构》,在 2.6 内核中,只有大约 100 个不同的点会调用内部的 request_module () 函数。

3. modprobe 使用的数据库

modprobe uses a database of installed MODULE_ALIAS’s. These are specified in the module source code explicitly, or derived from it’s MODULE_DEVICE_TABLE, which is a list of OEM device IDs that the module services.

modprobe 使用已安装的 MODULE_ALIAS 数据库。这些别名在模块源代码中被明确指定,或者从 MODULE_DEVICE_TABLE 中派生而来,MODULE_DEVICE_TABLE 是该模块所支持的 OEM 设备 ID 列表。

4. initrd 和 initramfs 的作用

Many systems are set up to use an initrd or initramfs. These are filesystem images that are loaded by the bootloader and made available to the kernel before it mounts the root partition. This allows drivers that are necessary to mount the root partition (disk drivers, filesystem drivers, device mapper or logical volume drivers, …) to be compiled as modules and loaded from the initrd/initramfs.

许多系统都设置为使用 initrd 或 initramfs。它们是文件系统映像,由引导加载程序加载,并在挂载根分区之前提供给内核。这使得挂载根分区所需的驱动程序(磁盘驱动程序、文件系统驱动程序、设备映射器或逻辑卷驱动程序等)可以被编译为模块,并从 initrd/initramfs 中加载。

5. 不同发行版加载模块的相关设置

The startup scripts on the initrd (/linuxrc) or initramfs (/init) typically loads some modules and locates the root filesystem. Each distribution has its own setup. Ubuntu uses an initramfs which is assembled from components in the initramfs-tools package and regenerated for each kernel based on the necessary drivers to mount the root filesystem. After the root filesystem is mounted, during the system boot, modules listed in /etc/modules (Debian, …) or /etc/modules.conf (Red Hat, Arch, …) are loaded. This file usually lists few modules if any. Most modules are loaded on demand.

initrd(/linuxrc)或 initramfs(/init)上的启动脚本通常会加载一些模块并找到根文件系统。每个发行版都有自己的设置。Ubuntu 使用 initramfs,它由 initramfs-tools 软件包中的组件组装而成,并根据挂载根文件系统所需的驱动程序为每个内核重新生成。在挂载根文件系统之后,系统启动过程中,/etc/modules(Debian 等)或 /etc/modules.conf(Red Hat、Arch 等)中列出的模块会被加载。这个文件中通常很少列出模块(如果有的话)。大多数模块是按需加载的。

6. 手动编译内核模块后的处理方法

If you have an extra kernel module that you compiled manually for a hardware device, drop it into /lib/modules/$VERSION/local (create the local subdirectory if it doesn’t exist) and run depmod -a to regenerate the alias database. If the module is for some unusual feature that the kernel is unable to detect automatically, drop it into /lib/modules/$VERSION/local, run depmod -a to analyze its dependencies, and add the module name to /etc/modules.

如果你为硬件设备手动编译了一个额外的内核模块,将其放入 /lib/modules/$VERSION/local 目录(如果 local 子目录不存在则创建它),并运行 depmod -a 重新生成别名数据库。如果该模块用于某些内核无法自动检测到的特殊功能,将其放入 /lib/modules/$VERSION/local 目录,运行 depmod -a 分析其依赖关系,并将模块名称添加到 /etc/modules 文件中。


What is the proper way to start a driver on boot?

启动驱动程序的正确方法是什么?

edited Apr 13, 2017 at 12:37 Comn Community
asked Jan 1, 2016 at 2:39 BotSopalajo de Arrierez

1. 不同系统加载驱动模块的常规位置

On Debian and derivatives (Ubuntu, Mint, elementary, …), modules listed in /etc/modules (one per line, and you can specify arguments) are loaded at boot time. On systemd - based systems, modules listed in /etc/modules - load.d/*.conf (one per line) are loaded at boot time.

在 Debian 及其衍生系统(如 Ubuntu、Mint、elementary 等)中,/etc/modules 文件里列出的模块(每行一个,并且可以指定参数)会在系统启动时加载。在基于 systemd 的系统中,/etc/modules - load.d/*.conf 文件里(每行一个模块)列出的模块会在启动时加载。

2. Linux 自动加载驱动的机制及原理

However you don’t normally need to load a module explicitly. For most hardware, Linux will automatically detect the hardware and load the appropriate module. Linux finds the right module through a database stored in /lib/modules/VERSION/modules.alias which is populated from declarations in the module’s source code.

然而,通常情况下你无需显式加载模块。对于大多数硬件,Linux 会自动检测硬件并加载合适的模块。Linux 通过存储在 /lib/modules/VERSION/modules.alias 中的数据库来找到正确的模块,该数据库是根据模块源代码中的声明生成的。

3. 手动编译驱动模块后的处理步骤

Put the .ko file that you compiled somewhere under /lib/modules/VERSION/ (you need to do that anyway to get modprobe to find it) and run depmod -a as root. This will update the modules.aliases database. After that, the module will be loaded automatically during startup, when the kernel detects the hardware.

将你编译好的 .ko 文件放置在 /lib/modules/VERSION/ 目录下的某个位置(无论如何你都需要这样做,以便 modprobe 能找到它),然后以 root 用户身份运行 depmod -a。这会更新 modules.aliases 数据库。之后,当内核检测到硬件时,该模块会在启动过程中自动加载。

4. 处理系统自带驱动有问题的情况

If your hardware is also supported by a module that’s provided by your distribution, but the distribution’s driver is buggy, then I don’t think there’s an easy way to force one of the modules to be used. In this case, you should add the module you compiled to /etc/modules and blacklist the standard module by adding a line blacklist MODULE_NAME to /etc/modprobe.conf.

如果你的硬件也受发行版提供的某个模块支持,但发行版的驱动存在问题,那么我认为没有简单的方法来强制使用其中一个模块。在这种情况下,你应该将自己编译的模块添加到 /etc/modules 中,并通过在 /etc/modprobe.conf 中添加一行 blacklist MODULE_NAME 来将标准模块列入黑名单。


How does the linux kernel deal with drivers?

linux 内核如何处理驱动程序?

edited Feb 5, 2017 at 4:54 Zanna
asked Dec 22, 2016 at 14:54 Uday Kumar

1. 早期 Linux 驱动程序处理方式

In the earlier days of Linux, device drivers were indeed compiled directly into the kernel. In fact, it’s possible still to compile drivers directly in and many kernels may still have some very common drivers included this way. The way it worked was the generic kernel shipped with the early distributions like SLS or Slackware had virtually every driver the kernel supported compiled directly into it so it would work on the widest possible variety of hardware possible.

在 Linux 早期,设备驱动程序确实是直接编译进内核的。实际上,现在仍然可以直接将驱动程序编译进去,许多内核可能仍以这种方式包含一些非常常见的驱动程序。早期如 SLS 或 Slackware 等发行版所附带的通用内核,几乎将内核支持的每一个驱动程序都直接编译进去了,这样它就能在尽可能多的硬件上运行。

2. 现代 Linux 驱动程序处理方式

The kernel has, for a very long time, supported loadable modules. These are kernel drivers compiled so as to become a type of shared library that can be loaded or unloaded on demand. Now the way a kernel boots is you have the kernel file itself and a small compressed filesystem (look at initramfs on Wikipedia) that has in it all the kernel modules that kernel supports. The kernel loads, it finds its initial filesystem and then it can start loading all the drivers it needs.

长期以来,内核一直支持可加载模块。这些是被编译成一种共享库形式的内核驱动程序,可以按需加载或卸载。现在内核的启动方式是,有内核文件本身以及一个小型压缩文件系统(可查看维基百科上的 initramfs),其中包含内核支持的所有内核模块。内核加载后,找到其初始文件系统,然后就可以开始加载所需的所有驱动程序。

So, since modern kernels demand load the vast majority of the drivers they need, there are no significant amount of redundant hardware drivers taking up resources in the kernel of any modern Linux distribution.

所以,由于现代内核按需加载它们所需的绝大多数驱动程序,在任何现代 Linux 发行版的内核中,都不存在大量冗余的硬件驱动程序占用资源的情况。


via:

  • How Linux Kernel Boots? - GeeksforGeeks
    https://www.geeksforgeeks.org/how-linux-kernel-boots/

  • Linux: How to load a kernel module automatically at boot time
    https://www.cyberciti.biz/faq/linux-how-to-load-a-kernel-module-automatically-at-boot-time/

  • Kernel Module Loading at Boot and modprobe Automation | Baeldung on Linux
    https://www.baeldung.com/linux/kernel-module-load-boot

  • How to Add, Remove, and Manage Linux Kernel Modules (Drivers)
    https://www.cyberciti.biz/faq/add-remove-list-linux-kernel-modules/

  • What is the sequence loading linux kernel module on startup? How priority is set to them?
    https://unix.stackexchange.com/questions/90027/what-is-the-sequence-loading-linux-kernel-module-on-startup-how-priority-is-set

  • linux - What is the proper way to start a driver on boot?
    https://unix.stackexchange.com/questions/252623/what-is-the-proper-way-to-start-a-driver-on-boot

  • How does the linux kernel deal with drivers?
    https://askubuntu.com/questions/863521/how-does-the-linux-kernel-deal-with-drivers


http://www.kler.cn/a/535545.html

相关文章:

  • 基础相对薄弱怎么考研
  • Oracle(windows安装遇到的ORA-12545、ORA-12154、ORA-12541、ORA-12514等问题)
  • 网络编程day1
  • openGauss 3.0 数据库在线实训课程2:学习客户端工具gsql的使用
  • stm32点灯 GPIO的输出模式
  • 零代码搭建个人博客—Zblog结合内网穿透发布公网
  • Python aiortc API
  • Redis单线程架构
  • Redis - 全局ID生成器 RedisIdWorker
  • TypeScript+React+Redux:类型安全的状态管理最佳实践
  • MySQL知识大总结(进阶)
  • 如何开设一个Facebook账户:详细步骤与注意事项
  • 人工智能丨利用人工智能与自动化实现高效运营推广
  • 十. Redis 事务和 “锁机制”——> 并发秒杀处理的详细说明
  • python爬虫常用库
  • 深入浅出 NVM:如何管理 Node 版本?
  • 8.[网鼎杯 2020 青龙组]AreUSerialz
  • DeepSeek技术报告解析:为什么DeepSeek-R1 可以用低成本训练出高效的模型
  • Beans模块之工厂模块注解模块InitDestroyAnnotationBeanPostProcessor
  • PostgreSQL存储过程和执行
  • 【工具篇】深度揭秘 Midjourney:开启 AI 图像创作新时代
  • 备战蓝桥杯-洛谷
  • 使用Python的Tabulate库优雅地格式化表格数据
  • SpringCloud Gateway 动态路由配置全解
  • 如何用大语言模型提高工作效率
  • fio使用手册