Linux OS kernel compile detailed _unix Linux

Source: Internet
Author: User
Tags bz2

Introduction to the Kernel

Kernel, is the core of an operating system. It is responsible for managing the system's process, memory, device driver, file and network system, which determines the performance and stability of the system.

linux is an important feature of the openness of its source code, all the kernel source can be found under the/usr/src/linux, most of the application software is also followed the GPL design, you can get the corresponding source code. Any software engineer in the world can add to the code that they think is good, and the obvious benefit is the rapid use of Linux patch vulnerabilities and the latest software technologies. The Linux kernel is the most direct representation of these features.

Imagine what it means for you to have a kernel source program? First, we can understand how the system works. By reading through the source code, we can understand how the system works, which is impossible under Windows. Second, we can tailor the system to suit our own situation, so we need to recompile the kernel. What's the situation under Windows? I believe many people have been baffled by the increasingly large windows. Again, we can make changes to the kernel to suit our needs. What does that mean? Yes, the equivalent of developing an operating system, but most of the work is done, all you have to do is to add and implement the functions you need. Under Windows, you don't have to be wishful thinking unless you're a Microsoft core technician.

Kernel version number

Since Linux's source program is completely public, anyone who follows the GPL can modify the kernel and publish it to others. Linux was developed using a market model (bazaar, corresponding to the cathedral--Church model), which uses a dual-tree system to ensure that these unordered development processes are carried out in an orderly fashion. One tree is a stable (stable tree), the other is a tree that is not stable (unstable tree) or a development tree (development). Some new features, experimental improvements, and so on will be done first in the development tree. If improvements made in the development tree can also be applied to the stable tree, the same improvements will be made in the stable tree after being tested in the development tree. Once the development tree has been developed enough, the development tree becomes the new stable tree. The number of development is reflected in the source program version number, the source version number is in the form of x.y.z: For the stable tree, y is even; for the development tree, Y is higher than the corresponding stable tree (therefore, it is odd). So far, the highest version of the stable tree is 2.2.16, and the latest release of Redhat7.0 is the 2.2.16 kernel; The latest version of the development tree is 2.3.99. Maybe you've found a kernel like 2.4.0-test9-pre7 on multiple sites, but it's not an official version.

Why recompile the kernel

linux as a free software, with the support of a wide range of enthusiasts, the kernel version is constantly updated. The new kernel fixes bugs in the old kernel and adds a number of new features. If users want to use these new features, or want to customize a more efficient, more stable kernel based on their own system, they need to recompile the kernel.

Typically, newer kernels support more hardware, have better process management capabilities, run faster and more stably, and typically fix many of the vulnerabilities found in older versions, and frequently choose to update the system kernel as a necessary operating element for Linux users.

In order to properly set the kernel compile configuration options, so that only the code to compile the functions required by the system, generally have the following four considerations:

A custom compiled kernel runs faster (with less code)
The system will have more memory (the kernel portion will not be swapped into virtual memory)
The unwanted functionality compiled into the kernel may increase vulnerabilities exploited by system attackers
It is slower to compile a feature into a module than it is to be compiled into the kernel

Kernel compilation mode


To increase support for a certain part of the function, such as the network, you can compile the corresponding parts into the kernel (build-in), you can also compile the section into modules (module), dynamic invocation. If you compile into the kernel, you can automatically support the function of the corresponding part when the kernel starts. This advantage is convenient, fast, you can use this part of the function when the machine starts, the disadvantage is that the kernel will become huge, whether you need this part of the function, it will exist, this is the Windows idiomatic tricks , it is recommended that the parts that are used frequently are compiled directly into the kernel, such as network adapters. If compiled into a module, it will generate the corresponding. o file, can be dynamically loaded when used, the advantage is not to make the kernel too large, the disadvantage is that you have to call these modules yourself.

Kernel compile detailed

Acquisition and update of new version kernel

