Welcome to the tutorial guide. The guide will provide a user with advise and guidance on how to carry out automatic loading and unloading for the kernel.
Automatic Loading
Let’s have a look at automatic loading. You can cause an LKM to be loaded automatically when the kernel first needs it. This can be done by either the kernel module loader, which is part of the Linux kernel, or the older version of it, a kerneld daemon.
As an example, let’s say you run a program that executes an open system call for a file in an MS-DOS filesystem. But you don’t have a filesystem driver for the MS-DOS filesystem either bound into your base kernel or loaded as an LKM. So the kernel does not know how to access the file you’re opening on the disk.
The kernel recognises that it has no filesystem driver for MS-DOS, but that one of the two automatic module loading facilities are available and uses it to cause the LKM to be loaded. The kernel then proceeds with the open.
Please note that automatic kernel module loading is not worth the complexity in most modern systems. It may make sense in a very small memory system, because you can keep parts of the kernel in memory only when you need them. But the amount of memory these modules uses is so cheap today that you will normally be a lot better off just loading all the modules you might need via startup scripts and leaving them loaded.
Red Hat Linux uses automatic module loading via the kernel module loader and the kernel module loader and kerneld use modprobe, ergo insmod, to insert LKMs.
- Kernel Module Loader
There is some documentation of the kernel module loader in the file Documentation/kmod.txt in the Linux 2.4 source tree. This section is more complete and accurate than that file. You can also look at its source code in kernel/kmod.c.
The kernel module loader is an optional part of the Linux kernel. You get it if you select the CONFIG_KMOD feature when you configure the kernel at build time.
When a kernel that has the kernel module loader needs an LKM, it creates a user process (owned by the superuser, though) that executes modprobe to load the LKM, then exits. By default, it finds modprobe as /sbin/modprobe, but you can set up any program you like as modprobe by writing its file name to /proc/sys/kernel/modprobe. For example:
# echo “sbin/mymodprobe” >/proc/sys/kernel/modprobe
The kernel module loader passes the following arguments to the modprobe: Argument Zero is the full file name of modprobe. The regular arguments are -s, -k, and the name of the LKM that the kernel wants. -s is the user-hostile form of –syslog; -k is the cryptic way to say –autoclean. I.e. messages from modprobe will go to syslog and the loaded LKM will have the “autoclean” flag set.
The most important part of the modprobe invocation is, of course, the module name. Note that the “module name” argument to modprobe is not necessarily a real module name. It is often a symbolic name representing the role that module plays and you use an alias statement in modules.conf to tell what LKM gets loaded. For example, if your Ethernet adapter requires the 3c59x LKM, you would have probably need the line
alias eth0 3c59x
in /etc/modules.conf. Here is what the kernel module loader uses for a module name in some of the more popular cases (there are about 20 cases in which the kernel calls on the kernel module loader to load a module):
• When you try access a device and no device driver has registered to serve that device’s major number, the kernel requests the module by the name block-major-N or char-major-N where N is the major number in decimal without leading zeroes.
• When you try to access a network interface (maybe by running ifconfig against it) and no network device driver has registered to serve an interface by that name, the kernel requests the module named the same as the interface name (e.g. eth0). This applies to drivers for non-physical interfaces such as ppp0 as well.
• When you try to access a socket in a protocol family which no protocol driver has registered to drive, the kernel requests the module named net-pf-N, where N is the protocol family number (in decimal without leading zeroes).
• When you try to NFS export a directory or otherwise access the NFS server via the NFS system call, the kernel requests the module named nfsd.
• The ATA device driver (named ide) loads the relevant drivers for classes of ATA devices by the names: ide-disk, ide-cd, ide-floppy, ide-tape, and ide-scsi.
The kernel module loader runs modprobe with the following environment variables (only): HOME=/; TERM=linux; PATH=/sbin:/usr/sbin:/bin:/usr/bin.
The kernel module loader was new in Linux 2.2 and was designed to take the place of kerneld. It does not, however, have all the features of kerneld.
In Linux 2.2, the kernel module loader creates the above mentioned process directly. In Linux 2.4, the kernel module loader submits the module loading work to Keventd and it runs as a child process of Keventd.
The kernel module loader is a pretty strange beast. It violates layering as Unix programmers generally understand it and consequently is inflexible, hard to understand, and not robust. Many system designers would bristle just at the fact that it has the PATH hardcoded. You may prefer to use kerneld instead, or not bother with automatic loading of LKMs at all.
- Kerneld
kerneld is a user process, which runs the kerneld program from the modutils package. kerneld sets up an IPC message channel with the kernel. When the kernel needs an LKM, it sends a message on that channel to kerneld and kerneld runs modprobe to load the LKM, then sends a message back to the kernel to say that it is done.
- Automatic Unloading - Autoclean
This will show a user about how to automatically unload (autoclean) the kernel.
Please note that each loaded LKM has an autoclean flag which can be set or unset. A user can control this flag with parameters to the init_module system call. If you do that through insmod, you use the –autoclean option.
You can see the state of the autoclean flag in /proc/modules. Any LKM that has the flag set has the legend autoclean next to it.
As we know that the purpose of the autoclean flag is to let a user automatically remove LKMs that haven’t been used in a while. So by using automatic module loading and unloading, a user can keep loaded only parts of the kernel that are presently needed, and can save memory.
There is a form of the delete_module system call that says, “remove all LKMs that have the autoclean flag set and haven’t been used in a while.” Kerneld typically calls this once per minute. You can call it explicitly with an rmmod –all command.
As the kernel module loader does not do any removing of LKMs, if you use that you might want to have a cron job that does a rmmod –all periodically.
If you followed advise and guidance as provided in the tutorial guide then you would have learnt about automatic and non-automatic kernel module loading.