How does one automatically load modules during system startup in Linux?

Source: Internet
Author: User
The following is what I wrote when I was studying Linux. Later I carefully studied RC. sysinit and found that I only need to modify the following places. It doesn't have to be so troublesome:
RC. sysinit has the following code:
# Load other user-defined modules
For file in/etc/sysconfig/modules/*. modules; do
[-X $ file] & $ File
Done

# Load modules (for backward compatibility with vars)
If [-F/etc/rc. Modules]; then
/Etc/rc. Modules
Fi

It can be seen that you only need to configure either of the two places (take loading the fuse kernel module as an example)
(1) Create the *. Modules file under/etc/sysconfig/modules/. Refer to the existing *. Modules file. For example, I wrote the Create File My. modules with the content modprobe Fuse
Remember the last chmod 755 my. Modules
(2) Add modprobe fuse to/etc/rc. modules. If no modprobe fuse exists, create the file.
Then reboot, lsmod | grep fuse verify it and then OK.
====================

Automatically load kernel modules:

To find out how to automatically load modules when the system starts, I have searched for a long time. Many people on the Internet have asked this question, but there is no correct answer, either in the Chinese community or in the English community, the answer is nothing more than about modprobe. conf and modprobe should be discussed, or try different methods for specific problems. Some also suggest writing modprobe modulename into Rc. local, but never thought, RC. local execution is placed behind the entire startup sequence, and init is started. d. The service defined below is in RC. if a service needs to use this module, it will not work.

During LVS testing, because the kernel (2.6.21-1) of my fedora7 does not load the ip_vs module by default, and the kernel contains compiled ipvs-related modules, put them in: /lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/, which is shown below:
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_dh.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_ftp.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_lblc.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_lblcr.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_lc.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_nq.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_rr.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_sed.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_sh.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_wlc.ko
/Lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/ip_vs_wrr.ko
Ip_vs.ko is the basic module of ipvs and cannot work without loading ipvs (errors will be reported when running ipvsadm), while others are the scheduling algorithms of ipvs or the auxiliary modules of specific protocols, it must be loaded when needed.

If the system is manually loaded during running, modprobe ip_vs and modprobe ip_vs_sh are required.

To learn how to automatically load the module (automatically load kernel modules) at system startup, you must first understand the order in which the system is started, all these startup operations are controlled by files and scripts. Because Google and Baidu cannot solve the problem, man modprobe and man modprobe. conf find that they are not files to be modified.

So let's look at laruence'sHttp://linux.vbird.org/"Boot and Shutdown Process and loader ":
1. The entire boot process is
(1) load the BIOS hardware information and obtain the code of the first boot device
(2) read the boot information of the boot loader (grub) of the mbr of the first boot device.
(3) load the OS kernel information, decompress the kernel, and try to drive the hardware.
(4) The kernel executes the INIT program and obtains the Run-Lebel information (such as 3 or 5)
(5) run/etc/rc. d/rc. sysinit in init.
(6) Start the kernel plug-in Module (/etc/modprobe. conf)
(7) Init executes various run-level scripts and starts the service
(8) run/etc/rc. d/rc. Local in init.
(9) run/bin/login and wait for the user login
(10) log in to Shell

It seems that the correct method is to put the modules to be loaded in (5) or (6), but as many people try to modify modprobe on the network. conf fails (for example, in modprobe. install ip_vs...) in Conf ...). So I modified/etc/rc. d/rc. sysinit and loaded it successfully.

Initially try to add modprobe. conf ip_vs at the end of RC. sysinit. After restart, lsmod | grep ip_vs will find that it is loaded automatically.

Then, follow the Loading Method of other modules in RC. sysinit to expand and modify the script file, and add the following section at the end:
# Load LVS ipvs modules
If [-D/lib/modules/$ unamer/kernel/NET/IPv4/ipvs]; then
For module in/lib/modules/$ unamer/kernel/NET/IPv4/ipvs/*; do
Module =$ {module ##*/}
Module =$ {module %. Ko}
Modprobe $ module>/dev/null 2> & 1
Done
Fi
All modules under/lib/modules/2.6.21-1.3194.fc7/kernel/NET/IPv4/ipvs/are automatically loaded. Where:
If statement to check whether the directory of ipvs module exists
The for loop traverses all files in the directory.
Module =$ {module ### */}: where # indicates to delete the character from the front, */indicates to delete the character to the last /, if one # is used, only the first/is deleted /. If the variable is followed by #, the string after # gets the longest (until the end). If the variable is followed by #, the smallest segment is obtained.
Module =$ {module %. Ko}: indicates to delete. Ko from the backend. If the variable is followed by %, the string after % gets the longest (until the beginning). If % is followed, the smallest segment is obtained.
In this way, the module name is obtained after two modifications of multiple modules, that is, the file name does not contain the path or. Ko suffix.
Modprobe $ module>/dev/null 2> & 1: load the module. The output points to an empty device.

After the restart, lsmod | grep ip_vs will get:
Ip_vs_wrr 6977 0
Ip_vs_wlc 6081 0
Ip_vs_sh 6593 0
Ip_vs_sed 6081 0
Ip_vs_rr 6081 0
Ip_vs_nq 5953 0
Ip_vs_lc 5953 0
Ip_vs_lblcr 10565 0
Ip_vs_lblc 9797 0
Ip_vs_ftp 10053 0
Ip_vs_dh 6593 0
Ip_vs 79425 22 ip_vs_wrr, ip_vs_wlc, ip_vs_sh, ip_vs_sed, ip_vs_rr, ip_vs_nq, ip_vs_lc, scheme, scheme, ip_vs_ftp, ip_vs_dh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.