The major domestic FTP on the general can find some version of the kernel. The release of the new version of the kernel has two forms, one is the complete kernel version, the other is the patch file, the patch. Full kernel version of the larger, such as LINUX-2.4.0-TEST8.TAR.BZ2 has 18M, fast users can download the use. The full kernel version is typically a. tar.gz (. tgz) file or a. bz2 file, which is a file that is compressed using either gzip or bzip2, and needs to be decompressed when used. Patch files are relatively small, generally only dozens of k to hundreds of K, very few will exceed 1M, slow speed users can use patch files to upgrade the kernel. But the patch file is for a specific version, you need to find your own version to use.

The compiler kernel requires root permissions, and the following actions assume that you are the root user. Please copy the kernel you need to upgrade to/usr/src/below (for example, the linux-2.4.0test8.tar.gz of the 2.4.0TEST8 kernel below) and the command is

#cp LINUX-2.4.0TEST8.TAR.GZ/USR/SRC

Let's take a look at the current/USR/SRC and notice that there is a symbolic link to Linux that points to a directory similar to linux-2.2.14 (the kernel version number that you are using now). First delete this link:

#cd/USR/SRC
#rm-F Linux

Now unzip the source program files that we downloaded. If you are downloading a. tar.gz (. tgz) file, use the following command:

#tar-XZVF linux-2.4.0test8.tar.gz

If you are downloading a. bz2 file, such as linux-2.4.0test8.tar.bz2, please use the following command

#bzip2-D linux-2.4.0test8.tar.bz2
#tar-XVF Linux.2.4.0.test8.tar

Now let's take a look at what's under/USR/SRC, and you'll find a directory called Linux, which is the source of the kernel that we need to upgrade to. Do you remember the link called Linux? The reason why you use that link is to prevent the original version of the kernel from inadvertently overwriting the source program. We also need to deal with the same:

#mv Linux Linux-2.4.0test8
£ #ln-S LINUX-2.4.0TEST8 Linux

So we also have a symbolic link called Linux, there is no need to worry about it will be covered up (perhaps you will find it necessary to re-establish Linux symbolic links, but in fact this is essential, as described below). If you have also downloaded patch files, such as Patch-2.4.0test8, you can perform patch operations (assuming that PATCH-2.4.0TEST8 is already in the/USR/SRC directory, otherwise you will need to copy the file to the/usr/src below):

#patch-p0 < Patch-2.4.0test8

Now, we have upgraded the kernel source program to the latest version, let's start the kernel compilation of the journey.

Preparatory work


The first command to run normally is:

#cd/usr/src/linux;make Mrproper

This command ensures that there are no incorrect. o files and dependencies of files under the source code directory. Since we are compiling using the full source package just downloaded, this step can be omitted. And if you use these source programs to compile the kernel many times, it's best to run the command first.
Ensure that the ASM, Linux, and SCSI links in the/usr/include/directory point to the kernel source code to be upgraded. They are linked to the true include subdirectory of the real, the computer architecture (for PCs, the architecture that is i386) that is needed in the source code directory. such as: ASM point to/usr/src/linux/include/asm-i386 and so on. Without these links, you need to create them by hand, following 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 Linux
# ln-s/USR/SRC/LINUX/INCLUDE/SCSI SCSI

This is a very important part of the configuration. After removing the ASM, Linux, and SCSI links under/usr/include, create a new link 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 compiles correctly on the system. Now you can see why we've created a link named Linux on top of the/USR/SRC, right?
Configuration

The next kernel configuration process is cumbersome, but the appropriate configuration is directly related to the future operation of Linux, it is necessary to understand some of the main and frequently used options settings.
The configuration kernel can use one of the following commands, depending on your needs and preferences:

#make Config (the most traditional text-based configuration interface, deprecated)
#make menuconfig (Configuration interface based on text menu, recommended by character terminal)
#make xconfig (Configuration Interface based on graphical window mode, Xwindow recommended)
#make Oldconfig (if you only want to modify some small places on the basis of the original kernel configuration, it will save a lot of trouble)

