Linux Kernel compilation and Installation

Source: Internet
Author: User

Linux Kernel compilation and Installation
GuideThe Linux kernel is a POSIX-compliant Unix-like operating system written in C language. the kernel is the most basic part of the operating system and provides a mechanism for many applications to access computer hardware. A major feature of the Linux kernel is the integral structure, which consists of many processes. Each process can be compiled independently. Its Module mechanism makes the kernel independent and easy to expand.Linux Kernel Introduction

The Linux release is based on the Linux kernel and packaged and configured with the out-of-band application software and tools. The first Linux kernel was developed in 1991 by Linus Torvalds, who was still studying at the computer department at the University of Helsinki, Finland. After that, Linus soon gathered a large number of developers and users from other free software projects to contribute code to the Linux kernel. At present, it is estimated that thousands of developers are contributing code to the Linux kernel.

Linux Kernel is named as A. B .C.D after version 2.6.0 is released. Changes in A and B can be said to be irrelevant. C is the real version of the kernel, and changes in each version will bring new features. For example, internal API changes often involve tens of thousands of changes. D. security patches and bug fixes. If you are a beginner or user of Linux, you only need to know about stable. It represents a stable version of kernel update. Mainline refers to the current official kernel, which is updated and maintained by Linus Torvalds. The Code contributed by developers is mainly merged into mainline. Linux-next and snapshot are the snapshots generated before the end of the Code submission cycle. They are used for testing and use by Linux code contributors. Currently, the update cycle of the stable version is six to ten weeks. The rc of the next stable version is updated every week. The new version has two kernel types: Full Source and Full kernel. Generally, it is the tar.gz).bz2 file. The other is the patch file. Patch files can only be used in a timely manner from K to several hundred K, but for a specific version, you must find your own version.

Compile and install the kernel1. Download and unzip the kernel

Kernel download Website: https://www.kernel.org/
Unzip the kernel: tar xf linux-2.6.XX.tar.xz

2. Custom kernel: make menuconfig

For more information, see makefile menuconfig.

3. Compile the kernel and module: make

Generate the kernel module and vmlinuz, initrd. img, and Symtem. map Files.

4. install the kernel and module: sudo make modules_install install

Copy the module file to the/lib/modules directory, copy the config, vmlinuz, initrd. img, and Symtem. map files to the/boot directory, and update grub.

5. Other commands:

Make mrprobe: The command is used to clear the source code tree by executing the "make mrproper" command every time you configure and re-compile the kernel, including the previously configured Kernel configuration file. config will be cleared. That is, the old configuration file will be deleted during the new compilation to avoid affecting the new kernel compilation.
Make dep: generate the dependency between kernel functions and prepare for compiling the kernel.

Several important Linux kernel introductionsConfig

The Kernel configuration file generated by make menuconfig determines whether to compile each function system of the kernel into the kernel, compile it into a module, or not compile it.

Vmlinuz and vmlinux

Vmlinuz is a bootable and compressed kernel. "vm" represents "Virtual Memory ". Linux supports Virtual Memory. Unlike the old operating system, such as DOS, which has a limit of KB memory, Linux can use hard disk space as virtual memory. Therefore, it is named "vm ". Vmlinuz is an executable Linux kernel. There are two ways to establish vmlinuz: one is to create the kernel through "make zImage" when compiling the kernel. zImage is suitable for small kernels, it exists for backward compatibility. Second, it is created by running the make bzImage command during kernel compilation. bzImage is a compressed kernel image. Note that bzImage is not compressed with bzip2, bz in bzImage is easy to misunderstand. bz indicates "big zImage" and B in bzImage indicates "big. Both zImage (vmlinuz) and bzImage (vmlinuz) are compressed using gzip. They are not only a compressed file, but are embedded with gzip decompression code at the beginning of the two files. Therefore, you cannot use gunzip or gzip-dc to unpack vmlinuz. The kernel file contains a micro gzip used to extract the kernel and boot it. The difference between the two is that the old zImage decompress the kernel to the low-end memory (the first 640 K), and The bzImage decompress the kernel to the high-end memory (more than 1 MB ). If the kernel is small, zImage or bzImage can be used. The two methods guide the same system runtime. BzImage is used for large kernels, and zImage cannot be used. Vmlinux is an uncompressed kernel and vmlinuz is a compressed file of vmlinux.

Initrd. img

