Kernel modules permit enabling hardware features on a given system. For example, if we need to read from a particular filesystem from a hard drive, we need to load a particular kernel module. Or use a specific network card, a sound card or sound device, a video display, etc. This is mostly done automatically in many instances but knowing how to load and unload kernel modules in Linux is a good thing anytime you find yourself in trouble.
List current modules
First of all, let’s list the loaded modules in a given system, so we can see what is already running. For that we’ll make use of the ‘lsmod’ command.
Let’s imagine we need to load a Linux kernel module so we can read a FAT formatted hard drive. We need to load two kernel modules to acquire that capacity. We’ll use the ‘insmod’ command and we will also have to point to the full path when loading the modules.
Info to load modules
Let’s first look for information related to the module we want to load which is called vfat. To read such information we need to use the ‘modinfo’ command.
Reading carefully we realize there’s a dependency for the vfat module, and that is called simply fat. We can look for that too.
It seems that the ‘fat’ module has no dependencies, so we can now proceed and load the two of them.
Loading the modules
We can load kernel modules with the ‘insmod’ command. And we can do that by simply invoking the command and pointing to the path where the module is sitting on the system. Information which is pointed out in the ‘filename’ parameter after issuing the ‘modinfo’ command.
Let’s check if the module has been loaded correctly by issuing ‘lsmod’ and using grep to pull out the content we are looking for.
And yes, it’s been loaded, otherwise the result of the above command would have resulted in blank.
With the ‘fat’ module loaded we will not be able to read from a modern formatted drive with a FAT filesystem on it. We need to load a second module named ‘vfat’. All we need is to repeat a similar operation as we have done with the ‘fat’ module.
We have first looked for the ‘vfat’ module’s location, and that has been provided by the ‘modinfo’ command, more specifically by the ‘filename’ parameter in the result.
Then we have used the ‘insmod’ command to load the module.
Lastly, we have checked if it was correctly loaded with the ‘lsmod’ command and grep’s assistance.
What if we have finished using that FAT formatted hard drive and we want to unload those modules?
To unload modules we have another command, called ‘rmmod’. The shortening of ‘remove module’. And as we did with the loading, it’s very easy to use, since we only have to invoke ‘rmmod’ and point out which is the full path of the kernel module we want to unload.
We have unloaded the ‘vfat’ module. Let’s unload the ‘fat’ module.
With both modules unloaded, let’s know check if they have been really unloaded. We’ll use, again, the ‘lsmod’ command working in conjunction with ‘grep’.
The empty output ensures us both modules have been unloaded. Grepping ‘fat’ grants us ‘fat’ as well as ‘vfat’ are covered since the string ‘fat’ is the full one for one of the module’s name and it is also contained in the other’s name.
Loading modules without a hassle.
All of the above is nice but not as nice as it can get.
There’s a catch with using ‘insmod’ and ‘rmmod’. And that is they are not resolving dependencies. They are quite literal in what they load and unload to and from the kernel. And that is problematic, since in order to use VFAT to read a file system formatted in FAT we need to use the ‘fat’ module alongside the ‘vfat’ one. To know that we need to use the ‘modinfo’ command. With more complex dependency models this operation gets trickier and prone to errors. And no one wants to make mistakes when touching kernel infrastructure. So, what solves this?
The ‘modprobe’ command does all the above and it does also resolve dependencies for us. Let’s demonstrate this with the same example as above.
We want to read a FAT formatted drive and all we know is we need to load the ‘vfat’ kernel module.
What has been loaded?
Both have been loaded, the ‘vfat’ module and its dependency ‘fat’ module. With one command and without having to declare the full path.
What if we do not need to read from that filesystem anymore? Let’s unload the ‘vfat’ module.
What is loaded now?
As it happened when they were loaded together, both ‘fat’ and ‘vfat’ modules have been unloaded.
lsmod: list the currently loaded kernel modules.
insmod: loads specific modules and needs the full path. It doesn’t resolve dependencies.
modinfo: provides information from a specific module.
rmmod: unloads specific modules and needs the full path. It doesn’t resolve dependencies.
modprobe: loads and unloads modules, resolves dependencies and doesn’t need full path declaration.
Knowing how a system works can help us tremendously, specially when automount features don’t work properly or we are given a task to resolve with limited capabilities as it often happens when dealing with servers or challenging scenarios. Knowing how to load and unload kernel modules in Linux can help us out in some of those scenarios.