Linux Kernel dynamic loading module and Linux kernel module

Source: Internet
Author: User

Linux Kernel dynamic loading module and Linux kernel module
1. Install the kernel module:General steps: (1) run make menuconfig in/usr/src/linux/to mark the items to be compiled into modules (M), save and exit. (2) run make modules to generate *. o or *. ko files under/usr/src/linux. (3) run make modeules_install to install the file. In this step, the generated. o or ko file is copied to/lib/modules/'uname-R. If you only need to compile one or several modules, you can use the following quick method: (1) Find the. config file required for compiling the kernel. The/usr/src/linux/arch directory contains several configurations used to compile the kernel. Select the desired configuration, copy it to the/usr/src/linux directory, and change it to. config. Cp/usr/src/linux/arch/x86/xxconfig/usr/src/linux /. config (2) modify. config File, remove unnecessary modules, and add the desired modules. Open. config, there are many XXXX = m items, these are to be compiled as module items, because we do not want to compile these modules, so we need to remove all XXXX = m items. Then add the desired module. For example, change # CONFIG_NTFS_FS is not set to CONFIG_NTFS_FS = m. Of course, you can use various tools to do this. (3) Compile the NTFS module. Run the make modules command in the/usr/src/linux directory to compile the desired module. (4) install the module. After compilation, the. o file is in the/usr/src/linux/directory and manually copied to the correct directory. For example, cp/usr/src/linux/fs/ntfs. o/lib/modules/2.2.16-22/fs/Note: Do not run the command make modules_install. Otherwise, it will cause serious consequences and delete all modules in your system, only the compiled module (ntfs. o ).Ii. After the installation is complete, we can load the module:The commands related to the loading module in linux are as follows: depmod, modprobe, and lsmod: depmod is a program used to generate the modules. dep and map files. The blank lines in the modules. dep file and the lines starting with '#' are ignored. depmod createsRecordList of module dependencies. This list is modules. dep under the/lib/modules/version directory. Depmod also creates many map files in the/lib/modules/version directory, such as modules. dep, modules. isapnpmap, modules. pcimap, modules. alias 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 module name. -A-quick This option scans to see if any modules are newer than the modules. dep file before any work is done % 3 now let's take a look at the modprobe command: The modprobe command is based on the output of depmod-a/lib/modules/version/modules. dep to load all required modules. You can use modprobe-l to display the modules that can be loaded currently. Modprobe does not need to specify the path of the module file or the file Suffix in the mounting module. o or. ko, while insmod requires the absolute path of the directory where the module is located, and must carry the module File name suffix (modulefile. o or modulesfile. ko ). Insmod is mainly usedTestModule correctness. Generally, the load depends on modprobe. Usage: modprobe xxx. ko # load a module modprobe-r xxx. ko # Uninstall a certain module lsmod: lsmod displays all the modules currently loaded, which is equivalent to cat/proc/modules. If you have not set the Boot Mode to load a module, such as ntfs, run lsmod after the boot, there will be no ntfs module in the list. After you run mount-t ntfs xxx, the ntfs module will be displayed in the column table after lsmod is executed. Note that lsmod displays the module name rather than the alias (alias ).3. An "Automatic kernel module loading" function is compiled into the kernel. When you try to open a certain type of file, the kernel will try to load the corresponding module as needed. Let's see how the automatic loading of the driver is implemented:Each Device has Verdon ID, Device ID, SubVendor ID, and other information. Each device driver must specify the Verdon ID, Deviece ID, and subvendorid devices that can provide services. Taking a PCI device as an example, it implements this function through a data structure of pci_device_id. For example, the pci_device_id of RTL8139 is defined as static struct pci_device_id pair [] = {0x10ec, 0 × 8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139}, {0x10ec, 0 × 8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },......} During module installation, depmod generates the following information based on the rtl8139_pci_tbl information in the module and saves it to/lib/modules/uname-r/modules. the alias file contains the following content: alias pci: v201710ecd3168138sv * sd * bc * SC * I * 8139too alias pci: v201710ecd3868139sv * sd * bc * SC * I * 8139too ...... In addition, the dependency between the modules is saved in the/lib/modules/uname-r/modules. dep file. The content of the dependency between the modules is as follows: (path information is omitted here .) 8139too. ko: mii. during kernel startup, ko bus drivers enumerate bus protocols (bus drivers are always integrated into the kernel and cannot be loaded as modules, you can use make menuconfig to enter Bus options. You can select Y or N instead of M .), create a device object for each device. Each bus object has a kset object, each device object is embedded with a kobject object, and the kobject is connected to the kset object, so that between the bus and the bus, A tree structure is formed between the bus and the device. When the bus drive creates a device object for the scanned device, it initializes the kobject object and connects it to the Device Tree. It also calls kobject_uevent () add the new backup event and related information (including the VendorID and DeviceID of the device .) It is sent to the user State through netlink. After detecting this event in user-mode udevd, you can enable/lib/modules/uname-r/modules based on this information. according to the alias pci: v201710ecd2138138sv * sd * bc * SC * I * 8139too file, the device driver module that is newly scanned is 8139too. So modprobe will know to load the 8139too module, while modprobe according to modules. dep file found that 8139too depends on mii. ko, if the mii. if ko is not loaded, modprobe first loads mii. ko, and then load 8139too. ko. Test in yourShellRun: # ps aux | grep udevd # kill-9 25063 and track udevd. Run: # strace-f/sbin/udevd-daemon in shell, the output of udevd is as follows :...... Close (8) = 0 munmap (0xb7f8c000, 4096) = 0 select (7, [3 4 5 6], NULL, NULL, NULL we find that udevd is blocked in the select () function. The select function is prototype: int select (int nfds, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * timeout); the first parameter: nfds indicates the largest file description symbol. Here it is 7 (obviously 6 ?). The second parameter: readfds is the set of read file descriptors. Here it is 3, 4, 5, 6. The third parameter: writefds is the set of write file descriptors, and here it is NULL. The fourth parameter: Invalid TFDs is a collection of abnormal file descriptors. Here it is NULL. The fifth parameter: timeout specifies the time-out time, which is NULL. The select function is used to return data if any file in readfds is readable, any file in witefds can be written, or any file in limit TFDs has an exception. Otherwise, the current process is blocked until the appeal conditions are met, or because the blocking time exceeds the time specified by timeout, the current process is awakened and the select statement is returned. Therefore, here udevd waits for the 3, 4, 5, 6 files to be wakened only when the data is readable. Run the following command in shell: # ps aux | grep udevd root 27615 ...... Strace-o/tmp/udevd. debug-f/sbin/udevd-daemon root 27617 ...... The process id of/sbin/udevd-daemon udevd is 27617. Let's take a look at several files waiting for the select statement: # cd/proc/27615/fd # ls-l udevd standard input, standard output. All standard errors are/dev/null. 0->/dev/null 1->/dev/null 2->/dev/null udevd wait on the following files. 3->/inotify 4-> socket: [331468] 5-> socket: [331469] 6-> pipe: [331470] 7-> pipe: [331470] because it is not convenient to insert a 8139 Nic In the running process, now we use a USB flash drive for testing. When you insert a USB flash drive, you will see the strace output, from its output, we can see that after udevd returns the select statement, it calls modprobe to load the driver module, calls sys_mknod, and creates 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 a module in modules. alias. You can use udevmonitor to view the message sent by the kernel to udevd through netlink. Run: # udevmonitor-env in the shell and insert it into the USB flash disk. Then you will see the message sent to udevd.Iv. Configuration loaded by the kernel module:Sometimes many modules need to be loaded at a time, and The modprobe option must be configured in a uniform place. There is an important file:/etc/modprobe. conf. In opensuse, there is also modprobe related to it. d/many files in the folder and modprobe. conf. local file in/etc/modprobe. conf will include other mentioned files. It is generally recommended to use modprobe. conf. local. /Etc/modprobe. conf is actually used to write the module's load command or module alias definition. Man modprobe. conf: alias my-mod really_long_modulename: defines an easy-to-use alias for the module options modulename option... When loading a module, add the option install modulename command... Use your own commands to load specified modules, such as install fred/sbin/modprobe barney; /sbin/modprobe-ignore-install fred "is used each time the fred module is loaded"/sbin/modprobe barney; /sbin/modprobe-ignore-install fred "command remove modulename command... Use a custom command to delete a specified module. Include filename introduce other files blacklist modulename and no longer load a module5. automatic mounting of the kernel module upon startup:In the redhat system, during kernel startup, init runs/etc/rc. d/rc. after sysinit is started, the kernel plug-in module will read/etc/modprobe. conf file. In the 2.4 kernel, you only need to directly modify/etc/modprobe. conf to add install xxx. 2.6 The/etc/rc. d/rc. sysinit file must be modified for the kernel. Specific process can be seen: http://blog.csdn.net/ioriqqe/archive/2009/11/05/4772033. in suse, You can edit the/etc/sysconfig/kernel file with the root permission and add the module to be started.

Do coders have to work overtime? NO!

I know that coders all want to get rid of the title of overtime dogs and takeout faces, so we are here!

We created an APP that allows programmers to share knowledge and skills, and thought it could subvert the programmer's staff.
Type!

Some people say we are crazy, but we don't think so.

In order to break down the faces of people who are infatuated with us, we are in urgent need of coders to give
Our "number pulse "! "Medical fees" are generous! After all, we just want to do our best!

As mentioned in the circle dictionary, niuba-people refer to QQ Group Owners or followers who have more than 1000 members in the group.
The number of webmasters or moderators with 2000 or more followers is higher than 10000.
Or the reading volume of a single post is higher than that of 2000 bloggers or celebrities with a wide network of contacts.

For the future leaders who fail to reach the standard, we can only say with tears: Shu, we are here for a long time.
Do you have an appointment? Wait for the day when you become a god, and I will survive and die!

Come? Or not?

Phone number of the lap interactive joint: 1955246408 (QQ)

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.