Mastering Kernel Modules with Caldera

October 1st, 1998 by David B. Nelson in

Mr. Nelson gives us step-by-step instructions for loading kernel modules, so we can keep our kernel lean.
Your rating: None

You shouldn't have to read this article. The concept of Linux kernel modules is fairly simple. Unfortunately, information needed to compile, install and use modules is scattered over several HOWTOs, READMEs, and man pages. Plus, the files which need to be modified are in several obscure directories.

I finally wrote this cookbook approach to get myself, and you, started. Once you are up and running with modules, you can dig into the details later. I tested this material on an X86 processor running Caldera Open Linux 1.1, which is close to Red Hat 4.2. Your mileage with other processors and distributions may vary.

Why use modules? They let you compile a small, fast kernel, then install and remove device drivers on demand. Without modules, the Linux kernel could become bloated and resemble a certain commercial OS.

First, I recommend you compile a base kernel including all essential capabilities for your system without modules. I know this sounds like we are going backwards, but you don't want to lose the ability to boot up because you messed up your modules. The README in /usr/src/linux is your guide, but basically you execute the command:

make mrproper; make xconfig

(or menuconfig or config) to include all needed capabilities, then run:

make dep; make clean; make zImage
Save your kernel configuration to a file named kernelconf.base, in case you need to recompile. The xconfig menu prompts you to save and load configuration files. If you use menuconfig or config, the current configuration is in the file /usr/src/linux/.config; copy that file to kernelconf.base. If you configured too big a kernel, final compilation will fail. If this happens, execute make bzImage instead of zImage. Your compiled kernel will be in the directory /usr/src/linux/arch/i386/boot.

You might have made a mistake in compiling your base kernel, so don't throw away your old one. If you are running LILO, rename your new kernel to zImage.base and copy it to the location of your current kernel, usually / or /boot. Add a section to /etc/lilo.conf that lets you select either your default or base kernel on bootup. My lilo.conf is shown in Listing 1, minus some comment lines.

The important addition to lilo.conf is the last section (#base kernel) which tells LILO about your new kernel. Also, be sure lilo.conf has prompt and timeout lines. Now execute lilo and then reboot. LILO will pause, giving the prompt boot:. If you hit TAB, you will be given the choices linux and base. Enter base and your new kernel will boot. You may get complaining messages about bad module dependencies, but if your base kernel is complete, that shouldn't bother you. If something goes wrong, reboot and enter linux (or just wait through the timeout interval) and your old kernel will boot. Make sure you have a working base kernel before proceeding. With this approach, you never burn your bridges (or kernel) behind you.

If you don't use LILO, make a boot floppy for your base kernel. To do this, insert a floppy and execute make zdisk, instead of zImage.

You are now ready to compile a kernel with modules tailored to your system. Execute the same commands as above, but when you execute xconfig or menuconfig, pick some features to compile as modules. I suggest you experiment first by picking nice-to-have, but not-necessary, modules to add to zImage.base. Good choices might be printer support or floppy support (unless you are booting from the floppy). Save your configuration as kernelconf.mod in case you need to go back. Also, write down which modules you are compiling. To know exactly which modules are compiled, I suggest you move or delete your old modules (if any). The Caldera release includes a lot of modules. They are in /lib/modules/2.0.29. I moved my old ones into subdirectories rather than deleting them, in case I needed to back up. If you are working with a different release of the kernel, instead of subdirectory 2.0.29 you will have a subdirectory corresponding to your release number.

After executing make zImage, run:

make modules; make modules_install

As before, go (using cd) to the directory containing zImage, rename it zImage.mod and move it to the directory where LILO will look for it. Put a new section at the bottom of lilo.conf to let you boot this kernel with the label modules. If you don't use LILO, make another zDisk.

Now, execute depmod -a. This creates /lib/modules/2.0.29/modules.dep, needed by module utilities. Next, execute the following:

modprobe -c | grep -v '^path' > /etc/conf.modules

This command sets up another file needed by the module utilities.

Now reboot, choosing label modules at the boot prompt. Next, move to the /etc/modules/2.0.29 directory. It should contain a file with a very long name like the following:

#1 Tue Feb 11 20:36:48 MET 1997.default

This file is read at boot time by /etc/rc.d/rc.modules. [Debian and Red Hat use /etc/modules—Ed.] It contains a list of the default modules loaded when the kernel boots. You need to change both the name and the contents. Fixing the name is the hard part. In directory /etc/modules/2.0.29, execute the commands:

FILE=`uname -v`.default
cp "#1 Tue"* "$FILE"
This magic creates a file with the name that rc.modules will look for on bootup. The name is based on the time the kernel was compiled. If you recompile the kernel, you must repeat the magic.

Edit this file to contain just the modules you want loaded at bootup. For example, it might contain the lines

floppy
lp

which would load the floppy and printer modules, assuming you compiled them as modules. To get your editor to accept this file, you may need to put quotes around the name.

To load a module manually, execute insmod with your module name as argument. To remove it, execute rmmod with your module name as argument. To tell which modules are currently loaded, execute lsmod.

The best toy is kerneld; it automatically loads and unloads modules as needed. Assume you have compiled the floppy driver as a module. Check whether it is loaded by executing lsmod. If it is, remove it by executing rmmod floppy. Then execute kerneld. Now execute mount /mnt/floppy (or whatever mounts your floppy). Magically, kerneld installs the floppy module when needed. It will also uninstall modules which haven't been used for a while, keeping your kernel lean and mean.

You now know enough to experiment with modules without crashing your kernel on bootup. Read the Module mini-HOWTO, the kerneld mini-HOWTO, and the man pages for the utilities to become a real expert. Happy moduling!

Resources

David B. Nelson (nelson@er.doe.gov) manages scientific research at the U.S. Department of Energy. Before that, he earned his living as a theoretical plasma physicist. He started programming on the IBM 650 using absolute machine language and later graduated to CDC, DEC and Cray machines for his research, but thinks Linux is the most fun. He and his wife, Kathy, live near Washington DC; they enjoy tennis, skiing, sailing, music, theater and good food.

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

linux

On October 2nd, 2007 enigmah video switcher download (not verified) says:

does bug? report a Why count Is lsmod use modules? -1 of this for some . Best regards.

Featured Videos

Email is one of the least private and least secure forms of communication, although few people realize this. MixMaster is one way to allow secure, anonymous communication even over the very public medium of email. This tutorial will get you started with MixMaster quickly and easily.

In case you were wondering about the fun side of Linux World Expo, we thought we'd give you a peek at our shenanigans. We at Linux Journal love what we do so much, that we can't help but have a ball wherever we go.

From the Magazine

September 2008, #173

Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox is not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.

Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.

Read this issue