Of these three commands, make Xconfig's interface is the most friendly, if you can use Xwindow, then recommend you to use this command.

If you can't use Xwindow, then use make menuconfig good. The interface is much better than the one on the top, and it's always a lot better than make config.

When choosing the appropriate configuration, there are three choices, each representing the following meanings:

y--the feature into the kernel
n--does not compile this feature into the kernel
m--compiles this functionality into modules that can be dynamically inserted into the kernel when needed

If you are using make xconfig, you can use the mouse to select the corresponding option. If you are using make menuconfig, you need to use the SPACEBAR to select it. You'll find that you have parentheses in front of each option, but there are brackets in parentheses and parentheses. When you choose with the space bar, you can find that the brackets are either empty, or "*", and the angle brackets can be empty, "*" and "M", which means that the corresponding items are either not or compiled into the kernel, and the latter is much the same and can be compiled into modules. The content of the parentheses is to select one of the several options provided.

In the process of compiling the kernel, the most tumultuous thing is this step configuration work, many beginners are not clear exactly how to choose these options. In fact, when configured, most options can use their default values, and only a small portion of it needs to be selected according to the user's different needs. The principle of the selection is to compile the partial functional code which is far away from the other parts of the kernel and not frequently used to be loaded into the Loadable module, which is helpful to reduce the kernel length, reduce the memory consumed by the kernel, and simplify the effect on the kernel when the corresponding environment changes. Do not select , and some of the functional code that the kernel cares about closely and often uses is compiled directly into the kernel. Let's introduce the common options.

1. Code Maturity Level Options
Code maturity level. There is only one item here: Prompt for development and/or incomplete code/drivers, if you want to experiment with features that are still in the experimental phase, such as KHTTPD, IPV6, and so on, you must select the item Y. , otherwise you can select it as N.

2. Loadable Module Support
Support for the module. Here are three items:

enable loadable Module Support: Unless you are ready to compile all the required content into the kernel, the item should be optional.
set version information on the All module symbols: You can not select it.
kernel Module Loader: Allows the kernel to load the required modules at boot time, recommended for selection.

3. Processor type and Features
CPU type. A lot of content, not to be introduced, the following several:


processor family: Select the CPU type according to your own situation.
high Memory Support: Support for large-capacity memory. can support to 4G, 64G, generally can not choose.
math emulation: Co-processor emulation. Coprocessor is the darling of the 386 era, it is no longer used.
mttr support:mttr Support. I don't choose.
symmetric multi-processing Support: Symmetric multi-processing support. Unless you're rich with multiple CPUs, you don't have to choose.

4. General Setup
Here are some of the most common properties to set. This part of the content is very much, generally use the default settings on it. Here are some of the most frequently used options:

networking Support: Network support. must, no network card also suggest you choose.
PCI SUPPORT:PCI Support. If you use a PCI card, of course, you must choose.
PCI Access MODE:PCI access mode. Available with BIOS, direct, and any, select any.
support for Hot-pluggabel devices: Hot-swappable device support. The support is not too good, not choice.
pcmcia/cardbus Support:pcmcia/cardbus Support. If you have a PCMCIA, you must choose.
system V IPC
bsd Process Accounting
SYSCTL Support: The above three items are related to process/IPC calls, mainly the System V and BSD two styles. If you are not using BSD, follow the default.
power Management Support: Power management support.
advanced Power Management BIOS Support: Advanced source management biod support.

5. Memory Technology Device (MTD)
MTD device Support. I don't choose.

6. Parallel Port Support
Serial port support. If you don't intend to use a serial port, don't choose.

7. Plug and Play Configuration
Plug and Play support. Although Linux is not as good as Windows for Plug and Play, it's a choice, so you can unplug the mouse and experience the Plug and Play of Linux.

