- Landoflinux
- Managing Hardware and Kernel Modules with Linux
- Hardware and Modules
- Listing hardware information
- lsdev
- lspci
- lspci command example
- lsusb
- lsusb options
- Displaying information about currently loaded Modules
- lsmod
- Working with Modules
- insmod
- modinfo
- modprobe
- Kernel module
- Contents
- Obtaining information
- Automatic module loading with systemd
- Manual module handling
- Setting module options
- Manually at load time using modprobe
- Using files in /etc/modprobe.d/
- Using kernel command line
- Aliasing
- Blacklisting
- Using files in /etc/modprobe.d/
- Using kernel command line
- Troubleshooting
- Modules do not load
- How to Load and Unload Kernel Modules in Linux
- List All Loaded Kernel Modules in Linux
- How to Load and Unload (Remove) Kernel Modules in Linux
- How to Manage Kernel Modules Using modprobe Command
- If You Appreciate What We Do Here On TecMint, You Should Consider:
Landoflinux
Managing Hardware and Kernel Modules with Linux
Hardware and Modules
An important part of managing any Linux server is to know how to display your current hardware and modules that are currently loaded into your kernel. Thankfully there are some commands that can be used to display this information.
Listing hardware information
A commonly used command to list information regarding your hardware is the «lsdev» command. The «lsdev» command is used to display your systems interrupt addresses and I/O ports. Information is also displayed for IRQ and DMA channel information.
lsdev
To display information about your installed hardware you simply issue the «lsdev» command. There are no parameters or arguments that can be passed to this command. The below example was taken from a Linux Mint system.
The information displayed from the «lsdev» command retrieves information from the «/proc» directories:
IRQ channels : /proc/interrupts
I/O memory addresses : /proc/ioports
DMA channels : /proc/dma
lspci
The «lspci» command is used to display information about PCI buses in the system and devices connected to them. Below are some of the options that can be passwd to the «lspci» command:
lspci command example
Issuing «lspci» by itself with no parameters will display output in a format similar to the example below:
Adding the «-k» option to the «lspci» command will display kernel drivers handling each device:
lsusb
The «lsusb» command is used to display usb buses and devices that are connected to them on your system. Below is an example of the «lsusb» command used with the «-t» option. The «-t» option causes the output to be displayed in a tree like hierarchy.
lsusb options
A list of all known vendors, products, classes, subclasses and protocols can be found in «/usr/share/hwdata/usb.ids» It is always useful to use the «dmesg» command to check that the kernel has recognized your usb device. This can be achieved by issuing the «dmesg | grep -i usb» command. Below is an example of the dmesg command being run shortly after attaching a mobile device via usb connection.
Displaying information about currently loaded Modules
The command used for displaying information about the status of loaded modules in the Linux kernel is the «lsmod» command.
lsmod
Basically «lsmod» is a command that is used to display the output from the «/proc/modules» file. Below is an example of the «lsmod» command in action:
Note: Below is only a small section of the output.
The output from the lsmod command is formatted into three columns:
Module: Module Name
Size: Size of the Module
Used by: The dependent module that is using it.
To display information regarding a specific module we can pipe the output of the «lsmod» command into «grep«:
«lsmod | grep vhost»
Working with Modules
Modules are simply code that can be loaded and unloaded into the kernel. Modules allow the functionality of the kernel to be extended without the need for a system re-boot. A good example of a module is one that is used for a device driver. Device drivers allow the kernel to access hardware connected to the system. Typically when working with modules, you will use commands such as «lsmod«, «modinfo«, «modprobe«, «insmod» and «rmmod«. These commands are generally used for displaying, loading and unloading modules.
insmod
modinfo
Modinfo is used to display information about a Linux Kernel module. «modinfo» extracts information from the Linux Kernel modules given on the command line. If the module name is not a filename, then the «/lib/modules/version» directory is searched.
modprobe
Modprobe is a command that is used to load a module into the Linux Kernel or remove a module from the Linux kernel. Modprobe is a wrapper around the insmod command. Modprobe is the preferred method of loading a module into the kernel because it is capable of handling dependencies. Modprobe also has the capability of removing modules with the «-r» option. Below is an example of modprobe removing and loading a module into the kernel:
The following example will remove the module «parport«. First lets use the «lsmod» command to check to see if any other modules are dependant on «parport«.
From the above output we can see that the modules «ppdev» and «parport_pc» are dependant on «parport«. This means that if we unload the module «parport«, then these modules will need to be unloaded too:
Источник
Kernel module
Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.
To create a kernel module, you can read The Linux Kernel Module Programming Guide. A module can be configured as built-in or loadable. To dynamically load or remove a module, it has to be configured as a loadable module in the kernel configuration (the line related to the module will therefore display the letter M ).
Contents
Obtaining information
Modules are stored in /usr/lib/modules/kernel_release . You can use the command uname -r to get your current kernel release version.
To show what kernel modules are currently loaded:
To show information about a module:
To list the options that are set for a loaded module:
To display the comprehensive configuration of all the modules:
To display the configuration of a particular module:
List the dependencies of a module (or alias), including the module itself:
Automatic module loading with systemd
Today, all necessary modules loading is handled automatically by udev, so if you do not need to use any out-of-tree kernel modules, there is no need to put modules that should be loaded at boot in any configuration file. However, there are cases where you might want to load an extra module during the boot process, or blacklist another one for your computer to function properly.
Kernel modules can be explicitly listed in files under /etc/modules-load.d/ for systemd to load them during boot. Each configuration file is named in the style of /etc/modules-load.d/program.conf . Configuration files simply contain a list of kernel modules names to load, separated by newlines. Empty lines and lines whose first non-whitespace character is # or ; are ignored.
See modules-load.d(5) for more details.
Manual module handling
Kernel modules are handled by tools provided by kmod package. You can use these tools manually.
To load a module:
To load a module by filename (i.e. one that is not installed in /usr/lib/modules/$(uname -r)/ ):
To unload a module:
Setting module options
To pass a parameter to a kernel module, you can pass them manually with modprobe or assure certain parameters are always applied using a modprobe configuration file or by using the kernel command line.
Manually at load time using modprobe
The basic way to pass parameters to a module is using the modprobe command. Parameters are specified on command line using simple key=value assignments:
Using files in /etc/modprobe.d/
Files in /etc/modprobe.d/ directory can be used to pass module settings to udev, which will use modprobe to manage the loading of the modules during system boot. Configuration files in this directory can have any name, given that they end with the .conf extension. The syntax is:
Using kernel command line
If the module is built into the kernel, you can also pass options to the module using the kernel command line. For all common bootloaders, the following syntax is correct:
Simply add this to your bootloader’s kernel-line, as described in Kernel Parameters.
Aliasing
Aliases are alternate names for a module. For example: alias my-mod really_long_modulename means you can use modprobe my-mod instead of modprobe really_long_modulename . You can also use shell-style wildcards, so alias my-mod* really_long_modulename means that modprobe my-mod-something has the same effect. Create an alias:
Some modules have aliases which are used to automatically load them when they are needed by an application. Disabling these aliases can prevent automatic loading but will still allow the modules to be manually loaded.
Blacklisting
Blacklisting, in the context of kernel modules, is a mechanism to prevent the kernel module from loading. This could be useful if, for example, the associated hardware is not needed, or if loading that module causes problems: for instance there may be two kernel modules that try to control the same piece of hardware, and loading them together would result in a conflict.
Some modules are loaded as part of the initramfs. mkinitcpio -M will print out all automatically detected modules: to prevent the initramfs from loading some of those modules, blacklist them in a .conf file under /etc/modprobe.d and it shall be added in by the modconf hook during image generation. Running mkinitcpio -v will list all modules pulled in by the various hooks (e.g. filesystems hook, block hook, etc.). Remember to add that .conf file to the FILES array in /etc/mkinitcpio.conf if you do not have the modconf hook in your HOOKS array (e.g. you have deviated from the default configuration), and once you have blacklisted the modules regenerate the initramfs, and reboot afterwards.
Using files in /etc/modprobe.d/
Create a .conf file inside /etc/modprobe.d/ and append a line for each module you want to blacklist, using the blacklist keyword. If for example you want to prevent the pcspkr module from loading:
However, there is a workaround for this behaviour; the install command instructs modprobe to run a custom command instead of inserting the module in the kernel as normal, so you can force the module to always fail loading with:
This will effectively blacklist that module and any other that depends on it.
Using kernel command line
You can also blacklist modules from the bootloader.
Simply add module_blacklist=modname1,modname2,modname3 to your bootloader’s kernel line, as described in Kernel parameters.
Troubleshooting
Modules do not load
In case a specific module does not load and the boot log (accessible by running journalctl -b as root) says that the module is blacklisted, but the directory /etc/modprobe.d/ does not show a corresponding entry, check another modprobe source folder at /usr/lib/modprobe.d/ for blacklisting entries.
A module will not be loaded if the «vermagic» string contained within the kernel module does not match the value of the currently running kernel. If it is known that the module is compatible with the current running kernel the «vermagic» check can be ignored with modprobe —force-vermagic .
Источник
How to Load and Unload Kernel Modules in Linux
A kernel module is a program which can loaded into or unloaded from the kernel upon demand, without necessarily recompiling it (the kernel) or rebooting the system, and is intended to enhance the functionality of the kernel.
In general software terms, modules are more or less like plugins to a software such as WordPress. Plugins provide means to extend software functionality, without them, developers would have to build a single massive software with all functionalities integrated in a package. If new functionalities are needed, they would have to be added in new versions of a software.
Likewise without modules, the kernel would have to be built with all functionalities integrated directly into the kernel image. This would mean having bigger kernels, and system administrators would need to recompile the kernel every time a new functionality is needed.
A simple example of a module is a device driver – which enables the kernel to access a hardware component/device connected to the system.
List All Loaded Kernel Modules in Linux
In Linux, all modules end with the .ko extension, and they are normally loaded automatically as the hardware is detected at system boot. However a system administrator can manage the modules using certain commands.
To list all currently loaded modules in Linux, we can use the lsmod (list modules) command which reads the contents of /proc/modules like this.
How to Load and Unload (Remove) Kernel Modules in Linux
To load a kernel module, we can use the insmod (insert module) command. Here, we have to specify the full path of the module. The command below will insert the speedstep-lib.ko module.
To unload a kernel module, we use the rmmod (remove module) command. The following example will unload or remove the speedstep-lib.ko module.
How to Manage Kernel Modules Using modprobe Command
modprobe is an intelligent command for listing, inserting as well as removing modules from the kernel. It searches in the module directory /lib/modules/$(uname -r) for all the modules and related files, but excludes alternative configuration files in the /etc/modprobe.d directory.
Here, you don’t need the absolute path of a module; this is the advantage of using modprobe over the previous commands.
To insert a module, simply provide its name as follows.
To remove a module, use the -r flag like this.
Note: Under modprobe, automatic underscore conversion is performed, so there is no difference between _ and – while entering module names.
For more usage info and options, read through the modprobe man page.
Do not forget to check out:
That’s all for now! Do you have any useful ideas, that you wanted us to add to this guide or queries, use the feedback form below to drop them to us.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник