modprobe command and its difference from insmod & depmod

1. Detailed explanation of modprobe command

The modprobe tool can intelligently add and delete a module. The reason why it is intelligent is that it can parse the dependencies between modules through some predefined rules configured and automatically load dependent modules.

modprobe will search for the module to be loaded and the corresponding dependency rules from the /lib/modules/uname -r directory. In addition to this directory, modprobe also has a configuration directory /etc/modprobe.d. In this configuration directory, users can customize Some modprobe behavior defined.

1.1 Dependency configuration
modprobe searches for dependencies from the /lib/modules/uname -r/modules.dep file. By default, modprobe also searches for modules in the /lib/modules/uname -r/ directory for loading.

As shown below, taking my machine as an example, I intercept part of the dependency file /lib/modules/4.19.125/modules.dep;

Insert image description here

extra/host/xhci-plat-hcd.ko: extra/host/xhci-hcd.ko
extra/gadget/function/usb_f_ss_lb.ko: extra/gadget/libcomposite.ko
extra/gadget/function/usb_f_uvc.ko: extra/gadget/libcomposite.ko
extra/gadget/function/u_ether.ko:
extra/serial/usbserial.ko:
extra/serial/ch341.ko: extra/serial/usbserial.ko
extra/gadget/libcomposite.ko:
extra/gadget/function/usb_f_acm.ko: extra/gadget/function/u_serial.ko extra/gadget/libcomposite.ko
extra/gadget/function/usb_f_mass_storage.ko: extra/gadget/libcomposite.ko
extra/gadget/function/usb_f_rndis.ko: extra/gadget/function/u_ether.ko extra/gadget/libcomposite.ko
extra/gadget/function/usb_f_serial.ko: extra/gadget/function/u_serial.ko extra/gadget/libcomposite.ko
extra/gadget/function/u_serial.ko:
extra/serial/pl2303.ko: extra/serial/usbserial.ko
extra/cdns3/cdns3.ko:
extra/typec/axera_sgm7220.ko:
extra/storage/uas.ko: extra/storage/usb-storage.ko
extra/uio/uio_pdrv_genirq.ko:
extra/host/xhci-hcd.ko:
extra/typec/tcpci.ko: extra/typec/tcpm.ko
extra/spi/spidev.ko:
extra/misc/usbtest.ko:
extra/class/cdc-acm.ko:
extra/storage/usb-storage.ko:
extra/typec/tcpm.ko:

1.2 modprobe.d configuration

The key functions defined in /etc/modprobe.d are as follows:

Insert image description here

Both modprobe and insmod are used to load modules under linux. The difference between the two usages is as follows:
(1) insmod needs to specify the path of the module to be loaded, and only load the specified module. If the specified module depends on other modules, insmod It will not be added automatically. The method of using insmod is as follows: insmod a certain path/xxx.ko
(2) modprobe is more intelligent than insmod. When using modprob to load a module, you only need to specify the module name instead of the module path. When using modprobe to load the kernel module , if the loaded module depends on other modules, modprobe will automatically load the dependent modules, for example, we need to load module A, but module A depends on module B, then when we use modprobe to load kernel module A, modprobe will load the kernel first Module B, and then load kernel module A. The usage is as follows: modprobe xxx

Q1: When modprobe loads a kernel module, it only needs to specify the module name without a path. How does modprobe know the path of the kernel module to be loaded?

Q2: How does modprobe determine whether the loaded module depends on other modules when loading a module?

Question 1: The file for modprobe to load the kernel must be located in the /lib/modules/(shell uname -r) directory. That is, the reason why modprobe does not need to specify a path to load the kernel module is because the modprobe command has a default loading path.

Question 2: When modeprobe loads the kernel module, it depends on the /lib/modules/(shell uname -r)/modules.dep file. The modules.dep file lists the kernel files that modproe can load and the files that the kernel file depends on. Suppose a kernel module is located in the /lib/modules/(shell uname -r) directory, but the module is not added to the modules.dep file and cannot be loaded through the modeprobe command. So the question is, how is the modules.dep file generated? The modules.dep file is generated by depmode. If you place a new kernel module XXX in the /lib/modules/(shell uname -r) directory and want to load the module XXX through the modprobe command, then put the kernel module XXX in /lib/ modules/(shell uname -r) directory, you need to run the depmode command, and then run modeprobe XXX to successfully load the kernel module, otherwise an error will be reported: modeprobe XXX not found.

The role of depmod:
As mentioned above, it is used to generate modules.dep that modprobe depends on.

Guess you like

Origin blog.csdn.net/qq_41483419/article/details/132708173