如何检查特定驱动程序是否已在 Linux 中加载

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何检查 Linux 系统中是否加载了特定的驱动程序。驱动程序通常以内核模块的形式实现,对于与硬件进行交互至关重要。你将探索三种实现此目的的方法:使用 lsmod 列出所有已加载的模块、检查 /proc/modules 文件以获取详细状态,以及使用 modinfo 验证特定驱动程序的信息。

通过完成这些步骤,你将掌握识别和了解 Linux 系统中驱动程序状态的实用技能,这对于解决硬件问题和进行系统管理至关重要。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/printf("Text Formatting") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") subgraph Lab Skills linux/echo -.-> lab-558755{{"如何检查特定驱动程序是否已在 Linux 中加载"}} linux/printf -.-> lab-558755{{"如何检查特定驱动程序是否已在 Linux 中加载"}} linux/ls -.-> lab-558755{{"如何检查特定驱动程序是否已在 Linux 中加载"}} linux/cat -.-> lab-558755{{"如何检查特定驱动程序是否已在 Linux 中加载"}} linux/ps -.-> lab-558755{{"如何检查特定驱动程序是否已在 Linux 中加载"}} end

使用 lsmod 列出驱动程序

在这一步中,你将学习如何列出当前已加载的内核模块,在硬件相关的语境中,这些内核模块通常被称为驱动程序。内核模块是可以根据需要加载到内核中或从内核中卸载的代码片段,它们可以在不重启系统的情况下扩展内核的功能。

我们将使用的命令是 lsmod。该命令用于显示 Linux 内核中模块的状态。

如果终端尚未打开,请打开它。你可以在桌面左侧找到 Xfce Terminal 图标。

输入以下命令并按回车键:

lsmod

你将看到类似如下的输出:

Module                  Size  Used by
...
snd_hda_codec_generic    ...   ...
ledtrig_audio          ...   ...
snd_hda_codec_hdmi     ...   ...
snd_hda_intel          ...   ...
snd_intel_dspcfg       ...   ...
snd_hda_codec          ...   ...   snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel
snd_hda_core           ...   ...   snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec
snd_hwdep              ...   ...   snd_hda_codec
snd_pcm                ...   ...   snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec,snd_hda_core,snd_hwdep
snd_seq_midi           ...   ...
snd_seq_midi_event     ...   ...   snd_seq_midi
snd_rawmidi            ...   ...   snd_seq_midi,snd_seq_midi_event
snd_seq                ...   ...   snd_seq_midi,snd_seq_midi_event
snd_seq_device         ...   ...   snd_seq_midi,snd_rawmidi,snd_seq
snd_timer              ...   ...   snd_pcm,snd_seq
snd                    ...   ...   snd_hda_codec_generic,ledtrig_audio,snd_hda_codec_hdmi,snd_hda_intel,snd_intel_dspcfg,snd_hda_codec,snd_hda_core,snd_hwdep,snd_pcm,snd_rawmidi,snd_seq,snd_seq_device,snd_timer
soundcore              ...   ...   snd
...

lsmod 的输出有三列:

  • Module: 内核模块的名称。
  • Size: 模块的大小(以字节为单位)。
  • Used by: 使用此模块的其他模块列表,以及该模块被使用的次数。

此命令对于查看系统上当前哪些驱动程序处于活动状态非常有用。

点击 Continue 进入下一步。

使用 cat /proc/modules 检查驱动程序状态

在这一步中,你将探索另一种查看已加载内核模块信息的方法,即检查 /proc/modules 文件。/proc 文件系统是一个虚拟文件系统,它提供有关进程和其他系统信息。

/proc/modules 文件包含的信息与 lsmod 命令的输出类似,但格式略有不同。我们将使用 cat 命令来显示该文件的内容。

在终端中输入以下命令并按回车键:

cat /proc/modules

你将看到类似如下的输出:

snd_hda_codec_generic ... ... - Live 0xffffffff...
ledtrig_audio ... ... - Live 0xffffffff...
snd_hda_codec_hdmi ... ... - Live 0xffffffff...
snd_hda_intel ... ... - Live 0xffffffff...
snd_intel_dspcfg ... ... - Live 0xffffffff...
snd_hda_codec ... ... snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel, Live 0xffffffff...
snd_hda_core ... ... snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec, Live 0xffffffff...
snd_hwdep ... ... snd_hda_codec, Live 0xffffffff...
snd_pcm ... ... snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec,snd_hda_core,snd_hwdep, Live 0xffffffff...
snd_seq_midi ... ... - Live 0xffffffff...
snd_seq_midi_event ... ... snd_seq_midi, Live 0xffffffff...
snd_rawmidi ... ... snd_seq_midi,snd_seq_midi_event, Live 0xffffffff...
snd_seq ... ... snd_seq_midi,snd_seq_midi_event, Live 0xffffffff...
snd_seq_device ... ... snd_seq_midi,snd_rawmidi,snd_seq, Live 0xffffffff...
snd_timer ... ... snd_pcm,snd_seq, Live 0xffffffff...
snd ... ... snd_hda_codec_generic,ledtrig_audio,snd_hda_codec_hdmi,snd_hda_intel,snd_intel_dspcfg,snd_hda_codec,snd_hda_core,snd_hwdep,snd_pcm,snd_rawmidi,snd_seq,snd_seq_device,snd_timer, Live 0xffffffff...
soundcore ... ... snd, Live 0xffffffff...
...

