3. Loadable Modules

Loadable kernel modules can save memory and ease configuration. The scope of modules has grown to include filesystems, ethernet card drivers, tape drivers, printer drivers, and more.

Loadable modules are pieces of kernel code which are not linked (included) directly in the kernel. One compiles them separately, and can insert and remove them into the running kernel at almost any time. Due to its flexibility, this is now the preferred way to code certain kernel features. Many popular device drivers, such as the PCMCIA drivers and the QIC-80/40 tape driver, are loadable modules.

See the Module-HOWTO at "http://www.tldp.org/HOWTO/Module-HOWTO" .

And see these man pages


	bash# rpm -i /mnt/cdrom/Redhat/RPMS/modutils*.rpm
	bash# man lsmod
	bash# man insmod
	bash# man rmmod
	bash# man depmod
	bash# man modprobe
      
For example to load the module /lib/modules/2.4.2-2/kernel/drivers/block/loop.o , you would do :

	bash# man insmod
	bash# modprobe loop
	bash# insmod loop
	bash# lsmod
      
You can set the PATH which the insmod searches in /etc/modules.conf.

3.1. Installing the module utilities

You can install the Module Utilities RPM with:


	bash# rpm -i /mnt/cdrom/Redhat/RPMS/modutils*.rpm
        

insmod inserts a module into the running kernel. Modules usually have a .o extension; the example driver mentioned above is called drv_hello.o , so to insert this, one would say ` insmod drv_hello.o '. To see the modules that the kernel is currently using, use lsmod . The output looks like this: blah# lsmod Module: #pages: Used by: drv_hello 1 ` drv_hello ' is the name of the module, it uses one page (4k) of memory, and no other kernel modules depend on it at the moment. To remove this module, use ` rmmod drv_hello '. Note that rmmod wants a module name, not a filename; you get this from lsmod 's listing. The other module utilities' purposes are documented in their manual pages.

3.2. Modules distributed with the kernel

As of version 2.0.30, most of everything is available as a loadable modules. To use them, first make sure that you don't configure them into the regular kernel; that is, don't say y to it during ` make config '. Compile a new kernel and reboot with it. Then, cd to /usr/src/linux again, and do a ` make modules '. This compiles all of the modules which you did not specify in the kernel configuration, and places links to them in /usr/src/linux/modules . You can use them straight from that directory or execute ` make modules_install ', which installs them in /lib/modules/x.y.z , where x.y.z is the kernel release.

This can be especially handy with filesystems. You may not use the minix or msdos filesystems frequently. For example, if I encountered an msdos (shudder) floppy, I would insmod /usr/src/linux/modules/msdos.o , and then rmmod msdos when finished. This procedure saves about 50k of RAM in the kernel during normal operation. A small note is in order for the minix filesystem: you should always configure it directly into the kernel for use in ``rescue'' disks.

3.3. Howto Install Just A Single Module ?

Let us assume that you already did 'make modules' and 'make modules_install'. And later you did 'make clean' to free up disk space. And now, you want to change a "C" file in one of the modules and want to rebuild just that module and copy the module file to /lib/modules. How do you do it?

You can compile just a single module file (say like foo.o) and install it. For this simply edit the Makefile and change the SUBDIRS to add only those directories you are interested.

For an example, if I am interested in installing only fs/autofs module, then I do the following :


	cd /usr/src/linux
	cp Makefile Makefile.my
	vi Makefile.my
	# And comment out the line having 'SUBDIRS' and add the
	# directory you are interested, for example like fs/autofs as below :
		#SUBDIRS	=kernel drivers mm fs net ipc lib abi crypto
		SUBDIRS		=fs/autofs
	# Save the file Makefile.my and give -
	make -f Makefile.my modules
	# This will create module autofs.o
	# Now, copy the module object file to destination /lib/modules
	make -f Makefile.my modules_install
	# And this will do 'cp autofs.o /lib/modules/2.4.18-19.8.0/kernel/fs/autofs'
        

Learn more about Makefile and make. See the manual for GNU make at

  • "http://www.gnu.org/manual/make" .

  • University of Utah Makefile "http://www.math.utah.edu/docs/info/make-stds_toc.html"

  • University of Hawaii Makefile "http://www.eng.hawaii.edu/Tutor/Make"

  • In Linux - man make

  • In Linux - info make

Get familiar with the Makefile which makes the modules. The Makefile has module line like


	modules: $(patsubst %, _mod_%, $(SUBDIRS))
        

The patsubst function has the syntax $(patsubst pattern,replacement,text). It uses the percent symbol ([percnt]) the same way pattern rules do - as a string which matches in both the pattern and the replacement text. It searches the text for whitespace-separated words that match the pattern and substitutes the replacement for them.

This makefile includes shell functions as well as standard make functions. The syntax for a shell function is $(shell command). This returns the output of the shell function (stripping new lines).