Linux Kernel compilation practices

Source: Internet
Author: User
Linux Kernel compilation practices-general Linux technology-Linux programming and kernel information. The following is a detailed description. I recently read several articles on Linux kernel compilation on the Internet. I have read a few articles about this. I encountered many problems when I did it myself. It seems that you cannot follow other people's methods, because your current machine working environment may be different. The entire process is tortuous. But it succeeded! Below I will give my own practices and points of attention.

I. Why do we need to 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:

(1) self-built compiled kernels run faster (with less code)
(2) The system will have more memory (the kernel part will not be exchanged to the virtual memory)
(3) Compilation of unnecessary functions into the kernel may add vulnerabilities exploited by system attackers
(4) compiling a function as a module is slower than compiling it into the kernel.

Ii. 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.

3. Obtain and update the new Linux Kernel
Linux Kernel acquisition, you can purchase a CD, copy from a friend, of course, the most direct and convenient way is to the Linux kernel official Web site: http://www.kernel.org to get the latest version, the other is the patch file. All of them are packaged in the form of tar.gzw.tar.bz2. Note that the root permission is required for compiling the kernel. The following compilation and update work is implemented by the root user.

Iv. Kernel configuration
The kernel package I downloaded from the linux official website is: linux-2.6.26.2.tar.bz2 and copied to the/usr/src/kernels/directory under my fc6 (kernel 2.6.18. The kernel source code of my running system is named (2.6.18.1.2798.fc6-i686) in this directory ). Decompress the new kernel package and run the following command:

# Linux-2.6.26.2.tar.bz2/usr/src/kernels/
# Cd/usr/src/kernels/
# Tar jvxf linux-2.6.26.2.tar.bz2
# Tar cvzf 2.6.18.1.2798.fc6-i686/* you can back up the old version. Optional */

Several important files should be backed up in case of backup. (Optional)

1. Back up the kernel Startup File/boot
# Cd/boot
# Tar cvzf boot.tar.gz boot

2. Backup System header files
# Cd/usr/include
# Tar cvzf linux.tar.gz linux

3. database files in the backup module
# Cd/lib/modules
# Tar cvzf 2.6.18.1.2798.fc6.tar.gz 2.6.18.1.2798.fc6

4. Backup important configuration directories
# Cd/
# Tar cvzf etc.tar.gz etc

The next step is the actual work.
# Cd/usr/src/kernels/linux-2.6.26.2
# 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.

The next step is the configuration before the kernel compilation. Whether or not the configuration is appropriate is closely related to your use of linux in the future. But most of them only need to use the default configuration, Unless you want special applications. For details about how to configure important common kernel options, see http://lamp.linux.gov.cn/linux/kernel_options.html.

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, I use the # make menuconfig command. Although this is an operation on a text character terminal, as a LINUX learner, you must get used to and enjoy the operations under the command line. When selecting the corresponding configuration, it is necessary to explain the meanings of the three symbols:

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 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" indicate that the items corresponding to the former are either not required 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.

As mentioned above, in actual configuration, most options can use their default values. Only a small part needs to be selected according to 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.

V. kernel and kernel module Compilation

1. Enter the new kernel source code directory
# Cd/usr/src/kernels/linux-2.6.26.2

2. Generate a configuration file during the read configuration process and create a dependency tree corresponding to the configuration to determine which need to be compiled and which do not need to be compiled
# Make dep

3. Delete all files left in the previous step to avoid errors. (Skip this step if the new download kernel has not been compiled)
# Make clean

4. Compile the kernel
# Make bzImage
After the above command is compiled successfully, the/usr/src/kernels/linux-2.6.26.2/arch/x86/boot/
Directory to generate a new kernel image bzImage

5. Compile and load the kernel module
# Make modules

6. Copy the generated modules to the desired directory.
# Make modules_install

7. Generate the dependency between modules. After the new kernel is started, the module can be determined when the modprobe command is used to load the module.
# Depmod-

Note that step 5-7 is necessary only when Enable loadable module support (CONFIG_MODULES) is selected during Kernel configuration.

After the above steps, a new kernel and module are generated. In order to be able to use the new kernel, the following work should be done.

# Cp/usr/src/kernels/linux-2.6.26.2/System. map/boot
# Cp/usr/src/kernels/linux-2.6.26.2/arch/x86/boot/bzImage/boot
# Cd/boot
# Mv bzImage vmlinuz-2.6.26

Note that the previous two commands did not overwrite the original kernel image and kernel symbol stealth table like many other posts on the Internet. I put the new one directly under/boot, so that the new kernel can be started when the new kernel cannot be started. (I was deeply touched by this. In fact, I successfully compiled and generated bzImage at the beginning, but I copied him wrong and copied the vmlinux in the same directory, as a result, the linux system won't be able to start up. Later, I went to the grub command line and found the old kernel image to start up. We can find that the kernel is updated, there seems to be no updates to the file system. Are there any mistakes ?)

6. Modify and restart the manager
I used grub to boot the kernel, but lilo has never used it, but there are many such posts on the Internet for reference. The following is my file/boot/grub. conf.

# Grub. conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You do not have a/boot partition. This means that
# All kernel and initrd paths are relative to/, eg.
# Root (hd0, 7)
# Kernel/boot/vmlinuz-version ro root =/dev/hda8
# Initrd/boot/initrd-version.img
# Boot =/dev/hda
Default = 1
Timeout = 5
Splashimage = (hd0, 7)/boot/grub/splash.xpm.gz
Hiddenmenu

Title Fedora Core (2.6.26.2.fc6)
Root (hd0, 7)
Kernel/boot/vmlinuz-2.6.26 ro root = LABEL =/rhgb quiet
Initrd/boot/initrd-2.6.18-1.2798.fc6.img

Title Fedora Core (2.6.18-1.2798.fc6)
Root (hd0, 7)
Kernel/boot/vmlinuz-2.6.18-1.2798.fc6 ro root = LABEL =/rhgb quiet
Initrd/boot/initrd-2.6.18-1.2798.fc6.img

Title Windows XP
Rootnoverify (hd0, 0)
Chainloader + 1

The above simhei part is added later and saved after being added. The original kernel startup has not changed, so that the old kernel can be started successfully after the new kernel fails to be started! It is important to keep the startup items of the old kernel. Restart the computer and wait for the new kernel to start successfully!

During the Linux kernel compilation process, there may be many problems in practice. As long as you break through the issues and stick to the ground, it will certainly be a surprise!
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.