Replacing the new kernel in linux (compiling, installation, replacement, and packaging)

Source: Internet
Author: User
I. Introduction to the kernel is the core of an operating system. It manages system processes, memory, device drivers, files, and network systems, and determines system performance and stability. Linux, as a free software, is constantly updated with the support of many enthusiasts. Introduction to the new kernel

Kernel is the core of an operating system. It manages system processes, memory, device drivers, files, and network systems, and determines system performance and stability. 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.

This article uses kernel 3.12.5 as an experiment and RedHat 6.2 as the operating platform. However, the kernel compilation and installation methods described in this article are suitable for most Linux systems (including RedHat, Ubuntu, Debian, and so on ).

II. download the new kernel source code

Kernel download website: https://www.kernel.org/
Decompress the kernel: # tar xvf linux-3.12.5.tar.xz/opt

Go to the kernel source code directory: # cd/opt/linux-3.12.5

You can decompress the package to any Directory. I can decompress the package to the/opt directory. On the Internet, I can see many friends decompress the package to the/usr/src Directory. In fact, it doesn't matter because the method described in this article is not limited to adding a kernel to the local machine, it is also possible to add a new kernel to other machines, even if it is added to the local kernel, it does not matter, because the corresponding files will be copied to the corresponding directory of the local machine by executing # make install.

Note: all the "3.12.5" related to color labels below are replaced with their actual kernel version.

III. replacing the kernel

This article introduces two methods to replace the kernel: compile and replace the kernel, and package and replace the kernel (. deb installation package ).

(1) compile and replace the kernel

Compiling and replacing the kernel is suitable for all Linux systems and is also a common method for most people.

1. customized kernel

# Make mrproper

The purpose is to execute the "make mrproper" command to clean up the source code tree every time you configure and re-compile the kernel. the kernel configuration file ". config" previously configured will be cleared. That is, the old configuration file will be deleted during the new compilation to avoid affecting the new kernel compilation.

Check whether the. o file and dependency are correct. if you use the downloaded source package for the first compilation, this step can be omitted. If you use these source programs to compile the kernel multiple times, you 'd better run this command first.

# Make menuconfig

Note: 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.

We will not introduce specific kernel configuration operations here, but we recommend that you execute the make menuconfig command even if you do not plan to configure anything, if you do not execute this operation, you will be prompted to answer a lot of questions when making the kernel later.

For kernel configuration, refer:

Make menuconfig configuration explanation: http://blog.csdn.net/xuyuefei1988/article/details/8635539

Http://www.linuxidc.com/Linux/2012-06/63092.htm

There are many other methods to configure the kernel. The main differences are as follows:

# Make menuconfig // graphic tool interface compiled based on the ncurse Library

# Make config // A text-based command line tool, which is not recommended

# Make xconfig // X11-based graphical tool interface

# Make gconfig // gtk +-based graphical tool interface

Select the simple kernel configuration method, that is, make menuconfig. Enter make menuconfig on the terminal. wait a few seconds before the terminal changes to a graphical kernel configuration interface. During configuration, most options use their default values. only a small part needs to be selected based on different needs.

For each configuration option, you have three options, which represent the following meanings:

<*> Or [*] -- compile the function into the kernel

[] -- Do not compile this function into the kernel

[M] -- compile this function into code that can be dynamically inserted into the kernel as needed

2. Compile and install the kernel and modules

(1) copy the. confg file:

Run # cp/boot/config-, press the Tab key, and the system will automatically fill in the qualified file name under the Directory, and then continue to enter. config to use the original configuration file in the boot directory.

If you do not execute this step, the system will prompt that the. confg file cannot be found later in make.

If the kernel is configured in the previous step, it is automatically generated..Config file, but here I only executed the # make menuconfig command and didn't configure the kernel.

(2)Create the dependent files required for compilation:

# Make dep

Create the file dependency based on the options selected in the previous step.

(3) clear the target file compiled by the kernel:

# Make clean

Clear unnecessary files. if you delete some options based on the previous compilation, we recommend that you perform this step. Otherwise, it is unnecessary.

(4) compile and install the kernel and modules:

[Replace the kernel on the local machine]

1) Compile the kernel and module:

# Make-j4

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

