In this lesson, you will learn how to dynamically manage the lifecycle of Linux kernel modules at runtime using core utility commands.
After compiling your kernel module, the final step in the development loop is moving it from your host machine to the target embedded system. Since you don't want to reboot your entire system every time you make a code change to a driver, Linux provides a mechanism to insert and remove code directly into the running kernel memory space.
The Runtime Toolkit
To manipulate these modules, we rely on three primary command-line tools:
lsmod: This command parses/proc/modulesto list all currently loaded kernel modules. It shows you the module name, the memory size it occupies, and its "use count"—which tracks how many other processes or modules are currently relying on that driver.insmod: This utility manually inserts a module file (typically with a.koextension) into the kernel. It maps the module’s instructions into kernel space and resolves symbol dependencies.rmmod: This command triggers the removal of a module. It invokes the module's cleanup function (the one you defined withmodule_exit) and unloads the code from memory.
Practical Workflow
Suppose you have a compiled module named my_driver.ko. You can deploy and test it on your target using the following sequence:
# 1. Verify if the module is already present
lsmod | grep my_driver
# 2. Insert the module into the kernel
sudo insmod my_driver.ko
# 3. Confirm status and check for errors in dmesg
lsmod | grep my_driver
dmesg | tail
# 4. Remove the module when testing is complete
sudo rmmod my_driver
When you use insmod, the kernel executes the module_init routine. If your module fails to load, it is often because of unresolved symbols or missing hardware resources. Conversely, rmmod will fail if your module is currently "in use" (e.g., a file descriptor is still open). In such cases, you must ensure all user-space applications have released their handles before the kernel will permit the module to be unloaded.
Key Takeaway: Dynamic module management allows you to iterate on kernel drivers without system reboots by using insmod, rmmod, and lsmod to control memory residency.