8. Block devices
Block equipment support. This has to be the case for their own choice, simply explain it:

normal PC Floppy disk support: Normal PC floppy disk support. This should be a must choice.
XT Hard disk Support:
compaq SMART2 Support:
mulex dac960/dac1100 PCI RAID Controller support:raid mirroring.
loopback Device Support:
network Blocks Device Support: Network block device support. If you want to visit your online neighborhood, choose it.
logical Volume Manager (LVM) Support: Logical volume management support.
multiple Devices Driver Support: Multi-device driver support.
ram disk Support:ram disk support.

9. Networking options
Network options. The network protocol is configured here. The content is too much, not one to introduce, see for yourself, if you understand the network agreement, should be able to read. If you don't bother to look, use the default option (sure to select TCP/IP networking OH) on it. Let's take a look at TCP/IP, ATM, IPX, DECnet, Appletalk ... Supported protocols Oh, IPV6 also supported, Qos and/or fair queueing (quality of Service fair Dispatch) also supported, but also KHTTPD, but these are still in the experimental stage.

10. Telephony Support
Telephone support. What's this stuff? Let me check help, the original Linux can support the phone card, so you can use the IP on the regular phone to provide voice services. Remember, the phone card has nothing to do with the modem.

11. ATA/IDE/MFM/RLL Support
This is about the various interfaces of hard disk/optical drive/tape/floppy support, too much content, use the default option, if you use a more special equipment, such as PCMCIA, and so on, go to find the appropriate options.


SCSI Support
Support for SCSI equipment. I don't have a SCSI device, so I don't have to choose it, and if you use a SCSI hard drive/optical drive/tape device, you can find it yourself.


13. IEEE 1394 (FireWire) support
What is this? The lower version of the has not seen, see Help again. The original is to fireware hardware to improve the performance of the serial bus, I did not choose.

14. I2O Device Support
This is also not clear, help said this need to I2O interface adapter to support, in the intelligent input/output (I2O) system interface to use, but also to hardware, not selected.

15. Network Device Support
Network equipment support. The above selected the agreement, now the choice of equipment, it is conceivable that the content must be very much. It's okay, it's probably sorted, with arcnet equipment, Ethernet (Mbit), Ethernet (1000Mbit), Wireless LAN (Non-hamradio), Token ring device, Wan interfaces, PCMCIA network device support several major categories. I'm using 10/100m Ethernet, which seems to be the only choice. or 10/100m Ethernet equipment is familiar with the content, although more, at a glance I can see the Realteck RTL-8139 PCI Fast Ethernet Adapter Support, in order to avoid trouble, compiled into the kernel well, do not select m, select Y. Be patient, generally you can find your own network card. If not, you have to go to the manufacturer to drive.

16. Amateur Radio Support
Another do not understand, should be configured amateur radio broadcast it, no, no.

17. IrDA (infrared) support
This should be infrared support, free of.

18. ISDN Subsystem
If you use ISDN to surf the Internet, this is essential. I'll see what I can do.

19. Old CD-ROM drivers (not SCSI, not IDE)
Can do is really thoughtful, those who are not scsi/ide the optical drive who still use Ah, choose it, anyway, I am using the IDE CD-ROM, do not choose this.

20. Character devices
Character equipment. This content is too much, first use the default settings, you need to modify them. Let's introduce the Big class:

I2C SUPPORT:I2C is a Low-speed serial bus protocol used in Philips's highly-driven micro-control applications. This is required if you want to select the video for Linux below.
mice: Mouse. Now can support the bus, serial, PS/2, c&t 82c710 mouse port, PC110 digitizer pad, according to their own choice.
joysticks: Handle. Even under Linux the handle drive is not too big, the game is too few.
watchdog Cards: Although called Cards, this can be achieved with pure software, of course, there are hardware. If you select this, you will create a file called watchdog under your/dev, which can record the operation of your system until about 1 minutes after the system reboots. With this file, you can restore the system to the state before the reboot.
video for Linux: support for related audio/video cards.
ftape, the floppy tape device driver:
pcmcia Character Device Support:

