欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux 内核源码 编译_如何在现有Linux操作系统中从源代码编译和安装Linux内核

程序员文章站 2024-03-14 20:28:46
...

linux 内核源码 编译

Building Linux kernel may sound a complex and geek-only thing. However, as Linux kernel itself has much less depended tools/packages compared to other software packages, it is quite easy to compile, build and install a Linux kernel from the source code in an existing Linux OS. Building Linux kernel is needed if you need to build a specific Linux kernel or update your drivers for a kernel by yourself. This post will go through the steps for compiling and installing Linux kernel. In this post, we will use the example of building kernel 4.10.1 on CentOS 7.

构建Linux内核听起来可能是一件复杂且极客的事情。 然而,随着Linux内核本身更依赖工具/包相比其他软件的软件包,它是很容易编写,构建和在现有的Linux操作系统上安装从源代码Linux内核。 如果需要构建特定的Linux内核或自己更新内核驱动程序,则需要构建Linux内核。 这篇文章将介绍编译和安装Linux内核的步骤。 在本文中,我们将使用在CentOS 7上构建内核4.10.1的示例。

linux 内核源码 编译_如何在现有Linux操作系统中从源代码编译和安装Linux内核

下载Linux内核的源代码 (Download the source code of Linux kernel)

Download a release from [[https://www.kernel.org/|kernel.org]].

从[[ https://www.kernel.org/|kernel.org ]]下载发行版。

For the example, we download the 4.10.1 kernel:

对于此示例,我们下载了4.10.1内核:

# wgethttps://www.kernel.org/pub/linux/kernel/v4.x/linux-4.10.1.tar.gz

After downloading, unpack the source code and change the working directory to the unpacked directory:

下载后,解压缩源代码并将工作目录更改为解压缩目录:

# tar xf linux-4.10.1.tar.gz
# cd linux-4.10.1/

安装所需的工具 (Install needed tools)

You need to install the tools needed depending on the default packages installed on your system.

您需要根据系统上安装的默认软件包来安装所需的工具。

Here are some common ones as follows. If it is reported more are needed/missing, you may install them accordingly.

以下是一些常见的问题。 如果报告需要/缺少更多信息,则可以相应地安装它们。

# yum install gcc make ncurses-devel openssl-devel

准备配置文件 (Prepare a config file)

To compile Linux kernel, a .config file under the top directory is needed. Configuring the Linux kernel from scratch will be quite time consuming and require quite good understanding of the kernel componenets. However, things are not that complex as we can re-use existing configuration files that are usually installed together with existing kernel.

要编译Linux内核,需要顶层目录下的.config文件。 从头开始配置Linux内核将非常耗时,并且需要对内核组件有很好的了解。 但是,事情并没有那么复杂,因为我们可以重用通常与现有内核一起安装的现有配置文件。

You can find some config files for kernels already installed. On Fedora/RHEL/CentOS, you can find some in /boot/

您可以找到一些已安装内核的配置文件。 在Fedora / RHEL / CentOS上,您可以在/ boot /中找到一些

# ls /boot/config-*

Such as

# ls /boot/config-4.*
/boot/config-3.10.0-514.6.1.el7.x86_64  /boot/config-3.18.41-20.el7.x86_64

Pickup one that is closing to the kernel version you are compiling and copy the config to .config:

拾取一个接近您正在编译的内核版本的文件,并将配置复制到.config:

# cp /boot/config-3.18.41-20.el7.x86_64 .config

Then run the config tool to make it a good one for the version of Linux kernel you are building:

然后运行配置工具,使其成为要构建的Linux内核版本的好工具:

# make menuconfig

It will several tools:

它将使用几种工具:

HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/mconf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  SHIPPED scripts/kconfig/zconf.hash.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTCC  scripts/kconfig/lxdialog/checklist.o
  HOSTCC  scripts/kconfig/lxdialog/util.o
  HOSTCC  scripts/kconfig/lxdialog/inputbox.o
  HOSTCC  scripts/kconfig/lxdialog/textbox.o
  HOSTCC  scripts/kconfig/lxdialog/yesno.o
  HOSTCC  scripts/kconfig/lxdialog/menubox.o
  HOSTLD  scripts/kconfig/mconf
scripts/kconfig/mconf  Kconfig

And a tool like the following one will appear:

然后将出现类似以下工具的工具:

linux 内核源码 编译_如何在现有Linux操作系统中从源代码编译和安装Linux内核

You do not need to configure anything by default unless you need to configure the kernel specifically like adding/removing drivers/features.

默认情况下,您不需要配置任何东西,除非您需要专门配置内核,例如添加/删除驱动程序/功能。

Select "< Exit >" and hit entry, it will ask whether you would like to save your new configuration for the kernel like:

选择“ <Exit>”,然后单击entry,它将询问您是否要为内核保存新的配置,例如:

┌──────────────────────────────────────────────────────────┐
│  Do you wish to save your new configuration?             │
│  (Press <ESC><ESC> to continue kernel configuration.)    │
├──────────────────────────────────────────────────────────┤
│                   < Yes >      <  No  >                  │

Choose "< Yes >" and it will save a new configuration file in .config:

选择“ <是>”,它将在.config中保存一个新的配置文件:

.config:444:warning: symbol value 'm' invalid for MICROCODE
.config:618:warning: symbol value 'm' invalid for CPU_FREQ_STAT
.config:866:warning: symbol value 'm' invalid for NF_CT_PROTO_DCCP
.config:868:warning: symbol value 'm' invalid for NF_CT_PROTO_SCTP
.config:869:warning: symbol value 'm' invalid for NF_CT_PROTO_UDPLITE
.config:887:warning: symbol value 'm' invalid for NF_NAT_PROTO_DCCP
.config:888:warning: symbol value 'm' invalid for NF_NAT_PROTO_UDPLITE
.config:889:warning: symbol value 'm' invalid for NF_NAT_PROTO_SCTP
.config:3258:warning: symbol value 'm' invalid for SERIAL_8250_FINTEK
.config:5262:warning: symbol value 'm' invalid for USB_ISP1760_HCD
configuration written to .config

*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

构建内核并安装 (Build the kernel and install it)

Now the kernel is configured and you can build and install it by command:

现在已经配置了内核,您可以通过命令构建和安装它:

# make -j `nproc` && make modules_install && make install

The first make command compile the source code files. The -j option make make compile in parallel using concurrent tasks of at most number of the processors in the system so that the compilation can be much faster. The make moduels_install one installs the modules and make install install the kernel.

第一个make command编译源代码文件。 -j选项使make make使用系统中最多数量的处理器的并发任务并行编译,因此编译可以快得多。 make moduels_install一个将安装模块,而make install将安装内核。

If everything works well, you will have the new kernel installed in your system after you seeing message like

如果一切正常,您将在看到类似以下消息后在系统中安装新内核

...
  INSTALL /lib/firmware/mts_cdma.fw
  INSTALL /lib/firmware/mts_gsm.fw
  INSTALL /lib/firmware/mts_edge.fw
  INSTALL /lib/firmware/edgeport/boot.fw
  INSTALL /lib/firmware/edgeport/boot2.fw
  INSTALL /lib/firmware/edgeport/down.fw
  INSTALL /lib/firmware/edgeport/down2.fw
  INSTALL /lib/firmware/edgeport/down3.bin
  INSTALL /lib/firmware/whiteheat_loader.fw
  INSTALL /lib/firmware/whiteheat.fw
  INSTALL /lib/firmware/keyspan_pda/keyspan_pda.fw
  INSTALL /lib/firmware/keyspan_pda/xircom_pgs.fw
  INSTALL /lib/firmware/cpia2/stv0672_vp4.bin
  INSTALL /lib/firmware/yam/1200.bin
  INSTALL /lib/firmware/yam/9600.bin
  DEPMOD  4.10.1
sh ./arch/x86/boot/install.sh 4.10.1 arch/x86/boot/bzImage \
        System.map "/boot"

Then you can reboot you Linux OS and choose the new kernel in grub/grub2 entries to use the new Linux kernel. If it is not the default entry, you may use the grub2-select tool to change the default grub2 boot entry. Enjoy!

然后,您可以重新启动Linux操作系统,并在grub / grub2条目中选择新内核以使用新Linux内核。 如果不是默认条目,则可以使用grub2-select工具更改默认grub2引导条目。 请享用!

翻译自: https://www.systutorials.com/compile-install-linux-kernel-source-code-existing-linux-os/

linux 内核源码 编译