current position:Home>How to use lsmod in Linux (take a practical example)

How to use lsmod in Linux (take a practical example)

2022-01-27 01:21:29 mikes zhang

 Insert picture description here
Linux The kernel is a mystery to most people , But it doesn't have to ! Welcome to the exciting lsmod The world , This tool allows you to view the modules being used by the kernel, etc !

What is? lsmod?

To explain lsmod This is allowed in Linux Tools for listing modules in the kernel , First, you must understand what the module is .Linux A module is a piece of modular code ( Run in compiled format ), You can choose from Linux Kernel load or unload . The main advantage of using these modules is their modularity and granularity .

You can think of kernel modules as plug-ins in other software . Whenever you need a specific —— Less common or common —— When the function , It can be used as a plug-in , Or in Linux In the case of kernel, it is used as a module . Plug ins in other software and Linux There is little difference between kernel modules . however , They work ​​ The level is closer to the hardware and core running software , Not at the end of the stack at the user level .

for example , Your graphics card driver —— Especially when you use NVidia Driver —— Load as a kernel module . Error installing driver , The kernel module will be configured and loaded ( Maybe the next time the system restarts ).

go back to lsmod, It should be included in your Linux The default installation of the operating system , The tool will list an overview of the currently available kernel modules .

lsmod: At first sight

Interestingly , it lsmod Is a kind of will ( fictitious )/proc/modules A tool for rendering the contents of a file as more readable content .

The /proc/modules The file is a 0 Byte virtual file , When using, for example cat or When viewing vi, It will contain ( Or better present ) Currently available for lsmod On the executing system Linux List of kernel modules .

Let's first check /proc/modules Whether the contents of the file exist AHCI( Advanced host controller interface )Linux The kernel module :

cat /proc/modules | grep ahci

 Insert picture description here

Here we use cat To display ( Rendering ) The content of /proc/modules, then grep be used for AHCI Modular grep. Please note that , We didn't specify -i Options ( It does a case insensitive search ) To grep, Because the complete list of modules /proc/modules Only lowercase .

We can see the currently available... From the output provided AHCI Linux The kernel module . otherwise , The output looks a little mysterious and less descriptive . Let us lsmod Now try .

lsmod | grep -E '^Module|ahci'

 Insert picture description here

ad locum , We use lsmod A tool without options , be used for grep Get multiple results of interest . The -E Option grep So that we can , The choice of projects we want to see / Selection list ( strand ), All separations consist of one or more pipe symbols (|).

therefore , We're looking for anything here ahci appear ( In the second selector / Select... In the string ) And anything Module appear ( In the first selector / Select... In the string ), among It's a regular expression ( Applies only to the first selector ), Choose start a line, Means we just want to see the beginning of the line followed by “Module” The latter result , This will be our first title line .

We see that the output is now well rendered ; first line ( our grep The command is due to ^Module Selector is included ) And the second row and the third row ahci We grep Module selected due to the second selector .

The header column output tells us what each column contains ; The first column is the module name (Module), The second column is bytes (Size), The third column provides the reference count (Used by), Or to put it another way , How many times has the module been referenced ( May be vaguely translated by the kernel into used ).

If the reference count in the third column is zero , It means that although the module “ stay ” Available in the kernel , But not currently used .

Tips : If you -2 stay See in the output of lsmod, You may need to be in Stackexchange Of “Used by” Check in column lsmod Show -2.

Use modinfo Dig deep

although lsmod Does the tool have its own options , We can combine other utilities with lsmod Discovery information modinfo.

modinfo ahci -F 'description'
modinfo ahci -F 'author'

 Insert picture description here

Here we use the modinfo Command to find information about ahciLinux Kernel More about modules . especially , We use -F Option to modinfo Select the specific fields we want to know about . Please note that , If you are in modinfo ahci Execute without any options , You will see about ahci A complete list of all available information for the kernel module . This is a very verbose output , Because it includes signature key data, etc .

We can grep Use a similar method to select two pieces of information in a single command as before . So , We use modinfo ahci | grep -E ‘description|author’, It presents the same output of two lines , Although directly under each other .

 Insert picture description here

Attention, please. , You can use regular expressions again ’ Select only the output string starting from the beginning of the line ( For the two fields being searched ), namely modinfo ahci | grep -E ‘description|author’

And comparison lsmod,modinfo There are other options you might be interested in . Check out man modinfo To see all available options .

wrap up

In this paper , We discussed no options lsmod Tools and their convenient output for us , Show us what Linux Kernel modules are available and each such kernel module is currently referenced by the kernel (“ Use ”) How many times . We also studied how to use modinfoLinux The commands in get more information about a specific module . We also studied how to grep -E Get two pieces of information at a time by scanning with multiple selection strings .

copyright notice
author[mikes zhang],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270121248217.html

Random recommended