Loadable kernel modules
Loadable kernel module (LKM) support allows a device driveror any other kernel serviceto be linked into and removed from the kernel while it is running. This facility makes the installation of drivers much easier, since the kernel binary does not need to be changed. It also allows the kernel to be smaller because drivers are not loaded unless they are needed.
Loadable modules are implemented by one or more documented "hooks" into the kernel that additional device drivers can grab onto. A user-level command communicates with the kernel and tells it to load new modules into memory. There is usually a command that unloads drivers as well.
Although loadable drivers are convenient, they are not entirely safe. Any time you load or unload a module, you risk causing a kernel panic. We don't recommend loading or unloading an untested module when you are not willing to crash the machine.
Under Linux, almost anything can be built as a loadable kernel module. The exceptions are the root filesystem type, the device on which the root filesystem resides, and the PS/2 mouse driver.
Loadable kernel modules are conventionally stored under /lib/modules/version, where version is the version of your Linux kernel as returned by uname -r. You can inspect the currently loaded modules with the lsmod command:
# lsmod Module Size Used by ppp 21452 0 slhc 4236 0 [ppp] ds 6344 1 i82365 26648 1 pcmcia_core 37024 0 [ds i82365]
This machine has the PCMCIA controller modules, the PPP driver, and the PPP header compression modules loaded.
Linux LKMs can be manually loaded into the kernel with insmod. For example, we could manually insert our example snarf module with the command
# insmod /path/to/snarf.o
Parameters can also be passed to loadable kernel modules; for example,
# insmod /path/to/snarf.o io=0xXXX irq=X
Once a loadable kernel module has been manually inserted into the kernel, it will only be removed if you explicitly request its removal or if the system is rebooted. We could use rmmod snarf to remove our snarf module.
You can use rmmod at any time, but it works only if the number of current references to the module (listed in the Used by column of lsmod's output) is 0.
You can also load Linux LKMs semiautomatically with modprobe, a wrapper for insmod that understands dependencies, options, and installation and removal procedures. modprobe uses the /etc/modules.conf file to figure out how to handle each module.
You can dynamically generate an /etc/modules.conf file that corresponds to all your currently installed modules by running modprobe -c. This command generates a long file that looks like this:
#This file was generated by: modprobe -c (2.1.121) path[pcmcia]=/lib/modules/preferred path[pcmcia]=/lib/modules/default path[pcmcia]=/lib/modules/2.3.39 path[misc]=/lib/modules/2.3.39 ... # Aliases alias block-major-1 rd alias block-major-2 floppy ... alias char-major-4 serial alias char-major-5 serial alias char-major-6 lp ... alias dos msdos alias plip0 plip alias ppp0 ppp options ne io=x0340 irq=9
The path statements tell where a particular module can be found. You can modify or add entries of this type if you want to keep your modules in a nonstandard location.
The alias statement provides a mapping between block major device numbers, character major device numbers, filesystems, network devices, and network protocols and their corresponding module names.
The options lines are not dynamically generated. They specify options that should be passed to a module when it is loaded. For example, we could use the following line to tell the snarf module its proper I/O address and interrupt vector:3
options snarf io=0xXXX irq=X
modprobe also understands the statements pre-install, post-install, pre-remove, post-remove, install, and remove. These statements allow commands to be executed when a specific module is inserted into or removed from the running kernel. They take the following form
pre-install module command ... install module command ... post-install module command ... pre-remove module command ... remove module command ... post-remove module command ...
and are run before insertion, simultaneously with insertion (if possible), after insertion, before removal, during removal (if possible), and after removal.
Debian systems execute /etc/cron.daily/modutils once a day to shoo away any modules that have overstayed their welcome in the kernel.