Linux solution: Concepts and usage of Modules

Source: Internet
Author: User
Tags network function
Article title: Linux solution: Concepts and usage of Modules. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   1. what is modules?
The literal meaning of modules is the module, which refers to the kernel modules. Simply put, a module provides a function, such as isofs, minix, nfs, and lp. Traditionally, there are two ways to solve modularization: The designer can separate various functions into separate processing called threads, or re-compile the kernel by including/excluding some functions. If functions are separated into threads, the kernel is called "micro-kernel". This solution increases the communication overhead of inter-thread coordination. As the name implies, the advantage of this solution lies in the size of the kernel.
  
Linux solutions include kernel modules that can be loaded and detached at any time as needed. In this way, the kernel size and traffic can be minimized. The module is independent from the kernel and does not need to be pre-bound to the kernel codes. There are three advantages: first, you do not have to re-compile the kernel in the future, which saves a lot of time; second, if you need to install a new modules, you do not have to re-compile the kernel, you only need to insert the corresponding modules (using the insmode command). third, to reduce the kernel's occupation of system resources, the kernel can focus on the most basic tasks, some extended functions are implemented by modules.
  
The module can also be used to try new kernel code without the need to create and reactivate the kernel every time. However, the problem is that using the kernel module usually slightly increases performance and memory consumption. A loadable module will certainly generate more code. this code and additional data structure will occupy more memory. In addition, indirect access to kernel resources also reduces the module efficiency slightly.
  
The idea of modularization has been widely accepted, mainly because it can expand the functions of the system and allow users to flexibly configure the system. Apache also adopts this function extension method. in this article, we mainly discuss how to install and uninstall the kernel module. for details about how to install the Apache module, refer to the Apapce documentation.
  
   II. how to load modules?
There are two ways to load the kernel module. First, use the insmod command to manually insert it to the kernel. Another more intelligent way is to load this module as needed? This is called demand loading ). When the kernel discovers that a module is required, for example, when you install a file system that is not in the kernel, the kernel will request the kernel Daemon (kerneld) to try to load the appropriate module. Speaking of this, we can't help but mention the kernel daemon kerneld. it is very intelligent and can automatically insert the required modules into the kernel and remove unused modules from the kernel. Kerneld consists of two independent parts: one part is used in the Linux kernel and is responsible for sending requests to daemon. The other part is used in the user data area of the system and is responsible for transferring the modules specified by the kernel request. If this kerneld is missing, it can only be loaded manually using the insmode or modeprobe command.
  
   3. Introduction to modules commands
Commands related to modules include:
  
Lsmode: lists the modules that have been imported into the kernel.
Insmode: inserts a module into the kernel.
Rmmod: detaches a module from the kernel.
Depmod: generate a dependency file to tell the future insmod where to call modules. The dependency file is in/lib/modules/[your kernel version]/modules. dep.
Kerneld: automatically transfers the module to the kernel and detaches the module from the kernel.
  
   4. compile a minimum Linux kernel
The module is generally used to support functions that are not frequently used. For example, you usually only use a dial-up network, so the network function is not required at any time. Therefore, you should use a module that can be loaded to provide this function. This module is loaded only when you perform a dial-up connection. When you disconnect the connection, it will be automatically removed. This minimizes the amount of memory used by the kernel and reduces the system load.
  
Of course, those features that are always needed like hard disk access must be implemented in the kernel. If you build a network workstation or web server, the network function is always needed, and you should consider compiling the network function into the kernel. Another method is to mount the network module at startup. The advantage of this method is that you do not need to recompile the kernel. The disadvantage is that the network function cannot be particularly efficient.
  
In accordance with the above principles, we first List A list to see which options in the kernel are non-essential, that is, these things must be compiled into the kernel. Remove unnecessary modules from the kernel.
  
The first is the hard disk configuration of the root user. If your hard disk is an IDE interface, mark the ide options. For a SCSI interface, mark your interface parameters and SCSI id.
  
The second is to select which file system to use. In Linux, the default file system is ext2, so you must mark it. If other operating systems, such as win98 or windows NT, are also supported by FAT32 or NTFS, however, you can add new module support by manually loading later.
  
