Keywords: modprobe | insmod | Linux kernel module
Abstract: This article explores the difference between modprobe and insmod commands in Linux, focusing on the common 'Module not found' error. It explains why modprobe fails when loading modules from local paths and provides solutions to properly install modules for modprobe usage. Through comparison and practice, it enhances developers' understanding of kernel module loading mechanisms.
Introduction
In Linux kernel development, loading modules is a common task. Two primary commands are used: insmod and modprobe. A frequent issue arises when modprobe returns a FATAL: Module not found error, even though insmod works fine with the same module file. This article will analyze the cause of this error and provide solutions.
Error Analysis
The root of the error lies in modprobe searching for modules in predefined directories, specifically under /lib/modules/$(uname -r)/. Unlike insmod, which can load a module directly from any file path, modprobe relies on the kernel's module directory structure and dependency information generated by depmod. Therefore, if a module is not installed in these directories, modprobe will fail.
Modprobe vs Insmod Comparison
insmod is a low-level command that inserts a module directly from a specified file and does not handle dependencies. In contrast, modprobe is a higher-level utility that resolves dependencies and loads modules from the standard kernel modules directory. This design ensures that modprobe can manage module loading more robustly but requires modules to be installed in the correct location.
Solution: Installing Modules for Modprobe
To use modprobe, the module must be installed into the kernel modules directory. One method is to modify the Makefile to include an install target. For example, add the following to your Makefile:
install:
$(MAKE) -C $(KERNEL_BUILD) M=$(PWD) \
INSTALL_MOD_PATH=$(INSTALL_ROOT) modules_installAfter running sudo make install, the module will be placed in a subdirectory like /lib/modules/$(uname -r)/extra/ or /lib/modules/$(uname -r)/misc/, and depmod will be run to update dependencies. This allows modprobe to find and load the module correctly.
Conclusion
Understanding the difference between modprobe and insmod is crucial for Linux kernel module development. By properly installing modules, you can leverage modprobe's dependency management features, avoid the 'Module not found' error, and improve development efficiency.