Initrd is short for "initial ramdisk. Initrd is generally used to temporarily boot the hardware to the State where the actual kernel vmlinuz can take over and continue to boot. For example, initrd-2.4.7-10. img is mainly used to load file systems such as ext3 and the drivers of scsi devices. If you are using a scsi hard disk and the kernel vmlinuz does not have the scsi hardware driver, the kernel cannot load the root file system before it is loaded into the scsi module, however, the scsi module is stored in/lib/modules of the root file system. To solve this problem, we can guide an initrd kernel that can read the actual kernel and correct the scsi Boot problem with initrd, The initrd-2.4.7-10.img is a file compressed with gzip. The initrd image file is created using mkinitrd. The mkinitrd utility can create the initrd image file. This command is proprietary to RedHat. Other Linux distributions may have corresponding commands. This is a very convenient utility. For details, see help: man mkinitrd

System. map

System. map is a kernel symbol table of a specific kernel. It is generated by "nm vmlinux" and irrelevant symbols are filtered out.
The following lines are from/usr/src/linux-2.4/Makefile:

nm vmlinux | grep -v '(compiled)|(.o$$)|( [aUw] )|(..ng$$)|(LASH[RL]DI)' | sort > System.map

Some symbols such as variable names or function names are named during program design. The Linux kernel is a complex code block with many global symbols. the Linux kernel uses the variable or function address instead of the symbol name to identify the variable or function name, for example, instead of using a symbol like size_t BytesRead, the variable is referenced like c0343f20. For people who use computers, they prefer names like size_t BytesRead rather than c0343f20. The kernel is mainly written in c, so the compiler/connector allows us to use a symbolic name for encoding, while the kernel uses an address for running. However, in some cases, we need to know the address of the symbol or the symbol corresponding to the address, which is completed by the symbol table, A symbol table is a list of all symbols along with their addresses.

The Linux symbol table uses two files:/proc/ksyms and System. map. /Proc/ksyms is a "proc file" created during kernel boot. In fact, it is not really a file. It is just a representation of kernel data, but it gives people the illusion of a disk file, which can be seen from its file size being 0. However, System. map exists in the actual file System. When you compile a new kernel, the address of each symbol name changes. Your old System. map has incorrect symbolic information, which generates a new System each time the kernel is compiled. map, you should use the new System. map to replace the old System. map.

Although the kernel itself does not actually use System. map, other programs such as klogd, lsof, and ps need a correct System. map. If you use incorrect or do not have System. map, the output of klogd will be unreliable, which will cause difficulties in troubleshooting the program. Without System. map, you may face annoying prompts. In addition, a few drivers need System. map to parse the symbols. If System. map is not created for the specific kernel you are running, they cannot work normally.

For Linux kernel log daemon klogd to perform name-Address Resolution, klogd needs to use System. map. System. map should be placed where the software that uses it can find it. Run: man klogd. map is used as the location of a variable to klogd, so it will query System in three places in the following order. map:/boot/System. map,/System. map,/usr/src/linux/System. map
System. map also has version information. klogd can intelligently find the correct map file.

Makefile menuconfig

What does the system do for us when we execute the make menuconfig command? This involves a total of several files. We will discuss them one by one.

1. scripts folder under the Linux kernel root directory

2. The arch/$ ARCH/Kconfig file and the Kconfig file under each layer directory

3. makefile files under the Linux kernel root directory and makefile files under directories at different layers

4. The. config file and the files under arch/$ ARCH/configs/in the root directory of the Linux Kernel

5. Linux kernel root directory include/generated/autoconf. h file
1) the scripts folder stores files related to drawing images on the make menuconfig configuration interface. as users, we do not need to care about the contents of this folder.

2) When we execute the make menuconfig command and the above blue configuration interface appears, the system helps us do the following:
First, the system will read the Kconfig file in the arch/$ ARCH/directory to generate the entire configuration interface option (Kconfig is the core of the linux configuration mechanism). What is the value of the ARCH environment variable equal? It is determined by the makefile file in the root directory of the Linux kernel. The makefile contains the definition of this environment variable:

SUBARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \                  -e s/arm.*/arm/ -e s/sa110/arm/ \                  -e s/s390x/s390/ -e s/parisc64/parisc/ \                  -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \                  -e s/sh[234].*/sh/ )      ..........      export KBUILD_BUILDHOST := $(SUBARCH)      ARCH        ?= $(SUBARCH)      CROSS_COMPILE   ?=

You can also use the make ARCH = arm menuconfig command to generate the configuration interface, such as the Academic Affairs Office, to take an exam. The exam subjects may include foreign languages, Chinese languages, and mathematics. Here we choose arm to take the exam, the system will read the arm/kconfig file to generate configuration options (select the arm section of the paper), the system also provides x86 section, milps Section and other more than 10 course questions

