Manipulating Kernel Modules
Kernel modules are pieces of code that can be loaded and unloaded into the kernel at runtime. They extend the kernel's capabilities without the need to reboot the system. Understanding how to manipulate kernel modules is crucial for managing system functionality and troubleshooting issues.
Key Commands for Manipulating Kernel Modules
Loading Modules:
modprobe: Loads a module and its dependencies.insmod: Loads a module without resolving dependencies.
Examples:
shsudo modprobe module_name sudo insmod /path/to/module.koUnloading Modules:
modprobe -r: Removes a module and its dependencies.rmmod: Removes a module without resolving dependencies.
Examples:
shsudo modprobe -r module_name sudo rmmod module_nameListing Modules:
lsmod: Lists all currently loaded modules.
Example:
shlsmodModule Information:
modinfo: Displays information about a module, such as its dependencies, parameters, and description.
Example:
shmodinfo module_nameViewing Kernel Logs:
dmesg: Displays kernel and module-related messages, useful for debugging module loading issues.
Example:
shdmesg | grep module_name
Loading Kernel Modules at Boot
To ensure kernel modules are loaded at boot, various methods can be employed depending on the Linux distribution and init system. Here are some common methods:
Using
/etc/modules(Debian-based Systems):- Add the module names to this file, one per line.
 
Example
/etc/modules:plaintext# /etc/modules: kernel modules to load at boot time. fuse vboxdrvUsing
/etc/modprobe.d/Configuration Files:- Create a 
.conffile in this directory to specify modules to load and their options. 
Example
/etc/modprobe.d/custom.conf:plaintextinstall dummy /sbin/modprobe --ignore-install dummy options dummy numdummies=2- Create a 
 Using Systemd:
- Create a custom systemd service to load modules at boot.
 
Example
/etc/systemd/system/load-modules.service:ini[Unit] Description=Load Custom Kernel Modules After=network.target [Service] Type=oneshot ExecStart=/sbin/modprobe dummy ExecStart=/sbin/modprobe vboxdrv RemainAfterExit=true [Install] WantedBy=multi-user.targetEnable the Service:
shsudo systemctl enable load-modules.service sudo systemctl start load-modules.service
Managing Module Dependencies and Options
1. Specifying Module Options:
- Module options can be set in files within 
/etc/modprobe.d/. 
Example /etc/modprobe.d/custom_options.conf:
plaintextoptions module_name option_name=option_value
2. Blacklisting Modules:
- Prevent certain modules from loading automatically.
 
Example /etc/modprobe.d/blacklist.conf:
plaintextblacklist module_name
Example Scenario
Loading and Configuring the dummy Module:
Load the Module:
shsudo modprobe dummySet Module Options: Create a configuration file
/etc/modprobe.d/dummy.conf:plaintextoptions dummy numdummies=2Ensure the Module Loads at Boot: Add the module to
/etc/modules:plaintextdummyVerify the Module is Loaded:
shlsmod | grep dummyCheck Kernel Messages:
shdmesg | grep dummy
Summary
Manipulating kernel modules involves loading, unloading, listing, and configuring modules to extend kernel functionality dynamically. Key commands include modprobe, insmod, rmmod, and lsmod. Modules can be configured to load at boot using /etc/modules, /etc/modprobe.d/, systemd services, and other methods. Proper management of module dependencies and options ensures a stable and functional system.