Why does linux compile the kernel?

青灯夜游
Release: 2023-03-30 14:03:54
Original
2101 people have browsed it

Reason: The new kernel has revised the bugs of the old kernel and added many new features; if users want to use these new features, or want to customize a more efficient and stable system according to their own system kernel, you need to recompile the Linux kernel. Usually, newer kernels will support more hardware, have better process management capabilities, run faster and more stably, and will generally fix many vulnerabilities found in older versions. Frequently choose to upgrade to newer system kernels. It is a necessary operation content for Linux users.

Why does linux compile the kernel?

The operating environment of this tutorial: Ubuntu 18.04 system, Dell G3 computer.

Why should we compile the Linux kernel?

The new kernel has revised the bugs of the old kernel and added many new features. If users want to use these new features, or want to tailor a more efficient and stable kernel to their own system, they need to recompile the Linux kernel.

Usually, updated kernels will support more hardware, have better process management capabilities, run faster and more stably, and generally fix many vulnerabilities found in older versions, etc., frequently. Choosing to upgrade the updated system kernel is a necessary operation for Linux users.

In order to correctly and reasonably set the kernel compilation configuration options, so as to compile only the code for the functions required by the system, there are generally the following four considerations:

  • (1) Your own custom-compiled kernel runs faster (has less code)

  • (2) The system will have more memory (kernel parts will not be swapped into virtual memory)

  • (3) Compiling unnecessary functions into the kernel may increase the vulnerabilities that can be exploited by system attackers

  • (4) Putting some kind of Compiling functions into modules will be slower than compiling them into the kernel.

The purpose of this type of compilation is mainly to understand the Linux kernel compilation process through compilation and become familiar with the work of the kernel. principles, and you can even try to make some modifications.
Compilation only compiles the source code into a program. It will not replace the current system or affect the operation of the current system.

Compiling the kernel may be due to certain needs, such as kernel size requirements and removing some unused parts of the kernel. This scenario is often embedded systems.
Or if you have modified some part of the kernel code yourself, you need to verify the function after compilation.

When compiling the module, some functional modules are compiled into .ko. You can insmod xxx.ko to use the written code functions in the system without recompiling the kernel.
After compiling the kernel, the current kernel will not be replaced. The compiled new kernel is often in a directory similar to the following, and its name is mostly bzImage

/usr/src/kernels/3.xx.x-.x86_64/arch/x86/boot/
Copy after login

Then you can edit the system's grub list to add the latest kernel to use it.

The new kernel integrates new drivers, such as Intel core display: /lib/modules/`uname -r`/kernel/drivers/gpu/drm/i915/i915.ko

A system Multiple kernels can be installed, such as startup files, and the new kernel will not overwrite the old kernel:

/boot/vmlinuz-VERSION /boot/initrd.img-VERSION
Copy after login

During the installation of a new kernel, some kernel modules need to be recompiled, such as VirtualBox:

/lib/modules/`uname -r`/updates/dkms/vboxdrv.ko
Copy after login

If new The kernel is not running properly. You can select the old kernel to start in the GRUB boot process.

You can also change back to the original kernel like this:

ln -sf /boot/vmlinuz-VERSION /vmlinuz ln -sf /boot/initrd.img-VERSION /initrd.img
Copy after login

where VERSION is the version of the original kernel.

Compile a Linux kernel

The entire kernel compilation process is very simple, but kernel compilation takes a long time. This is mainly because the kernel has a lot of code. Of course, if your computer is powerful, the time will be much shorter. Another thing to note is that it is recommended to compile in a virtual machine environment to avoid errors that may cause system problems. If testing in a virtual machine,It is recommended that the size of the system partition and kernel source code partition be greater than 20GB.

Step 1: Download the source code

1. Go to the official website of the Linux kernel and download the latest version, or other versions of the kernel code. Here is a compressed archive of the source code.

Why does linux compile the kernel?

#2. Assuming that we are now in a Linux operating system, enter the following command on the command line to download the kernel.

wget 链接(由于链接会被认为是广告,本文省略链接,请自行复制)
Copy after login

You can see the download progress during the download process. After the download is completed, the information is roughly as follows.

Why does linux compile the kernel?

Step 2: Unzip the source code

After the compressed package is downloaded, it can be decompressed through the tar command.

tar xvf linux-5.9.6.tar.xz
Copy after login

在解压的时候可以看到文件列表,这个会很多,可能需要等一会儿。

Why does linux compile the kernel?

Step 3: 安装需要的软件包

安装编译工具以及其它一下依赖的软件包,在Ubuntu 18.04环境下执行如下命令。

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
Copy after login

如果一切顺利,大概安装输出信息如下所示。

Why does linux compile the kernel?

Step 4: 配置内核代码

在编译内核之前,我们可以对内核源代码进行配置。配置的目的主要是确定哪些模块会编译到内核当中。

1. 进入源代码目录

cd linux-5.9.6
Copy after login

2. 从当前机器的启动目录拷贝配置信息到源代码目录。这步操作的意思是我们编译内核的配置采用用当前环境一致的配置。

cp -v /boot/config-$(uname -r) .config
Copy after login
Why does linux compile the kernel?

3. 可以通过如下命令启动配置界面

make menuconfig
Copy after login

该命令会运行一些脚本,然后打开一个配置界面

Why does linux compile the kernel?

4. 下面是打开的配置界面。可以看出里面包含所有的内核组件,包括文件系统,网络,IO栈,虚拟化和设备驱动等等。如果你不熟悉,可以不做任何修改。

Why does linux compile the kernel?

Step 5: 编译内核

1. 通过如下命令就可以编译内核了

make -j 10
Copy after login

上面参数是并发数量,通常可以是CPU的2倍。

Why does linux compile the kernel?

2. 安装模块

sudo make modules_install
Copy after login
Why does linux compile the kernel?

3. 安装内核

sudo make install
Copy after login

安装完成后会有如下提示信息。

Why does linux compile the kernel?

Step 6: 重启,验证版本

当上述步骤都没有出错的情况下,我们重启一下计算机,然后运行如下命令。

uname -mrs
Copy after login

此时就可以看到内核版本已经是我们编译的版本了。

1Why does linux compile the kernel?

结论

通过上面几步,我们可以很简单的编译一个内核。如果后面开发内核模块,也是要基于内核代码树的,因此这个是内核开发的基础。

可能遇到的问题

编译内核的时候可能会遇到这个问题:

没有规则可制作目标
debian/certs/debian-uefi-certs.pem,由certs/x509_certificate_list需求停止

在要编译的内核目录下编辑一下配置文件即可。简单的方式是执行如下命令

vim .config
Copy after login

然后找到
CONFIG_SYSTEM_TRUSTED_KEYS,将其设置为空,也就是下面这个样子。

CONFIG_SYSTEM_TRUSTED_KEYS=”
Copy after login

相关推荐:《Linux视频教程

The above is the detailed content of Why does linux compile the kernel?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!