Note: # make is equivalent to executing # make bzImage and # make modules separately. because the kernel is replaced by the local machine, you do not need to execute them separately.

This step takes a long time. to speed up compilation, we can add the "-j" option, followed by the number jobsnum. we recommend that you set it to the number of CPU cores + 1, the jobsnum value can be obtained by the command "cat/proc/cpuinfo | sed-n'/^ processor/p' | wc-l.

2) installation module:

# Makemodules_install

After the compilation is successful, the system will generate a 3.12.5 subdirectory under the/lib/modules Directory, it stores all the loaded modules of the new kernel (the compiled modules will be copied to/lib/modules ).

3) install the kernel:

# Makeinstall

Copy the. config, vmlinuz, initrd. img, and System. map files to the/boot directory and update grub. The following three grub files in the RedHat system are automatically updated. the new kernel is started by default.

/Etc/grub. conf

/Boot/grub/menu. lst

/Boot/grub. conf

Note: The first two configuration files are the third soft connection.

Ubutun and Debian systems are the/boot/grub. conf files.

4) restart the system:

# Reboot

[Replacing the kernel on a non-local machine]

You can also execute # make directly like replacing the kernel on the local machine. However, this article separates the compiling kernel and the compilation module for execution, but the principle and result are the same.

1) compile the kernel:

# Make bzImage

After the kernel is compiled, a new kernel image file bzImage will be generated in the source code root directory, namely the linux-3.12.5/arch/x86_64/boot/directory.

Note: for large kernels (such as SCSI support), make bzImage (recommended)

For small kernels, make zImage.

2) compilation module:

# Make modules

Compile and load the module (that is, select M as the kernel option) to use the insmod command for future loading. The compilation time depends on the number of M options.

3) installation module:

# Make modules_install

Copy the compiled modules to/lib/modules.

4) install the kernel:

Because the kernel is not replaced by the local machine, we need to copy the new kernel file to the machine where the kernel is to be replaced. There are four kernel files to be copied:. config, bzImage, System. map and System/lib/modules/linux-3.12.5 directory.

The procedure is as follows:

(1) copy the generated linux-3.12.5/. config to the/boot to replace the kernel system and rename it to the config-3.12.5

(2) copy the generated linux-3.12.5/arch/x86-64/boot/bzImage to/boot to replace the kernel system and rename it to vmlinuz-3.12.5 (note: here you need to pay special attention to the copy of the file name to the vmlinuz-x.x.x ).

(3) copy the generated linux-3.12.5/System. map to/boot to replace the kernel System and rename it System. map-3.12.5

(4) copy the system directory/lib/modules/linux-3.12.5 generated by make modules_install to the/lib/modules to replace the kernel system.

(5) run the following command in the/lib/modules Directory of the system to replace the new kernel:

# Update-initramfs-c-k3.12.5, generate/boot/initrd. img-3.12.5

# Update-grub (or # update-grub2) to update the boot load file.

Note: the RedHat system does not seem to have the above two update commands, so the above kernel update method is suitable for Ubuntu or Debian systems.

Then how does the RedHat system update the kernel?We recommend that you use the previous [replace local kernel] method to directly make install for installation. it is not convenient to use the [replace local kernel] method, but here we will also introduce it, we can manually update the kernel startup file. (You can also manually download and install a grub installation package, and then use update-grub ).

The procedure is as follows (replace step (5) with the following operation ):

The RedHat system has three configuration files:/etc/grub. conf;/boot/grub/menu. lst;/boot/grub. conf

The first two are the third connections, so we only need to modify the/boot/grub. conf startup configuration file.

The content is as follows:

Title name displayed on the startup menu

The root file system mounts partitions according to the system. you can enter partitions based on the old version of the kernel.

Kernel file name compressed by kernel

Initrd root file system file name

For example:

TitleMy new kernel

Root (hd0, 0)

Kernel/boot/vmlinuz-x.y.z

Initrd/boot/initrd-x.y.z.img

I have not tried to directly modify the grub startup file on the red hat system. this part of content is as follows.

The new kernel content in my system/boot/grub. conf is as follows:

Title Red Hat Enterprise LinuxServer (3.12.5)

Root (hd0, 0)

Kernel/vmlinuz-3.12.5 orroot =/dev /......