3) assume that the Office of Academic Affairs is "benevolent". In order to prevent some students from making wrong questions, we have prepared a reference answer (default configuration options ), stored in the arch/$ ARCH/configs/directory. For arm, it is the arch/arm/configs Folder:

There are many options in this folder. Which one will the system read? By default, the kernel reads data from the root directory of the Linux kernel. the config file is used as the default kernel option (answers to the questions ), we usually select a series that is the closest to our development board to the Linux kernel root directory based on the type of the Development Board (select the nearest reference answer)

4). config
Assuming that the academic affairs office has been focused, his reference answers are not completely correct (. the config file does not exactly match our board. run the make menuconfig command to read the new options. However, we generally do not adopt this scheme. In the configuration interface, we choose to use spaces, esc, and press enter to select some options or do not select, and finally save and exit, the Linux Kernel updates the new options (correct answers). config. config is renamed to another file and saved (when you execute make distclean, the system will. delete the config file). We will not need to go to arch/arm/configs to obtain the corresponding file when we configure the kernel later, saving the trouble of reconfiguration and saving it directly. copy the config file. config.

5) after the above two steps, we can correctly read and configure the interface we need. How can they establish a compilation relationship with the makefile file? When you save the make menuconfig option, the system will automatically update it. in addition to config, all options are saved as Macros in the Linux Kernel root directory include/generated/autoconf. h file

The source code in the kernel will contain the above. h file and implement Conditional compilation with the macro definition. When we need to select whether to compile a file as a whole, we also need to modify the corresponding makefile file, for example:

When we choose whether to compile the s3c2410_ts.c file, makefile will decide to compile this file according to CONFIG_TOUCHSCREEN_S3C2410. This macro is defined in the Kconfig file. After the configuration is complete, it will appear in. in config and autconf, we have completed the entire Linux kernel compilation process. Finally, we will find that throughout the Linux Kernel configuration process, only the Kconfig, makefile files, and corresponding source files of each layer are left for the user interface.

For example, if you want to add a function to the kernel and use make menuconfig to control its claim process, you must first modify the Kconfig file in the corresponding directory, add the corresponding options according to the Kconfig syntax. Then execute make menuconfig to select whether to compile into the kernel or not to compile into the kernel, or compile it as a module ,. config File and autoconf. the H file is automatically generated. The makefile file in the corresponding directory is modified to add the compilation option. The make command is executed to compile the file.

Kconfig and Makefile

Each directory of the Linux kernel source code tree contains two documents: Kconfig and Makefile. The Kconfig distributed to each directory constitutes a distributed Kernel configuration database. Each Kconfig describes the Kernel configuration menus related to the source document of the directory. When you execute the Kernel configuration make menuconfig, read the menu from Kconfig and save it to the Kernel configuration document of. config. When the kernel is compiled, the main Makefile calls this. config to understand the user's choice. This content indicates that Kconfig corresponds to each level of configuration menu of the kernel.

If you want to add a new driver to the kernel source code, you need to modify Kconfig so that you can select this driver. If you want to compile this driver, you need to modify the Makefile. There are two types of files to be modified when adding a new driver (if only files are added, you only need to modify the Kconfig and Makefile files of the current layer; if the directory is added, you need to modify a pair of Kconfig and Makefile in the current layer and directory) Kconfig and Makefile. To know how to modify these two documents, you need to know the syntax structure of the two documents. For the syntax of Kconfig, see the reference document "[linux-2.6.31] kbuild".
The Makefile contains five parts:

Makefile at the top layer of Makefile. config Kernel configuration file arch/$ (ARCH)/Makefile architecture Makefile scripts/Makefile. * Applicable to the general rules of all kbuild Makefiles and other kbuild Makefiles, there are about 500 such files

The top-level Makefile reads the. config file generated by the Kernel configuration operation. The top-level Makefile builds two main objectives: vmlinux (kernel image) and modules (all module files ). It recursively accesses sub-directories under the kernel source code tree to build these targets. Which subdirectories are accessed depends on the Kernel configuration. The top-level Makefile contains an architecture Makefile, which is specified by arch/$ (ARCH)/Makefile. The architecture Makefile file provides information about the specific architecture for the top-level Makefile. Each subdirectory has a kbuild file and Makefile file to execute commands passed from the upper layer. The kbuild and Makefile files use the information in the. config file to construct a list of various files built in by kbuild or used by module objects. Scripts/Makefile. * contains all the definitions/rules, and so on. This information is used to build the kernel using the kbuild and Makefile files.

Address: http://www.linuxprobe.com/linux-kernel-compilation.html


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.