The third is to select the executable file format supported by Linux. Two formats are available:
  
Elf: this is a commonly supported executable file format in Linux. it must be compiled into the kernel.
  
A. out: This is the format of different function libraries in the earlier version of Linux executable files. if you are sure you cannot use the executable files in this format, you can not compile them into the kernel.
  
The above content must be compiled into the kernel. For other content that is prompted by m in all options, select m. you can manually add this module.
  
** Loadable module support * Enable loadable module support (CONFIG_MODULES) [Y/n/?] Set version
Information on all symbols for modules (CONFIG_MODVERSIONS) [N/y/?] Kernel daemon support (e.g.
Autoload of modules) (CONFIG_KERNELD) [Y/n/?]
  
Answer Y, N, and Y respectively. The default value of CONFIG_KERNELD is N, so select Y.
  
After make config is complete, it is still make dep; make clean.
  
Next, make zlilo or make zImage.
  
Then make modules; make modules_install. After compilation, a "clean" kernel image file is compiled without calling any redundant modules.
  
   5. how to manually load Modules?
If you want to manually load a module, we recommend that you use modprobe because it can solve the dependency problem between modules. in the sound card section, sound blster is used as an example and has the following modules:
  
Sb 33652 0 (autoclean)
Uart401 6160 0 (autoclean) [sb]
Sound 56492 0 (autoclean) [sb uart401]
Soundcore 2372 5 (autoclean) [sb sound]
  
These modules must be loaded, and the entire sound card can work, and there is a dependency between them. The most core soundcore must be loaded first, and finally sb. However, most people do not know their order. Therefore, modprobe is used to solve this problem.
  
Usually we only need
  
Modprobe sb
  
It will automatically find all the modules used by the sb and load them one by one, so the general user does not have to worry about it.
  
So how does the kernel know the dependency between these modules? It turns out that there is a 'depmod-a' command in the system startup script, which creates a list of dependencies for all available modules in the system. The 'modprobe module-name' will use this list to load the specified
  
Before the module, load the modules that are previously loaded. If 'module-name' is not found in this slave list, it will provide the corresponding error information.
  
However, if you use insmod, it does not automatically call other modules. For example, to add the PPP module, run the following command:
  
Root/root> insmod ppp
Root/root>
  
If the operation is successful, an operation prompt is displayed. If it fails, the following information may appear:
  
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_init_Rsmp_1ca65fca
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_compress_Rsmp_cfd3a418
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_free_Rsmp_b99033d9
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_toss_Rsmp_a152cec0
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_remember_Rsmp_07972313
/Lib/modules/2.2.10/net/ppp. o: unresolved symbol slhc_uncompress_Rsmp_3bb36b01
[Root/root] #
  
This indicates that the PPP module is not loaded successfully. the unresolved symbol in the error prompt indicates that some modules required by the PPP module are not loaded yet. The first line of the error message is: slhc_init_Rsmp_1ca65fca. which module is this? In this case, some experience may be needed to make a judgment. it starts with slhc, so try slhc.
  
Root/root> insmod slhc is normal, and then we load the PPP module
Root/root> insmod ppp
Root/root>
  
No information is returned, indicating that the PPP module has been loaded successfully.
  
   6. detach a Modules from the memory.
To uninstall a module, first use lsmod to check whether the module has been loaded and then perform the operation. In addition, when encountering a dependency module, the process of uninstalling the module from the kernel is the opposite of the loading process. It follows the "first in last out" principle, that is, in a series of dependency modules, you must first uninstall the last loaded module, and finally uninstall the first loaded module. For example, if you want to use rmmod to remove a module in use (in the above example, you want to uninstall slhc, but there are still PPP modules using it), the error message "Device or resource busy" is displayed. Therefore, the slhc module can be detached from the memory only after the PPP module is detached from the memory.
  
In short, when uninstalling a module, Linux will prompt you with sufficient information for possible inter-module dependency issues and carefully check the information, it can help you take appropriate actions and finally solve the problem.
  
  
Related Article

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.