[Translated] 20180427 How to Compile a Linux Kernel.md

This commit is contained in:
icecoobe 2018-05-08 13:36:29 +08:00
parent 43cf586b9c
commit 9516bf96cb
2 changed files with 137 additions and 146 deletions

View File

@ -1,146 +0,0 @@
icecoobe translating
How to Compile a Linux Kernel
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/chester-alvarez-644-unsplash.jpg?itok=aFxG9kUZ)
Once upon a time the idea of upgrading the Linux kernel sent fear through the hearts of many a user. Back then, the process of upgrading the kernel involved a lot of steps and even more time. Now, installing a new kernel can be easily handled with package managers like apt. With the addition of certain repositories, you can even easily install experimental or specific kernels (such as real-time kernels for audio production) without breaking a sweat.
Considering how easy it is to upgrade your kernel, why would you bother compiling one yourself? Here are a few possible reasons:
* You simply want to know how its done.
* You need to enable or disable specific options into a kernel that simply arent available via the standard options.
* You want to enable hardware support that might not be found in the standard kernel.
* Youre using a distribution that requires you compile the kernel.
* Youre a student and this is an assignment.
Regardless of why, knowing how to compile a Linux kernel is very useful and can even be seen as a right of passage. When I first compiled a new Linux kernel (a long, long time ago) and managed to boot from said kernel, I felt a certain thrill coursing through my system (which was quickly crushed the next time I attempted and failed).
With that said, lets walk through the process of compiling a Linux kernel. Ill be demonstrating on Ubuntu 16.04 Server. After running through a standard sudo apt upgrade, the installed kernel is 4.4.0-121. I want to upgrade to kernel 4.17. Lets take care of that.
A word of warning: I highly recommend you practice this procedure on a virtual machine. By working with a VM, you can always create a snapshot and back out of any problems with ease. DO NOT upgrade the kernel this way on a production machine… not until you know what youre doing.
### Downloading the kernel
The first thing to do is download the kernel source file. This can be done by finding the URL of the kernel you want to download (from [Kernel.org][1]). Once you have the URL, download the source file with the following command (Ill demonstrate with kernel 4.17 RC2):
```
wget https://git.kernel.org/torvalds/t/linux-4.17-rc2.tar.gz
```
While that file is downloading, there are a few bits to take care of.
### Installing requirements
In order to compile the kernel, well need to first install a few requirements. This can be done with a single command:
```
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
```
Do note: You will need at least 12GB of free space on your local drive to get through the kernel compilation process. So make sure you have enough space.
### Extracting the source
From within the directory housing our newly downloaded kernel, extract the kernel source with the command:
```
tar xvzf linux-4.17-rc2.tar.gz
```
Change into the newly created directory with the command cd linux-4.17-rc2.
### Configuring the kernel
Before we actually compile the kernel, we must first configure which modules to include. There is actually a really easy way to do this. With a single command, you can copy the current kernels config file and then use the tried and true menuconfig command to make any necessary changes. To do this, issue the command:
```
cp /boot/config-$(uname -r) .config
```
Now that you have a configuration file, issue the command make menuconfig. This command will open up a configuration tool (Figure 1) that allows you to go through every module available and enable or disable what you need or dont need.
![menuconfig][3]
Figure 1: The make menuconfig in action.
[Used with permission][4]
It is quite possible you might disable a critical portion of the kernel, so step through menuconfig with care. If youre not sure about an option, leave it alone. Or, better yet, stick with the configuration we just copied from the running kernel (as we know it works). Once youve gone through the entire list (its quite long), youre ready to compile!
### Compiling and installing
Now its time to actually compile the kernel. The first step is to compile using the make command. So issue make and then answer the necessary questions (Figure 2). The questions asked will be determined by what kernel youre upgrading from and what kernel youre upgrading to. Trust me when I say theres a ton of questions to answer, so give yourself plenty of time here.
![make][6]
Figure 2: Answering the questions for the make command.
[Used with permission][4]
After answering the litany of questions, you can then install the modules youve enabled with the command:
```
make modules_install
```
Once again, this command will take some time, so either sit back and watch the output, or go do something else (as it will not require your input). Chances are, youll want to undertake another task (unless you really enjoy watching output fly by in a terminal).
Now we install the kernel with the command:
```
sudo make install
```
Again, another command thats going to take a significant amount of time. In fact, the make install command will take even longer than the make modules_install command. Go have lunch, configure a router, install Linux on a few servers, or take a nap.
### Enable the kernel for boot
Once the make install command completes, its time to enable the kernel for boot. To do this, issue the command:
```
sudo update-initramfs -c -k 4.17-rc2
```
Of course, you would substitute the kernel number above for the kernel youve compiled. When that command completes, update grub with the command:
```
sudo update-grub
```
You should now be able to restart your system and select the newly installed kernel.
### Congratulations!
Youve compiled a Linux kernel! Its a process that may take some time; but, in the end, youll have a custom kernel for your Linux distribution, as well as an important skill that many Linux admins tend to overlook.
Learn more about Linux through the free ["Introduction to Linux" ][7] course from The Linux Foundation and edX.
--------------------------------------------------------------------------------
via: https://www.linux.com/learn/intro-to-linux/2018/4/how-compile-linux-kernel-0
作者:[Jack Wallen][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.linux.com/users/jlwallen
[1]:https://www.kernel.org/
[2]:/files/images/kernelcompile1jpg
[3]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kernel_compile_1.jpg?itok=ZNybYgEt (menuconfig)
[4]:/licenses/category/used-permission
[5]:/files/images/kernelcompile2jpg
[6]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kernel_compile_2.jpg?itok=TYfV02wC (make)
[7]:https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux

View File

@ -0,0 +1,137 @@
如何编译 Linux 内核
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/chester-alvarez-644-unsplash.jpg?itok=aFxG9kUZ)
曾经一段时间内,升级 Linux 内核让很多用户打心里有所畏惧。在那个时候,升级内核包含了很多步骤以及更多的时间。现在,内核安装可以轻易地通过像 `apt` 这样的包管理器来处理。通过添加特定的仓库,你能很轻易地安装实验版本的或者指定版本的内核 (比如针对音频产品的实时内核)。
考虑一下,既然升级内核如此容易,为什么你不愿意自行编译一个呢?这里列举一些可能的原因:
* 你想要简单了解它 (编译内核) 的过程
* 你需要启用或者禁用内核中特定的选项,因为它们没有出现在标准选项里
* 你想要启用标准内核中可能没有添加的硬件支持
* 你使用的发行版需要你编译内核
* 你是一个学生,而编译内核是你的任务
不管出于什么原因,懂得如何编译内核是非常有用的,而且可以被视作一个通行权。当我第一次编译一个新的 Linux 内核(很久以前),然后尝试从它启动,我从中 (系统快速地崩溃,然后不断地尝试和失败) 感受到一种特定的兴奋。
既然这样,让我们来实验一下编译内核的过程。我将使用 `Ubuntu 16.04 Server` 来进行演示。在运行一次常规的 `sudo apt upgrade` 之后,当前安装的内核版本是 `4.4.0-121`。我想要升级内核版本到 `4.17`. 我们小心地开始吧。
有一个警告:强烈建议你在虚拟机里实验本模块。基于虚拟机,你总能创建一个快照,然后轻松地从任何问题中回退出来。不要在产品机器上使用这种方式升级内核,除非你知道你在做什么。
### 下载内核
我们要做的第一件事是下载内核源码。可以找到所需内核 (在 [Kernel.org][1]) 的 URL 来下载。找到 URL 之后,使用如下命令 (我以 `4.17 RC2` 内核为例) 来下载源码文件:
```
wget https://git.kernel.org/torvalds/t/linux-4.17-rc2.tar.gz
```
在下载期间,有一些事需要去考虑。
### 安装需要的环境
为了编译内核,我们首先得安装一些需要的环境。这可以通过一个命令来完成:
```
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
```
务必注意: 你将需要至少 128GB 的本地可用磁盘空间来完成内核的编译过程。因此你必须确保有足够的空间。
### 解压源码
在新下载的内核所在的文件夹下,使用该命令来解压内核:
```
tar xvzf linux-4.17-rc2.tar.gz
```
使用命令 `cd linux-4.17-rc2` 进入新生成的文件夹。
### 配置内核
在正式编译内核之前,我们首先必须配置需要包含哪些模块。实际上,有一些非常简单的方式来配置。使用一个命令,你能拷贝当前内核的配置文件,然后使用可靠的 `menuconfig` 命令来做任何必要的更改。使用如下命令来完成:
```
cp /boot/config-$(uname -r) .config
```
现在你有一个配置文件了,输入命令 `make menuconfig`。该命令将打开一个配置工具 (图 1),它可以让你遍历每个可用模块,然后启用或者禁用你需要或者不需要的模块。
![menuconfig][3]
图 1: 运行中的 `make menuconfig`.
[Used with permission][4]
很有可能你会禁用掉内核中的一个重要部分,所以在 `menuconfig` 期间小心地一步步进行。如果你对某个选项不确定,不要去管它。或者更好的方法是使用我们拷贝的当前运行的内核的配置文件 (因为我们知道它可以工作)。一旦你已经遍历了整个配置列表 (它非常长),你就准备好开始编译了。
### 编译和安装
现在是时候去实际地编译内核了。第一步是使用 `make` 命令去编译。那么调用 `make` 命令然后回答必要的问题 (图 2)。这些问题取决于你将升级的现有内核以及升级后的内核。相信我,将会有非常多的问题要回答,因此你得预留大量的时间。
![make][6]
图 2: 回答 `make` 命令的问题
[Used with permission][4]
回答了长篇累牍的问题之后,你就可以用如下的命令安装那些之前启用的模块:
```
make modules_install
```
又来了,这个命令将耗费一些时间,所以要么坐下来看着编译输出,或者去做些其他事 (因为编译期间不需要你的输入)。可能的情况是,你想要进行别的任务 (除非你真的喜欢看着终端界面上飞舞的输出)。
现在我们使用这个命令来安装内核:
```
sudo make install
```
又一次,另一个将要耗费大量可观时间的命令。事实上,`make install` 命令将比 `make modules_install` 命令花费更多的时间。去享用午餐,配置一个路由器,将 Linux 安装在一些服务器上,或者小睡一会。
### 启用内核作为引导
一旦 `make install` 命令完成了,就是时候将内核启用来作为引导。
使用这个命令来实现:
```
sudo update-initramfs -c -k 4.17-rc2
```
当然,你需要将上述内核版本号替换成你编译完的。当命令执行完毕后,使用如下命令来更新 grub:
```
sudo update-grub
```
现在你可以重启系统并且选择新安装的内核了。
### 恭喜!
你已经编译了一个 Linux 内核!它是一项耗费一些时间的活动;但是,最终你的 Linux 发行版将拥有一个定制的内核,同时你也将拥有一项被许多 Linux 管理员所倾向忽视的重要技能。
从 Linux 基金会和 edX 提供的免费 ["Introduction to Linux" ][7] 课程来学习更多的 Linux 知识。
--------------------------------------------------------------------------------
via: https://www.linux.com/learn/intro-to-linux/2018/4/how-compile-linux-kernel-0
作者:[Jack Wallen][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[icecoobe](https://github.com/icecoobe)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.linux.com/users/jlwallen
[1]:https://www.kernel.org/
[2]:/files/images/kernelcompile1jpg
[3]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kernel_compile_1.jpg?itok=ZNybYgEt (menuconfig)
[4]:/licenses/category/used-permission
[5]:/files/images/kernelcompile2jpg
[6]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kernel_compile_2.jpg?itok=TYfV02wC (make)
[7]:https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux