linux内核源代码_如何从源代码构建和安装最新的Linux内核

linux内核源代码

by Sreehari

通过Sreehari

如何从源代码构建和安装最新的Linux内核 (How to build and install the latest Linux kernel from source)

I just finished my first assignment for a course on Advanced Operating Systems. And I decided to document my approach for building the Linux kernel from source and implementing my own system call.

我刚刚完成了有关高级操作系统的课程的第一次任务。 然后,我决定记录一下从源代码构建Linux内核并实现自己的系统调用的方法。

There are a number of blogs that already tell you how to go about doing this, but some of them are outdated, and some seem unnecessarily complicated. My goal is to present a straightforward approach for doing this, which should hopefully help you save a lot of time.

有许多博客已经告诉您如何进行此操作,但是其中一些已经过时,并且有些看起来不必要地复杂。 我的目标是提出一种简单的方法,希望可以帮助您节省很多时间。

Compiling the Linux Kernel from source can seem like a daunting task, even to someone who’s pretty comfortable with computers in general. It can also get really irritating if you aren’t following the right instructions.

从源代码编译Linux内核似乎是一项艰巨的任务,即使对于一般熟悉计算机的人而言。 如果您没有遵循正确的说明,它也可能会令人非常恼火。

So, here’s a guide to help you through the process of building the kernel from source, and it’s a guide that works! You will not have to worry about messing up your system or wasting your time.

因此,这是一个指南,可帮助您完成从源代码构建内核的过程,并且该指南行之有效! 您不必担心会弄乱系统或浪费时间。

为什么要从源代码构建内核? (Why build the kernel from source?)

If you plan to work on the internals of the Linux kernel or change its behavior, you’ll need to recompile the kernel on your system.

如果您打算使用Linux内核的内部结构或更改其行为,则需要在系统上重新编译内核。

Here are a few specific cases where you’ll need to know how to work with the kernel’s source code:

在一些特定的情况下,您需要了解如何使用内核的源代码:

  1. You want to write a really cool ‘Hello world’ program. (Each time you implement your own system call or modify kernel source code, you will need to recompile the kernel to implement the changes)

    您想编写一个非常酷的“ Hello world”程序。 (每次实现自己的系统调用或修改内核源代码时,都需要重新编译内核以实现更改)

  2. You want to enable experimental features on your kernel that are not enabled by default (or, disable default features that you don’t want)

    您想启用默认情况下未启用的内核实验功能(或禁用不需要的默认功能)
  3. You want to debug kernel source code, enable support for a new piece of hardware, or make modifications to its existing configurations

    您要调试内核源代码,启用对新硬件的支持或对其现有配置进行修改
  4. You’re doing a course on Advanced Operating Systems and have no choice but to do this!

    您正在学习高级操作系统课程,别无选择,只能这样做!

In each of the above situations, learning how to build the kernel from source will come in handy.

在上述每种情况下,学习如何从源代码构建内核将非常有用。

你需要什么 (What you’ll need)

A Linux based Operating System (I tried this on Ubuntu 14.04 LTS and the instructions written here are for the same).

一个基于Linux的操作系统(我在Ubuntu 14.04 LTS上尝试过此操作,此处编写的说明相同)。

You will need to install a few packages before you can get started. Use the following commands for the same.

在开始之前,您将需要安装一些软件包。 相同地使用以下命令。

sudo apt-get update
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc

You will also need up to at least 12 GB of free space on disk, an internet connection to download the source code, and a lot of time (about 45 to 90 minutes).

您还将需要至少12 GB的磁盘可用空间,用于下载源代码的Internet连接以及大量的时间(大约45至90分钟)。

下载并解压缩最新的内核源代码 (Downloading and extracting the latest kernel source)

To check your current kernel version, open the terminal and type:

要检查您当前的内核版本,请打开终端并输入:

uname -r

Go to kernel.org and download the latest stable version. At the time of writing this, the latest stable kernel version was 4.7.1, and I will refer to the same in this article. (Note: Try to avoid downloading source from other websites)

转到kernel.org并下载最新的稳定版本。 在撰写本文时,最新的稳定内核版本为4.7.1,在本文中我将引用相同的版本。 (注意:请尽量避免从其他网站下载源代码)

Change to the directory where the file was downloaded and extract using:

转到使用以下命令下载文件并解压缩的目录:

tar xf linux-4.7.1.tar.xz

Change to the extracted linux-4.7.1 directory.

转到解压缩的linux-4.7.1目录。

cd linux-4.7.1

It should contain folders called arch, fs, crypto, etc.

它应该包含称为archfscrypto等的文件夹。

配置和编译: (Configuring and Compiling:)

Before compiling the kernel, we need to configure which modules are to be included and which ones are to be left out.

在编译内核之前,我们需要配置要包含的模块和要保留的模块。

There are many ways to go about doing this.

有很多方法可以做到这一点。

An easy and straightforward way to do this is to first copy your existing kernel config file and then use ‘menuconfig’ to make changes (if necessary). This is the fastest way to do it and probably, the safest.

一种简单而直接的方法是首先复制现有的内核配置文件,然后使用“ menuconfig”进行更改(如有必要)。 这是最快的方法,而且可能是最安全的方法。

