[[Wikipedia:Loadable_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.

[[Wikipedia:Loadable_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.

−

==Overview==

+

== Overview ==

−

To create kernel module, you can read [http://tldp.org/LDP/lkmpg/2.6/html/index.html this guide]. A module can be configured to be build-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 {{ic|M}}).

+

+

To create a kernel module, you can read [http://tldp.org/LDP/lkmpg/2.6/html/index.html this guide]. A module can be configured to be build-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 {{ic|M}}).

Modules are stored in {{ic|/usr/lib/modules/''kernel_release''}}. You can use the command {{ic|uname -r}} to get your current kernel release version.

Modules are stored in {{ic|/usr/lib/modules/''kernel_release''}}. You can use the command {{ic|uname -r}} to get your current kernel release version.

−

−

The {{ic|/etc/modprobe.d/}} directory can be used to pass module settings to [[udev]], which will use {{ic|modprobe}} to manage the loading of the modules during system boot. You can use configuration files with any name in the directory, given that they end with the {{ic|.conf}} extension.

{{Note|Module names often use underscores ({{ic|_}}) or dashes ({{ic|-}}), however those symbols are interchangeable both when using the {{ic|modprobe}} command and in configuration files in {{ic|/etc/modprobe.d/}}.}}

{{Note|Module names often use underscores ({{ic|_}}) or dashes ({{ic|-}}), however those symbols are interchangeable both when using the {{ic|modprobe}} command and in configuration files in {{ic|/etc/modprobe.d/}}.}}

−

==Obtaining information==

+

== Obtaining information ==

+

+

To show what kernel modules are currently loaded:

−

* Format the contents of {{ic|/proc/modules}} and show what kernel modules are currently loaded:

$ lsmod

$ lsmod

−

* Use {{ic|modinfo}} to show information about a module:

+

To show information about a module:

+

$ modinfo ''module_name''

$ modinfo ''module_name''

−

* Use {{ic|modprobe}} to list the dependencies of a module (or alias), including the module itself:

+

To list the options that are set for a loaded module:

−

$ modprobe --show-depends ''module_name''

+

−

* Use {{ic|systool}} to list the options that are set for a loaded module:

$ systool -v -m ''module_name''

$ systool -v -m ''module_name''

−

===Configuration===

+

To display the comprehensive configuration of all the modules:

−

+

−

If you want to display the comprehensive configuration of all the modules you can use the command:

+

$ modprobe -c | less

$ modprobe -c | less

−

To display the configuration of a particular module, use:

+

To display the configuration of a particular module:

$ modprobe -c | grep ''module_name''

$ modprobe -c | grep ''module_name''

−

===Parameters===

+

List the dependencies of a module (or alias), including the module itself:

+

+

$ modprobe --show-depends ''module_name''

+

+

== Loading ==

+

+

The {{ic|/sbin/modprobe}} command handles the addition and removal of modules from the Linux kernel.

+

To manually load (or add) a module, run:

+

+

# modprobe ''module_name''

+

+

Most modules should be loaded on-demand. Modules to be unconditionally loaded at boot can be specified in {{ic|/etc/modules-load.d/}}, for example:

+

+

{{hc|/etc/modules-load.d/virtio-net.conf|

+

#Load 'virtio-net.ko' at boot.

+

+

virtio-net}}

+

+

== Removal ==

+

+

Occasionally you could need to remove (or unload) a module; in this case use the following command:

+

+

# modprobe -r ''module_name''

+

+

Or, alternatively:

+

+

# rmmod ''module_name''

+

+

== Configuration ==

+

+

To pass a parameter to a kernel module you can use a modprobe conf file or use the kernel command line. See also [[Systemd#Kernel_modules]].

+

+

=== Using files in /etc/modprobe.d/ ===

+

+

The {{ic|/etc/modprobe.d/}} directory can be used to pass module settings to [[udev]], which will use {{ic|modprobe}} to manage the loading of the modules during system boot. You can use configuration files with any name in the directory, given that they end with the {{ic|.conf}} extension. The syntax is:

+

{{hc|/etc/modprobe.d/myfilename.conf|2=

+

options modname parametername=parametercontents}}

+

+

For example:

+

+

{{hc|/etc/modprobe.d/thinkfan.conf|2=

+

#On Thinkpads, this lets the 'thinkfan' daemon control fan speed.

+

+

options thinkpad_acpi fan_control=1}}

+

+

=== 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:

+

+

modname.parametername=parametercontents

+

+

for example:

+

+

thinkpad_acpi.fan_control=1

+

+

Simply add this to your bootloaders kernel-line as described in [[Kernel_parameters#When_starting_the_kernel|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:

+

+

{{hc|/etc/modprobe.d/myalias.conf|

+

alias mymod ''really_long_module_name''}}

+

+

Some modules have aliases which are used to autoload them when they are needed by an application. Disabling these aliases can prevent auto-loading, but will still allow the modules to be manually loaded.

+

+

{{hc|/etc/modprobe.d/modprobe.conf|

+

#Prevent Bluetooth autoload.

+

+

alias net-pf-31 off}}

+

+

== 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]]. {{ic|mkinitcpio -M}} will print out all autodetected modules: to prevent the initramfs from loading some of those modules, blacklist them in {{ic|/etc/modprobe.d/modprobe.conf}}. Running {{ic|mkinitcpio -v}} will list all modules pulled in by the various hooks (e.g. filesystem hook, SCSI hook, etc.). Remember to add that {{ic|.conf}} file to the FILES section in {{ic|/etc/mkinitcpio.conf}} (if you have not done so already) and rebuild the initramfs once you have blacklisted the modules, and to reboot afterwards.

+

+

=== Using files in /etc/modprobe.d/ ===

+

+

Create a {{ic|.conf}} file inside {{ic|/etc/modprobe.d/}} and append a line for each module you want to blacklist, using the {{ic|blacklist}} keyword. If for example you want to prevent the {{ic|pcspkr}} module from loading:

+

+

{{hc|/etc/modprobe.d/nobeep.conf|

+

#Do not load the 'pcspkr' module on boot.

+

+

blacklist pcspkr}}

+

+

{{Note|The {{ic|blacklist}} command will blacklist a module so that it will not be loaded automatically, but the module may be loaded if another non-blacklisted module depends on it or if it is loaded manually.

+

+

However, there is a workaround for this behaviour; the {{ic|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:

+

+

{{hc|/etc/modprobe.d/blacklist.conf|

+

...

+

install ''module_name'' /bin/false

+

...}}

+

+

This will effectively blacklist that module and any other that depends on it.}}

+

+

=== Using kernel command line ===

+

+

{{Tip|This can be very useful if a broken module makes it impossible to boot your system.}}

+

+

You can also blacklist modules from the bootloader.

+

+

Simply add {{ic|1=modprobe.blacklist=modname1,modname2,modname3}} to your bootloader's kernel line, as described in [[Kernel_parameters#When_starting_the_kernel|Kernel Parameters]].

+

+

{{Note|When you are blacklisting more than one module, note that they are separated by commas only. Spaces or anything else might presumably break the syntax.}}

+

+

== Tips and tricks ==

+

+

=== Bash function to list module parameters ===

Here is a nice bash function to be run as root that will show a list of all the currently loaded modules and all of their parameters, including the current value of the parameter. It uses {{ic|/proc/modules}} to retrieve the current list of loaded modules, then access the module file directly with modinfo to grab a description of the module and descriptions for each param (if available), finally it accesses the sysfs filesystem to grab the actual parameter names and currently loaded values.

Here is a nice bash function to be run as root that will show a list of all the currently loaded modules and all of their parameters, including the current value of the parameter. It uses {{ic|/proc/modules}} to retrieve the current list of loaded modules, then access the module file directly with modinfo to grab a description of the module and descriptions for each param (if available), finally it accesses the sysfs filesystem to grab the actual parameter names and currently loaded values.

The {{ic|/sbin/modprobe}} command handles the addition and removal of modules from the Linux kernel.

+

−

To manually load (or add) a module, run:

+

−

+

−

# modprobe ''module_name''

+

−

+

−

Most modules should be loaded on-demand. Modules to be unconditionally loaded at boot can be specified in {{ic|/etc/modules-load.d/}}, for example:

+

−

+

−

{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki>

+

−

# Load virtio-net.ko at boot

+

−

virtio-net</nowiki>}}

+

−

+

−

==Removal==

+

−

Occasionally you could need to remove (or unload) a module; in this case use the following command:

+

−

+

−

# modprobe -r ''module_name''

+

−

+

−

Or, alternatively:

+

−

+

−

# rmmod ''module_name''

+

−

+

−

==Options==

+

−

To pass a parameter to a kernel module you can use a modprobe conf file or use the kernel command line.

+

−

+

−

===Using files in /etc/modprobe.d/===

+

−

To pass options to a module using modprobe config files, a .conf file with any name (you can even use {{ic|/etc/modprobe.d/modprobe.conf}}) needs to be placed in {{ic|/etc/modprobe.d/}} with this syntax:

{{hc|/etc/modprobe.d/thinkfan.conf|2=# On thinkpads, this lets the thinkfan daemon control fan speed

+

−

options thinkpad_acpi fan_control=1}}

+

−

+

−

===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 (e.g. in [[GRUB]], [[LILO]] or [[Syslinux]]) using the following syntax:

+

−

modname.parametername=parametercontents

+

−

for example:

+

−

thinkpad_acpi.fan_control=1

+

−

+

−

==Aliasing==

+

−

{{Expansion}}

+

−

{{hc|/etc/modprobe.d/myalias.conf|<nowiki># Lets you use 'mymod' in MODULES, instead of 'really_long_module_name'

+

−

alias mymod really_long_module_name</nowiki>}}

+

−

+

−

Some modules have aliases which are used to autoload them when they are needed by an application. Disabling these aliases can prevent auto-loading, but will still allow the modules to be manually loaded.

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]]. {{ic|mkinitcpio -M}} will print out all autodetected modules: to prevent the initramfs from loading some of those modules, blacklist them in {{ic|/etc/modprobe.d/modprobe.conf}}. Running {{ic|mkinitcpio -v}} will list all modules pulled in by the various hooks (e.g. filesystem hook, SCSI hook, etc.). Remember to rebuild the initramfs once you have blacklisted the modules and to reboot afterwards.

+

−

+

−

===Using files in /etc/modprobe.d/===

+

−

+

−

Create a {{ic|.conf}} file inside {{ic|/etc/modprobe.d/}} and append a line for each module you want to blacklist, using the {{ic|blacklist}} keyword. If for example you want to prevent the {{ic|pcspkr}} module from loading:

+

−

+

−

{{hc|/etc/modprobe.d/nobeep.conf|<nowiki># Do not load the pcspkr module on boot

+

−

blacklist pcspkr</nowiki>}}

+

−

+

−

{{Note|The {{ic|blacklist}} command will blacklist a module so that it will not be loaded automatically, but the module may be loaded if another non-blacklisted module depends on it or if it is loaded manually.

+

−

+

−

However, there is a workaround for this behaviour; the {{ic|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:

+

−

+

−

{{hc|/etc/modprobe.d/blacklist.conf|<nowiki>...

+

−

install MODULE_NAME /bin/false

+

−

...</nowiki>}}

+

−

+

−

This will effectively "blacklist" that module and any other that depends on it.}}

+

−

+

−

===Using kernel command line===

+

−

+

−

{{Tip|This is useful in an emergency where a broken module makes it impossible to boot your system.}}

+

−

You can also blacklist modules from the bootloader. Add {{ic|1=modprobe.blacklist=modname1,modname2,modname3}} to kernel parameters. See [[kernel parameters]] for detailed info.

Overview

To create a kernel module, you can read this guide. A module can be configured to be build-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).

Modules are stored in /usr/lib/modules/kernel_release. You can use the command uname -r to get your current kernel release version.

Note: Module names often use underscores (_) or dashes (-), however those symbols are interchangeable both when using the modprobe command and in configuration files in /etc/modprobe.d/.

Obtaining information

To show what kernel modules are currently loaded:

$ lsmod

To show information about a module:

$ modinfo module_name

To list the options that are set for a loaded module:

$ systool -v -m module_name

To display the comprehensive configuration of all the modules:

$ modprobe -c | less

To display the configuration of a particular module:

$ modprobe -c | grep module_name

List the dependencies of a module (or alias), including the module itself:

$ modprobe --show-depends module_name

Loading

The /sbin/modprobe command handles the addition and removal of modules from the Linux kernel.
To manually load (or add) a module, run:

# modprobe module_name

Most modules should be loaded on-demand. Modules to be unconditionally loaded at boot can be specified in /etc/modules-load.d/, for example:

/etc/modules-load.d/virtio-net.conf

#Load 'virtio-net.ko' at boot.
virtio-net

Removal

Occasionally you could need to remove (or unload) a module; in this case use the following command:

# modprobe -r module_name

Or, alternatively:

# rmmod module_name

Configuration

To pass a parameter to a kernel module you can use a modprobe conf file or use the kernel command line. See also Systemd#Kernel_modules.

Using files in /etc/modprobe.d/

The /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. You can use configuration files with any name in the directory, given that they end with the .conf extension. The syntax is:

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:

/etc/modprobe.d/myalias.conf

alias mymod really_long_module_name

Some modules have aliases which are used to autoload them when they are needed by an application. Disabling these aliases can prevent auto-loading, but will still allow the modules to be manually loaded.

/etc/modprobe.d/modprobe.conf

#Prevent Bluetooth autoload.
alias net-pf-31 off

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 autodetected modules: to prevent the initramfs from loading some of those modules, blacklist them in /etc/modprobe.d/modprobe.conf. Running mkinitcpio -v will list all modules pulled in by the various hooks (e.g. filesystem hook, SCSI hook, etc.). Remember to add that .conf file to the FILES section in /etc/mkinitcpio.conf (if you have not done so already) and rebuild the initramfs once you have blacklisted the modules, and to 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:

/etc/modprobe.d/nobeep.conf

#Do not load the 'pcspkr' module on boot.
blacklist pcspkr

Note: The blacklist command will blacklist a module so that it will not be loaded automatically, but the module may be loaded if another non-blacklisted module depends on it or if it is loaded manually.

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:

/etc/modprobe.d/blacklist.conf

...
install module_name /bin/false
...

This will effectively blacklist that module and any other that depends on it.

Using kernel command line

Tip: This can be very useful if a broken module makes it impossible to boot your system.

You can also blacklist modules from the bootloader.

Simply add modprobe.blacklist=modname1,modname2,modname3 to your bootloader's kernel line, as described in Kernel Parameters.

Note: When you are blacklisting more than one module, note that they are separated by commas only. Spaces or anything else might presumably break the syntax.

Tips and tricks

Bash function to list module parameters

Here is a nice bash function to be run as root that will show a list of all the currently loaded modules and all of their parameters, including the current value of the parameter. It uses /proc/modules to retrieve the current list of loaded modules, then access the module file directly with modinfo to grab a description of the module and descriptions for each param (if available), finally it accesses the sysfs filesystem to grab the actual parameter names and currently loaded values.