Loading Modules


Very early versions of Linux had all the necessary kernel drivers compiled into the kernel. This meant that the kernel knew exactly what was supported. If you needed to add a new device, you would need to compile a new kernel.

Unfortunately, including every driver in the kernel led to a big problem: the kernel became too large. Back in the days of floppy disks, it would take two 1.44 MB disks to boot Linux-one for the kernel and the other for the rest of the operating system. The kernel developers introduced compressed kernels, but even those became too large for floppy disks.

Fortunately, the barbaric days of compiling all desired modules into the kernel are long gone. Today Linux uses loadable kernel modules (LKMs). Each LKM can be placed in the kernel as needed. This keeps the kernel small and fast. Some LKMs can even perform a check and see if they are required. If you don't have a SCSI card on your computer, then the SCSI LKM driver won't bother to load and won't consume kernel resources. Usually hardware is found through device identifiers, but sometimes you need to tell the operating system to enable the device.

Ubuntu includes a healthy selection of common and uncommon kernel modules. If the device has any type of stable Linux support, then it is very likely that Ubuntu has the LKM on the system.

Viewing Modules

The basic command to see what modules are currently loaded is lsmod. Running lsmod displays the LKM's common name, size of the LKM, and any other LKMs that depend on it. For example:

 $ lsmod | head Module                  Size  Used by floppy                 64676  0 rfcomm                 43604  0 l2cap                  28192  5 rfcomm bluetooth              54084  4 rfcomm,l2cap ppdev                   9668  0 speedstep_lib           4580  0 cpufreq_userspace       6496  0 cpufreq_stats           6688  0 freq_table              4928  1 cpufreq_stats 

This shows that the bluetooth module is loaded and is in use by the rfcomm and l2cap modules. A second command, modprobe, can be used to show the actual LKM files.

 $ modprobe -l bluetooth /lib/modules/2.6.15-26-686/kernel/net/bluetooth/bluetooth.ko 

The modprobe command can also list available modules-not just ones that are loaded. For example, to see all the asynchronous transfer mode (ATM) network drivers, you can use:

 $ modprobe -l -t atm /lib/modules/2.6.15-26-686/kernel/net/atm/pppoatm.ko /lib/modules/2.6.15-26-686/kernel/net/atm/mpoa.ko /lib/modules/2.6.15-26-686/kernel/net/atm/lec.ko /lib/modules/2.6.15-26-686/kernel/net/atm/br2684.ko /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/xusbatm.ko /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/usbatm.ko /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/speedtch.ko /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/cxacru.ko /lib/modules/2.6.15-26-686/kernel/drivers/atm/zatm.ko /lib/modules/2.6.15-26-686/kernel/drivers/atm/uPD98402.ko /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko ... 

The -t atm parameter shows all modules with the ATM tag. LKMs are stored in an organized directory, so the tag indicates the directory name. This is different than using modprobe -l ‘*atm*‘ since that will only show modules containing "atm" in the LKM file name.

Installing and Removing Modules

Modules are relatively easy to install. The insmod command loads modules, and rmmod removes modules. The modprobe command actually uses insmod and rmmod, but adds a little more intelligence. The modprobe command can resolve dependencies and search for modules.

As an example, let's look at the suni.ko ATM driver (you probably do not have it installed and you probably don't need it). Listing 3-1 shows different queries for the driver, installing the driver, and removing it.

Note 

Asynchronous Transfer Mode (ATM) network cards are uncommon on home PCs, so this is a good type of device driver to play with when learning how to load and unload LKMs. If we used a common driver for this example, then you could end up disabling your floppy drive, printer, or other device. If you do happen to have a Saturn User Network Interface (SUNI) ATM card, then consider using a different driver for this example, such as pppoatm.

Listing 3-1: Sample LKM Queries and Management

image from book
     $ lsmod | grep suni     [none found]     $ modprobe -l -t atm  # show all ATM modules     /lib/modules/2.6.15-26-686/kernel/net/atm/pppoatm.ko     /lib/modules/2.6.15-26-686/kernel/net/atm/mpoa.ko     /lib/modules/2.6.15-26-686/kernel/net/atm/lec.ko     /lib/modules/2.6.15-26-686/kernel/net/atm/br2684.ko     /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/xusbatm.ko     /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/usbatm.ko     /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/speedtch.ko     /lib/modules/2.6.15-26-686/kernel/drivers/usb/atm/cxacru.ko     /lib/modules/2.6.15-26-686/kernel/drivers/atm/zatm.ko     /lib/modules/2.6.15-26-686/kernel/drivers/atm/uPD98402.ko     /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko     ...     $ modprobe -l '*suni*'  # Show only the suni.ko module     /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko     $ modprobe -l -a 'suni'  # Show all suni modules     /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko     $ sudo modprobe -a suni  # install all suni modules     $ lsmod | grep suni  # show it is installed     suni                    7580  0     $ sudo modprobe -r suni  # remove it     $ lsmod | grep suni  # show it is removed     [none found] 
image from book

Note 

Using modprobe -l without any other parameters will list every module on the system.

The installation step could also be accomplished using

 sudo insmod /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko 

Similarly removal could also use any of the following commands:

 sudo rmmod /lib/modules/2.6.15-26-686/kernel/drivers/atm/suni.ko sudo rmmod suni.ko sudo rmmod suni 

To make the installation permanent, you can either add the module name to /etc/modules or /etc/modprobe.d/. (See the man pages for modules and modprobe.conf.) In general, /etc/modules is simpler for adding a new module, but the /etc/modprobe.d/ configuration files provide more control.

Optimizing Modules

If you're trying to streamline your system, you may not want to have all of the various modules installed or accessible. Although unused modules take virtually no resources (even if they are loaded into memory), systems with limited capacity or that are hardened for security may not want unnecessary LKMs. Between the lsmod and modprobe -l commands, you can identify which modules are unnecessary and either remove them from the system or just not load them.

For example, if you do not have a printer on your parallel port, then you probably do not need the lp module loaded. Similarly, if you want to disable the floppy disk, you can remove that driver, too.

 sudo modprobe -r lp sudo modprobe -r floppy 

You can make these changes permanent by removing lp from /etc/modules and adding both lp and floppy to /etc/modprobe.d/blacklist, or if you need the disk space then you can just delete the drivers from the system (use modprobe -l lp and modprobe -l floppy to find the files).



Hacking Ubuntu
Hacking Ubuntu: Serious Hacks Mods and Customizations (ExtremeTech)
ISBN: 047010872X
EAN: 2147483647
Year: 2004
Pages: 124
Authors: Neal Krawetz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net