cp /boot/config-$(uname -r) .config
make menuconfig

This is the part where you could end up removing support for a device driver or do something of the sort which will eventually result in a broken kernel. If you are unsure about making changes, just save and exit.

在这部分中,您最终可能会删除对设备驱动程序的支持,或者执行某些最终会导致内核损坏的操作。 如果不确定要进行更改,请保存并退出。

Note: One of the alternatives to menuconfig is an interactive command line interface accessible using ‘make config’. This helps you configure everything from scratch. Do not use this. You will be asked over a thousand yes/no questions about enabling or disabling modules, which I promise is no fun whatsoever. I did try this out once and somehow managed to mess up the display driver configurations.

注意:menuconfig的替代方法之一是使用'make config'进行访问的交互式命令行界面。 这可以帮助您从头开始配置所有内容。 不要使用这个。 您将被询问超过一千个关于启用或禁用模块的是/否问题,我保证这绝不会很有趣。 我确实尝试过一次,但设法以某种方式弄乱了显示驱动程序配置。

gconfig and xconfig are alternate GUI based configuration tools that you could use. I haven’t tried these myself. For this, you’ll need to use make gconfig (or make xconfig) instead of make menuconfig.

gconfigxconfig是可以使用的基于GUI的备用配置工具。 我自己还没有尝试过。 为此,您需要使用make gconfig (或make xconfig )而不是make menuconfig

Now, we’re all set!

现在,我们都准备好了!

To compile the kernel and its modules, we use the make command.

要编译内核及其模块,我们使用make命令。

This is followed by using make modules_install to install the kernel modules.

接下来是使用make modules_install安装内核模块。

Finally, we use make install to copy the kernel and the .config file to the /boot folder and to generate the system.map file (which is a symbol table used by the kernel).

最后,我们使用make install将内核和.config文件复制到/ boot文件夹,并生成system.map文件(这是内核使用的符号表)。

These three steps put together usually take up a lot of time. Use the following command to perform the above tasks:

这三个步骤通常会占用大量时间。 使用以下命令执行上述任务:

sudo make -j 4 && sudo make modules_install -j 4 && sudo make install -j 4

Note: I have used the -j option to specify the number of cores to be used. This tends to speed up the process considerably. You can use nproc to check the number of processing units available. In my case, it was 4 cores.

注意:我已经使用-j选项来指定要使用的内核数。 这往往会大大加快该过程。 您可以使用nproc检查可用的处理单元数。 就我而言,它是4个核心。

Ideally, you shouldn’t need sudo privileges, but, I was running into problems when I didn’t run it with sudo privileges.

理想情况下,您不需要sudo特权,但是当我没有使用sudo特权运行它时,我遇到了问题。

最后步骤 (Final steps)

Once the kernel and its modules are compiled and installed, we want to be using the new kernel the next time we boot up.

编译并安装了内核及其模块之后,我们希望下次启动时使用新内核。

For this to happen, we need to use the following command:

为此,我们需要使用以下命令:

update-initramfs -c -k 4.7.1

Then, use the following command, which automatically looks for the kernels present in the /boot folder and adds them to the grub’s config file.

然后,使用以下命令,该命令自动查找/ boot文件夹中存在的内核,并将其添加到grub的配置文件中。

update-grub

Now, restart the system and you should see that the new kernel is added to the boot loader entries.

现在,重新启动系统,您应该看到新内核已添加到引导加载程序条目中。

On following the instructions, assuming there’s enough space available on disk and the current kernel configuration works fine, you shouldn’t encounter any problems. Note that you could always use the old kernel version in case of any problem and try the whole thing again!

按照说明进行操作,假设磁盘上有足够的可用空间并且当前的内核配置可以正常工作,那么您应该不会遇到任何问题。 请注意,如果出现任何问题,您始终可以使用旧的内核版本,然后再次尝试整个操作!

The command uname -r should now show you the current kernel version being used.

命令uname -r现在应该 显示当前使用的内核版本。

重要说明 (An important note)

The above steps are needed to build the kernel from source, for the first time. Once, this is done at least once and a new kernel image is ready, making changes and writing our own modules is simple. You will only be using the steps listed under Configuring and Compiling each time something new is to be implemented or configured differently.

第一次需要从源代码构建内核的上述步骤。 一旦完成,至少完成一次,并且准备好新的内核映像,进行更改和编写我们自己的模块非常简单。 每当要实施或以不同方式配置新功能时,您将仅使用“ 配置和编译”下列出的步骤。

Meaning, just remember the following:

含义,请记住以下几点:

cp /boot/config-$(uname -r) .config
make menuconfig
sudo make -j 4 && sudo make modules_install -j 4 && sudo make install -j 4

I must give credit to the following worthwhile resources — they were hugely helpful with this task: Ramkitech.com, askubuntu.com, kernel.org and cyberciti.biz

我必须赞扬以下有价值的资源-它们在完成此任务方面大有帮助: Ramkitech.comaskubuntu.comkernel.orgcyberciti.biz

翻译自: https://www.freecodecamp.org/news/building-and-installing-the-latest-linux-kernel-from-source-6d8df5345980/

linux内核源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值