Linux Kernel Technical Analysis
[Date:] |
Source: Chinese software author: |
[Font: large, medium, and small] |
I. kernel Introduction
Kernel is the core of an operating system. It manages system processes, memory, device drivers, files, and network systems, and determines system performance and stability.
An important feature of linux is its open source code. All kernel source programs can be found in/usr/src/linux. Most application software is designed to comply with GPL, you can obtain the source code. Any software engineer in the world can add code that he thinks is excellent. One obvious benefit of this is the fast fixing of Linux vulnerabilities and the use of the latest software technology. The Linux kernel is the most direct representative of these features.
Imagine what the source program with the kernel means to you? First, we can understand how the system works. By reading the source code, we can understand the operating principle of the system, which is simply a fantasy in Windows. Secondly, we can tailor the system to suit our own situation, so we need to re-compile the kernel. What is the situation in Windows? I believe that many people have been inexplicably confused by the increasing size of Windows. Again, we can modify the kernel to meet our own needs. What does this mean? Yes, it is equivalent to developing an operating system on your own, but most of the work is done. All you have to do is add and implement the functions you need. In Windows, unless you are the core technical staff of Microsoft, you don't have to worry about it.
Ii. kernel version number
Since the Linux source program is completely open, anyone who follows GPL can modify the kernel and publish it to others. The development of Linux uses the market model (bazaar, which corresponds to the cathedral-church model). To ensure that these disordered development processes can be carried out in an orderly manner, Linux uses a dual-tree system. One tree is a stable tree, and the other is a non-stable tree or a development tree ). Some new features and experimental improvements will be implemented in the Development tree first. If the improvements made in the Development tree can also be applied to the stability tree, the same improvements will be made in the stability tree after tests in the Development tree. Once the development tree has been sufficiently developed, the development tree will become a new stable tree. The development number is reflected in the source program version number. The source program version number is in the form of X. y. z: for a stable tree, Y is an even number. For a development tree, Y is bigger than the corresponding stable tree (therefore, it is an odd number ). So far, the highest version of the stability tree is 2.4.18; the latest version of the Development tree is 2.5.10. To download the kernel version, visit the http://www.kernel.org.
Iii. Why re-compile the kernel?
Linux, as a free software, is constantly updated with the support of many enthusiasts. The new kernel fixes bugs in the old kernel and adds many new features. If you want to use these new features or customize a more efficient and stable kernel based on your system, you need to recompile the kernel.
Generally, the updated kernel supports more hardware, provides better process management capabilities, and runs faster and more stably. It also fixes many vulnerabilities found in earlier versions, it is necessary for Linux users to regularly upgrade and update the system kernel.
In order to correctly set the kernel compilation configuration options and compile only the code of the functions required by the system, the following four considerations are generally taken:
Customized and compiled kernels run faster (with less code)
The system will have more memory (the kernel part will not be exchanged to the virtual memory)
Compilation of unnecessary functions into the kernel may increase the vulnerability exploited by system attackers.
Compiling a function as a module is slower than compiling it into the kernel.
Iv. kernel compilation Mode
To support a certain part of functions, such as the network, you can compile the corresponding part into the kernel (build-in) or compile the part into a module ), dynamic call. If it is compiled into the kernel, the corresponding functions can be automatically supported when the kernel is started. This advantage is convenient and fast, and the machine starts once, you can use this feature. The disadvantage is that it will make the kernel huge. Whether you need this feature or not, it will exist. This is the common practice of Windows, we recommend that you compile the frequently used parts directly into the kernel, such as the NIC. If it is compiled into a module, the corresponding. o file will be generated and can be dynamically loaded during use. The advantage is that the kernel will not be too large, but you have to call these modules by yourself.
V. Obtain and update the new kernel version
The official website for Linux kernel release is http://www.kernel.org. The new kernel version has two types: full Source and patch. The complete kernel version is relatively large, generally the tar.gz.pdf is the .bz2 file. The two files are compressed using gzip or bzip2, and need to be decompressed during use. The patch file is relatively small, generally only a few dozen K to several hundred K, but the patch file is for a specific version, you need to find your own version to use.
The root permission is required to compile the kernel. The following operations are assumed that you are the root user. Copy the kernel you want to upgrade to/usr/src/(the following uses linux-2.4.18.tar.gz as an example). The command is
# Cp linux-2.4.18.tar.gz/usr/src
Let's take a look at the contents of the current/usr/src and notice that there is a symbolic link to a linux-2.4 (take REDHAT7.2 as an example) directory. This is the kernel source code of the linux installation. Delete this link.
Decompress the downloaded source code file. If you download the .tar.gz (. tgz) file, use the following command:
# Tar-zxvf linux-2.4.18.tar.gz.tar.gz
If you download the. bz2 file, for example, linux-2.4.0test8.tar.bz2, use the following command
# Bzip2-D linux-2.4.18.tar.bz2
# Tar-xvf linux-2.4.18.tar
The file will be decompressed to the/usr/src/Linux directory, and we will slightly modify it:
# Mv Linux linux-2.4.18
# Ln-s linux-2.4.18 Linux
If you download the patch file, you can perform the patch operation (assuming that patch-2.4.18 is already in the/usr/src directory, otherwise, you need to copy the file to/usr/src first ):
# Patch-P0 <patching-2.4.18
6. kernel Compilation
The first command to be run is:
# Cd/usr/src/Linux
# Make mrproper
This command ensures that the source code directory does not contain the correct. o file and file dependencies. This step can be omitted because we use the downloaded full source program package for compilation. If you use these source programs to compile the kernel multiple times, you 'd better run this command first.
Make sure that the ASM, Linux, and SCSI links in the/usr/include/directory point to the source code of the kernel to be upgraded. They chain the real include sub-directories required by the real computer architecture (i386 for PCs) under the source code directory. For example, ASM points to/usr/src/Linux/include/asm-i386 and so on. If you do not have these links, you must create them manually. follow these steps:
# Cd/usr/include/
# Rm-r ASM Linux SCSI
# Ln-S/usr/src/Linux/include/asm-i386 ASM
# Ln-S/usr/src/Linux/include/Linux
# Ln-S/usr/src/Linux/include/SCSI
This is an important part of configuration. Delete the ASM, Linux, and SCSI links under/usr/include, and then create a new link pointing to the directory with the same name under the new kernel source code directory. These header file directories contain the important header files required to ensure that the kernel is correctly compiled on the system. Now you should understand why we have created a link named Linux in the "redundant" Directory of/usr/src?
The following Kernel configuration process is cumbersome, but the proper configuration is directly related to Linux running in the future. It is necessary to know some of the main and frequently used options.
You can use one of the following commands to configure the kernel based on your needs and hobbies:
# Make config (the most traditional text-based configuration interface is not recommended)
# Make menuconfig (Text menu-based configuration interface, recommended for character terminals)
# Make xconfig (the configuration interface based on the graphic window mode is recommended in XWindow)
# Make oldconfig (if you only want to modify some small points based on the original Kernel configuration, it will save a lot of trouble)
Among the three commands, the make xconfig interface is the most friendly. If you can use XWindow, we recommend that you use this command. The interface is as follows:
If you cannot use XWindow, use make menuconfig. Although the interface is worse than the above one, it is always much better than make config. For the make menuconfig interface:
When you select the corresponding configuration, there are three options, which represent the following meanings:
Y -- compile this function into the kernel
N -- do not compile this function into the kernel
M -- compile this function into a module that can be dynamically inserted into the kernel as needed
If you are using make xconfig, you can select the corresponding option with the mouse. If you are using make menuconfig, you need to use the space key to select. You will find that there is a bracket before each option, but there are brackets, angle brackets, and parentheses. When using the space key, you can find that the brackets are either empty or "*", while the angle brackets can be empty, "*" and "M ". This indicates that the items corresponding to the former are either not or compiled into the kernel. The latter selects the same item and can be compiled into modules. The content of the parentheses is to select one of the options provided.
In the process of compiling the kernel, the most annoying thing is the configuration work in this step. Many new users do not know how to choose these options. In fact, the default value can be used for most options during configuration. Only a small part of options need to be selected based on different user needs. The selection principle is to compile some functional code that is far away from other parts of the kernel and is not frequently used into a loadable module, which helps reduce the length of the kernel and reduce the memory consumption of the kernel, the impact on the kernel when the corresponding environment change of this function is simplified; do not select a function that is not required; compile some function code that is closely related to and frequently used by the kernel directly into the kernel.
As for the options, it is complicated, just a brief introduction. You should select the options based on the actual situation and the help content.
1. Code maturity level options
Code maturity level. There is only one item: prompt for development and/or incomplete code/drivers. If you want to test functions that are still in the experimental stage, such as khttpd and IPv6, you must select Y. Otherwise, you can select N.
2. Loadable module support
Module support. There are three items:
Enable loadable module support: This item is required unless you want to compile all required content into the kernel.
Set version inFORMation on all module symbols: Optional.
Kernel module loader: enables the Kernel to load required modules at startup. We recommend that you select this module.
3. Processor type and features
CPU type. There are a lot of content. I will not introduce them one by one. The following are some of the relevant content:
Processor family: select the CPU type based on your situation.
High Memory Support: supports large Memory capacity. It can be 4 GB or 64 GB.
Math emulation: coprocessor simulation. Coprocessor is the darling of the 386 age and is no longer needed.
MTTR support: supported by MTTR. Optional.
Hierarchical Ric multi-processing support: supports Symmetric multi-processing. Unless you are rich enough to have multiple CPUs, you do not need to choose.
4. General setup
Here are some of the most common attributes. This part contains a lot of content. Generally, you can use the default settings. The following describes some frequently used options:
Networking support: network support. Required. We recommend that you select this option if no network adapter is available.
PCI support: supports PCI. If the PCI Card is used, it is required.
PCI access mode: PCI access mode. Options include BIOS, Direct, and Any. Select Any.
Support for hot-pluggabel devices: supports hot swapping devices. It is not very good to support.
PCMCIA/carw.support: PCMCIA/carw.support. PCMCIA is required.
System V IPC
BSD Process Accounting
Sysctl support: the above three types of support are related to process processing and IPC calls, mainly System V and BSD. If you are not using BSD, follow the default setting.
Power Management support: Power Management support.
Advanced Power Management BIOS support: Advanced Power Management BIOS support.
5. Memory Technology Device (MTD)
MTD device support. Optional.
6. Parallel port support
Parallel Port support. If you do not want to use the serial port, do not select it.
7. Plug and Play configuration
Plug-and-play is supported. Although plug-and-play in linux is not as good as that in Windows currently, you can choose it. In this way, you can unplug the mouse and try plug-and-play in Linux.
8. Block devices
Block device support. You have to select one based on your own situation. Here is a brief description:
Normal PC floppy disk support: general PC floppy disk support. This should be required.
XT hard disk support:
Compaq SMART2 support:
Mulex DAC960/DAC1100 pci raid Controller support: For RAID images.
Loopback device support:
Network block device support: supported by Network block devices. If you want to access the Internet neighbors, choose.
Logical volume manager (LVM) support: Logical volume management support.
Multiple devices driver support: supports multiple device drivers.
RAM disk support: ramdisk support.
9. Networking options
Network options. The network protocol is configured here. There is too much content. I will not introduce it one by one. Let's take a look at it. If you have some knowledge about network protocols, you should be able to understand it. If you are too lazy to check the network, use the default option (You must select the TCP/IP networking option. Let's take a look at TCP/IP, ATM, IPX, decnet, appletalk ...... Many protocols are supported, IPv6 is also supported, QoS and/or fair queueing is also supported, and khttpd is also supported, but these are still in the experimental phase.
10. telephony support
Phone support. Linux supports phone cards, so that you can use a common phone on an IP address to provide voice services. Remember, the phone card has nothing to do with modem.
11. ATA/IDE/MFM/rll support
This is supported by hard disks, optical drives, tapes, and floppy disks with many interfaces. Use the default options. If you use special devices, such as PCMCIA, find the corresponding options.
12. scsi support
Support for SCSI devices. I don't have a SCSI device, so I don't need to select it. If you use a SCSI hard drive, optical drive, tape, and other devices, you can find them yourself.
13. Fusion MPT Device Support
Fusion MPT compatible with PCI adapter is required.
14. i2o Device Support
The i2o interface adapter is required for use in Intelligent Input/output (i2o) system interfaces.
15. Network Device Support
Network Device Support. I have selected the Protocol above. Now I have to select a device. As you can imagine, the content must be much more. Fortunately, it is probably classified into ARCnet devices, Ethernet (10 or 100 Mbit), Ethernet (1000 Mbit), and Wireless LAN (non-hamradio), Token Ring device, Wan interfaces, and PCMCIA network device support. I use 10/100 M Ethernet. It seems that I only need to select this one. Or 10/100 M Ethernet device familiar, although the content is much, at a glance can see my RealTeck RTL-8139 PCI Fast Ethernet Adapter support, in order to avoid trouble, compiled into the kernel is good, if M is not selected, select Y. Be patient. Generally, you can find your own network card. If not, you have to go to the vendor to get the driver.
16. Amateur Radio support
Configure amateur Wireless broadcast.
17. IrDA (infrared) support
Infrared support.
18. ISDN subsystem
If you use ISDN to access the Internet, this is essential.
19. Old CD-ROM drivers (not SCSI, not IDE)
Do really thoughtful, originally those who are not SCSI/IDE optical drive who is still using ah, choose it yourself, with IDE CD-ROM don't have to choose.
20. Character devices
Character device. This content is too much. Use the default settings first, and modify the content as needed. Let's introduce the main categories:
I2C support: I2C is a low-speed serial bus protocol used in micro-control applications promoted by Philips. This option is required if you want to select the following Video For linux.
Mice: Mouse. Now you can select bus, serial port, PS/2, C & T 82c710 mouse port, and pc110 digitizer pad as needed.
Joysticks: handle. Even if you drive the handle in Linux, it doesn't make much sense, and there are too few games.
Watchdog cards: although it is called cards, it can be implemented with pure software, and of course there are hardware. If you select this option, a file named watchdog will be created under your/dev, which can record the running status of your system, it takes about 1 minute to restart the system. With this file, you can restore the system to the state before restart.
Video for Linux: audio/video cards are supported.
Ftape, the floppy tape device driver:
PCMCIA character device support:
21. File Systems
File System. There are too many contents. The old method is to modify the content based on the default options. This section describes the following items:
Quota support: quota can limit the maximum disk space available to each user, which is very effective when multiple users share a single host.
Dos fat FS Support: supports the format of dos fat files, including fat16 and FAT32.
ISO 9660 CD-ROM file system support: the disc uses the ISO 9660 file format.
NTFS file system support: NTFS is the file format used by NT.
/Proc file system support:/proc file system is a channel provided by Linux for users to interact with the system. It is recommended that you select it. Otherwise, some functions cannot be correctly executed.
The other three categories are here: Network File Systems (Network File System), Partition Types (Partition type), and Native Language Support (local Language Support ). It is worth mentioning that there are two types of Network File Systems: NFS and SMB, respectively, linux and Windows, access each other's File Systems in the form of Network neighbors, and select as needed.
22. Console drivers
Console driver. Generally, you can use the VGA text console, the standard 80*25 text console.
23. Sound
Sound Card Driver. If you can find the sound card driver in the list, it is naturally best, otherwise you will try OSS.
24. USB supprot
USB support. Many USB devices, such as the mouse, modem, printer, and scanner, can be supported in linux and can be selected as needed.
25. Kernel hacking
With this configuration, you can do some work even when the system crashes. Common users do not need this function.
After the configuration, save the disk and exit. Of course, you can save the current configuration file so that the next configuration will save effort.
Next, compile and enter the following command.
# Make dep
# Make clean
# Make bzImage or make zImage
# Make modules
# Make modules_install
# Depmod-
The first command, make DEP, actually reads the configuration file generated during the configuration process to create the dependency tree corresponding to the configuration, so as to determine which need to be compiled and which do not need to be; the second command is make clean to delete the files left in the previous step to avoid some errors. Make zimage and make bzimage are used to fully compile the kernel, and Gzip is used to compress the kernel generated by both, one is enough. The difference between them is that make bzimage can generate a larger kernel. We recommend that you use the make bzimage command.
The following three commands are necessary only when you answer enable loadable module support (config_modules) in the configuration process, make modules and make modules_install generate the corresponding modules and copy the modules to the required directories.
Strictly speaking, the depmod-a command has nothing to do with the compilation process. It is used to generate dependencies between modules. After you start the new kernel, the module can be correctly located when the module is loaded using The modprobe command.
Update
After the above steps, we finally get the new kernel version. To be able to use the new kernel version, we also need to make some changes:
# Cp/usr/src/Linux/system. MAP/boot/system. Map-2.4.18
# Cp/usr/src/Linux/ARCH/i386/bzimage/boot/vmlinuz-2.4.18
The above two files are newly generated during compilation. Next, modify the two links system. Map and vmlinuz under/boot to point them to the new kernel file:
# Cd/boot; RM-F system. Map vmlinuz
# Ln-s vmlinuz-2.4.18 vmlinuz
# Ln-S system. Map-2.4.18 system. Map
7. Modify the startup Manager
If LILO is used, modify/etc/lilo. conf and add the following items:
Image =/boot/vmlinuz-2.4.18
Label = linux240
Read-only
Root =/dev/hda2
The root =/dev/hda2 line must be modified as needed.
Run:
#/Sbin/lilo-v
Make sure that you have edited/etc/lilo. conf correctly. Now restart the system:
# Shutdown-r now
If you use Grub to start the manager, add the following items.
Title Red Hat linux (2.4.18)
Root (hd0, 0)
Kernel/vmlinuz-2.4.18 ro root =/dev/hda2
Grub automatically takes effect without calling the command again.
After the restart, you can use the new kernel.