linux内核动态加载模块(Linuxkerneldynamicloadingmodule)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
linux内核动态加载模块(Linux kernel dynamic loading module)Linux kernel dynamic loading module
Install kernel module:
General steps:
(1) run make menuconfig under /usr/src/linux/, M the items that need to be compiled into modules, save and exit.
(2) running make modules, and this step will generate *.o or *.ko files under /usr/src/linux/.
(3) run make modeules_install to install, and this step copies the generated.O or Ko files to /lib/modules/`uname -r`/.
If you just compile one or more modules, you can use the quick method below:
(1) find the.Config files required to compile the kernel.
In the /usr/src/linux/arch directory, there are several configurations used to compile the kernel. Select the configuration we want, copy it to the /usr/src/linux directory, and rename it to.Config.
CP /usr/src/linux/arch/x86/xxconfig /usr/src/linux/.config
(2) modify the.Config file, remove unnecessary modules, plus the module you want.
Open.Config, there are many XXXX=m items, these are the items to be compiled as modules, because we do not want to compile these modules, so we have to remove all the items of XXXX=m. Then we want to # CONFIG_NTFS_FS module, such as is not set to CONFIG_NTFS_FS=m of course, you can use familiar tools to do this thing.
(3) compile the NTFS module.
In the /usr/src/linux directory, run the command make modules to compile the modules we want.
(4) installation module.
The compiled.O file is in the /usr/src/linux/ directory and is manually copied to the correct directory.
For example, CP /usr/src/linux/fs/ntfs/ntfs.o
/lib/modules/2.2.16-22/fs/
Note: never run the command make modules_install, otherwise it will have serious consequences. It will delete all modules in your system and install only the modules that have just been compiled (ntfs.o).
Two: after the installation is complete, we can load the module:
Several commands related to loading modules in Linux are as follows:
Depmod, modprobe, lsmod
Let's look at the depmod command first:
Depmod is a program used to generate modules.dep and map files. The list of blank lines in the modules.dep file and to'#'at the beginning of the.Depmod will be ignored by each module reads the /lib /modules/version directory to create a record module dependencies. This list is the modules.dep in the
/lib/modules/version directory. Depmod will also create many map files in the /lib/modules/version directory, such as modules.dep, modules.isapnpmap, modules.pcimap, and modules.alias. These files will be used by hotplug.
OPTIONS:
-a, --all, Probe, all, modules., This, option, is, enabled, by,, default, if, no
File, names, are, given, in, the, command-line.
Check all modules. This command is default if you do not specify the name of the module.
-A, --quick, This, option, scans, to, see, if, any, modules,, are, newer, than, the
Modules.dep, file, before, any, work, is, done%3
Look at the modprobe command again:
The modprobe command loads all the required modules based on
the output /lib/modules/version/modules.dep of the depmod -a. You can display modules that can be loaded by modprobe -l. The modprobe module is not specified in the mount path module file, or to take the file suffix.O or.Ko, and insmod needs is the absolute path module directory, and must be a module file name suffix (modulefile.o or modulesfile.ko). The more important use of insmod is to test the correctness of the module, which is generally dependent on modprobe.
Usage: modprobe xxx.ko # load a module
Modprobe -r xxx.ko # uninstall a module
Lsmod:
Lsmod display all the modules currently loaded, equivalent to cat /proc/modules, if you do not set the boot load a module, such as NTFS, then after the start of the implementation of lsmod, the list will be in NTFS this module, then you re implementation of Mount -t NTFS XXX, after the lsmod list will be NTFS this module.
Notice also that lsmod shows the name of the module, not the alias (alias).
Three, there is a "Automatic kernel module loading" in the kernel. The function is compiled into the kernel. When a user attempts to open a certain type of file, the kernel attempts to load the appropriate module as needed. Let's see how the driver automatically loads it:
Each device has information such as Verdon, ID, Device, ID, SubVendor, ID, etc.. And each device driver must specify what kind of Verdon, ID, and Deviece it can be
ID, SubVendor, ID devices provide services. Take the PCI device, for example, to implement this functionality through a
pci_device_id data structure. For example, the pci_device_id of RTL8139 is defined as:
Static, struct, pci_device_id, rtl8139_pci_tbl[] = {
{0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139},
{0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139},
......
}
When the module is installed, depmod generates the following information according to the information of the
rtl8139_pci_tbl in the module, and is saved to the
/lib/modules/uname-r /modules.alias file, which reads as follows:
Alias pci:v000010ECd00008138sv*sd*bc*sc*i* 8139too
Alias pci:v000010ECd00008139sv*sd*bc*sc*i* 8139too
......
In addition, the dependencies between this module are preserved in the /lib/modules/uname-r /modules.dep file as follows:
(here, the path information is omitted. )
8139too.ko:mii.ko
In the boot process, the bus driver will bus protocol bus enumeration (bus driver always integrated in the kernel, can not be loaded according to a module you can enter Bus by make menuconfig
Options, the various buses inside, you can only select Y or N instead of M., and establish a device object for each device. Each bus object has a kset object, each one
The device object is embedded with a kobject object, and the kobject is connected to the kset object so that the bus and bus, the bus, and the device device are organized into a tree structure. When the bus drives
When an application program creates a device object for the scanned device, it initializes the kobject object and connects it to the device tree, and calls kobject_uevent () to add the new setting
Events, and related information (including equipment VendorID, DeviceID, etc.). Send to user mode via netlink. This is detected in the udevd of the user state
Event, you can open /lib/modules/uname-r on the basis of this
information
/modules.alias file, according to
Alias pci:v000010ECd00008138sv*sd*bc*sc*i* 8139too
Knowing that the new scanned device driver module is 8139too. So modprobe knew that the module would be loaded with 8139too, and that modprobe was at the same time
The modules.dep file finds that 8139too relies on mii.ko, and if mii.ko is not loaded, modprobe loads the mii.ko and then loads it
8139too.ko. test
Run in your shell:
PS aux grep udevd | #
# kill -9 25063 and then track udevd, run in shell:
Strace -f /sbin/udevd --daemon #
At this point, we see the output of udevd as follows:
......
Close (8) = 0
Munmap (0xb7f8c000, 4096) = 0
Select (7, [3, 45, 6], NULL, NULL, NULL), we find that udevd is blocked here in the select () function.
The select function prototype is as follows:
Int, select (int, NFDS, fd_set, *readfds, fd_set, *writefds, fd_set, *exceptfds, struct, timeval, *timeout);
The first argument: NFDS represents the largest file descriptor, which is 7 (obviously 6).
The second parameter: readfds is the read file descriptor collection, here is 3,4,5,6.
The third parameter: writefds is the write file descriptor collection, here is NULL.
The fourth parameter: exceptfds is the collection of exception file descriptors, here is NULL.
The fifth parameter: timeout specifies the timeout time, here is NULL. The select function is that if any file in readfds is readable, or any file in witefds can be written, or in the exceptfds
When any file has an exception, it returns. Otherwise, the current process is blocked until the appeal condition is satisfied,
Or because the blocking time is longer than the timeout
specified time, the current process is awakened and select returns
Hui. So, here, udevd waits for 3,4,5,6. These files are readable before they are awakened. Now, run in shell:
PS aux grep udevd | #
Root 27615... Strace, -o, /tmp/udevd.debug, -f, /sbin/udevd, --daemon
Root 27617... /sbin/udevd, --daemon
The udevd process ID is 27617. Now let's look at several files that select is waiting for:
# CD /proc/27615/fd
# LS -l
Udevd standard input, standard output, standard errors are all /dev/null.
0 - /dev/null
1 - /dev/null
2 - /dev/null
Udevd waits on the following files.
3 - /inotify
4 - socket:[331468]
5 - socket:[331469]
6 - pipe:[331470]
7 - > pipe:[331470] because it is inconvenient to insert a 8139 card in operation, so now we use a U disk to do the test, when you insert a U disk, you will see the output of strace, it can be seen from the output
After select returns, udevd calls the modprobe to load the driver module, and calls sys_mknod to establish the corresponding node in the dev directory.
Execve ("/sbin/modprobe", [/sbin/modprobe], "-Q",
"usb:v05ACp1301d0100dc00dsc00dp00"...
......
Mknod ("/dev/sdb", "S_IFBLK|0660", "makedev" (8, 16)) = 0
... here, the modprobe parameter "usb:v05AC..." corresponds to one of the modules in modules.alias.
You can see through the udevmonitor that the kernel sends messages to udevd via netlink and runs in shell:
# udevmonitor --env
Then insert the U disk, and you'll see the relevant messages sent to udevd.
Four, the kernel module loaded configuration:
Sometimes requires a one-time loading a lot of modules, need a place in modprobe unified configuration options, there is a more important document: /etc/modprobe.conf, in openSUSE, many documents about it are under the modprobe.d/ folder and modprobe.conf.local files, include said the other file in
/etc/modprobe.conf, modify their general recommendations the configuration in modprobe.conf.local. /etc/modprobe.conf is the definition of the loading command or the alias of the module, which is used to write the module. Man modprobe.conf:
Alias my-mod really_long_modulename defines an easy to use alias for the module
Options modulename option... Add options when loading modules
Install modulename command... Uses the command defined by itself to load the specified modules, such as install, Fred, /sbin/modprobe, Barney, /sbin/modprobe
"--ignore-install Fred" uses "/sbin/modprobe Barney",
"/sbin/modprobe", every time a Fred module is loaded
"--ignore-install Fred" command
Remove modulename command... Deletes the specified module with
the command defined by itself.
Include filename introduces other files
Blacklist modulename no longer loads a module
Five 、 the kernel module is automatically mounted on the machine:
To boot automatically mount module in RedHat system, in the process of online said boot, init after the
/etc/rc.d/rc.sysinit, start the kernel module can read the /etc/modprobe.conf file. In the 2.4 kernel, as long as you modify the /etc/modprobe.conf directly, you can join install xxx. The 2.6 kernel needs to modify the /etc/rc.d/rc.sysinit file.
Specific process can be seen:
/Linux/2010-01/23939.htm
In SUSE, you can edit the /etc/sysconfig/kernel file at the root permissions and add the modules that need to be started.
How does Linux automatically load modules when the system starts?
For figuring out how the system start automatically when loading the module, search for a long time, there is a lot of people put forward this problem, but there is no right answer, either Chinese community or English community, all the answers are not got to the point, is around modprobe.conf, modprobe to
speak in terms of, or for a specific problem try different methods. Some also recommend writing modprobe modulename to rc.local, but never thought that rc.local execution is placed behind the entire startup sequence and starts init.
The service defined below in rc.local is in front of D, so if a service uses this module, then it won't work.
In the test of LVS, because I Fedora7 Kernel (2.6.21 -1) the default is not loaded ip_vs module, and the kernel already contains the compiled IPVS related module, on the
/lib/modules/2.6.21- 1.3194.fc7/kernel/net/ipv4/ipvs/ below, there are:
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs.k o
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_d h.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_f tp.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_l blc.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_l blcr.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_l c.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_n q.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_r r.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_s ed.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_s h.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_w lc.ko
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ip_vs_w rr.ko
Ip_vs.ko is the basic module of IPVS, which can not work without loading IPVS (running ipvsadm will be wrong), while others are IPVS scheduling algorithm or auxiliary module of specific protocol, and need to be loaded when needed.
If the system is running manually loading, you need: modprobe, ip_vs and modprobe, ip_vs_sh and so on.
To understand how the system starts automatically when loading the module (Automatically load kernel modules), you must first understand the system is like a start, start the process in what order do what, how to do, these operations have started those
files and script control. Because neither Google nor Baidu can solve the problem, and man, modprobe, and man modprobe.conf find that it is not a file that needs to be modified.
So my brother of the [url]/[/url] "boot shutdown process and Loader":
1., the entire boot process is
(1) load the hardware information of BIOS and get 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) loading OS Kernel information, extracting Kernel, trying to drive hardware
(4) Kernel executes the init program and obtains run-lebel information (such as 3 or 5)
(5) init executes /etc/rc.d/rc.sysinit
(6) start the kernel plug-in module (/etc/modprobe.conf)
(7) init executes run-level's various Scripts, startup services
(8) init executes /etc/rc.d/rc.local
(9) execute /bin/login and wait for user Login
(10) enter Shell after Login
It seems that the correct way is to put the modules that need to be loaded (5) or (6), but as many people on the network try, modifying modprobe.conf has not been successful (for example, adding install ip_vs... In modprobe.conf). So I modified the /etc/rc.d/rc.sysinit and successfully loaded it.
A preliminary attempt at the end of rc.sysinit modprobe.conf ip_vs lsmod grep ip_vs |, after the restart, automatic loading has found success.
Then, follow the loading method of other modules in rc.sysinit, expand and change the script file, and then add a paragraph at last:
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
Just load all the modules under
/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/ automatically. Among them:
The if statement checks if the directory of the IPVS module exists
The for loop traverses all the files below that directory
Module=${module##*/} said: the ## delete characters from the front, said at the end of a * / delete /, if a # said only to delete the first /. If the variable followed by ##,
In the ## behind a string that represents the longest (until the back), if #, said a minimum.
Module=${module%.ko}: indicates deleting.Ko from the rear. If the variable followed by%, said in the back of the string% long (to the front), if%, the minimum value of the said period.
Thus, the two revision of multiple module gets the module name, which is the file name without path and.Ko suffix.
Modprobe $module >/dev/null 2>&1: load module, output point to empty device
Lsmod grep ip_vs | will restart:
Ip_vs_wrr 69770
Ip_vs_wlc 60810
Ip_vs_sh 65930
Ip_vs_sed 60810
Ip_vs_rr 60810
Ip_vs_nq 59530
Ip_vs_lc 59530
Ip_vs_lblcr 105650
Ip_vs_lblc 97970
Ip_vs_ftp 100530
Ip_vs_dh 65930
Ip_vs 7942522, ip_vs_wrr, ip_vs_wlc, ip_vs_sh, ip_vs_sed,
ip_vs_rr, ip_vs_nq, ip_vs_lc, ip_vs, ip_vs_lblcr, ip_vs_lblc, ip_vs_ftp。