/proc/modules 中的各列表示:

  1. 模块名称
  2. 模块大小
  3. 模块被使用的次数
  4. 模块是已加载(Live)还是正在加载(Loading)或卸载(Unloading
  5. 模块的内存偏移量(即 0xffffffff... 部分)
  6. 依赖模块(类似于 lsmod 中的“Used by”列)

虽然 lsmod 通常是查看已加载模块的首选命令,但了解 /proc 文件系统中也提供这些信息对于深入进行系统检查非常重要。

点击 Continue 继续。

使用 modinfo 验证驱动程序详细信息

在这一步中,你将学习如何使用 modinfo 命令获取特定内核模块的详细信息。该命令可以提供诸如模块的文件名、作者、描述、许可证和参数等详细内容。

让我们来获取与音频驱动相关的 snd_hda_intel 模块的信息。

在终端中输入以下命令并按回车键:

modinfo snd_hda_intel

你将看到类似如下的输出:

filename:       /lib/modules/.../kernel/sound/hda/snd-hda-intel.ko
description:    Intel HDA driver
license:        GPL v2
srcversion:     ...
alias:          pci:v00008086d0000...sv*sd*bc04sc03i00*
alias:          pci:v00008086d0000...sv*sd*bc04sc01i00*
alias:          pci:v00008086d0000...sv*sd*bc04sc03i00*
...
depends:        snd-hda-codec,snd-pcm,snd-hda-core,snd-hwdep,snd-timer,snd
retpoline:      Y
intree:         Y
name:           snd_hda_intel
vermagic:       ... SMP mod_unload
sig_id:         PKCS#7
signer:         Build time autogenerated kernel key
sig_key:        ...
sig_hashalgo:   sha512
signature:      ...
parm:           bdl_pos_adj:Adjust the BDL position (int)
parm:           probe_mask:Bitmask of codec probe options.
                bit 0: force codec probe
                bit 1: allow probing codecs with basic capabilities
                bit 2: allow probing codecs with no capabilities
                (int)
parm:           enable_msi:Enable MSI (int)
parm:           enable_msix:Enable MSI-X (int)
parm:           model:Use the specified codec model.
                See Documentation/sound/kernel-modules-aliases.txt for details. (charp)
parm:           patch:Load the specified patch file. (charp)
parm:           power_save:Automatic power-saving timeout (in ms, 0 to disable). (int)
parm:           power_save_controller:Controller power-saving timeout (in ms, 0 to disable). (int)
parm:           pm_blacklist:Force non-power-save mode (D0) (bool)
parm:           position_fix:DMA pointer position fix (0, 1, 2) (int)
parm:           probe_only_force:Only probe codecs with force option (bool)
parm:           single_cmd:Use single command to communicate with codecs (bool)
parm:           snoop:Enable/disable snoop mode (bool)
parm:           jackpoll_ms:Polling interval for jack events (in ms, 0 to disable). (int)

此输出提供了关于 snd_hda_intel 模块的大量信息,包括它在文件系统中的位置(filename)、简要描述、分发所遵循的许可证,以及可用于配置其行为的各种参数。

了解如何使用 modinfo 对于解决驱动程序问题或深入了解内核模块的特定功能和选项至关重要。

点击 Continue 完成此实验。

总结

在本次实验中,你学习了如何检查特定驱动程序是否已在 Linux 系统中加载。你首先使用 lsmod 命令列出了当前所有已加载的内核模块,这些模块通常被称为驱动程序。该命令会提供模块名称、大小以及哪些其他模块正在使用它的相关信息。

你还了解到,可以使用 cat 命令检查 /proc/modules 文件的内容,以此来查看已加载驱动程序的状态。最后,你掌握了如何使用 modinfo 命令来获取特定驱动程序的详细信息,例如其文件名、作者、描述和参数等。这些步骤为验证 Linux 系统中已加载驱动程序的存在和详细信息提供了全面的方法。