Linux Kernel entry-how to obtain the Linux kernel source code and generate the configuration Kernel

Source: Internet
Author: User

How to obtain the Linux kernel source code
How to obtain the Linux kernel source code

To download the Linux kernel, you must go to the official website. The website provides two types of file downloads: one is the complete Linux kernel, and the other is the incremental kernel patch. They are all tar archive packages. Unless you need to use the old Linux kernel for special reasons, you should always upgrade to the latest version.

Use git

The Linux kernel development team led by Linus began using the GIT version control system to manage the Linux kernel a few years ago (refer to: What is git ?), The GIT project is also created by Linus. Unlike traditional CVs, git is distributed. Therefore, many developers may feel unfamiliar with its usage and workflow, however, I strongly recommend that you use git to download and manage the Linux kernel source code.

You can use the following git command to obtain the latest "push" version of the Linus kernel code tree:

$ Git clone git: // git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

 

Run the following command to synchronize the latest status of your code tree with the Linus code tree:

$ Git pull

Install kernel source code

The kernel packages are in the GNU zip and Bzip2 formats. Bzip2 is the default and preferred format, because its compression ratio is usually better than gzip, Bzip2 Linux kernel package in the form of generic linux-x.y.z.tar.bz2 file name, here X. y. Z is the specific version of the kernel source code. After downloading it to the source code package, it is very easy to extract and decompress it. If you download the Bzip2 package, run:

$ tar xvjf linux-x.y.z.tar.bz2

If you download a gzip package, run:

$ tar xvzf linux-x.y.z.tar.gz

No matter which command is executed, the source code will be decompressed and extracted to the linux-x.y.z directory, if you use git to download and manage the kernel source code, you do not need to download the tar package, you only need to run the GIT clone command to automatically download and decompress it.

The kernel source code is usually installed in/usr/src/Linux, but it is best not to use this source code tree during development, because the kernel version compiled for your C library is usually linked here.

Application Patches

Linux Kernel developers will make their modifications into patches to share with others, and the patches are incremental. Incremental patching is an effective way to move from one kernel tree to another, you can upgrade the kernel without downloading the complete kernel package. This not only saves bandwidth, but also saves kernel upgrade time. Before applying the patch, go to the directory of the kernel source code tree and run:

$ patch –p1 < ../patch-x.y.z

Note that the patch package also has a clear version number. The version number here must be the same as the version number of the Linux kernel source code. When the kernel and patch version number are inconsistent, force application of the patch will cause unexpected consequences.

Kernel source code Tree Introduction

The kernel source code tree is divided into many directories, which contain many subdirectories. The top-level directories of the source code tree and their descriptions are listed in the table below.

Directory Description
Arch Source code of a specific architecture
Block Block I/O layer
Crypto Encryption API
Documentation Kernel source code documentation
Drivers Device Driver
Firmware Use the device firmware required by a driver
FS VFS and independent File System
Include Kernel header
Init Kernel startup and initialization
IPC Inter-process communication
Kernel Core subsystems, such as schedulers
Lib Assistant routine
Mm Memory Management Subsystem and Vm
Net Network subsystem
Samples Sample Code
Scripts Script used to generate the kernel
Security Linux Security Module
Sound Sound Subsystem
USR Early user space code (called initramfs)
Tools Tools used for Linux development
Virt Virtualization infrastructure

There are still many files in the root directory of the source code tree. Copying is the kernel license description file (gnu gpl V2), and credits is the list of developers involved in the Linux kernel, maintainers lists individuals who maintain subsystems and drivers. makefile is the basis of kernel makefile.

Generate and configure the kernel

 

Generate Kernel

It is actually easier to generate a kernel than to compile and install other system-level components, such as glibc. From version 2.6, the Linux kernel introduces a new configuration and generation system, it makes operations on the Production kernel easier.

Configure the kernel

Although we have obtained the kernel source code, we can configure and customize it as needed before compiling, and compile the specified functions and desired drivers, configuring the kernel is a required step to generate the kernel. Because the kernel provides a large number of features and supports various hardware, many of which need to be configured. The Kernel configuration is controlled by the configuration options, configuration options have a Config prefix. For example, Symmetric Multi-processing (SMP) is configured by the config_smp configuration option. If this option is set, SMP is enabled, and vice versa, the configuration option can determine the file to be generated or manipulate the Code through the preprocessing command.

