This article will introduce step by step how to configure and compile a Linux kernel that meets your needs, as well as how to install the compiled kernel and delete unnecessary kernels.
The actual operation of this article is carried out on the Ubuntu operating system. Other Linux distributions can refer to this method to make appropriate adjustments to complete the addition and deletion of the kernel.
Adding kernels is generally not necessary, and certainly not entirely necessary. Installing a new kernel is generally due to the following requirements:
- Try new features
- The original kernel is bloated
- compatible with hardware
- Special needs
The author installs a new kernel just for special needs. The work done by the author is to use the analysis tool that comes with Linux - ftrace. Some specialized tools in this tool (let's call it plug-ins) are not compiled into the kernel in the release version, so I need to recompile the kernel, check these plug-ins, and install the compiled kernel into my own system middle. Although the whole process is only a few simple steps, the author still took a lot of detours. For example, I didn't want to install the new kernel directly on my machine at first. After all, some environments were deployed by the author with great effort, and adding a new kernel to the existing system is also a big girl's marriage. ——The first time, if it goes wrong, it will be scrapped. So I started it on virtualbox, but executing make menuconfig when configuring the kernel always prompts that curses.h cannot be found. After searching on the Internet for a long time, it is not enough to install all libraries related to curses.h. . Later, I had to do it on the physical machine, but there were still some strange problems. For example, the second method of deleting the kernel was the author's painful experience. At that time, the author entered the system after installing the new kernel, and all peripherals were unavailable, so I had to enter the original system to delete the newly installed kernel, and the result was the situation in the second method. Later, in retrospect, it should be that the make modules_install was not executed, resulting in the driver not being installed.
3、Compile the new kernel
The preparation work is of course to download the corresponding version of the kernel, the kernel official website www.kernel.org. Of course, there are various ways to download. Needless to say, you can put the kernel anywhere you want after downloading. But having said that, in fact, it is better to put it under /usr/src, after all, the kernel code is there. Of course, some people put it under /opt/ (you can refer to this article: Linux system replaces the new kernel), which is not bad, just install a large program. After all, when executing # make install, the corresponding files will be copied to the corresponding directory. Where the source code is placed does not affect the installation of the kernel.
The kernel needs to be compiled before installation, which requires cleaning and configuration of the code.
This step is mainly to clean up the .o and .config files in the code generated in the previous compilation process, and these files are different on different machines, so they must be removed. If it is a newly downloaded kernel, this step is not required. In short, this step is not necessary, but there is no harm in doing it after all. The specific commands are as follows.
In this article, the command execution directory is /usr/src/linux-3.19.8/, linux-3.19.8 is the new kernel that the author needs to install, the same below unless otherwise specified.
# make mrproper
Reference article: Detailed explanation of Make Menuconfig.
This step is mainly to configure compilation options, such as which modules need to be compiled into the kernel, which ones are not needed, and which ones are compiled to be dynamically inserted into the kernel when needed. This step will generate a .config file that records the rules we set. Generally speaking, this step can be considered or solved in two cases:
- Copy the existing .config file directly. This file exists in the /boot/ directory. Of course, it is not called .config. For example, the author's system is ***config-3.13.0-32-generic ***, The original version of the kernel in the author's system is 3.13.0, as shown in the figure，Execute the following command to copy:
#cp /boot/config-3.13.0-32-generic ./.config
- Reconfigure according to your own needs. At this time, a .config file will be generated in the current directory. There are many ways to configure it. Only the common methods are given here:
# make menuconfig
After the configuration is complete, the kernel can be compiled. This process is a bit long, you can do something else. The specific commands are as follows:
The #make command is equivalent to executing # make bzImage and # make modules. Since it is a native replacement for the kernel, there is no need to execute them separately. This step takes a long time. In order to speed up the compilation, we can add the "-j" option, followed by the number of jobsnum. It is recommended to set it to the number of CPU cores + 1. The jobsnum value can be set by the command cat /proc/cpuinfo |sed -n '/^processor/p' |wc -l to get.
4、install the kernel
4.1、Install the module
Execute the following command:
# make modules_install
After the compilation is successful, the system will generate a 3.19.8 subdirectory in the /lib/modules directory, which stores all loadable modules of the new kernel (that is, copy the compiled modules to /lib/modules).
Note: The module must be installed before the kernel is installed, otherwise, a fatal error will be prompted when installing the kernel - the corresponding module cannot be found in /lib/modules.
4.2、install the kernel
Execute the following command:
# make install
The function of this command is to transfer .config, vmlinuz, initrd.img, and System.map files to the /boot/ directory and update grub. Starts a new kernel by default.
4.3、remove the kernel
4.3.1、View the kernel used by the current system
# uname -a
This command can give the detailed information of the kernel version currently used by the system, such as: Viewing the current kernel version Figure 3 Viewing the current kernel version
4.3.2、 View the kernels already installed in the system
# dpkg --get-selections | grep linux
This command can view all the kernels that have been installed in the system, such as: Viewing all the kernels that have been installed Figure 4 Viewing all the kernels that have been installed As shown in the figure above, the image part in the list of the figure is the kernel that has been installed , it can be determined from the above two steps that the kernel version in Figure 4 but not in Figure 3 can be deleted.
4.3.3、 Remove the kernel After completing steps 1-2, we can determine the kernel version to be removed.
# apt-get remove linux-image-****-generic
4.3.4 Update grub After the above three steps, the kernel version has been completely deleted. However, at this time, there will be an option to delete the kernel when booting. This is probably unbearable for Virgo comrades. We can fix this by updating the contents of grub.cfg.
After the above four steps, even if the kernel to be deleted is completely deleted.
However, things don't always go so smoothly. For example, the author himself encountered such a strange problem: after installing the kernel, through
# dpkg --get-selections|grep linux
The command cannot find the kernel that was just installed. So the above method seems to be no good, then, only simple and rude methods can be used.
4.3.5 Directly delete the corresponding kernel code to be deleted under /usr/src/. 2. Delete the corresponding kernel-related files under /boot/. For example, if the author wants to delete the kernel version 3.19.8, execute the following command:
# cd /boot/ # rm -rf *3.19.8*
In this way, the corresponding config, System.map, vmlinuz and other files can be deleted.
4.3.6 Update grub. This step is the same as Method 1 and will not be repeated here.
So far, we have completed the configuration, compilation, installation and deletion of the kernel on the Ubuntu operating system. Other Linux distributions can also install and delete the kernel in a similar way, and readers and friends can practice it by themselves.
Original article, please indicate the source for reprinting：http://127.0.0.1:8000/system-administration-linux-tutorial/linux-kernel-addition-and-removal-practices/