In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to compile and install the Linux kernel". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to compile and install the Linux kernel" can help you solve the problem.
Introduction to Linux Kernel
The Linux distribution is based on the Linux kernel and is packaged and configured with external applications and tools. The original Linux kernel was developed in 1991 by Linus Torvalds, then a computer science student at the University of Helsinki in Finland, and Linus soon gathered a large number of developers and users from other free software projects to contribute code to the Linux kernel. Currently, it is estimated that thousands of developers are contributing code to the Linux kernel.
Since the release of version 2.6.0, the Linux kernel has been named as A.B.C.D. It can be said that the changes in An and B don't matter. C is the real version of the kernel, and each change brings new features. For example, the internal API changes and so on, the number of changes is often tens of thousands. D is a security patch and bug fix. If you are a beginner or user of Linux, all you need to know is stable, which represents a stable kernel update. Mainline refers to the current official kernel, updated and maintained by Linus Torvalds, and the code contributed by developers is mainly merged into mainline. Both linux-next and snapshot are snapshots generated before the end of the code submission cycle for testing by Linux code contributors. The current stable version has an update cycle of six to ten weeks, and the next stable version of rc will be updated almost weekly. There are two new versions of the kernel, one is the Full Source version, the complete kernel version. Larger, usually tar.gz or .bz2 files. The other is the patch file, the patch file. Patch files are usually only available from K to several hundred K in time, but for a particular version, you have to find your own version before you can use it.
Compile and install the kernel
1. Download and extract the kernel
Kernel download official website: https://www.kernel.org/
Extract the kernel: tar xf linux-2.6.XX.tar.xz
two。 Custom kernel: make menuconfig
3. Compile kernels and modules: make
Generate kernel modules and vmlinuz,initrd.img,Symtem.map files
4. Install kernels and modules: sudo make modules_install install
Copy module files to / lib/modules directory, copy config,vmlinuz,initrd.img,Symtem.map files to / boot directory, update grub
5. Other commands:
Make mrprobe: the purpose of the command is to execute the "make mrproper" command to clean up the source code tree before configuring and recompiling the kernel, including the kernel configuration file ".config" that has been configured in the past. That is, the old configuration file will be deleted when the new compilation work is carried out, so as not to affect the new kernel compilation.
Make dep: generate dependencies between kernel functions in preparation for kernel compilation.
Introduction of several important linux kernels
Config
Use the kernel configuration file generated by make menuconfig to decide whether to compile each functional system of the kernel into the kernel, as a module or not.
Vmlinuz and vmlinux
Vmlinuz is a bootable, compressed kernel, and "vm" stands for "Virtual Memory". Linux supports virtual memory, unlike old operating systems such as DOS, which has the limitation of 640KB memory, Linux can use hard disk space as virtual memory, hence the name "vm". Vmlinuz is an executable Linux kernel, and there are two ways to establish vmlinuz: one is to create the kernel through "make zImage" when compiling the kernel, and zImage is suitable for the case of small kernel, which exists for backward compatibility; second, when the kernel is compiled, it is created through the command make bzImage, and bzImage is a compressed kernel image, so it should be noted that bzImage is not compressed with bzip2, and the bz in bzImage is easy to be misunderstood. Bz means "big zImage" and b in bzImage means "big". Both zImage (vmlinuz) and bzImage (vmlinuz) are compressed with gzip. They are not only a compressed file, but also have gzip unzipped code embedded in the beginning of both files, so you cannot unpack vmlinuz with gunzip or gzip-dc. The kernel file contains a miniature gzip to extract the kernel and boot it. The difference between the two is that the old zImage decompresses the kernel to low-end memory (the first 640K) and bzImage decompresses the kernel to high-end memory (more than 1m). If the kernel is small, you can use either zImage or bzImage, both of which boot the system at the same runtime. Large kernels use bzImage, not zImage. Vmlinux is the uncompressed kernel and vmlinuz is the compressed file of vmlinux.
Initrd.img
Initrd is an abbreviation for "initial ramdisk". Initrd is generally used to temporarily boot the hardware to a 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 drivers for file systems such as ext3 and scsi devices. If you are using a scsi hard drive and the kernel vmlinuz does not have a driver for this scsi hardware, the kernel cannot load the root file system until the scsi module is mounted, but the scsi module is stored in the root file system under / lib/modules. To solve this problem, you can boot an initrd kernel that can read the actual kernel and fix the scsi boot problem with initrd. Initrd-2.4.7-10.img is a file compressed with gzip. Initrd image files are created using mkinitrd, and the mkinitrd utility can create initrd image files, which are proprietary to RedHat and may be available in other Linux distributions. This is a very convenient utility. For details, please see help: man mkinitrd
System.map
System.map is a kernel-specific kernel symbol table that 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
When programming, symbols such as variable names or function names are named. The Linux kernel is a complex block of code with many global symbols. Instead of using symbolic names, the Linux kernel identifies variable or function names by the address of a variable or function, for example, not using symbols like size_t BytesRead, but referencing the variable like c0343f20. For people who use computers, they prefer names like size_t BytesRead to names like c0343f20. The kernel is mainly written in c, so the compiler / connector allows us to use symbolic names when coding, while the kernel uses addresses at runtime. However, in some cases, we need to know the address of the symbol, or we need to know the symbol corresponding to the address, which is done by the symbol table, which is a list of all symbols together with their addresses.
The Linux symbol table uses two files: / proc/ksyms and System.map. / proc/ksyms is a "proc file" that is created during kernel boot. In fact, it's not really a file, it's just a representation of kernel data, but it gives people the illusion of a disk file, as can be seen from its file size of 0. However, System.map is the actual file that exists on your file system. When you compile a new kernel, the address of each symbol name changes, your old System.map has the wrong symbol information, each time the kernel compiles a new System.map, you should replace the old System.map with the new System.map.
Although the kernel itself does not really use System.map, other programs such as klogd, lsof, and ps require a correct System.map. If you use the wrong output or without System.map,klogd, it will be unreliable, which will make it difficult to troubleshoot the program. Without System.map, you may be faced with some annoying prompts. A few other drivers need System.map to parse symbols, and they won't work without a System.map created for the specific kernel you're running.
Linux's kernel log daemon, klogd, requires System.map for klogd to perform name-to-address resolution. System.map should be placed where the software that uses it can find it. Execution: man klogd knows that if System.map is not given to klogd as a variable, it will look up System.map:/boot/System.map, / System.map, / usr/src/linux/System.map in the following order
System.map also has version information, and klogd can intelligently find the correct image (map) file.
Makefile menuconfig process explanation
What exactly did the system do for us when we executed the make menuconfig command? There are a total of several documents involved here. Let's discuss them one by one.
The scripts folder under the root of the 1.Linux kernel
2.arch/$ARCH/Kconfig file, Kconfig file in each layer directory
The makefile file under the root directory of the 3.Linux kernel and the makefile file under each layer directory
.config files under the root directory of the 4.Linux kernel, files under arch/$ARCH/configs/
Include/generated/autoconf.h files in the root directory of the 5.Linux kernel
1) the scripts folder stores files related to the graphical drawing of the make menuconfig configuration interface. As users, we do not need to care about the contents of this folder.
2) before the above blue configuration interface appeared when we executed the make menuconfig command, the system did the following work for us:
First of all, the system will read the Kconfig file in the arch/$ARCH/ directory to generate the entire configuration interface options (Kconfig is the core of the entire linux configuration mechanism), so what is the value of the ARCH environment variable? It is determined by the makefile file in the root directory of the linux kernel, and this environment variable is defined under makefile:
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? =
Or through the make ARCH=arm menuconfig command to generate the configuration interface, such as the academic Affairs Office for the examination, the number of examination subjects may be foreign language, Chinese, mathematics and other subjects, here we choose arm to take the examination, the system will read the arm/arm/kconfig file generation configuration options (selected arm papers), the system also provides more than 10 subjects, such as x86, milps, etc.
3) suppose the academic Affairs Office is more "benevolent", and for fear that some students will make mistakes in the examination questions, we have also prepared a reference answer (default configuration option), which is stored in the arch/$ARCH/configs/ directory, which is the arch/arm/configs folder for arm:
There are many options in this folder. Which one will the system read? By default, the kernel reads the .config file under the root directory of the linux kernel as the default option of the kernel (the reference answer of the test question). We usually select a series closest to our development board to the root directory of the Linux kernel according to the type of development board (choose the closest reference answer)
4). Config
Assuming that the academic Affairs Office is careful and the reference answer he provides is not exactly correct (the .config file does not exactly match our board), we can choose to modify the .config file directly and execute the make menuconfig command to read the new option. But in general, we do not adopt this solution. We choose to select or unselect some options through spaces, esc, and enter in the configuration interface, and finally save and exit, the Linux kernel will update the new options (the correct reference answer) to .config. At this time, we can rename the .config to another file (the .config file will be deleted when you execute make distclean). In the future, when we configure the kernel, we don't need to go to arch/arm/configs to get the corresponding files, so we don't have to reconfigure. We just copy the saved .config file to .config.
5) after the above two steps, we can correctly read and configure the interface we need, so how do they establish a compilation relationship with the makefile file? When you save make menuconfig options, in addition to automatically updating .config, all options are saved as macros in the include/generated/autoconf.h file under the root directory of the Linux kernel.
The source code in the kernel will contain the above .h file and compile conditionally with the definition of the macro. When we need to choose whether to compile a file as a whole, we also need to modify the corresponding makefile file, for example:
When we choose whether or not to compile the s3c2410_ts.c file, makefile will decide whether to compile the file according to CONFIG_TOUCHSCREEN_S3C2410. This macro is defined in the Kconfig file and will appear in .config and autconf when we are configured. At this point, we have completed the compilation process of the entire linux kernel. Finally, we will find that in the whole process of linux kernel configuration, the interfaces left to users are actually only Kconfig, makefile files and corresponding source files.
For example, if we want to add a function to the kernel and control the claimed process through make menuconfig, the first thing we need to do is: modify the Kconfig file in the corresponding directory and add the corresponding option according to the Kconfig syntax; secondly, execute make menuconfig to choose to compile into the kernel or not into the kernel, or compile to a module, .config file and autoconf.h file will be generated automatically Finally, modify the makefile file in the corresponding directory to complete the addition of compilation options; finally, execute the make command to compile.
Kconfig and Makefile
There are two documents Kconfig and Makefile in each directory of the Linux kernel source tree. The Kconfig distributed to each directory constitutes a distributed kernel configuration database, and each Kconfig describes the kernel configuration menu related to the directory source document. When performing the kernel configuration make menuconfig, the menu is read out from the Kconfig and saved to the .config kernel configuration document after the user selects it. When the kernel is compiled, the main Makefile calls this .config to know the user's choice. This content shows that Kconfig corresponds to the configuration menu at each level of the kernel.
If you want to add a new driver to the kernel source code, modify the Kconfig so that you can select the driver, and if you want the driver to be compiled, modify the Makefile. There are two kinds of documents that need to be modified when adding a new driver (if you only add files, you only need to modify the current layer Kconfig and Makefile files; if you add directories, you need to modify a total pair of Kconfig and Makefile under the current layer and directory) Kconfig and Makefile. If you want to know how to modify these two documents, you need to know the syntax structure of both documents. The syntax of Kconfig can be found in the reference "[linux-2.6.31] kbuild".
The Makefile file consists of five parts:
Makefile top-level Makefile .config kernel configuration file arch/$ (ARCH) / Makefile architecture Makefile scripts/Makefile.* common rules for all kbuild Makefile, etc. Kbuild Makefiles has about 500 such files
The top-level Makefile reads the .config file produced by the kernel configuration operation, and the top-level Makefile builds two main targets: vmlinux (kernel image) and modules (all module files). It builds these targets by recursively accessing subdirectories under the kernel source tree. Which subdirectories to access depends on the kernel configuration. The top-level Makefile contains an architecture Makefile, specified by arch/$ (ARCH) / Makefile. The architecture Makefile file provides architecture-specific information for the top-level Makefile. Each subdirectory has a kbuild file and a Makefile file to execute the 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 used by kbuild to build built-in or module objects. Scripts/Makefile.* contains all the definitions / rules, and so on. This information is used to build the kernel using kbuild and Makefile files.
This is the end of the introduction to "how to compile and install the Linux kernel". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.