Adding Linux kernel modules is a crucial aspect of any Linux server administrator’s job description. This article will give you a basis in understanding the principles and tools concerned. The official terminology is “loadable kernel modules”; however they are often referred to as just “kernel modules” or “modules”.
The modern day Linux kernel uses a modular model allowing users to add additional functionality to the core of the operating system as and when required. This is the opposite of the monolithic kernel model, which requires all functionality to be compiled into the kernel prior to loading the operating system. In the past a monolithic kernel was preferred, as it is more efficient with regards the usage of memory resources, however nowadays most computers have multiple gigabytes of RAM available, so this is less of an issue. Nowadays, the advantages being able to dynamically load kernel modules far outweigh any disadvantages of slightly increased memory consumption.
So what is a kernel module? Well, it is very similar to a Windows device driver Modules often provide hardware specific software functionality. Examples include:
- serial devices
- delivering access to hard drives
- DVD players
- video adapters
It is normal for kernel modules to be loaded at boot time, however due to their dynamic nature they can also be loaded at any time. There are a number of Linux commands available in order to facilitate loading, unloading and gathering information about a module.
The most basic commands for loading and unloading modules consist of “insmod” and “rmmod”. The insmod command inserts a module in the kernel, the rmmod command removes a module from the kernel. You will find a variety of other commands which are necessary to understand when working with kernel modules. These are:
- modinfo – displays the .modinfo section of the module object file (i.e. a file with a .ko or .o file)
- depmod – ascertain any dependencies a module has
- lsmod – shows all currently loaded modules
Each of the above commands are vital when manually loading modules into the kernel. The modinfo command delivers detailed data about the module, for example the kernel version which the module was compiled for, which is critical when troubleshooting any issues encountered when trying to load a kernel module. In addition there is a higher-level Linux module command which is most often used by administrators. It is:
- modprobe – intelligently inserts or removes a Linux kernel module and all dependencies.
The modprobe command is basically a wrapper around insmod, rmmod and depmod, supplying a single, user-friendly command. It is strongly recommended that you read the help guide for modprobe should you be considering doing any work with loading or unloading Linux kernel modules.
Linux kernel modules are normally found within /lib/modules/[kernel-version] where by [kernel-version] will be the kernel version number you are looking at. The kernel version you are doing work on ought to have a matching directory in /lib/modules, it is important that you ensure you are doing work with the right directory whenever adding your modules.