Configuration options can control the generation process either Boolean or tri-state, and Boolean is "yes" or "no". Most Kernel configuration options are Boolean, such as config_preempt, the three-state model adds a "module" option on the basis of "yes" and "no". The module option indicates that the configuration option is set, but it is finally compiled into a module, instead of directly compiling the data into the kernel, the module can be understood as an independent and dynamic loading object. Generally, the driver configuration is usually in three states.

The configuration option can also be a string or integer. This option does not control the generation process. The specified value is used when the kernel source code accesses the pre-processing macro. For example, you can specify the size of the static allocation array for a configuration option.

Linux vendors also provide pre-compiled kernels with the release, such as the kernel provided by canonical for Ubuntu or red hat for Fedora. Such kernels usually only enable the required kernel functions, almost all drivers are compiled into modules. Such a kernel provides a good basic kernel and extensive hardware module support. In any case, you want to become a kernel master, you should compile your own kernel.

Fortunately, the kernel provides many tools to simplify the configuration. The simplest tool is a text command line-based utility, such:

$ make config

This tool will be configured with one option, but users need to participate, such as specifying "Yes (y)", "No (m)", or "module (m )", the entire configuration process takes a long time. Therefore, unless someone is billed hourly, please upgrade the kernel. There is no reason to use this primitive method to configure the kernel. On the contrary, there are ready-made ncurses-based graphical tools to replace.

$ make menuconfig

Or a graphical Tool Based on GTK +

$ make gconfig

All three of the preceding tools divide configuration options into multiple categories, such as "Processor type and feature". You can move back and forth between these categories to view kernel options, of course, you can also modify their settings.

The following command creates a default configuration basis based on your architecture.

$ make defconfig

Although the default configuration is somewhat arbitrary (on i386, the default configuration is configured by Linus), if you have never configured the kernel, it provides a good start.

The configuration option is stored in the source code root directory named. in the config file, you can open this file and manually edit the configuration options. After modification, or you want to apply the existing configuration file to the new kernel source code tree, you can use the following command to verify and update the Configuration:

$ make oldconfig

You must run this command before generating the kernel.

The configuration option config_ikconfig_proc specifies the location of the complete Kernel configuration file compressed package. The default value is/proc/config.gz, which makes it very easy to clone the existing configuration when generating the new kernel. If this option is enabled for your current kernel, you can copy the configuration file from/proc and then generate a new kernel:

$ zcat /proc/config.gz > .config $ make oldconfig

After the kernel is configured, use the following command to generate it:

$ make

Unlike the kernel earlier than 2.6, you no longer need to execute the make Dep command before generating the kernel. The dependency tree is automatically maintained and you do not need to specify a specific generation type, such as bzimage, or an independent generation module. By default, makefile rules automatically process everything.

Minimize interference Information

Alarms and errors may interfere with the generation process. One trick to minimize interference information is to redirect the make output, but you will still see some warnings and errors:

$ make > ../detritus

If you want to view the generated output, you can read this file later. If you do not want to see any output at all, redirect to/dev/null:

$ make > /dev/null

Execute multiple generated jobs at the same time

The make command provides a function to split the generation process into multiple parallel jobs, which can be run independently or in parallel, in a multi-processor system, the generation speed is greatly improved, and the processor utilization is also improved, because a large source code tree will generate a large amount of I/O wait time.

By default, make can only be split into one job, because makefiles often contain incorrect dependency information. If so, multiple parallel jobs will cause confusion, this will eventually cause the generation process to fail. If the dependency information in makefiles is correct, it can be split into multiple jobs for execution, for example:

$ make –jn

Here, N indicates the number of jobs to be split. Generally, one or two jobs are split by each processor. For example, on a 16-core machine, you can run:

$ Make-j32>/dev/null

From: http://www.360its.com/category/%E6%96%87%E7% AB %A0%E6%A0%87%E7%AD%BE/linux
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.