Initrd/initramfs-3.12.5.img

Then regenerate the ram disk:

If/etc/lilo. conf in your system does not use the ram disk option initrd, skip

If/etc/lilo. conf in your system uses the ram disk option initrd,

Run the mkinitrd initrd-kernel version number command to regenerate the ram disk file, for example, my Redhat 6.2:

Mkinitrd initrd-3.12.5 3.12.5

Then point initrd in/etc/lilo. conf to the new initrd-3.12.5 file:

Initrd =/boot/initrd-3.12.5

The ram disk can optimize the system performance as much as possible. for details, see The/usr/src/linux/Documents/initrd.txt file.

5) restart the system:

# Reboot

3. several important Linux kernel files

(1). config

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.

(2) 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:

First, it is created by "make zImage" when compiling the kernel. zImage is suitable for small kernels and is available for backward compatibility;

The second is to create bzImage by running the make bzImage command during Kernel Compilation. bzImage is a compressed kernel image. Note that bzImage is not compressed with bzip2, and bz in bzImage is easy to cause misunderstanding, 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.

(3) 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

(4)System. mapIs a kernel symbol table of a specific kernel, 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 search for 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.

(2) package and replace the kernel

The make-kpkg command is used to package and replace the new kernel. Therefore, this method is only applicable to system versions that support the make-kpkg command. it is generally applicable to Ubuntu, Debian, and other systems, and is not suitable for RedHat systems.

1. customized kernel

# Make mrprobe: clear the source code tree.

# Make menuconfig: configure the kernel.

2. package the kernel

(1) copy the. confg file:

Run # cp/boot/config-, press the Tab key, and the system will automatically fill in the qualified file name under the Directory, and then continue to enter. config to use the original configuration file in the boot directory.

If you do not execute this step, the system will prompt that the. confg file cannot be found later in make.

If the kernel is configured in the previous step, it is automatically generated..Config file, but here I only executed the # make menuconfig command and didn't configure the kernel.

(2) compile and package the kernel

# Make-kpkg -- initrd--append-to-version =-pyh--revision = zxxiong.0716-j 4kernel_image kernel_headers

After the preceding commands are executed, two. deb packages will be generated in the upper-level Directory:

Header package: linux-headers-3.12.5-pyh_zxxiong.0716_amd64.deb

Kernel package: linux-image-3.12.5-pyh_zxxiong.0716_amd64.deb

The [-- initrd] option will allow make-kpkg to automatically generate initramfs for us;

[-- Revision] adds a version information to the generated deb file. This parameter only affects the file name. If this parameter is not specified, it will be "10.00.Custom" by default ";

[-- Append-to-version] is also a version information. it not only appears in the file name of the deb installation package, but also affects the name of the kernel. for example, in this example, after the kernel is updated, use "uname-r" to view "3.12.5-pyh ";

Kernel_image indicates the installation package for the kernel and default modules.

[Kernel_headers] in this way, make-kpkg will generate an installation package for the kernel header file.

If we use a common user to execute make-kpkg, we need to add fakeroot to run

# Fakeroot make-kpkg -- initrd--append-to-version =-pyh--revision = zxxiong.0716-j 4 kernel_image kernel_headers

(Note: You need to set the platform, for example,-amd64. the configuration file is already configured by default here, so you do not need to add it here. the kernel_image option is used to generate the kernel. deb package, the option kernel_headers is to generate the header file. deb package. you can use only one of these two options)

(3) install the kernel

Copy the. deb package generated above to the machine that needs to replace the kernel and execute the following command:

# Dpkg-I linux-headers-3.12.5-pyh_zxxiong.0716_amd64.deblinux-image-3.12.5-pyh_zxxiong.0716_amd64.deb (next line)

Or run the following command in the directory where the deb package is located: # dpkg-I./*. deb

After the command is executed, the files under the/boot directory will be automatically updated (that is, the automatic command: # update-initramfs-c-k3.12.5 and # update-grub2 ), restart the system and select the loaded kernel. you can also modify/boot/grub. set default = "0" in the cfg configuration file. the default value is 0. after installation, check that the number of newly loaded kernels in the file is set to a number (starting from 0 ), in this way, you do not need to select the number of items to start when starting the instance.

(5) restart the system

# Reboot

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.