21. File Systems
File system. There's too much content, old ways to make changes based on the default options. Introduces the following:

quota Support:quota can limit the amount of hard disk space that can be used by each user, and is very effective in situations where multiple users use a single host together.
dos fat fs Support:dos FAT file format support for FAT16, FAT32.
iso 9660 CD-ROM file system support: The CD uses the ISO 9660 file format.
The ntfs file system Support:ntfs is the files format used by NT.
The /proc file System Support:/proc filesystem is the channel that Linux provides to users and systems to interact with, and it is recommended that some features fail to execute correctly.

There are three other big classes that are here: Network file Systems (Network File system), Partition Types (partition type), Native Language Support (native language support). It is worth mentioning two of the network file Systems: NFS and SMB are the file systems used by Linux and Windows to access each other in the form of network neighbors, and are selected as needed.

22. Console drivers
Control Console Drive. Generally use the VGA text console on it, the standard 80*25 text console.

23. Sound
Sound card driver. If you can find the sound card driver in the list, that's best, or try the OSS.

24. USB Supprot
USB support. Many USB devices, such as mice, modems, printers, scanners, and so on, can be supported on Linux, depending on the needs of the choice.

25. Kernel hacking
This is configured, even when the system crashes, you can do a certain job. Ordinary users do not need this function.

Finally configuration is finished, now save the exit, of course, you can also put the current configuration file, so the next time to configure the time will save energy.


Compile


In the complex configuration after the completion of the following you can own to a cup of tea patiently waiting. The compilation-related commands are as follows:

#make DEP
#make Clean
#make Zimage
#make Bzimage
#make Modules
#make Modules_install
#depmod-A

The first command make DEP actually reads the configuration file generated by the configuration process to create a dependency tree that corresponds to the configuration, which determines what needs to be compiled and those that are not needed; The second command makes clean completes deleting the files left by the previous steps to avoid some errors; the third command make The Zimage and Fourth command make Bzimage implement a fully-compiled kernel, both of which are built using gzip compression, as long as one is sufficient, and the difference is that using make bzimage can generate a larger kernel, For example, if you use the Make Zimage command when compiling the 2.4.0 version of the kernel, there will be a system too big error prompt. We recommend that you use the Make Bzimage command.

The following three commands are only necessary when you have configured the answer to the Enable Loadable Module support (config_modules), make MODULES and make Modules_ The install generates the corresponding modules and copies the modules to the desired directory.

Strictly speaking, the seventh command is not related to the compilation process, it is the dependencies between the generation module, so you start the new kernel, use the modprobe command to load the module can correctly locate the module.

Update

After the above steps, we finally got the new version of the kernel. In order to be able to use the new version of the kernel, we also need to make some changes:

#cp/usr/src/linux/system.map/boot/system.map-2.4.0test8
#cp/usr/src/linux/arch/i386/bzimage/boot/vmlinuz-2.4.0test8

The above two files are the only one we have just compiled the newly generated. Below, modify the two links System.map and Vmlinuz under/boot to point to the file for the new kernel:

#cd/boot;rm-f System.map Vmlinuz
#ln-S Vmlinuz-2.4.0test8 vmlinuz
#ln-S System.map-2.4.0test8 system.map

Then modify the/etc/lilo.conf:

#vi/etc/lilo.conf

Add the following paragraph:

Image=/boot/vmlinuz-2.4.0test8
label=linux240
Read-only
Root=/dev/hda2

One of the root=/dev/hda2 should be modified according to the need.
Run:

#/sbin/lilo-v

Confirm the correct edits to/etc/lilo.conf and reboot the system now:

#shutdown-R Now

In the machine restart after the emergence of Lilo when the TAB key, input linux240, our new kernel play a role, enjoy it.


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.