3.  Loadable Modules

Everyone who used 'Loadable Modules' really "loves" them!! I like the Loadable Modules, they are really cute and they break up a large task into tiny manageable pieces. When you start using them, I can bet that you will fall in love them! Loadable Module is peculiar only to Linux. Linux is the first operating system in the world to introduce the concept of Loadable Module. No other operating system has this concept, but in near future other operating systems may implement this.

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? And, you do not want to give 'make modules' as that will rebuild everything and will take about 2 to 3 hours!

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.

A good example is - I found that my default kernel did not include the NTFS filesystem support (I did make clean after make modules. Darn and Damn it!). So, I decided to compile the loadable module of NTFS. I did not want to compile the whole set (as it will take me about 2 hours), so I followed the procedure below and compiled just the fs/ntfs and did 'insmod ntfs'. It just took me about 5 minutes!!

Another example: If I am interested in installing only fs/autofs module, then I do the following :

	cd /usr/src/linux
	mv Makefile Makefile.original
	cp Makefile.original Makefile.my
	ln -s Makefile.my Makefile   # Because some files underneath still need "Makefile"
	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
	# DANGER: Do NOT DO THIS - "make -f Makefile.my modules_install" This may
	# clean up other good ones in /lib/modules !! But just copy as below:
	cp autofs.o /lib/modules/2.4.18-19.8.0/kernel/fs/autofs
	  
	# Now, reset everything back to normal
	rm Makefile  # This is a link to Makefile.my
	ln -s Makefile.original Makefile
	  
	# Record your changes to the config file for future use
	# Edit the /usr/src/linux/.config file and set the item as module
	cd /usr/src/linux
    mkdir /usr/src/kernelconfigs ;
    cp /usr/src/linux/.config  /usr/src/kernelconfigs/.config.save;
	cp /usr/src/linux/.config  /usr/src/linux/configs/.config.save  # ExtraSafe
	cp /boot/config*  /usr/src/linux/configs/  # ExtraSafe
	vi /usr/src/linux/.config 
	# And change the config parameter. For example in case of my
	# ntfs module I did CONFIG_NTFS_FS=m  to indicate as module.
        

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

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).