¡Esta es una revisión vieja del documento!
Users can add or remove functionalities to the Linux kernel while the system is running. These “programs” that can be added to the kernel at runtime are called “module” and built into individual files with .ko (Kernel object) extension. A kernel module can be modified without need of recompiling the full kernel.
Linux driver modules can be found in: /lib/modules/<version>/kernel/drivers/ where <version> would be the output of the command uname -r on the system.
lsmod - List the available loaded modules.cat /proc/modules - List the available loaded modules.lsmod | egrep -v “\s0” - List loaded modules.insmod module - Load the module specified by module.modprobe module - Load the module along with its dependencies.rmmod module - Remove/Unload the module.modprobe module - Install kernel module.modprobe -rv module - Remove the module.modinfo module - Gives information about the module.modinfo -n module - Gives the path were the module files are located.
depmod command generates modules.dep and map files.
Linux kernel modules can provide services (called “symbols”) for other modules to useusing one of the EXPORT_SYMBOL variants in the code). If a second module uses thissymbol, that second module clearly depends on the first module. These dependencies can getquite complex.
depmod creates a list of module dependencies by reading each module underlib/modules/version and determining what symbols it exports and what symbols it needs. By default, this list is written to modules.dep, and a binary hashed version namedmodules.dep.bin, in the same directory.
printk() is the equivalent of printf() for kernel modules.A device driver contains at least two functions:
These are specified as the init and exit functions, respectively, by the macros module_init() and module_exit(), which are defined in the kernel header module.h.
# include <linux/module .h >
# include <linux/version .h >
# include <linux/kernel .h >
static int __init init_mod ( void ) /* Constructor */
{
printk ( KERN_INFO " Module1 started ...\ n ");
return 0;
}
static void __exit end_mod ( void ) /* Destructor */
{
printk ( KERN_INFO " Module1 ended ...\ n ");
}
module_init ( init_mod );
module_exit ( end_mod );
MODULE_LICENSE (" GPL ");
MODULE_AUTHOR (" Uppsala University ");
MODULE _DESCRIPTION (" Test Driver Module ");
To build a module or driver, you need to have the kernel source (or, at least, the kernel headers) installed on your system. The kernel source is assumed to be installed at /usr/src/linux. If it’s at any other location on your system, specify the location in the KERNEL_SOURCE variable in the Makefile.
Example of Makefile:
MOD_NAME := veikk BUILD_DIR := /lib/modules/$(shell uname -r)/build obj-m := $(MOD_NAME).o all: make -C $(BUILD_DIR) M=$(CURDIR) modules chmod +x ./config.sh clean: make -C $(BUILD_DIR) M=$(CURDIR) clean install: make -C $(BUILD_DIR) M=$(CURDIR) modules_install depmod modprobe veikk uninstall: modprobe -r $(MOD_NAME) rm $(shell modinfo -n veikk) depmod
With this Makefile the commands would be:
For install_
make sudo make install clean
For uninstall:
sudo make uninstall