13.1. Objective 1: Manage/Query Kernel and Kernel Modules at Runtime With Linux, code for system devices can be compiled into the kernel. Because the kernel already has built-in support for most devices, it is said to be monolithic, since the kernel manages all system hardware by itself. Monolithic kernels aren't very flexible because a new kernel build is necessary for new peripheral devices to be added to the system. Monolithic kernels also have the potential to become "bloated" by drivers for hardware that aren't physically installed. Instead, most users run modular kernels, in which device drivers are inserted into the running kernel as needed. Modular configurations can adapt to changes in the hardware and provide a convenient way of upgrading driver software while a system is running. 13.1.1. Module Files Linux kernel modules are object files (.o) produced by the C compiler but not linked into a completed executable (in this case, the kernel executable file). Most modules are distributed with the kernel and compiled along with it. Because they are so closely related to the kernel, separate sets of modules are installed when multiple kernels are installed. This reduces the likelihood that a kernel module will be inserted into the wrong kernel version. Modules are stored in /lib/modules/kernel-version, where kernel-version is the string reported by uname -r, such as 2.4.18-10. In this example, the modules directory would be /lib/modules/2.4.18-10. Multiple module hierarchies may be available under /lib/modules if multiple kernels are installed. Within each kernel-version hierarchy is a kernel subdirectory that contains the kernel modules, sorted into additional subdirectories by category. Tip: The path to these additional category subdirectories changed for the 2.4 kernel series to include a kernel subdirectory to contain all the categories. Prior to 2.4, all of the category subdirectories were located directly under /lib/modules/kernel-version. Some of the categories are:
block Modules for a few block-specific devices such as RAID controllers or IDE tape drives.
cdrom Device driver modules for nonstandard CD-ROM devices.
fs Device drivers for filesystems such as MS-DOS (the msdos.o module).
ipv4 Kernel features having to do with TCP/IP networking, such as the Netfilter firewall modules.
misc Anything that doesn't fit into one of the other subdirectories ends up here. Note that no modules are stored at the top of this tree.
net Network interface driver modules.
scsi Driver modules for the SCSI controller.
video Driver modules for video adapters. 13.1.2. Manipulating Modules A module is dynamically linked into the running kernel when it is loaded. Much of Linux kernel module handling is done automatically. However, there may be times when it is necessary for you to manipulate the modules yourself, and you may come across the manipulation commands in scripts. For example, if you're having difficulty with a particular driver, you may need to get the source code for a newer version of the driver, compile it, and insert the new module in the running kernel. The commands listed in this section can be used to list, insert, remove, and query modules.
Syntax lsmod
Description For each kernel module loaded, display its name, size, use count, and a list of other referring modules. This command yields the same information as is available in /proc/modules.
Example Here, lsmod shows that quite a few kernel modules are loaded, including filesystem (vfat, fat), networking (3c59x), and sound (soundcore, mpu401, etc.) modules, among others: # lsmod Module Size Used by radeon 112996 24 agpgart 45824 3 parport_pc 18756 1 (autoclean) lp 8868 0 (autoclean) parport 36480 1 (autoclean) [parport_pc lp] e100 59428 1 ohci1394 19976 0 (unused) ieee1394 48300 0 [ohci1394] scsi_mod 106168 0 evdev 5696 0 (unused) printer 8832 0 wacom 7896 0 (unused) emu10k1 68104 1 ac97_codec 13512 0 [emu10k1] sound 73044 0 [emu10k1] soundcore 6276 7 [emu10k1 sound] keybdev 2912 0 (unused) mousedev 5428 1 hid 21700 0 (unused) input 5824 0 [evdev wacom keybdev mousedev hid] ehci-hcd 19432 0 (unused) usb-uhci 25964 0 (unused) usbcore 77760 1 [printer wacom hid ehci-hcd usb-uhci] ext3 87240 3 jbd 51156 3 [ext3]
Syntax insmod [options] module
Description Insert a module into the running kernel. The module is located automatically and inserted. You must be logged in as the superuser to insert modules.
Frequently used options
-s Direct output to syslog instead of stdout.
-v Set verbose mode.
Example The msdos filesystem module is installed into the running kernel. In this example, the kernel was compiled with modular support for the msdos filesystem type, a typical configuration for a Linux distribution for i386 hardware. To verify that you have this module, check for the existence of /lib/modules/kernel-version/fs/msdos.o: # insmod msdos /lib/modules/2.2.5-15smp/fs/msdos.o: unresolved symbol fat_add_cluster_Rsmp_eb84f594 /lib/modules/2.2.5-15smp/fs/msdos.o: unresolved symbol fat_cache_inval_inode_Rsmp_6da1654e /lib/modules/2.2.5-15smp/fs/msdos.o: unresolved symbol fat_scan_Rsmp_d61c58c7 ( ... additional errors omitted ... ) /lib/modules/2.2.5-15smp/fs/msdos.o: unresolved symbol fat_date_unix2dos_Rsmp_83fb36a1 # echo $? 1 This insmod msdos command yields a series of unresolved symbol messages and an exit status of 1, indicating an error. This is the same sort of message that might be seen when attempting to link a program that referenced variables or functions unavailable to the linker. In the context of a module insertion, such messages indicate that the functions are not available in the kernel. From the names of the missing symbols, you can see that the fat module is required to support the msdos module, so it is inserted first: # insmod fat Now the msdos module can be loaded: # insmod msdos Use the modprobe command to automatically determine these dependencies and install prerequisite modules first.
Syntax rmmod [options] modules
Description The rmmod command is used to remove modules from the running kernel. You must be logged in as the superuser to remove modules, and the command will fail if the module is in use or being referred to by another module.
Frequently used options
-a Remove all unused modules.
-s Direct output to syslog instead of stdout.
Example Starting with both the fat and msdos modules loaded, remove the fat module (which is used by the msdos module): # lsmod Module Size Used by msdos 8348 0 (unused) fat 25856 0 [msdos] # rmmod fat rmmod: fat is in use In this example, the lsmod command fails because the msdos module is dependent on the fat module. So, to unload the fat module, the msdos module must be unloaded first: # rmmod msdos # rmmod fat The modprobe -r command can be used to automatically determine these dependencies and remove modules and their prerequisites.
Syntax modinfo [options] module_object_file
Description Display information about a module from its module_object_file. Some modules contain no information at all, some have a short one-line description, and others have a fairly descriptive message.
Options
-a Display the module's author.
-d Display the module's description.
-p Display the typed parameters that a module supports.
Examples In these examples, modinfo is run using modules compiled for a multiprocessing (SMP) kernel Version 2.2.5. Your kernel version, and thus the directory hierarchy containing modules, will be different. # modinfo -d /lib/modules/2.2.5-15smp/misc/zftape.o zftape for ftape v3.04d 25/11/97 - VFS interface for the Linux floppy tape driver. Support for QIC-113 compatible volume table and builtin compression (lzrw3 algorithm) # modinfo -a /lib/modules/2.2.5-15smp/misc/zftape.o (c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de) # modinfo -p /lib/modules/2.2.5-15smp/misc/ftape.o ft_fdc_base int, description "Base address of FDC controller." Ft_fdc_irq int, description "IRQ (interrupt channel) to use." ft_fdc_dma int, description "DMA channel to use." ft_fdc_threshold int, description "Threshold of the FDC Fifo." Ft_fdc_rate_limit int, description "Maximal data rate for FDC." ft_probe_fc10 int, description "If non-zero, probe for a Colorado FC-10/FC-20 controller." ft_mach2 int, description "If non-zero, probe for a Mountain MACH-2 controller." ft_tracing int, description "Amount of debugging output, 0 <= tracing <= 8, default 3."
Syntax modprobe [options] module [symbol=value ...]
Description Like insmod, modprobe is used to insert modules. In fact, modprobe is a wrapper around insmod and provides additional functionality. In addition to loading single modules, modprobe has the ability to load modules along with their prerequisites or all modules stored in a specific directory. The modprobe command can also remove modules when combined with the -r option. A module is inserted with optional symbol=value parameters (see "Configuring Modules" in the next section for more on parameters). If the module is dependent upon other modules, they will be loaded first. The modprobe command determines prerequisite relationships between modules by reading modules.dep at the top of the module directory hierarchy (i.e., /lib/modules/2.2.5-15smp/modules.dep). You must be logged in as the superuser to insert modules.
Frequently used options
-a Load all modules. When used with the -t moduletype, all is restricted to modules in the moduletype directory. This action probes hardware by successive module-insertion attempts for a single type of hardware, such as a network adapter (in which case the moduletype would be net, representing /lib/modules/kernel-version/kernel/net). This may be necessary, for example, to probe for more than one kind of network interface.
-c Display a complete module configuration, including defaults and directives found in /etc/modules.conf (or /etc/conf.modules on older systems). The -c option is not used with any other options.
-l List modules. When used with the -t moduletype, list only modules in directory moduletype. For example, if moduletype is net, then modules in /lib/modules/kernel-version/net are displayed.
-r Remove module, similar to rmmod. Multiple modules may be specified.
-s Direct output to syslog instead of stdout.
-t moduletype Attempt to load multiple modules found in the directory moduletype until a module succeeds or all modules in moduletype are exhausted. This action "probes" hardware by successive module-insertion attempts for a single type of hardware, such as a network adapter (in which case moduletype would be net, representing /lib/modules/kernel-version/kernel/net).
-v Set verbose mode.
Example 1 Install the msdos filesystem module into the running kernel: # modprobe msdos Module msdos and its dependency, fat, will be loaded. modprobe determines that fat is needed by msdos when it looks through modules.dep. You can see the dependency listing using grep: # grep /msdos.o: /lib/modules/2.2.5-15smp/modules.dep /lib/modules/2.2.5-15smp/fs/msdos.o: /lib/modules/2.2.5-15smp/fs/fat.o
Example 2 Remove fat and msdos modules from the running kernel, assuming msdos is not in use: # modprobe -r fat msdos
Example 3 Attempt to load available network modules until one succeeds: # modprobe -t net
Example 4 Attempt to load all available network modules: # modprobe -at net
Example 5 List all modules available for use: # modprobe -l /lib/modules/2.2.5-15smp/fs/vfat.o /lib/modules/2.2.5-15smp/fs/umsdos.o /lib/modules/2.2.5-15smp/fs/ufs.o . . .
Example 6 List all modules in the net directory for 3Com network interfaces: # modprobe -lt net | grep 3c /lib/modules/2.2.5-15smp/net/3c59x.o /lib/modules/2.2.5-15smp/net/3c515.o /lib/modules/2.2.5-15smp/net/3c509.o /lib/modules/2.2.5-15smp/net/3c507.o /lib/modules/2.2.5-15smp/net/3c505.o /lib/modules/2.2.5-15smp/net/3c503.o /lib/modules/2.2.5-15smp/net/3c501.o 13.1.3. Configuring Modules You may sometimes need to control elements of a module such as hardware interrupt assignments or Direct Memory Access (DMA) channel selections. Other situations may dictate special procedures to prepare for, or clean up after, a module insertion or removal. This special control of modules is implemented in the configuration file /etc/modules.conf , which controls the behavior of modprobe. The /etc/modules.conf file can contain the following information:
Comments Blank lines and lines beginning with # are ignored.
keep The keep parameter, when found before any path directives, causes the default paths to be retained and added to any paths specified.
depfile=full_path This directive overrides the default location for the module dependency file, modules.dep (described in the next section). For example: depfile=/lib/modules/2.2.14/modules.dep
path=path_directory This directive specifies a directory to search for modules.
options module opt1=val1 opt2=val2 ... Options for modules can be specified using the options configuration line in modules.conf or on the modprobe command line. The command line overrides configurations in the file. module is the name of a single module without the .so extension. Options are specified as name=value pairs, where the name is understood by the module and reported using modinfo -p. For example: options opl3 io=0x388
alias Aliases can be used to associate a generic name with a specific module. For example: alias scsi_hostadapter aic7xxx alias eth0 3c59x alias parport_lowlevel parport_pc
pre-install module command This directive causes some shell command to be executed prior to insertion of module. For example, PCMCIA services need to be started prior to installing the pcmcia_core module: pre-install pcmcia_core /etc/rc.d/init.d/pcmcia start
install module command This directive allows a specific shell command to override the default module-insertion command.
post-install module This directive causes some shell command to be executed after insertion of module.
pre-remove module This directive causes some shell command to be executed prior to removal of module.
remove module This directive allows a specific shell command to override the default module-removal command.
post-remove module This directive causes some shell command to be executed after removal of module. The following is an example of the /etc/modules.conf file: alias scsi_hostadapter aic7xxx alias eth0 3c59x alias parport_lowlevel parport_pc pre-install pcmcia_core /etc/rc.d/init.d/pcmcia start alias sound opl3sa2 pre-install sound insmod sound dmabuf=1 alias midi opl3 options opl3 io=0x388 options opl3sa2 mss_io=0x530 irq=5 dma=0 dma2=1 mpu_io=0x388 io=0x370 On the Exam | While it is important for you to understand the configuration lines in /etc/modules.conf, detailed module configuration is beyond the scope of the LPIC Level 1 Exams. |
13.1.4. Module Dependency File modprobe can determine module dependencies and install prerequisite modules automatically. To do this, modprobe scans the first column of /lib/modules/kernel-version/modules.dep to find the module it is to install. Lines in modules.dep are in the following form: module_name.o: dependency1 dependency2 ... A typical dependency looks like this: /lib/modules/2.2.5-15smp/fs/msdos.o: /lib/modules/2.2.5-15smp/fs/fat.o Here, the msdos module is dependent upon fat. All of the modules available on the system are listed in the modules.dep file and are referred to with their full path and filenames, including their .o extension. Those that are not dependent on other modules are also listed, but without dependencies. Dependencies that are listed are inserted into the kernel by modprobe first, and when all of them are successfully inserted, the subject module itself can be loaded. The modules.dep file must be kept current to ensure the correct operation of modprobe. If module dependencies were to change without a corresponding modification to modules.dep, then modprobe may fail, because a dependency could be missed. As a result, modules.dep is created each time the system is booted. On most distributions, the depmod -a command is called during rc.sysinit: echo "Finding module dependencies" /sbin/depmod -a The depmod -a command re-creates and overwrites modules.dep each time the system is booted. This procedure is also necessary after any change in module dependencies. (The depmod command is actually a link to the same executable as modprobe. The functionality of the command differs depending on which name is used to call it.) On the Exam | Be sure you know what is in modules.dep, as well as what the file is used for, how it is created, and when it is created. Be prepared to cite the consequences of a missing or obsolete modules.dep file. |
|