Gentoo Linux 手册:安装Gentoo

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Handbook:AMD64/Full/Installation and the translation is 100% complete.



介绍

欢迎

欢迎使用 Gentoo!Gentoo 是一个基于 Linux 的自由操作系统,他可以针对任何应用或需求进行自动优化和定制。 它基于自由软件生态系统,不会向用户隐瞒背后运行的内容。

开放

Gentoo 的主要工具是用简单的编程语言构建的。Portage/zh-cn 是 Gentoo 的软件包维护系统,他是 由 Python 编写的。Ebuilds 为 Portage 定义软件包,他是由 bash 编写的。我们鼓励用户审查、修改和完善 Gentoo 所有源代码。

默认情况下,软件包只在必要时才会打补丁,比如修复漏洞或者提供与 Gentoo 互操作。软件包通过上游项目提供的源代码编译成二进制格式,然后安装到系统中(尽管也支持预编译二进制包)。也可以通过文本文件配置 Gentoo。

出于上述和其他原因:开放是 Gentoo 设计原则之一。

选择

Gentoo 的另一个设计原则选择

安装 Gentoo 时,选择贯穿于整个手册中。系统管理员可以选择两种完全支持的 init 系统(Gentoo 自己的 OpenRC 和 Freedesktop.org 的 systemd),存储磁盘的分区结构,磁盘使用什么文件系统,目标 系统配置文件,通过 USE 标记对全局(系统级)或特定软件包移除、添加特性,启动引导,网络管理工具,还有很多很多。

作为一种开发哲学, Gentoo 的作者尽量避免强迫用户使用特定的系统配置文件或桌面环境。如果一些东西在 GNU/Linux 生态系统中有,那很有可能在 Gentoo 中也能使用。即使 GNU/Linux 没有,我们也很乐意看到他。如果要添加新的软件包,请提交 bug 报告或者创建你自己的 ebuild 仓库。

强力

作为一个基于源代码的操作系统,Gentoo 可以移植到新计算机指令集架构上,还可以对所有安装的软件包进行调整。这一优势体现了 Gentoo 的另一个设计原则强力

A system administrator who has successfully installed and customized Gentoo has compiled a tailored operating system from source code. The entire operating system can be tuned at a binary level via the mechanisms included in Portage's make.conf file. If so desired, adjustments can be made on a per-package basis, or a package group basis. In fact, entire sets of functionality can be added or removed using USE flags.

手册的读者必须明白,这些设计原则正是 Gentoo 的独特之处。由于 Gentoo 着重强调了强大的功能、多种选择和极度开放的原则,因此在使用 Gentoo 时应多努力、多思考、多用心。

安装步骤

Gentoo的安装可以被分成10个步骤,分别对应后续的章节。执行完每个步骤,都会让系统进入某种确定的状态:

步骤 结果
1 用户处于一个准备好安装 Gentoo 的工作环境中。
2 用于安装 Gentoo 的互联网连接已经准备完毕。
3 硬盘已经为 Gentoo 的安装初始化完毕。
4 安装环境已经准备好,用户准备 chroot 到新环境中去。
5 那些在所有Gentoo安装中都相同的核心软件包已经安装完毕。
6 Linux内核已经安装完毕。
7 用户已经创建好大部分的 Gentoo 系统配置文件。
8 必要的系统工具已经安装完毕。
9 合适的启动引导程序 (Bootloader) 已经安装配置完毕。
10 登录系统,你就可以在已经全新安装完毕的 Gentoo Linux 系统中尽情探索了!

当面临选择的时候,手册上会尽量说明每个选择的利弊。虽然手册会以默认选择(标题中有 "默认Default:" 标记)继续描述下去,但其他可能的选择同样也会在文档中说明(标题中有 "可选Alternative:" 标记)。请不要认为默认选项是 Gentoo的推荐选项。这只是 Gentoo 认为大多数用户会使用的选项。

有时候你也可以进行一些可选的步骤。这样的步骤会被标注为"可选Optional:",当然这些步骤对于安装Gentoo来说就不是必须的。然而,有些可选的步骤依赖于你先前所做出的决定。我们将会在你需要做这样决定以及可选步骤出现的时候提醒你。

选择合适的方法安装Gentoo

Gentoo可以以许多不同的方式安装。 它可以从官方Gentoo安装介质(如我们的可引导ISO文件)下载和安装。 安装介质可以安装在USB盘上或通过网络引导环境访问。 或者,Gentoo可以从非官方介质安装,如已安装的发行版或非Gentoo可启动磁盘(如Knoppix)。

本文讲解了如何使用Gentoo官方安装光盘或者在某些情况下使用网络启动来安装Gentoo。

附注
需要其他安装方式的帮助(包括使用非Gentoo的Linux引导介质)请阅读我们的其他安装方式

我们也提供一篇 Gentoo 安装提示和技巧文档,它可能会有用。

遇到麻烦

如果在安装(或安装文档中)发现问题,请访问我们的 bug 跟踪系统,并确认这个 Bug 是否已经被提交。如果没有提交,请为它创建一个错误报告以方便我们的后续处理。不要害怕和被分配解决这个问题的开发人员交流——他们(通常)不吃人。

虽然你当前阅读的文档是针对某一特定的硬件架构,但是它可能还是会包含对其他平台架构的引用,因为Gentoo手册中的绝大部分在所有硬件架构都相同(避免重复劳动)。为了避免混淆,我们把这样的引用保持在最低限度。

如果你不能确定你的问题是使用者的操作问题(尽管你已经仔细地阅读了文档但仍然出了错)还是一个软件问题(尽管我们已经仔细地测试了安装步骤/文档但仍然出了错)。请联系我们: irc.libera.chat 上的 #gentoo (webchat)。当然,如果你有其他方面的关于Gentoo的问题,我们也欢迎你来这里讨论。

说到这里,如果还有任何额外的Gentoo问题,看看我们常见问题 ,或者Gentoo论坛 上的 FAQs





硬件需求

安装之前,为了使 amd64 系统架构能成功安装 Gentoo,我们应该满足最低硬件要求。


AMD64 安装镜像硬件要求
最小化CD LiveDVD
CPU 任何AMD64 CPU 或者 EM64T CPU (Core i3, i5, and i7 都是 EM64T)
内存 256 MB 512 MB
磁盘空间 8 GB(不包括swap空间)
Swap空间 至少 2 GB

AMD64 项目 是一个获取关于Gentoo的 amd64 支持的更多信息的好地方。


使用Gentoo Linux安装光盘

提示
虽然推荐使用官方安装媒介,但还是可以选择其他非 Gentoo 安装媒介。Gentoo 安装媒介可以确保 live 操作系统环境中有必要的工具。当使用非 Gentoo 安装媒介时,可以直接跳到准备磁盘

最小化安装CD

Gentoo最小化安装CD是一张可引导镜像:包含有完整Gentoo环境的。它允许用户从CD或其它安装媒介引导进入Linux。在引导过程中将检测硬件并加载适当的驱动。这个镜像由Gentoo开发人员维护,能让任何有Internet连接的人来安装Gentoo。

最小化安装 CD 命名的格式为: install-<arch>-minimal-<release timestamp>.iso

偶尔用到的Gentoo LiveDVD

有时,需要用一张特制的DVD安装Gentoo。本章的介绍是针对最小化安装CD的,因此从LiveDVD引导可能会有一点不同。不管怎么样,LiveDVD(或任何其他官方 Gentoo Linux 环境)支持通过在终端输入 sudo su - 或者 sudo -i 来获取root权限。

stage 文件是什么?

stage3压缩包是一个包含有最小化的特定profile的Gentoo环境的压缩包,Stage3可用来按照本手册介绍继续安装Gentoo。以前的Gentoo手册描述了使用三个 stage tarballs 的其中一个来进行安装。Gentoo不再提供stage1和stage2压缩包供下载,因为它们主要用于内部使用和在新架构上对Gentoo进行bootstrap。

stage 3 文件可以在任意一个Gentoo官方镜像站releases/amd64/autobuilds/ 内下载。Stage 文件更新比较频繁因此不不包含在官方 live 镜像中。

提示
For now, stage files can be ignored. They will be described in greater detail later when they are needed
附注
Historically, the handbook described installation steps for stage files with versions lower than 3. These stages contained environments unsuitable for typical installations, and are no longer covered in the handbook.

下载

获得安装媒介

Gentoo Linux使用最小化安装CD做为默认安装媒介,它带有一个非常小的可引导的Gentoo Linux环境。此环境包含所有正确的安装工具. CD镜像本身可以从官方下载页(推荐)或任意一个镜像站下载。

Navigating Gentoo mirrors

在这些镜像站上,最小化安装CD可以通过以下方式找到:

  1. 连接到镜像,通常在 Gentoo source mirrors 找到一个本地镜像使用。
  2. 导航到 releases/ 目录
  3. 选择相应的架构, 如 amd64/
  4. 选择 autobuilds/ 目录
  5. 对于 amd64x86 平台的用户,请选择 current-install-amd64-minimal/current-install-x86-minimal/ 目录。如果需要所有其它平台的,请进入 current-iso/ 目录。
附注
一些架构例如armmips,和s390 是没有最小化安装 CD 的。 这是因为 Gentoo 发行工程项目 ( Gentoo Release Engineering project ) 不支持构建这些平台的.iso文件。

在这个位置,安装媒体文件是那些带有.iso扩展名的文件。比如下面的清单:

代码 releases/amd64/autobuilds/current-install-amd64-minimal/ 内可下载的文件列表示例
[TXT]	install-amd64-minimal-20231112T170154Z.iso.asc	        2023-11-12 20:41        488
[TXT]	install-amd64-minimal-20231119T164701Z.iso.asc	        2023-11-19 18:41        488
[TXT]	install-amd64-minimal-20231126T163200Z.iso.asc	        2023-11-26 18:41        488
[TXT]	install-amd64-minimal-20231203T170204Z.iso.asc	        2023-12-03 18:41        488
[TXT]	install-amd64-minimal-20231210T170356Z.iso.asc	        2023-12-10 19:01        488
[TXT]	install-amd64-minimal-20231217T170203Z.iso.asc	        2023-12-17 20:01        488
[TXT]	install-amd64-minimal-20231224T164659Z.iso.asc	        2023-12-24 20:41        488
[TXT]	install-amd64-minimal-20231231T163203Z.iso.asc	        2023-12-31 19:01        488
[ ]     install-amd64-minimal-20240107T170309Z.iso              2024-01-07 20:42        466M
[ ]     install-amd64-minimal-20240107T170309Z.iso.CONTENTS.gz	2024-01-07 20:42        9.8K
[ ]     install-amd64-minimal-20240107T170309Z.iso.DIGESTS      2024-01-07 21:01        1.3K
[TXT]   install-amd64-minimal-20240107T170309Z.iso.asc	        2024-01-07 21:01        488
[ ]     install-amd64-minimal-20240107T170309Z.iso.sha256       2024-01-07 21:01        660
[TXT]	latest-install-amd64-minimal.txt                        2024-01-08 02:01        653

在上面的例子中,install-amd64-minimal-20240107T170309Z.iso 文件是最小化安装CD。但可以看到,还有其他相关文件存在:

  • .CONTENTS.gz 文件是使用 gz 压缩的文本文件,它列出了安装媒介中的所有文件。这个文件可用于在下载前确认安装媒介是否包含特定的固件和驱动程序。
  • .DIGESTS 文件包含了ISO文件的 Hash 值,有不同的 Hash 格式/算法。这个文件可以用来验证已 ISO 文件完整性。
  • .asc 文件是 ISO 文件的加密签名。这个文件即可用于验证镜像完整性和准确性 —— 下载的文件确实是由 Gentoo 发行工程团队 提供的,以免文件被篡改。

现在可以先忽略当前位置的其他文件——它们在安装的后续步骤中会被提到。下载 .ISO,另外如果想要验证下载的文件,同时下载ISO文件对应的 .iso.asc

提示
.DIGESTS 文件只在 .iso.asc 文件签名未验证时需要。

校验下载的文件

附注
这是一个可选步骤,并不是安装 Gentoo Linux 所必须的。但是,我们仍然推荐这么做,以此来确保下载的文件没有损坏,以及确保下载文件确实由 Gentoo基础设施团队提供。

.asc 文件提供了 ISO 的加密签名。验证了加密签名,可以确保安装文件是由Gentoo发行工程组( Gentoo Release Engineering team )提供,并且没有损坏和修改。

在微软 Windows 操作系统上校验

首先验证文件加密签名,可以使用类似 GPG4Win 这样的工具。安装完工具后,需要导入 Gentoo 发行工程组(Gentoo Release Engineering Team)的公钥。 公钥列表在: 数字签名页。导入完成后,用户就可以验证 .DIGESTS.asc 文件的签名。

在已有的 Linux 系统上校验

在 Linux 系统上,最常用的验证加密签名的方法就是使用 app-crypt/gnupg 这个软件。安装此程序后,就可以使用以下命令来验证 .asc 文件中的加密签名。

提示
导入 Gentoo 密钥时,需要验证指纹与 (BB572E0E2D182910) 匹配。
Gentoo 密钥可以从 hkps://keys.gentoo.org 下载,可用的指纹可以在数字签名页 找到:
user $gpg --keyserver hkps://keys.gentoo.org --recv-keys 13EBBDBEDE7A12775DFDB1BABB572E0E2D182910
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key BB572E0E2D182910: public key "Gentoo Linux Release Engineering (Automated Weekly Release Key) <releng@gentoo.org>" imported
gpg: Total number processed: 1
gpg:               imported: 1

或者,您可以使用 WKD 来下载密钥:

user $gpg --auto-key-locate=clear,nodefault,wkd --locate-key releng@gentoo.org
gpg: key 9E6438C817072058: public key "Gentoo Linux Release Engineering (Gentoo Linux Release Signing Key) <releng@gentoo.org>" imported
gpg: key BB572E0E2D182910: public key "Gentoo Linux Release Engineering (Automated Weekly Release Key) <releng@gentoo.org>" imported
gpg: Total number processed: 2
gpg:               imported: 2
gpg: no ultimately trusted keys found
pub   dsa1024 2004-07-20 [SC] [expires: 2025-07-01]
      D99EAC7379A850BCE47DA5F29E6438C817072058
uid           [ unknown] Gentoo Linux Release Engineering (Gentoo Linux Release Signing Key) <releng@gentoo.org>
sub   elg2048 2004-07-20 [E] [expires: 2025-07-01]

或者使用 Gentoo 官方发行的安装媒介,从 /usr/share/openpgp-keys/gentoo-release.asc 导入密钥(由 sec-keys/openpgp-keys-gentoo-release 提供):

user $gpg --import /usr/share/openpgp-keys/gentoo-release.asc
gpg: directory '/home/larry/.gnupg' created
gpg: keybox '/home/larry/.gnupg/pubring.kbx' created
gpg: key DB6B8C1F96D8BF6D: 2 signatures not checked due to missing keys
gpg: /home/larry/.gnupg/trustdb.gpg: trustdb created
gpg: key DB6B8C1F96D8BF6D: public key "Gentoo ebuild repository signing key (Automated Signing Key) <infrastructure@gentoo.org>" imported
gpg: key 9E6438C817072058: 3 signatures not checked due to missing keys
gpg: key 9E6438C817072058: public key "Gentoo Linux Release Engineering (Gentoo Linux Release Signing Key) <releng@gentoo.org>" imported
gpg: key BB572E0E2D182910: 1 signature not checked due to a missing key
gpg: key BB572E0E2D182910: public key "Gentoo Linux Release Engineering (Automated Weekly Release Key) <releng@gentoo.org>" imported
gpg: key A13D0EF1914E7A72: 1 signature not checked due to a missing key
gpg: key A13D0EF1914E7A72: public key "Gentoo repository mirrors (automated git signing key) <repomirrorci@gentoo.org>" imported
gpg: Total number processed: 4
gpg:               imported: 4
gpg: no ultimately trusted keys found

下一步,验证加密签名:

user $gpg --verify install-amd64-minimal-20240107T170309Z.iso.asc
gpg: assuming signed data in 'install-amd64-minimal-20240107T170309Z.iso'
gpg: Signature made Sun 07 Jan 2024 03:01:10 PM CST
gpg:                using RSA key 534E4209AB49EEE1C19D96162C44695DB9F6043D
gpg: Good signature from "Gentoo Linux Release Engineering (Automated Weekly Release Key) <releng@gentoo.org>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 13EB BDBE DE7A 1277 5DFD  B1BA BB57 2E0E 2D18 2910
     Subkey fingerprint: 534E 4209 AB49 EEE1 C19D  9616 2C44 695D B9F6 043D

为了绝对确定所有文件都是有效的,验证上面显示的密匙指纹信息是否和 数字签名页 的密匙指纹信息一致。

附注
It's generally good practice to mark an imported key as trusted, once it's certain the key is trustworthy. When trusted keys are verified, gpg will not say unknown and warn about the signature being untrusted.

Writing the boot media

当然,只是下载一个 ISO 文件是无法开始 Gentoo Linux 的安装的。需要将这个ISO文件刻录成一张用来启动的 CD 光盘,是要将 ISO 文件里的内容而不是 ISO 文件本身刻录到CD光盘上。下面介绍了一些常见的方式 - 这里可以找到其他更复杂的方式:如何刻录ISO文件

Writing a bootable USB

大多数的现代系统支持从 USB 设备启动。

使用 Linux 写入

dd is typically available on most Linux distros, and can be used to write the Gentoo boot media to a USB drive.

Determining the USB device path

Before writing, the path to the desired storage device must be determined.

dmesg will display detailed information describing the storage device as it is added to the system:

root #dmesg
[268385.319745] sd 19:0:0:0: [sdd] 60628992 512-byte logical blocks: (31.0 GB/28.9 GiB)

Alternatively, lsblk can be used to display available storage devices:

root #lsblk
sdd           8:48   1  28.9G  0 disk
├─sdd1        8:49   1   246K  0 part
├─sdd2        8:50   1   2.8M  0 part
├─sdd3        8:51   1 463.5M  0 part
└─sdd4        8:52   1   300K  0 part

Once the device name has been determined, this can be added to the path prefix /dev/ to get the device path /dev/sdd.

提示
Using the base device path, ie. sdd opposed to sdd1, is recommend as the Gentoo boot media contains a full GPT partition scheme.
使用 dd 写入
警告
Be sure to check the target (of=target) path before executing dd, as it will be overwritten.

With the device path (/dev/sdd) and boot media install-amd64-minimal-<release timestamp>.iso ready:

root #dd if=install-amd64-minimal-<release timestamp>.iso of=/dev/sdd bs=4096 status=progress && sync
附注
if= specifies the input file, of= specifies the output file, which in this case, is a device.
提示
bs=4096 is used as it speeds up transfers in most cases, status=progress displays transfers stats.

刻录光盘

另请参阅
A more elaborate set of instructions can be found in CD/DVD/BD_writing#Image_writing.

在Microsoft Windows 7和更高版本上刻录

Microsoft Windows 7 及更高版本可以将 ISO 映像装载和刻录到光学媒体,无需第三方软件。 只需插入可刻录磁盘,浏览到下载的 ISO 文件,在 Windows 资源管理器中右键单击该文件,然后选择“刻录磁盘映像”。

在已有的 Linux 系统上刻录

app-cdr/cdrtools中的cdrecord可以在Linux下烧录ISO镜像

将ISO文件刻录到 /dev/sr0 设备的 CD 光碟上(这是系统上的第一个 CD 设备-在必要时将其替换为正确的设备):

user $cdrecord dev=/dev/sr0 install-amd64-minimal-20141204.iso

喜欢使用图形化界面的用户可以使用 K3B ,它由 kde-app/k3b 软件包提供。在 K3B 软件中,选择“工具”Tools)菜单,然后选择“刻录CD镜像”(Burn CD Image)。

启动

启动安装媒介

安装媒介准备就绪后,就可以启动了。 将安装媒介插入系统中,重启,然后进入主板的固件用户界面。 通常是在开机自检(Power-On Self-Test POST)过程中通过在键盘上按DEL, F1, F10, 或 ESC 进入,“触发”键取决于系统和主板。 如果使用主板的型号作为关键字在互联网搜索引擎进行搜索, 结果应该很容易确定。进入主板的固件菜单后,更改引导顺序,以便在内部磁盘设备之前尝试外部可启动媒介(CD / DVD盘或USB驱动器)。 否则,系统很可能会重新启动到内部磁盘设备,从而忽略新安装的启动媒介。

重要
如果想安装使用 UEFI 引导的 Gentoo ,建议立即使用UEFI启动。如果不用 UEFI 来启动,可能就要在最后完成 Gentoo Linux 的安装之前制作一个可以启动的 UEFI U盘(或其他介质)。

确保将安装媒介插入系统,然后重新启动。然后会显示 GRUB 启动提示符,并且带有各种启动项。 此时按 Enter 键将使用默认的启动项启动。如果要通过自定义引导选项引导安装媒介,比如要传递额外的内核参数或 下面的硬件选项,高亮启动项,然后按 e 键编辑此启动项。做一些必要的修改,然后按 ctrl+xF10 启动这个修改过的启动项。

附注
在大多数情况下,默认的Gentoo内核可以像之前提到的那样可以在没有任何指定参数的情况下正常工作,有关启动故障排除和专家选项,请继续执行此部分。否则,只需按Enter并跳转至额外的硬件配置.

在启动提示符下,用户可以按 F1 键显示可用的内核,按 F2 按键显示可用的启动选项。如果在15秒内没做任何选择(既不显示信息,也不选择内核)安装媒介将会从硬盘启动。这样不用将 CD 光盘从光盘驱动器里拿出来,也可以在安装过程中重启和尝试已安装好的环境(这有时在远程安装的时候很有用)。

之前提到过要指定一个内核。在最小安装介质上,只提供了两个预定义的内核启动项。默认选项叫 gentoo。另一个多了 -nofb 变形; 此内核会禁用内核帧缓冲区支持。

下一节将简要介绍可用的内核及其说明:

内核选择

gentoo
默认内核,支持K8 CPU(包括NUMA支持)和EM64T CPU。
gentoo-nofb
与“gentoo”相同,但没有framebuffer支持。
memtest86
测试系统 RAM 的错误。

引导选项可以配合内核进一步调整引导过程的行为。

硬件选择

acpi=on
这个选项载入对 ACPI 的支持,同时也会让 CD 光盘在启动时运行 acpid 守护进程。在系统需要 ACPI 才能正常工作的情况下才需要设置此选项。超线程(Hyperthreading)的支持不需要此选项。
acpi=off
彻底禁用 ACPI。这个选项在一些较老的系统上比较有用,同样也是使用 APM 功能的必需项。这个选项也会禁用处理器的超线程支持。
console=X
这会启用对一些终端的访问许可。它的第一个参数是设备,默认是 ttyS0, 之后的其它选项请使用逗号分割。默认参数是 9600,8,n,1 。
dmraid=X
这会传递参数给 device-mapper RAID 子系统。需要在参数两端加上括号。
doapm
这会加载对 APM 驱动的支持。这同时需要 acpi=off.
dopcmcia
这会加载对 PCMCIA 和 Cardbus 硬件的支持,并且会使 pcmcia cardmgr 在 CD 启动时被启用. 这只有在从 PCMCIA/Cardbus 设备启动时才需要。
doscsi
这会加载对大部分 SCSI 控制器的支持。当从使用 SCSI 内核子系统的 USB 设备启动时需要这个参数。
sda=stroke
这会允许用户对整块硬盘进行分区,即使是 BIOS 无法控制的大容量硬盘。这个选项只有在使用老的 BIOS 的机器上才需要。注意,请把 “sda” 替换为需要这么做的设备。
ide=nodma
这会强制内核禁用 DMA ,一些 IDE 芯片组和一些 CDROM 的驱动需要这么做才能工作。如果系统无法正常读取 IDE 的 CDROM,可以试试这个选项。这同时也会禁止默认的 hdpram 设置被执行。
noapic
这会禁用一些新主板上的高级程序中断控制器(Advanced Programmable Interrupt Controller,APIC),因为这可能会造成一些旧的硬件无法正常工作。
nodetect
这会禁止 CD 的全部自动检测功能,包括对硬件的检测和 DHCP 探测。 这有助于对启动失败的 CD 或驱动器进行查错。
nodhcp
这会禁用在被发现的网卡上进行 DHCP 探测。这在需要使用固定 IP 的时候很有用。
nodmraid
禁用对 device-mapper RAID 的支持,比如板载的IDE/SATA RAID控制器。
nofirewire
这禁用了对 “火线”( Firewire ) 模块的加载。该选项只在“火线”(Firewire)造成 CD 无法正常启动时才需要。
nogpm
这禁用对 gpm 控制台的鼠标(gpm console mouse)的支持。
nohotplug
这会禁止在启动时加载对热插拔和冷插拔的脚本。这有助于对启动失败的 CD 或驱动器进行查错。
nokeymap
这会禁用选择键盘映射(只有不是 US 键盘时才需要进行对键盘映射的设置)。
nolapic
这会在单处理器内核里禁用本地APIC。
nosata
这会禁止加载 Serial ATA 模块. 这在 SATA 子系统出错时才需要。
nosmp
这会在支持 SMP 的内核上禁用 SMP(Symmetric Multiprocessing)。这在为排查与 SMP 相关的驱动或内核错误时很有用。
nosound
这会禁止对音频的支持和音量控制。这在音频系统造成问题时很有用。
nousb
这会禁止自动加载的 USB 模块。这在 USB 出现问题时很有用。
slowusb
这会为慢速的USB CDROM 在启动时添加更多额外的中断,就像 IBM BladeCenter 那样。

逻辑卷/设备管理

dolvm
这会启用 Linux 的逻辑分区管理器(Logical Volume Management)。

其他选项

debug
启用调试代码。这可能会显得乱糟糟的,因为这会向输出大量的数据。
docache
这会把整个 CD 运行环境缓存到内存中,这会使用户可以卸载 /mnt/cdrom 并挂载另外一个 CDROM 。这个选项需要至少两倍于 CD 大小的内存空间。
doload=X
这会使启动时内存盘(initial ramdisk,initrd)加载这之后列出来的模块和它们的依赖。把“X”替换为模块名称,当需要加载多个模块时请用逗号分割。
dosshd
在启动时启用 sshd 服务,这在无人值守安装时很有用。
passwd=foo
这会将等号后的字符设置为 root 用户的密码,当使用“dosshd”参数时需要这么做因为默认的 root 密码是留空的。
noload=X
这会使启动时内存盘(initial ramdisk,initrd)跳过对某些会造成问题的特定模块的加载。使用方法和“doload”相同。
nonfs
禁止在启动时启用 portmap/nfsmount 。
nox
这会使启用X的 LiveCD 不自动启动X,而是使用命令行。
scandelay
这会使 CD 在启动过程中等待十秒来使一些初始化很慢的设备完成初始化。
scandelay=X
这允许用户指定 CD 在启动过程中等待一些初始化很慢的设备完成初始化所需的延迟的时间。把X替换为所需要等待的时间(以秒为单位,只需要填写数字)。
附注
启动媒介将先检查no*选项,再检查do*选项,所以那些选项可以按照这个顺序覆盖。

现在启动安装媒介,选择一个内核(如果默认的 gentoo 的内核不能满足)和引导选项。作为示例,我们引导 gentoo 内核启动,并带有dopcmcia作为内核参数:

boot:gentoo dopcmcia

接下来迎接用户的是一个引导屏幕和进度条。如果用来安装系统的是一个非US键盘,确保马上按Alt + F1来切换到详细模式并遵照提示。如果在10秒钟内什么都没有选,则接受默认(US键盘)并继续引导过程。一旦引导过程完成,用户将自动以root超级用户身份登录到“Live”Gentoo Linux环境。当前控制台将显示一个root提示符,并且可以通过按Alt + F2Alt + F3Alt + F4切换到其他控制台。按Alt + F1返回到启动时的那个。


额外的硬件配置

当安装媒介启动时,它会尝试检测所有的硬件设备并加载合适的内核模块来支持硬件。在绝大多数的情况下,它工作得很好。然而,在某些情况下它可能没有自动加载系统所需的内核模块。如果 PCI 自动检测错过了一些系统硬件,相应的内核模块就必须手动加载了。

下面例子手工加载了 8139too 模块(它提供对某些类型的网卡的支持):

root #modprobe 8139too

可选:用户账号

如果其他人需要访问安装环境,或者需要以非 root 用户的身份在安装媒介上运行命令(例如出于安全原因使用没有 root 特权的 irssi 聊天),这时就需要创建额外的用户帐户,并将 root 用户密码设为强密码。

使用 passwd 命令来修改 root 用户密码:

root #passwd
New password: (Enter the new password)
Re-enter password: (Re-enter the password)

要创建一个用户账户,先输入他们的信息,然后设置密码。用 useraddpasswd 命令来完成这些操作。

在下面的例子中,创建了一个名为“john”的用户。

root #useradd -m -G users john
root #passwd john
New password: (Enter john's password)
Re-enter password: (Re-enter john's password)

使用 su 命令可以从 root 用户(当前用户)切换到新建的用户:

root #su - john

可选:在安装时查看文档

终端

要在安装期间查看 Gentoo 安装手册,首先要按照上面的方法创建一个新的用户帐户。然后按 Alt+F2 进入一个新的终端(TTY)。

在安装期间,可以用 links web 浏览器来浏览 Gentoo 安装手册——当然,只有在互联网连接可用的时候才行。

user $links https://wiki.gentoo.org/wiki/Handbook:AMD64/zh-cn

要回到原来的终端,请按 Alt+F1

提示
启动到最小化 Gentoo 或者 Gentoo 管理环境后,有 7 个终端可供使用。可以按 AltF1F7 之间的功能键来切换。当等待任务完成的时候,切换到一个新的终端可以很方便的用来打开文档等。

GNU Screen

Screen 是官方Gentoo安装介质中默认安装的实用程序。对于经验丰富的Linux爱好者来说,使用 screen 分割窗口查看安装说明,而不是上面提到的多个终端的方法, 这可能更高效。

可选:启动SSH服务

要在安装期间允许其他用户访问你的系统(可能是为了在安装过程中提供技术支持,甚至远程安装),需要添加一个用户账户(就像之前的文档描述的那样)同时 SSH 服务也要启动。

若要在 OpenRC init 启动 SSH 服务,请执行以下命令:

root #rc-service sshd start
附注
如果用户登录到系统,他们将看到一个本系统主机密钥需要确认的信息(也就是我们说的密匙指纹)。此行为是典型的并且可以像预期一样与SSH服务器进行初始连接。但是,以后当系统设置好,并有人登录到新安装的系统时,SSH客户端会警告主机密钥已被更改。这是因为现在用户登录 - 对于SSH来讲 - 是一个不同的服务器(即新安装的Gentoo系统,而不是现在正在使用的安装系统环境)。请按照屏幕上的指示,去替换用户端的主机密钥

网络需要能正常工作,sshd 才能使用。请参照 配置网络 的内容继续安装。





自动网络检测配置

它能够自动检测到么?

如果系统接入到一个有 DHCP 服务器的以太网络,那么很可能会自动配置好网络。这样的话,安装媒介所包含的很多网络命令,比如sshscppingirssiwgetlinks,以及其他的一些命令, 都可以立即工作。

使用DHCP

DHCP(动态主机配置协议)使自动接受网络信息(IP地址、掩码、广播地址、网关、名称服务器等)变得容易。这只在网络中有DHCP服务器(或者如果ISP提供商提供一个DHCP服务)时有用。要使一个网络接口自动接受信息,使用dhcpcd

DHCP requires that a server be running on the same Layer 2 (Ethernet) segment as the client requesting a lease. DHCP is often used on RFC1918 (private) networks, but is also used to acquire public IP information from ISPs.

提示
Official Gentoo boot media runs dhcpcd automatically at startup. This behavior can be disabled by adding the nodhcp argument to the boot media kernel commandline.

If it is not already running, dhcpcd can be started on enp1s0 with:

root #dhcpcd enp1s0

一些网络管理员要求你使用DHCP服务器所提供的主机名和域名。 这种情况下请用:

root #dhcpcd -HD enp1s0

如果要停止 dhcpcd,可以使用 -x

root #dhcpcd -x
sending signal Term to pid 10831
waiting for pid 10831 to exit
另请参阅
使用 Dhcpcd

测试网络

A properly configured default route is a critical component of Internet connectivity, route configuration can be checked with:

root #ip route
default via 192.168.0.1 dev enp1s0

If no default route is defined, Internet connectivity is unavailable, and additional configuration is required.

Basic internet connectivity can be confirmed with a ping:

root #ping -c 3 1.1.1.1
提示
It's helpful to start by pinging a known IP address instead of a hostname. This can isolate DNS issues from basic Internet connectivity issues.

Outbound HTTPS access and DNS resolution can be confirmed with:

root #curl --location gentoo.org --output /dev/null

如果这些都工作,则本章节中其余的部分可跳过,直接跳到安装介绍的下一步骤(准备磁盘)。

If curl reports an error, but Internet-bound pings work, DNS may need configuration.

If Internet connectivity has not been established, first interface information should be verified, then:

获取接口信息

If networking doesn't work out of the box, additional steps must be taken to enable Internet connectivity. Generally, the first step is to enumerate host network interfaces.

ip 命令,属于 sys-apps/iproute2 软件包,他可以用来查询和配置系统网络。

link 参数可以用来显示网络接口的链接:

root #ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether e8:40:f2:ac:25:7a brd ff:ff:ff:ff:ff:ff

address 参数可以查询设备地址信息:

root #ip address
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether e8:40:f2:ac:25:7a brd ff:ff:ff:ff:ff:ff
    inet 10.0.20.77/22 brd 10.0.23.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::ea40:f2ff:feac:257a/64 scope link 
       valid_lft forever preferred_lft forever

The output of this command contains information for each network interface on the system. Entries begin with the device index, followed by the device name: enp1s0.

提示
如果使用标准的ifconfig命令没有显示出接口,尝试使用带有-a选项的相同的命令。这个选项强制这个工具去显示系统检测到的所有的网络接口,不管他们是up或down状态。如果ifconfig -a没有提供结果,则硬件有错误或者接口驱动没有加载到内核中。这些情况都超过本手册的范围。联系#gentoo (webchat)需求支持。

为了保持一致性,本手册中假设主要的网络接口为 enp1s0

因为名称变成了预测的网络接口名称(Predictable Network Interface Names), 所以系统的接口名称可能和旧的 eth0 命名规则很不一样。近期的安装媒介上常规网络接口名字可能显示为 eno0ens1enp5s0。查看 ifconfig 输出中找到有你本地网络相关的IP地址的接口。

Optional: Application specific configuration

The following methods are not generally required, but may be helpful in situations where additional configuration is required for Internet connectivity.

可选:配置网络代理

如果需要通过代理来访问互联网。那么必须为 Portage 定义代理信息,这样才能正确访问每个支持协议的代理。 Portage 使用 http_proxyftp_proxyRSYNC_PROXY 环境变量,通过 wgetrsync 的检索机制来下载软件包。

Certain text-mode web browsers such as links can also make use of environment variables that define web proxy settings; in particular for the HTTPS access it also will require the https_proxy environment variable to be defined. While Portage will be influenced without passing extra run time parameters during invocation, links will require proxy settings to be set.

大多数情况下,只要将这个环境变量定义为代理服务器主机名。在下面的例子中,假定代理服务器的主机是 proxy.gentoo.org 并且端口为 8080。

附注
下列命令中的 # 符号是注释。添加 # 符号只是为了清楚起见,在输入命令时需要输入 # 符号。

定义一个 HTTP 代理(用于 HTTP 和 HTTPS 流量):

root #export http_proxy="http://proxy.gentoo.org:8080" # 同时应用到 Portage 和 Links
root #export https_proxy="http://proxy.gentoo.org:8080" # 只应用到 Links

如果代理需要验证,使用下面的语法来设置用户名和密码:

root #export http_proxy="http://username:password@proxy.gentoo.org:8080" # 同时应用到 Portage 和 Links
root #export https_proxy="http://username:password@proxy.gentoo.org:8080" # 只应用到 Links

使用下列参数启动可以为 links 使用代理:

user $links -http-proxy ${http_proxy} -https-proxy ${https_proxy}

为 Portage 和 links 定义 FTP 代理:

root #export ftp_proxy="ftp://proxy.gentoo.org:8080" # 同时应用到 Portage 和 Links

使用下列参数启动可以为 links 使用 FTP 代理:

user $links -ftp-proxy ${ftp_proxy}

为 Portage 定义 RSYNC 代理:

root #export RSYNC_PROXY="proxy.gentoo.org:8080" # 应用到 Portage:Links 不支持 rsync 代理

对 ADSL 使用 pppoe-setup

If PPPoE is required for Internet access, the Gentoo boot media includes the pppoe-setup script to simplify ppp configuration.

安装时,pppoe-setup 会询问:

  • 连接到 ADSL 调制解调器的以太网 接口 名称。
  • PPPoE 用户名和密码。
  • DNS 服务器 IP。
  • 是否需要防火墙。
root #pppoe-setup
root #pppoe-start

In the event of failure, credentials in /etc/ppp/pap-secrets or /etc/ppp/chap-secrets should be verified. If credentials are correct, PPPoE Ethernet interface selection should be checked.

使用 PPTP

如果需要 PPTP 支持,可以使用 pptpclient,但是需要在使用前提前配置。

编辑 /etc/ppp/pap-secrets 或者 /etc/ppp/chap-secrets,让他们包含正确的用户名和密码:

root #nano /etc/ppp/chap-secrets

如果需要,继续调整/etc/ppp/options.pptp

root #nano /etc/ppp/options.pptp

配置完成后,运行 pptp(带着一些options.pptp无法设定的选项)来连接到服务器:

root #pptp <server ipv4 address>

配置 WEP

警告
Do not use WEP unless it is the only option. WEP provides essentially no security over an open network.
附注
可能只有特定的架构支持iw命令。如果这个命令不可用,检查net-wireless/iw包是否可用于当前架构。除非安装net-wireless/iw包,否则iw命令将一直不可用。

当使用一块无线(802.11)网卡,在继续之前需要先配置无线设置。要查看当前无线网卡的设置,你可以使用iw。运行iw可能会显示如下:

root #iw dev wlp9s0 info
Interface wlp9s0
	ifindex 3
	wdev 0x1
	addr 00:00:00:00:00:00
	type managed
	wiphy 0
	channel 11 (2462 MHz), width: 20 MHz (no HT), center1: 2462 MHz
	txpower 30.00 dBm

检查当前连接:

root #iw dev wlp9s0 link
Not connected.

root #iw dev wlp9s0 link
Connected to 00:00:00:00:00:00 (on wlp9s0)
	SSID: GentooNode
	freq: 2462
	RX: 3279 bytes (25 packets)
	TX: 1049 bytes (7 packets)
	signal: -23 dBm
	tx bitrate: 1.0 MBit/s
附注
一些无线网卡的设备名可能是wlan0ra0而不是 wlp9s0。运行 ip link 可以识别正确的设备名称。

对于大多数用户,只需要两个设置来连接,即ESSID(也称无线网络名称)和可选的WEP密钥。

  • 首先,确保接口处于活动状态:
root #ip link set dev wlp9s0 up
  • 连接到名为“GentooNode”的开放网络:
root #iw dev wlp9s0 connect -w GentooNode
  • 设置一个WEP密钥:使用d:前缀:
root #iw dev wlp9s0 connect -w GentooNode key 0:d:1234123412341234abcd
  • 使用ASCII WEP密钥连接:
root #iw dev wlp9s0 connect -w GentooNode key 0:some-password
附注
如果无线网络配置为WPA或WPA2,则需要使用wpa_supplicant。关于为Gentoo Linux配置无线网络的更多信息,请阅读Gentoo手册中的无线网络章节

使用iw dev wlp9s0 link确认无线设置。如果无线已经工作,继续按下一章节(了解网络属于)配置IP级别的网络选项或者使用前面描述的net-setup工具。

使用 net-setup

In cases where automatic network configuration is unsuccessful, the Gentoo boot media provides scripts to aid in network configuration. net-setup can be used to configure wireless network information and static IPs.

root #net-setup enp1s0

net-setup将会询问关于网络环境的一些问题。当所有这些完成后,网络连接就应该工作。以前面的方式测试网络连接。如果测试通过,恭喜!跳过本章节剩余部分并继续准备磁盘

重要
Network status should be tested after any configuration steps are taken. In the event that configuration scripts do not work, manual network configuration is required.

互联网和 IP 基础

If all of the above fails, the network must be configured manually. This is not particularly difficult, but should be done with consideration. This section serves to clarify terminology and introduce users to basic networking concepts pertaining to manually configuring an Internet connection.

提示
Some CPE (Carrier Provided Equipment) combines the functions of a router, access point, modem, DHCP server, and DNS server into one unit. It's important to differentiate the functions of a device from the physical appliance.

接口和地址

Network interfaces are logical representations of network devices. An interface needs an address to communicate with other devices on the network. While only a single address is required, multiple addresses can be assigned to a single interface. This is especially useful for dual stack (IPv4 + IPv6) configurations.

For consistency, this primer will assume the interface enp1s0 will be using the address 192.168.0.2.

重要
IP addresses can be set arbitrarily. As a result, it's possible for multiple devices to use the same IP address, resulting in an address conflict. Address conflicts should be avoided by using DHCP or SLAAC.
提示
IPv6 typically uses StateLess Address AutoConfiguration (SLAAC) for address configuration. In most cases, manually setting IPv6 addresses is a bad practice. If a specific address suffix is preferred, interface identification tokens can be used.

网络和 CIDR

Once an address is chosen, how does a device know how to talk to other devices?

IP addresses are associated with networks. IP networks are contiguous logical ranges of addresses.

Classless Inter-Domain Routing or CIDR notation is used to distinguish network sizes.

  • The CIDR value, often notated starting with a /, represents the size of the network.
    • The formula 2 ^ (32 - CIDR) can be used to calculate network size.
    • Once network size is calculated, usable node count must be reduced by 2.
      • The first IP in a network is the Network address, and the last is typically the Broadcast address. These addresses are special and cannot be used by normal hosts.
提示
The most common CIDR values are /24, and /32, representing 254 nodes and a single node respectively.

A CIDR of /24 is the de-facto default network size. This corresponds to a subnet mask of 255.255.255.0, where the last 8 bits are reserved for IP addresses for nodes on a network.

The notation: 192.168.0.2/24 can be interpreted as:

  • The address 192.168.0.2
  • On the network 192.168.0.0
  • With a size of 254 (2 ^ (32 - 24) - 2)
    • Usable IPs are in the range 192.168.0.1 - 192.168.0.254
  • With a broadcast address of 192.168.0.255
    • In most cases, the last address on a network is used as the broadcast address, but this can be changed.

Using this configuration, a device should be able to communicate with any host on the same network (192.168.0.0).

The Internet

Once a device is on a network, how does it know how to talk to devices on the Internet?

To communicate with devices outside of local networks, routing must be used. A router is simply a network device that forwards traffic for other devices. The term default route or gateway typically refers to whatever device on the current network is used for external network access.

提示
It's a standard practice to make the gateway the first or last IP on a network.

If an Internet-connected router is available at 192.168.0.1, it can be used as the default route, granting Internet access.

To summarize:

  • Interfaces must be configured with an address and network information, such as the CIDR value.
  • Local network access is used to access a router on the same network.
  • The default route is configured, so traffic destined for external networks is forwarded to the gateway, providing Internet access.

域名系统

Remembering IPs is hard. The Domain Name System was created to allow mapping between Domain Names and IP addresses.

Linux systems use /etc/resolv.conf to define nameservers to be used for DNS resolution.

提示
Many routers can also function as a DNS server, and using a local DNS server can augment privacy and speed up queries through caching.

Many ISPs run a DNS server that is generally advertised to the gateway over DHCP. Using a local DNS server tends to improve query latency, but most public DNS servers will return the same results, so server usage is largely based on preference.

手动配置网络

配置接口地址

重要
When manually configuring IP addresses, the local network topology must be considered. IP addresses can be set arbitrarily; conflicts may cause network disruption.

To configure enp1s0 with the address 192.168.0.2 and CIDR /24:

root #ip address add 192.168.0.2/24 dev enp1s0
提示
The start of this command can be shortened to ip a.

配置默认路由

Configuring address and network information for an interface will configure link routes, allowing communication with that network segment:

root #ip route
192.168.0.0/24 dev enp1s0 proto kernel scope link src 192.168.0.2
提示
This command can be shortened to ip r.

The default route can be set to 192.168.0.1 with:

root #ip route add default via 192.168.0.1

配置 DNS

Nameserver info is typically acquired using DHCP, but can be set manually by adding nameserver entries to /etc/resolv.conf.

警告
If dhcpcd is running, changes to /etc/resolv.conf will not persist. Status can be checked with ps x | grep dhcpcd.

nano is included in Gentoo boot media and can be used to edit /etc/resolv.conf with:

root #nano /etc/resolv.conf

Lines containing the keyword nameserver followed by a DNS server IP address are queried in order of definition:

文件 /etc/resolv.confUse Quad9 DNS.
nameserver 9.9.9.9
nameserver 149.112.112.112
文件 /etc/resolv.confUse Cloudflare DNS.
nameserver 1.1.1.1
nameserver 1.0.0.1

DNS status can be checked by pinging a domain name:

root #ping -c 3 gentoo.org

Once connectivity has been verified, continue with Preparing the disks.





块设备简介

块设备

让我们来好好看看Gentoo Linux以及普通Linux中有关磁盘方面的知识,包括块设备、分区和Linux文件系统。一旦磁盘的来龙去脉都了解了,我们将设置分区和文件系统以进行安装。

首先,让我们来看看块设备。SCSI和SATA磁盘以/dev/sda,/dev/sdb,/dev/sdc等形式被标注。在更先进的设备中,基于PCI Express的NVMe固态磁盘一般以/dev/nvme0n1,/dev/nvme0n2等形式被标注。

下表将帮助读者确定在系统中哪里可以找到块设备的类型:

设备类型 默认设备句柄 社论笔记和注意事项
IDE,SATA,SAS,SCSI 或 USB 闪存 /dev/sda 从大约 2007 年到现在,这可能是 Linux 中最常用的设备句柄。这类设备可以连接在 SATA 总线SCSIUSB 总线上作为块储存。例如,在第一个 SATA 设备的第一个分区称为 /dev/sda1
NVM Express (NVMe) /dev/nvme0n1 NVMe设备是最新的固态技术,它连接在 PCI Express 总线,并且拥有市面上最快的传输速度。2014 年前后的系统可能会支持 NVMe 硬件。在第一个 NVMe 设备的第一个分区称为 /dev/nvme0n1p1
MMC,eMMC 和 SD /dev/mmcblk0 嵌入式 MMC 设备,SD 卡和其它类型的记忆卡 可以作为数据存储使用。也就是说,许多系统可能不允许从这些类型的设备启动。建议使用这些设备用来安装 Linux,而是考虑用它们来传输文件,这也是它们主要的设计意图。另外,这种储存类型很适合用来做短期的文件备份和快照。

上面的块设备代表磁盘的抽象接口。用户程序可以使用这些块设备来与你的磁盘进行交互,而无需担心驱动器到底是 SATA,SCSI 还是其他什么东西。该程序可以把磁盘当作一系列连续的,可随机访问的 4096 字节块(4K)的存储。


分区表

虽然理论上可以用一整块磁盘来安装一个Linux系统(比如当创建一个 btrfs RAID时),但是实践中几乎从不这样做。实际上,一块磁盘可以被分成小一些的、更容易管理的块设备。在 amd64 系统里,这被称为分区。有两个标准的分区技术可以被使用:MBR(有时也称为 DOS 磁盘标签)和GPT;这些与两种引导过程类型相关:传统 BIOS 引导和 UEFI。

GUID 分区表 (GPT)

GUID 分区表 (GPT)设置(也称为 GPT 磁盘标签)对分区使用 64 位标识符。它用来存储分区信息的空间也远比 MBR 分区表(DOS 磁盘标签)的512字节要大,GPT磁盘它也不对分区的数量作限制。分区的大小限制可以达到 8 ZiB(zebibytes)。 译者注:

  • 1ZiB = 1,024 EiB
  • 1EiB = 1024 PiB
  • 1PiB = 1024 TiB
  • 1TiB = 1024 GiB
  • 1GiB = 1024 MiB
  • 1MiB = 1024 KiB
  • 1KiB = 1024 B

当操作系统和系统固件之间的软件接口是UEFI (相对于BIOS)时,GPT几乎是必选的,因为这里 DOS 磁盘标签会引起很多兼容性问题。

GPT还利用校验和和冗余。 它携带CRC32校验和以检测报头和分区表中的错误,并在磁盘的末尾有一个备份GPT。 此备份表可用于恢复磁盘开头附近主GPT的损坏。

重要
关于 GPT 有一些注意事项:
  • 在基于 BIOS 的计算机上使用 GPT 是可行的,但不能与 Microsoft Windows 操作系统进行双重引导。原因是如果 Microsoft Windows 检测到 GPT 分区标签,它将以 UEFI 模式启动。
  • 一些配置为以 BIOS/CSM/legacy 模式启动的有问题的(旧)主板固件在从 GPT 标记的磁盘启动时也可能存在问题。

主引导记录 (MBR) 和 DOS 引导扇区

主引导记录引导扇区(也称为 DOS 引导扇区,DOS 磁盘标签,现在区别与 GPT/UEFI 安装,称为 传统 BIOS 引导)于 1983 年首次在 PC DOS 2.x 中引入。 MBR 使用 32 位标识符作为分区的起始扇区和长度,并支持三种分区类型:主分区、扩展分区和逻辑分区。主分区的信息存储在主引导记录本身——磁盘最开始的一个非常小的(通常是 512 字节)位置。由于空间很小,因此仅支持四个主分区(例如,/dev/sda1/dev/sda4)。

为了支持更多的分区,可以将 MBR 中的主分区之一标记为扩展分区。然后,该分区可以包含其它逻辑分区(分区内的分区)。

重要
虽然大多数主板制造商仍然支持,但 MBR 引导扇区及其相关的分区限制被认为是传统的分区方式。除非使用 2010 之前的硬件,否则最好使用 GUID 分区表 对磁盘进行分区。必须继续进行设置类型的读者应了解以下信息:
  • 大多数 2010 年后的主板都有接受用 MBR 引导扇区作为传统(受支持但不理想的)引导模式。
  • 由于使用 32 位标识符,MBR 中的分区表无法处理大于 2 TiB 的存储空间。
  • 除非创建扩展分区,否则 MBR 最多支持四个分区。
  • 此设置不提供备份引导扇区,因此如果某些内容覆盖分区表,所有分区信息将丢失。
也就是说,在 AWS 等虚拟化云环境中仍可能使用 MBR 和 传统 BIOS 启动。

手册作者建议读者安装Gentoo时尽可能使用 GPT

高级储存

amd64 安装 CD 提供了对逻辑卷管理器 (LVM) 的支持。 LVM 通过增加分区设置提供的灵活性。它允许将分区和磁盘组合到卷组中,并在快速的固态硬盘上为慢速的机械硬盘定义 RAID 组或缓存。下面的安装说明将侧重于"常规"分区,如果强烈需要 LVM,请访问 LVM 文章了解更多详情。新手请注意:LVM虽然完全支持 ,但不在本指南的范围内。

默认分区方案

在本手册的其余部分,我们将讨论和解释两种情况:

  1. GUID 分区表 (GPT)和 UEFI 引导。
  2. MBR 分区表和传统 BIOS 引导。

While it is possible to mix and match boot types with certain motherboard firmware, mixing goes beyond the intention of the handbook. As previously stated, it is strongly recommended for installations on modern hardware to use UEFI boot with a GPT disklabel disk.

以下分区方案是一种简单的布局示例。

重要
The first row of the following table contains exclusive information for either a GPT disklabel or a MBR DOS/legacy BIOS disklabel. When in doubt, proceed with GPT, since amd64 machines manufactured after the year 2010 generally support UEFI firmware and GPT boot sector.
分区 文件系统 大小 描述
/dev/sda1 fat32 EFI 系统分区所需的文件系统,总与 GPT 磁盘标签相关联。 1 GiB EFI 系统分区详情。 适用于支持 UEFI 的系统固件。通常为 2010 年前后至今生产的系统。
xfs MBR 分区表启动分区推荐的文件系统,仅限于 DOS/传统 BIOS 磁盘标签的旧固件一起使用。 MBR DOS/传统 BIOS 引导分区详情。 适用于传统 BIOS 机器固件。通常为 2010 年<u>以前</u>的系统,并且逐步停止维护。
/dev/sda2 linux-swap RAM 大小 * 2 交换分区详情。
/dev/sda3 xfs 磁盘剩余空间 根分区详情。

如果这些信息已经足够,高级读者可以直接跳转到实际分区操作。

fdiskparted 都是包含在官方 Gentoo live 镜像环境中的分区实用程序。 fdisk 大家都熟知,稳定,在 MBR 和 GPT 磁盘都可以使用。 parted 是最早支持 GPT 分区的 Linux 块设备管理的分区工具之一。如果读者愿意的话,parted 可以作为 fdisk 的替代品,但是因为在大多数 Linux 环境中常见 fdisk,所以本手册只介绍 fdisk

在进行创建分区的指导之前,关于分区方案和常见陷阱我们会先介绍更多的细节。


设计一个分区方案

多少个分区以及多大?

磁盘分区布局的设计高度依赖于系统需求和应用到设备的文件系统。如果有很多用户,建议将 /home 放在单独的分区上,这将提高安全性并使备份和其他类型的维护更容易。 以增强安全性及便于备份。如果安装 Gentoo 来做邮件服务器,那么 /var 应该单独分一个区,因为所有的邮件都储存于 /var/。游戏服务器可能应该有一个独立的 /opt/,因为大多数游戏服务器软件都安装在那里。原因也和 /home/ 目录一样:安全,备份和维护。

在 Gentoo 的大多数情况下,/usr/var 应该保持相对较大的空间。/usr 存放了系统中的大部分应用程序和 Linux 内核源代码(在/usr/src下)。默认情况下,/var 存放着 Gentoo ebuild 仓库(位于 /var/db/repos/gentoo ),根据文件系统的不同,它大约耗费 650MiB 的磁盘空间。这个空间估计"不包括" /var/cache/distfiles/var/cache/binpkgs 目录,当它们被添加到系统中时,将逐渐填写源代码文件和二进制包(可选)。

分区的数量和大小取决于权衡利弊后根据实际情况选择最佳选项。单独的分区或卷具有以下优点:

  • 为每个分区或者卷选择性能最好的文件系统。
  • 当一个失控的工具持续向一个分区或卷写文件时,也不至于让整个系统由于无可用空间而无法运行。
  • 如果有必要,可以简化文件系统检查,多个检查可以并行的完成(尽管使用多个磁盘比使用多个分区更多地实现了这一优势)。
  • 可以通过在挂载一些分区或卷时使用只读、nosuid(忽略setuid属性)、noexec(忽略可执行属性)等来增加安全性。


但是,多个分区也有一些缺点:

  • 如果配置不正确,系统可能在一个分区上有很多可用空间,而在另一个分区上可用空间很少。
  • /usr/ 的单独分区可能需要管理员使用 initramfs 引导,以便在其他引导脚本启动之前挂载该分区。由于 initramfs 的生成和维护超出了本手册的范围,我们建议新手不要为 /usr/ 使用单独的分区
  • SCSI 和 SATA 也有 15 个分区的限制,除非磁盘使用 GPT 标签。
附注
打算使用 systemd 作为服务和 init 系统安装时,/usr/ 目录必须在启动时可用,可以将该目录作为根文件系统的一部分,或通过 initramfs 挂载该目录。

那么交换空间呢?

对于交换空间大小,没有一个完美值。交换空间的目的是当内存(RAM)有压力时为内核提供磁盘存储。一个交换空间允许内核将看过来稍后不会被访问的内存页面移动到磁盘(交换或者 page-out)、为当前任务释放 RAM 中的内存。当然,如果突然要使用到交换到磁盘的页面,需要花费比从 RAM 读取要长得多的时间(相比较内存,硬盘是非常慢的),将这些页面需要放回到内存中(page-in)。

如果系统不运行很需要内存的应用程序或有足够多的可用 RAM,则不需要太多的交换空间。但是请注意,在休眠的情况下,交换空间用于存储"内存的全部内容"(可能在台式机和笔记本电脑系统上,而不是在服务器系统上)。如果系统需要支持休眠,那么有必要使用大于或等于内存数量的交换空间。

作为一般规则,建议交换空间大小为内部存储器 (RAM) 的两倍。对于具有多个硬盘的系统,明智的做法是在每个磁盘上创建一个交换分区,以便它们可以用于并行读/写操作。当必须访问交换空间中的数据时,磁盘交换的速度越快,系统运行的速度就越快。在机械和固态磁盘之间进行选择时,最好将交换放在 SSD 上以提高性能。此外,交换文件可以用作交换分区的替代方案;这对于磁盘空间非常有限的系统来说非常有趣。


什么是 EFI 系统分区 (ESP)?

在使用由 UEFI 引导(而不是 BIOS)的操作系统上安装 Gentoo 时,创建 EFI 系统分区 (ESP) 很重要。下面的说明包含正确处理此操作所需的关键点。 在 BIOS/Legacy 模式下启动时不需要 EFI 系统分区。

ESP 必须是 FAT 变体(有时在 Linux 系统上显示为 vfat)。官方 UEFI 规范 表示 UEFI 固件将识别 FAT12、16 或 32 文件系统,但建议使用 FAT32。分区后,相应地格式化 ESP:

root #mkfs.fat -F 32 /dev/sda1
警告
如果 ESP 没有使用 FAT 变体进行格式化,那么系统的 UEFI 固件将找不到引导加载程序(或 Linux 内核)并且很可能无法引导系统!

什么是BIOS引导分区?

只有在 BIOS/Legacy 模式下将 GPT 分区布局与 GRUB2 结合时,才需要 BIOS 引导分区。 在 EFI/UEFI 启动模式下引导时不需要它,使用 MBR 表时也不需要它。它是一个非常小的分区(1 到 2 MB),像 GRUB2 这样的可以在其中放置超出容量的引导加载程序。本指南中不会使用它。

使用 GPT for UEFI 对磁盘进行分区

以下部分解释了如何使用 fdisk 为 GPT/UEFI 引导安装创建示例分区布局。范例分区布局我们在前面已经提到过了。

请您根据自己的实际需要来调整您的分区布局。

设备路径 (sysfs) 挂载点 文件系统 DPS UUID (PARTUUID) 描述
/dev/sda1 /efi vfat c12a7328-f81f-11d2-ba4b-00a0c93ec93b EFI 系统分区(EFI system partition ESP) 详情。
/dev/sda2 N/A. 交换分区并不像设备文件那样挂载到文件系统。 swap 0657fd6d-a4ab-43c4-84e5-0933c84b4f4f 交换分区分区详情。
/dev/sda3 / xfs 4f68bce3-e8cd-4db1-96e7-fbcaf984b709 根分区详情。

查看当前分区布局

fdisk是一个流行的和强大的分区工具。用fdisk向磁盘开火吧!(在我们的例子里,我们使用/dev/sda):

root #fdisk /dev/sda

使用 p 键来显示磁盘当前的分区配置。

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 21AAD8CF-DB67-0F43-9374-416C7A4E31EA
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Device        Start        End    Sectors   Size Type
/dev/sda1      2048    2099199    2097152     1G EFI System
/dev/sda2   2099200   10487807    8388608     4G Linux swap
/dev/sda3  10487808 1953523711 1943035904 926.5G Linux root (x86-64)

这块特定的磁盘被配置为容纳 2 个 Linux 文件系统(每个都有一个相应的分区列为“Linux”)以及一个交换分区(列为“Linux swap”)。

创建一个新的磁盘标签/删除所有分区

输入 g 在磁盘上创建一个新的 GPT 磁盘标签;这将删除所有现有分区。

Command (m for help):g
Created a new GPT disklabel (GUID: 87EA4497-2722-DF43-A954-368E46AE5C5F).

对于现有的 GPT 磁盘标签(参见上面 p 的输出),或者考虑从磁盘中一一删除现有分区。输入 d 来删除一个分区。例如,要删除现有的 /dev/sda1

Command (m for help):d
Partition number (1-4): 1

这个分区已经计划被删除了,当您用p键打印分区清单时它将不会被显示了,但此时它还未被实际删除,直到改变被真正保存。这将允许用户在操作错误后中止——此时,输入q并按Enter可以立即防止分区被删除。

重复敲击 p来打印分区清单,然后敲击 d键和分区号码来删除它。最终,分区表将变得空空如也。

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 87EA4497-2722-DF43-A954-368E46AE5C5F

现在在内存中的分区表已经空了,我们是时候来创建分区了。

创建 EFI 系统分区 (ESP)

附注
虽然可以使用较小的 ESP,但是不推荐这么做,尤其是考虑到可能与其他操作系统共享 ESP。

首先创建一个小的 EFI 系统分区,该分区也将挂载为 /boot。输入 n 创建一个新分区,然后输入 1 选择第一个分区。当提示输入第一个扇区时,确保它从 2048(引导加载程序可能需要)开始并输入 Enter。当提示输入最后一个扇区时,输入 +1G 创建一个大小为 1G 字节的分区:

Command (m for help):n
Partition number (1-128, default 1): 1
First sector (2048-60549086, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60549086, default 60549086): +1G
 
Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.

Do you want to remove the signature? [Y]es/[N]o: Y The signature will be removed by a write command.

}}

将分区标记为 EFI 系统分区:

Command (m for help):t
Selected partition 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.

Optionally, to have the ESP conform to the Discoverable System Partition (DSP) specification, switch to expert mode and perform the following extra step to set the partition's UUID:

Command (m for help):x
Expert command (m for help):u
Selected partition 1
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
New UUID (in 8-4-4-4-12 format): c12a7328-f81f-11d2-ba4b-00a0c93ec93b
Partition UUID changed from 10293DC1-DF6C-4443-8ACF-C756B81B4767 to C12A7328-F81F-11D2-BA4B-00A0C93EC93B.

Press the r key to return to the main menu:

Expert command (m for help):r
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Command (m for help):

创建交换分区

接下来,要创建交换分区,请输入 n 创建一个新分区,然后输入 2 创建第二个分区 /dev/sda2。当提示输入第一个扇区时,输入 Enter。当提示输入最后一个扇区时,输入 +4G(或交换空间所需的任何其他大小)以创建大小为 4GB 的分区。

Command (m for help):n
Partition number (2-128, default 2): 
First sector (526336-60549086, default 526336): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (526336-60549086, default 60549086): +4G
 
Created a new partition 2 of type 'Linux filesystem' and of size 4 GiB.

完成后,输入t设置分区类型,2选择刚刚创建的分区,然后输入 19 设置分区类型为 "Linux Swap"。

Command (m for help):t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 19
 
Changed type of partition 'Linux filesystem' to 'Linux swap'.

Optionally, to have the swap partition conform to the Discoverable System Partition (DSP) specification, switch to expert mode and perform the following extra step to set the partition's UUID:

Command (m for help):x
Expert command (m for help):u
Partition number (1,2, default 2): 2
Selected partition 2
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
New UUID (in 8-4-4-4-12 format): 0657fd6d-a4ab-43c4-84e5-0933c84b4f4f
Partition UUID changed from 7529CDF6-9482-4497-B021-576745648B2A to 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F..

Press the r key to return to the main menu:

Expert command (m for help):r
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Command (m for help):

创建根分区

最后,要创建根分区,请输入 n 以创建新分区。然后输入 3 创建第三个分区,/dev/sda3。当提示输入第一个扇区时,按 Enter。当提示输入最后一个扇区时,按 Enter 以创建一个分区,该分区占用磁盘上的其余剩余空间。完成这些步骤后,输入 p 应该会显示一个类似于以下内容的分区表:

Command (m for help):n
Partition number (3-128, default 3): 3
First sector (10487808-1953525134, default 10487808):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-1953525134, default 1953523711):
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Created a new partition 3 of type 'Linux filesystem' and of size 926.5 GiB..
附注
Setting the root partition's type to "Linux root (x86-64)" is not required and the system will function normally if it is set to the "Linux filesystem" type. This filesystem type is only necessary for cases where a bootloader that supports it (i.e. systemd-boot) is used and a fstab file is not wanted.

After creating the root partition, press t to set the partition type, 3 to select the partition just created, and then type in 23 to set the partition type to "Linux Root (x86-64)".

Command(m for help):t
Partition number (1-3, default 3): 3
Partition type or alias (type L to list all): 23
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Changed type of partition 'Linux filesystem' to 'Linux root (x86-64)'

Optionally, to have the root partition conform to the Discoverable System Partition (DSP) specification, switch to expert mode and perform the following extra step to set the partition's UUID:

Command (m for help):x
Expert command (m for help):u
Partition number (1-3, default 3): 3
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
New UUID (in 8-4-4-4-12 format): 4f68bce3-e8cd-4db1-96e7-fbcaf984b709
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Partition UUID changed from 40465382-FA2A-4846-9827-640821CC001F to 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709.

Press the r key to return to the main menu:

Expert command (m for help):r
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Command (m for help):

After completing these steps, pressing p should display a partition table that looks similar to the following:

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 87EA4497-2722-DF43-A954-368E46AE5C5F
 
Device       Start      End  Sectors  Size Type
/dev/sda1     2048   526335   524288    1G EFI System
/dev/sda2   526336  8914943  8388608    4G Linux swap
/dev/sda3  8914944 60549086 51634143 24.6G Linux filesystem

Device Start End Sectors Size Type /dev/sda1 2048 2099199 2097152 1G Linux filesystem /dev/sda2 2099200 10487807 8388608 4G Linux swap /dev/sda3 10487808 1953523711 1943035904 926.5G Linux root (x86-64)

Filesystem/RAID signature on partition 1 will be wiped.

}}

保存分区布局

要保存分区布局并退出 fdisk,请敲击 w

Command (m for help):w

当分区创建完成后,就该在其上部署文件系统了。

使用 MBR 对磁盘进行分区以用于 BIOS/legacy 启动

下面解释了如何为 MBR/BIOS 传统引导安装创建示例分区布局。前面提到的示例分区布局现在是:

设备路径 (sysfs) 挂载点 文件系统 DPS UUID (PARTUUID) 描述
/dev/sda1 /boot xfs N/A MBR DOS / 传统 BIOS 引导分区详情。
/dev/sda2 N/A. 交换分区并不像设备文件那样挂载到文件系统。 swap 0657fd6d-a4ab-43c4-84e5-0933c84b4f4f 交换分区详情。
/dev/sda3 / xfs 4f68bce3-e8cd-4db1-96e7-fbcaf984b709 根分区详情。

根据个人喜好更改分区布局。

查看当前分区布局

针对磁盘启动 fdisk(在我们的示例中,我们使用 /dev/sda):

root #fdisk /dev/sda

输入p显示磁盘的当前分区配置:

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 21AAD8CF-DB67-0F43-9374-416C7A4E31EA
 
Device        Start      End  Sectors  Size Type
/dev/sda1      2048   526335   524288    1G EFI System
/dev/sda2    526336  2623487  2097152    1G Linux swap
/dev/sda3   2623488 19400703 16777216    8G Linux filesystem
/dev/sda4  19400704 60549086 41148383 19.6G Linux filesystem

Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 2099199 2097152 1G 83 Linux /dev/sda2 2099200 10487807 8388608 4G 82 Linux swap / Solaris /dev/sda3 10487808 1953525167 1943037360 926.5G 83 Linux

}}

直到现在,这个特定的磁盘被配置为使用 GPT 表容纳两个 Linux 文件系统(每个都有一个相应的分区列为 "Linux")以及一个交换分区(列为 "Linux swap")。

创建一个新的磁盘标签/删除所有分区

输入 o 在磁盘上创建一个新的 MBR 磁盘标签(这里也称为 DOS 磁盘标签);这将删除所有现有分区。

Command (m for help):o
Created a new DOS disklabel with disk identifier 0xe04e67c4.
The device contains 'gpt' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.

对于现有的 DOS 磁盘标签(参见上面 p 的输出),或者考虑从磁盘中一一删除现有分区。输入 d 删除分区。例如,要删除现有的 /dev/sda1


Command (m for help):d
Partition number (1-4): 1

该分区现已计划删除。打印分区列表时将不再显示 (p,但在保存更改之前它不会被删除。如果发生错误,用户可以中止操作 —— 在这种情况下, 立即输入 q 并按 Enter 不会删除分区。

重复输入 p 打印出一个分区列表,然后输入 d 和分区号来删除它。最终,分区表将为空:

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe04e67c4

现在我们已准备好创建分区。

创建引导分区

首先,创建一个将挂载到 /boot 的小分区。输入 n 创建一个新分区,然后输入 p 作为主分区,输入 1 选择第一个主分区。当提示输入第一个扇区时,确保它从 2048(引导加载程序可能需要)开始并按 Enter。当提示输入最后一个扇区时,输入 +1G 创建一个大小为 1 GB 的分区:

Command (m for help):n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-60549119, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60549119, default 60549119): +1G
 
Created a new partition 1 of type 'Linux' and of size 1 GiB.

Created a new partition 1 of type 'Linux' and of size 1 GiB.

}}

Mark the partition as bootable by pressing the a key and pressing Enter:

Command (m for help):a
Selected partition 1
The bootable flag on partition 1 is enabled now.

Note: if more than one partition is available on the disk, then the partition to be flagged as bootable will have to be selected.

创建 swap 分区

接下来,要创建交换分区,输入 n 创建一个新分区,然后输入 p,然后输入 2 创建第二个主分区,/dev/sda2。当提示输入第一个扇区时,按 Enter。当提示输入最后一个扇区时,输入 +4G(或交换空间所需的任何其他大小)以创建大小为 4GB 的分区。

Command (m for help):n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (526336-60549119, default 526336): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (526336-60549119, default 60549119): +4G
 
Created a new partition 2 of type 'Linux' and of size 4 GiB.

完成后,输入t设置分区类型,输入2选择刚刚创建的分区,然后输入 82 设置分区类型为 "Linux Swap"。

Command (m for help):t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

创建根分区

最后,要创建根分区,请输入 n 以创建新分区。然后输入 p3 以创建第三个主分区 /dev/sda3。当提示输入第一个扇区时,按 Enter。当提示输入最后一个扇区时,按 Enter 以创建一个分区,该分区占用磁盘上的剩余空间。完成这些步骤后,输入 p 应该会显示一个类似于以下内容的分区表:

Command (m for help):n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (10487808-1953525167, default 10487808):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-1953525167, default 1953525167):
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
Created a new partition 3 of type 'Linux' and of size 926.5 GiB.

After completing these steps, pressing p should display a partition table that looks similar to this:

Command (m for help):p
Disk /dev/sda: 28.89 GiB, 31001149440 bytes, 60549120 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe04e67c4
 
Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1          2048   526335   524288    1G 83 Linux
/dev/sda2        526336  8914943  8388608    4G 82 Linux swap / Solaris
/dev/sda3       8914944 60549119 51634176 24.6G 83 Linux

Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 2099199 2097152 1G 83 Linux /dev/sda2 2099200 10487807 8388608 4G 82 Linux swap / Solaris /dev/sda3 10487808 1953525167 1943037360 926.5G 83 Linux

}}

保存分区布局

要保存分区布局并退出 fdisk,输入 w

Command (m for help):w

现在是时候将文件系统应用在分区上了。


创建文件系统

警告
如果使用 SSD 或者 NVMe 驱动,请检查是否需要升级固件。特别是有些英特尔的 SSD(600p 和 6000p)需要升级固件来修复关键错误,以避免XFS I/O 使用模式导致的数据损坏。(尽管这并非文件系统的问题)。smartctl 可以检查型号和固件版本。

介绍

现在分区已经创建,该在上面设置文件系统了。下一章节中描述了 Linux 所支持的众多文件系统。知道使用哪一个文件系统的读者可以继续阅读为分区应用文件系统。剩下的人应该学习可用的文件系统……

文件系统

Linux 支持数十种文件系统,尽管其中许多只是为了特定目的而部署。在 amd64 架构上只能找到某些稳定的文件系统 - 建议在为重要分区选择更具实验性的文件系统之前查阅文件系统及其支持状态。XFS 是推荐的全平台通用的文件系统。下面是一个不完全的文件系统列表:

btrfs
下一代文件系统,提供许多高级功能,例如快照、通过校验和进行自我修复、透明压缩、子卷和集成 RAID。不能保证 5.4.y 之前的内核在生产中与 btrfs 一起使用是安全的,因为对严重问题的修复仅存在于 LTS 内核分支的最新版本中。文件系统损坏问题在较旧的内核分支上很常见,比如说启用压缩后,较旧的内核(比 5.4.y 小)更可能发生损坏。 RAID 5/6 和配额组在所有版本的 btrfs 上都不安全。
ext4
Ext4 是一个可靠的、通用的全平台文件系统,但是它缺乏诸如引用链接之类的现代化功能。
f2fs
Flash-Friendly File System 最初是由三星为与 NAND 闪存一起使用而创建的。截至 2016 年第二季度,这个文件系统仍然被认为是不成熟的,但在将 Gentoo 安装到 microSD 卡、USB 驱动器或其他基于闪存的存储设备上时,它是一个不错的选择。
XFS
具有元数据日志的文件系统,具有强大的功能集并针对可扩展性进行了优化。 XFS 不断升级来支持现代化功能。 XFS 唯一的缺点是还不能缩减分区,不过这个问题正在解决中。XFS 特别支持反向链接和写入时复制(CoW),因为用户需要完成大量编译工作,所以对 Gentoo 系统很有用。推荐使用 XFS 这个现代化的通用全平台文件系统。XFS 要求分区至少300MB。
VFAT
也称为 FAT32,Linux 支持但不支持标准的 UNIX 权限设置。它主要用于与其他操作系统(Microsoft Windows 或 Apple 的 macOS)的互操作和交换,但也是某些系统引导加载程序固件(如 UEFI)的必需品。UEFI 系统的用户需要使用 VFAT 格式化的 EFI 系统分区才能启动。
NTFS
这个“新技术”文件系统是自 Windows NT 3.1 以来 Microsoft Windows 的旗舰文件系统。与 vfat 类似,它不存储 BSD 或 Linux 正常运行所需的 UNIX 权限设置或扩展属性,大多数情况下,不应将其用作为根文件系统。它应该用于与Microsoft Windows 系统的互操作和数据交换(注意强调)。

更多有关于文件系统的信息可以在社区维护的 文件系统文章 中找到。

为分区应用文件系统

附注
安装结束后,请确保在重新启动之前,为之后在手册中选择的文件系统 emerge 相应软件包。

在一个分区或卷上创建一个文件系统,这里有用于每一个可能的分区的工具。 单击下表中的文件系统名称,了解每个文件系统的更多信息:

文件系统 创建命令 是否在最小化 CD 中已包含? 软件包
btrfs mkfs.btrfs sys-fs/btrfs-progs
ext4 mkfs.ext4 sys-fs/e2fsprogs
f2fs mkfs.f2fs sys-fs/f2fs-tools
xfs mkfs.xfs sys-fs/xfsprogs
vfat mkfs.vfat sys-fs/dosfstools
NTFS mkfs.ntfs sys-fs/ntfs3g
重要
The handbook recommends new partitions as part of the installation process, but it is important to note running any mkfs command will erase any data contained within the partition. When necessary, ensure any data that exists within is appropriately backed up before creating a few filesystem.

比如,在示例分区结构中,有使用 FAT32 的引导分区(/dev/sda1)和使用 xfs 的根分区(/dev/sda3),会使用下面的命令:

root #mkfs.xfs /dev/sda3

EFI system partition filesystem

The EFI system partition (/dev/sda1) must be formatted as FAT32:

root #mkfs.vfat -F 32 /dev/sda1

Legacy BIOS boot partition filesystem

Systems booting via legacy BIOS with a MBR/DOS disklabel can use any filesystem format supported by the bootloader.

For example, to format with XFS:

root #mkfs.xfs /dev/sda1

Small ext4 partitions

如果在较小的分区(少于8 GiB)上使用 ext4,则创建文件系统时必须带适当的选项以保留足够的 inode。可以使用以下命令创建文件系统:

root #mkfs.ext4 -T small /dev/<device>

这一般将是对于给定的文件系统inode数量的四倍,它的“字节每inode”从16kB每个减少到4kB每个。

激活 swap 分区

mkswap是用来初始化swap分区的命令:

root #mkswap /dev/sda2

要激活swap分区,使用swapon

root #swapon /dev/sda2

This 'activation' step is only necessary because the swap partition is newly created within the live environment. Once the system has been rebooted, as long as the swap partition is properly defined within fstab or other mount mechanism, swap space will activate automatically.

挂载 root 分区

附注
Installations which were previously started, but did not finish the installation process can resume the installation from this point in the handbook. Use this link as the permalink: Resumed installations start here.
提示
非 Gentoo 安装介质的用户需要通过运行以下命令来创建挂载点:
root #mkdir --parents /mnt/gentoo
root #mkdir --parents /mnt/gentoo

For EFI installs only, the ESP should be mounted under the root partition location:

root #mkdir --parents /mnt/gentoo/efi

Continue creating additional mount points necessary for any additional (custom) partition(s) created during previous steps by using the mkdir command.

现在分区都已初始化并有文件系统,接下来该挂载那些分区了。使用 mount 命令,但是不要忘记为每一个创建的分区创建需要的挂载目录。比如示例中我们挂载根分区:

Mount the root partition:

root #mount /dev/sda3 /mnt/gentoo

Continue mounting additional (custom) partitions as necessary using the mount command.

附注
如果/tmp/需要放在一个独立分区,确保在挂载后变更它的权限:
root #chmod 1777 /mnt/gentoo/tmp
这同样适用于/var/tmp

后面的介绍中将挂载proc文件系统(一个内核的虚拟接口)和其它内核伪文件系统。不过我们首先安装 Gentoo 安装文件





选择 stage 文件

提示
On supported architectures, it is recommended for users targeting a desktop (graphical) operating system environment to use a stage file with the term desktop within the name. These files include packages such as sys-devel/llvm and dev-lang/rust-bin and USE flag tuning which will greatly improve install time.

The stage file acts as the seed of a Gentoo install. Stage files are generated with Catalyst by the Release Engineering Team. Stage files are based on specific profiles, and contain an almost-complete system.

When choosing a stage file, it's important to pick one with profile targets corresponding to the desired system type.

附注
从技术上来说,可以将正在安装的 Gentoo 从 OpenRC 切换到 systemd,然后再切换回来。然而,切换需要花费一些功夫,而且超出了安装手册的范围。在下载 stage tarball 之前,请决定是否使用 OpenRC 或 systemd 作为目标 init 系统并下载相关的 stage tarball。
提示
大多数用户应该不需要使用“高级”tar包选项;它们用于一些不常见的和高级的软件或硬件配置。

OpenRC

OpenRC 是一个基于依赖的 init 系统(负责在内核启动后启动系统服务),OpenRC 与系统提供的 init 程序保持兼容,通常在 /sbin/init。它是 Gentoo 原创的 init 系统,但也部署在一些其它的 Linux 发行版和 BSD 系统。

默认情况下,OpenRC 不能替代 /sbin/init 文件 以及 100% 兼容 Gentoo init 脚本。这意味着可以在 Gentoo ebuild 仓库找到运行多个守护进程的解决方案。

systemd

systemd 是Linux 系统上一个现代化的 SysV 风格的 init 以及 rc 替代方案。大多数 Linux 发行版使用 systemd 作为主要的 init 系统。Gentoo 全面支持 systemd,并且已经达到预期。如果手册中似乎缺少 systemd 安装路径的内容,在寻求支持之前,请查看 systemd 文章

multilib(32 位和 64 位)

附注
不是所有架构都有 multilib 选项。许多只使用原生代码运行。Multilib 最常应用于amd64

选择一个基础压缩包的系统可以在稍后的安装过程节省大量的时间,特别是当它是一次选择正确的配置文件。stage 包的选择将直接影响未来的系统配置,可以在以后省的头痛。该压缩包 multilib 尽可能使用64位的库,只必要时对32位版本兼容。这对于大多数安装一个很好的选择,因为它在未来的定制提供了极大的灵活性量。那些谁希望自己的系统,能够容易地切换配置,应该下载根据各自的处理器架构 multilib的压缩包选项。

提示
Using multilib targets makes it easier to switch profiles later, compared to no-multilib

no-multilib(纯64位)

警告
刚开始使用 Gentoo 的读者,除非很有必要,否则应该选择 no-multilib 包。把一个系统从 no-multilib 迁移到 multilib 需要极其丰富的使用Gentoo 的知识并熟悉底层的工具链。这一做法甚至可能使工具链开发者不寒而栗。因此不适合胆小的用户,而且也超出了本指南的范围。

选择 no-multilib 压缩包可以在系统的基础上提供完整的 64 位操作系统环境(不包含 32 位的软件)。尽管技术上可行,但这样会使切换到 multilib 配置文件的工作变得很复杂。

下载 stage 文件

设置日期和时间

Stage archives are generally obtained using HTTPS which requires relatively accurate system time. Clock skew can prevent downloads from working, and can cause unpredictable errors if the system time is adjusted by any considerable amount after installation.

可以使用 date 验证当前时间和日期:

root #date
Mon Oct  3 13:16:22 PDT 2021

如果显示的日期/时间超时几分钟,则应使用下列方法之一进行精准更新。

自动

Using NTP to correct clock skew is typically easier and more reliable than manually setting the system clock.

chronyd, part of net-misc/chrony can be used to update the system clock to UTC with:

root #chronyd -q
重要
Systems without a functioning Real-Time Clock (RTC) must sync the system clock at every system start, and on regular intervals thereafter. This is also beneficial for systems with a RTC, as the battery could fail, and clock skew can accumulate.
警告
Standard NTP traffic not authenticated, it is important to verify time data obtained from the network.

手动

When NTP access is unavailable, date can be used to manually set the system clock.

建议所有 Linux 系统使用 UTC 时间。稍后在安装期间将定义时区,这将修改时钟的显示为本地时间。

以下参数格式用于设置时间:MMDDhhmmYYYY 语法(Month(月),Day(天),hour(小时),minute(分钟) 和 Year(年))。

比如,设置时间到 2021 年 10 月 3 日的 13:16,示例:

root #date 100313162021

前往挂载根文件系统的 Gentoo 挂载点(类似于 /mnt/gentoo):

root #cd /mnt/gentoo

图形化浏览器

可以使用图形化网页浏览器从主网站下载小节复制 stage 文件 URL。只需选择适当的选项卡,右键单击 stage 文件的链接,然后复制链接到剪贴板,然后将链接粘贴到命令行中的 wget 程序来下载 stage 文件:

root #wget <PASTED_STAGE_FILE_URL>

命令行浏览器

更多传统的读者或是 Gentoo 的“老前辈”专门使用命令行工作,他们可能更喜欢使用非图形化菜单驱动的浏览器 linkswww-client/links)。 要下载一个 stage,请像下面这样访问Gentoo镜像列表:

root #links https://www.gentoo.org/downloads/mirrors/

要设置links使用一个HTTP代理,在传入URL上加一个-http-proxy选项:

root #links -http-proxy proxy.server.com:8080 https://www.gentoo.org/downloads/mirrors/

links之外还有一个 lynxwww-client/lynx)浏览器。和links 类似,它也是一个非图形化的浏览器,但不是自带的。

root #lynx https://www.gentoo.org/downloads/mirrors/

如果需要定义一个代理,设置http_proxy和/或ftp_proxy变量:

root #export http_proxy="http://proxy.server.com:port"
root #export ftp_proxy="http://proxy.server.com:port"

在镜像列表中,选择一个附近镜像站。通常HTTP镜像站就足够了,但其他网络协议是可用的。请访问releases/amd64/autobuilds/ 。 那里将显示所有可用stage文件 (可能他们个别小组架构在命名的子目录中存储)。选择一个,然后按 d 下载。

stage 文件下载完成后,可以验证 stage 文件的完整性并验证其内容。感兴趣的人应该进行 下一节

对验证 stage 文件不感兴趣的用户可以通过按 q 来关闭命令行浏览器,并且可以直接移步到 解包 stage 文件 部分。

验证

附注
大多数 stages 现在明确带有 init 系统类型 (openrcsystemd)后缀,尽管某些架构现在可能仍然缺少后缀。

与最小安装CD一样,可以使用额外的下载来验证stage文件。 虽然这些步骤可以被跳过,但这些文件是为那些关心他们刚刚下载的文件合法性的用户提供的。

root #wget https://distfiles.gentoo.org/releases/
  • A .CONTENTS 文件包含stage压缩包内的所有文件的列表。
  • A .DIGESTS 文件,其中包含用不同的算法校验的stage文件。

使用 openssl,并且比较输出与 .DIGESTS 文件提供的校验和。

使用 openssl 验证 SHA512 校验值:

root #openssl dgst -r -sha512 stage3-amd64-<release>-<init>.tar.xz

dgst instructs the openssl command to use the Message Digest sub-command, -r prints the digest output in coreutils format, and -sha512 selects the SHA512 digest.

使用 openssl 验证 BLAKE2B512 校验值:

root #openssl dgst -r -blake2b512 stage3-amd64-<release>-<init>.tar.xz

将这些命令的输出与.DIGESTS 文件中的值进行对比。对应的值应该相同,否则下载的文件(或 digests 文件)可能已损坏。

使用sha512sum命令的另外一种方式:

root #sha256sum --check stage3-amd64-<release>-<init>.tar.xz.sha256

The --check option instructs sha256sum to read a list of expected files and associated hashes, and then print an associated "OK" for each file that calculates correctly or a "FAILED" for files that do not.

就像在 ISO 文件中一样,它也可以来验证 tar.xz 文件的加密签名。使用 gpg 确保压缩包未被篡改:

For official Gentoo live images, the sec-keys/openpgp-keys-gentoo-release package provides PGP signing keys for automated releases. The keys must first be imported into the user's session in order to be used for verification:

root #gpg --import /usr/share/openpgp-keys/gentoo-release.asc

For all non-official live images which offer gpg and wget in the live environment, a bundle containing Gentoo keys can be fetched and imported:

root #wget -O - https://qa-reports.gentoo.org/output/service-keys.gpg | gpg --import

Verify the signature of the tarball and, optionally, associated checksum files:

root #gpg --verify stage3-amd64-<release>-<init>.tar.xz.asc
root #gpg --verify stage3-amd64-<release>-<init>.tar.xz.DIGEST
root #gpg --verify stage3-amd64-<release>-<init>.tar.xz.sha256

If verification succeeds, "Good signature from" will be in the output of the previous command(s).

OpenPGP 密钥的指纹用来对释出的安装媒介签名,在 发行的安装媒介签名页面 上可以找到 OpenPGP 密钥的指纹。

安装 stage 文件

下载玩 stage 文件并且验证之后,可以使用 tar 提取 stage 文件:

root #tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner

验证你在命令中使用了同样的参数 ( xpf--xattrs-include='*.*')。 x表示解开(Extract),v表示详细信息(Verbose)可以用来查看解压缩时发生了什么(可选参数), p 表示保留权限(Preserve permissions),还有f 表示我们要解开一个文件,而不是标准输入。最后,--numeric-owner 被用于确保从tarball中提取的文件的用户和组ID与Gentoo发布工程团队预期的保持一致,即使大胆的用户使用的不是 Gentoo live 环境。

  • x extract, instructs tar to extract the contents of the archive.
  • p preserve permissions.
  • v verbose output.
  • f file, provides tar with the name of the input archive.
  • --xattrs-include='*.*' Preserves extended attributes in all namespaces stored in the archive.
  • --numeric-owner Ensure that the user and group IDs of files being extracted from the tarball remain the same as Gentoo's release engineering team intended (even if adventurous users are not using official Gentoo live environments for the installation process).

现在stage文件已经解压好了,下面我们继续配置编译选项

配置编译选项

介绍

优化系统时,可以设置影响 Portage 的变量,Gentoo 官方支持包管理器。 所有这些变量可以设置为环境变量(使用export),但通过 export 设置不是永久的。

附注
从技术上来说,可以通过 shell 的 配置文件或 rc 文件来导出变量,但这不是基本系统管理的最佳实践。

Portage 在运行时会读取 make.conf 文件,将根据文件中保存的值更改运行时行为。make.conf 可以认为是 Portage 的主要配置文件,因此请谨慎处理其内容。

提示
所有可能的变量的注释列表可以在 /mnt/gentoo/usr/share/portage/config/make.conf.example中找到。额外的关于 make.conf 文档可以运行 man 5 make.conf 查找。

要成功安装 Gentoo,只需设置下面提到的变量。

启动编辑器(在本指南中,我们使用 nano)来更改我们将在下面讨论的优化变量。

root #nano /mnt/gentoo/etc/portage/make.conf

make.conf.example文件中可以明显看出文件的结构:注释行以 # 开头,其他行使用 VARIABLE="value" 语法定义变量。接下来选取其中的几个进行讨论。

CFLAGS 和 CXXFLAGS

CFLAGSCXXFLAGS 变量分别定义了GCC C和C ++编译器的优化标志。 尽管这些标志一般在这里默认被定义过,但为了性能最大化,需要分别优化每个程序的这些配置。 原因是因为每个程序都不同。 但是,这是不可管理的,因此这些标志在 make.conf 文件中定义。

应该在make.conf中定义优化标志,这将使系统的响应速度最快。 不要在此变量中放置实验性的设置; 太多的优化可能会使程序产生问题(崩溃,甚至更糟,产生故障)。

我们不会解释所有可能的优化选项。 要了解它们,请阅读GNU在线手册或 gcc 信息页面 (info gcc)。make.conf.example 文件本身也包含了很多例子和信息; 不要忘了读它。

第一个设置是标志 -march=-mtune= ,指定目标体系结构的名称。 可能用到的选项在make.conf.example文件中有描述(作为注释)。 一个常用的值是“native”,它告诉编译器选择当前系统体系结构(用户正在安装Gentoo时的系统)。

第二个是标志 -O(即大写的字母O,而不是数字零),它指定了gcc优化级别标志。 可能用到级别的是s(对于大小最优化),0(零 - 无优化),1,2或甚至3等更多的优化选项(每个级别具有与前面相同的标志,加上一些额外选项)。 -O2是建议的默认值。 -O3在整个系统范围内使用时会导致问题,因此我们建议您坚持使用-O2

另一个普遍使用的优化标记是-pipe(不同编译阶段通信使用管道而不是临时文件)。它对产生的代码没有任何影响,但是会使用更多的内存。在内存不多的系统里,gcc可能会被杀掉。如果是那样的话,就不要用这个标记。

使用 -fomit-frame-pointer(它将不在寄存器里为不需要帧指针的函数保存帧指针)可能会在调试程序的时候造成严重后果!

在你定义 CFLAGSCXXFLAGS的时候,你需要把这些优化标记都合并起来。stage3文件里包含的你解压缩出来的默认值已经足够好了。下面这个例子仅仅是个例子:

代码 CFLAGSCXXFLAGS 变量示例
# 为所有语言设置编译标志
COMMON_FLAGS="-march=native -O2 -pipe"
# 为两个变量使用相同的设置
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
提示
通过 GCC 优化指导这篇文章获取有更多的信息,比如这些优化变量如何影响你的系统,Safe CFLAGS 也许是对初学者开始优化系统更实用的一篇文章

MAKEOPTS

通过使用 MAKEOPTS 可以定义在安装软件时,并行编译的数目。在 3.0.31 版本的 portage[1] 中,如果未定义该值, portage 默认的行为是,设置 MAKEOPTS 的值与 nproc 的返回线程数相同。

Further, as of Portage 3.0.53[2], if left undefined, Portage's default behavior is to set the MAKEOPTS load-average value to the same number of threads returned by nproc.

比较好的选择是从 CPU 线程数,或系统 RAM 总量除以 2 GiB 中选择是较小的那个值。

警告
大量的 job 显著消耗内存。建议每个 job 至少有 2 GiB RAM (所以,-j6 至少 需要 12 GiB)。避免内存溢出,根据可用内存降低 job 数量。
提示
当并行使用 emerge (--jobs),有效的 job 数量可以指数式加速(通过 emerge jobs 使 job 增加)。可以通过运行一个仅限于本地主机的 distcc 配置来解决这个问题,该配置将限制每个主机的编译器实例数量。
文件 /etc/portage/make.conf声明 MAKEOPTS 示例
# 如果未定义该值,portage 默认的行为是,设置 MAKEOPTS 的值与 `nproc` 的返回线程数相同。
MAKEOPTS="-j4"

man 5 make.conf 查找 MAKEOPTS 更多细节。

就位,预备,出发 !

根据你的喜好更新并保存 /mnt/gentoo/etc/portage/make.conf(nano 用户可以敲 Ctrl+o 可以保存更改,然后 Ctrl+x 退出)。

References




Chrooting

复制DNS信息

在进行新环境之前,还有一件要做的事情就是复制/etc/resolv.conf中的DNS信息。需要完成这个来确保即使进入到新环境后网络仍然可以使用。/etc/resolv.conf包含着当前网络中的DNS服务器。

要复制这个信息,建议通过cp命令的 --dereference 选项。这可以保障如果/etc/resolv.conf是一个符号链接的话,复制的是那个目标文件而不是这个符号文件自己。否则在新环境中,符号文件将指向一个不存在的文件(因为链接目标非常可能不会在新环境中)。

root #cp --dereference /etc/resolv.conf /mnt/gentoo/etc/

挂载必要的文件系统

稍等片刻,Linux 的根目录将变更到新的位置。

需要提供的文件系统是:

  • /proc/ 是一个伪文件系统,虽然它看起来像是常规文件,但是却是由 Linux 内核生成
  • /sys/ 是一个伪文件系统,它类似被取代的 /proc/,但是比 /proc/ 更结构化
  • /dev/ 是一个包含全部设备文件的常规文件系统,一部分由Linux设备管理器(通常是udev)管理
  • /run/ 是一个临时文件系统,用于运行时生成的文件,例如 PID 文件和锁

/proc/位置将要挂载到/mnt/gentoo/proc/,而其它的都是绑定挂载。字面上的意思是,例如/mnt/gentoo/sys/事实上就是/sys/(它只是同一个文件系统的第二个条目点),而/mnt/gentoo/proc/是(可以说是)文件系统的一个新的挂载。

提示
If using Gentoo's install media, this step can be replaced with simply: arch-chroot /mnt/gentoo.
root #mount --types proc /proc /mnt/gentoo/proc
root #mount --rbind /sys /mnt/gentoo/sys
root #mount --make-rslave /mnt/gentoo/sys
root #mount --rbind /dev /mnt/gentoo/dev
root #mount --make-rslave /mnt/gentoo/dev
root #mount --bind /run /mnt/gentoo/run
root #mount --make-slave /mnt/gentoo/run
附注
--make-rslave操作是稍后安装systemd支持时所需要的。
警告
当使用非Gentoo安装媒介时,这时可能还不算完。一些发行版将/dev/shm符号链接到/run/shm/,在chroot后将变得不可用。为了让/dev/shm/是一个正常挂载的tmpfs,可以这样修复:
root #test -L /dev/shm && rm /dev/shm && mkdir /dev/shm
root #mount --types tmpfs --options nosuid,nodev,noexec shm /dev/shm

同时确保设置了权限为1777:

root #chmod 1777 /dev/shm /run/shm

进入新环境

现在所有的分区已经初始化,并且基础环境已经安装,是时候进入到新的安装环境了。这意思着会话将把根目录(能访问到最顶层的位置)从当前的安装环境(安装CD或其他安装媒介)变为安装系统(叫做初始化分区)。因此叫作 change rootchroot

完成chroot有三个步骤:

  1. 如果可以的话,使用 chroot 或 arch-chroot 将根目录的位置从 /(在安装媒介里)更改成 /mnt/gentoo/ (在分区里)
  2. 使用 source 命令将一些设置(那些在 /etc/profile 中的)重新载入到内存中
  3. 更改主提示符来帮助我们记住当前会话在一个 chroot 环境里面。
root #chroot /mnt/gentoo /bin/bash
root #source /etc/profile
root #export PS1="(chroot) ${PS1}"

从现在开始,所有的动作将立即在新 Gentoo Linux 环境里生效。

提示
如果安装Gentoo时在这一步之后的任何地方中断,那么“应该”可以从这一步“继续”安装。不必再重新给磁盘分区!只需要挂载 root 分区 并运行上述步骤,然后通过复制 DNS 信息重新进入工作环境。 这也对修复引导程序问题很有用。更多的信息可以在 chroot 文章中找到。

Preparing for a bootloader

Now that the new environment has been entered, it is necessary to prepare the new environment for the bootloader. It will be important to have the correct partition mounted when it is time to install the bootloader.

UEFI 系统

For UEFI systems, /dev/sda1 was formatted with the FAT32 filesystem and will be used as the EFI System Partition (ESP). Create a new /efi directory (if not yet created), and then mount ESP there:

root #mkdir /efi
root #mount /dev/sda1 /efi

DOS/Legacy BIOS systems

For DOS/Legacy BIOS systems, the bootloader will be installed into the /boot directory, therefore mount as follows:

root #mount /dev/sda1 /boot

配置 Portage

Gentoo ebuild 软件仓库

选择镜像的第二个重要步骤是通过/etc/portage/repos.conf/gentoo.conf文件来配置 Gentoo的 ebuild 软件仓库。这个文件包含了更新 Portage 数据库(包含 Portage 需要下载和安装软件包所需要的信息的一个 ebuild 和相关文件的集合)所需要的同步信息。

通过几个简单的步骤就可以完成软件仓库的配置。首先,如果它不存在,则创建 repos.conf 目录:

root #mkdir --parents /mnt/gentoo/etc/portage/repos.conf

接下来,复制 Portage 提供的 Gentoo 仓库配置文件到这个(新创建的)目录:

root #cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf

使用一个文件编辑器或通过使用cat命令来看一眼。文件里的内容应该是.ini格式并且看起来像是这样:

文件 /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
[DEFAULT]
main-repo = gentoo
 
[gentoo]
location = /var/db/repos/gentoo
sync-type = rsync
sync-uri = rsync://rsync.gentoo.org/gentoo-portage
auto-sync = yes
sync-rsync-verify-jobs = 1
sync-rsync-verify-metamanifest = yes
sync-rsync-verify-max-age = 24
sync-openpgp-key-path = /usr/share/openpgp-keys/gentoo-release.asc
sync-openpgp-key-refresh-retry-count = 40
sync-openpgp-key-refresh-retry-overall-timeout = 1200
sync-openpgp-key-refresh-retry-delay-exp-base = 2
sync-openpgp-key-refresh-retry-delay-max = 60
sync-openpgp-key-refresh-retry-delay-mult = 4
sync-webrsync-verify-signature = yes
sync-git-verify-commit-signature = yes

上面列出的 sync-uri 变量默认值将根据轮询的确定一个镜像地址。这会缓解 Gentoo 基础设施上带宽的压力,并能在特定镜像离线时保证失效安全。除非使用本地或者私有的镜像,否则建议保留默认 URI。

提示
可以在 Portage 同步主题中找到关于 Portage 的同步API插件的官方规范。

从网站安装 Gentoo ebuild 数据库快照

接下来,是安装 Gentoo ebuild 数据库。这个快照包含一组文件,包括通知 Portage 中有关可用软件的标题(用于安装),系统管理员可以选择哪些配置文件,软件包或 profile 特定新闻 (news) 项目等。

建议那些使用限制性防火墙的用户使用 emerge-webrsync 命令(它使用 HTTP / FTP 协议下载快照)节省网络带宽。 没有网络或带宽限制的读者可以愉快地跳到下一节。

这将从Gentoo的一个镜像中获取最新的快照(每天发布)并将其安装到系统上:

root #emerge-webrsync
附注
在这个操作中,emerge-webrsync可能会报找不到 /var/db/repos/gentoo/ 位置。这是预期内的并且不用担心——这个工具将会创建这个位置。

从现在开始,Portage 可能会提示建议运行某些更新。这是因为在安装了一个新的repository 快照后,Portage 发现了 stage 文件中已经安装的某些软件包有更新的版本。现在可以安全的忽略包的更新;可以延迟到 Gentoo 安装完成之后更新。


可选:选择镜像站点

为了能更快的下载源代码,这里推荐选择一个快的镜像。Portage 将会在make.conf文件中查找GENTOO_MIRRORS变量,并使用其中所列的镜像。可以通过浏览 Gentoo 镜像列表搜索一个(或一组)最接近系统物理位置(往往那是最快的)的镜像。另外,我们提供一个叫作mirrorselect的好工具,它为用户选择所需镜像提供了一个很好的交换。只需要移动光标选择镜像并按Spacebar选择一个或多个镜像。

A tool called mirrorselect provides a pretty text interface to more quickly query and select suitable mirrors. Just navigate to the mirrors of choice and press Spacebar to select one or more mirrors.

root #mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf

Alternatively, a list of active mirrors are available online.

可选:更新Portage ebuild 数据库

Gentoo 数据库可以更新到最新版本。前面的emerge-webrsync命令将安装一个最近的快照(通常是24小时以内),所以这一步是可选的。

假设需要最新更新的软件包(1小时以内),可以使用emerge --sync。这个命令将使用rsync协议来更新 Gentoo ebuild 数据库(之前通过emerge-webrsync获得的)到最新状态。

root #emerge --sync

在慢速的终端上,比如一些framebuffer或者串口控制台,建议使用--quiet选项来加速这个进程:

root #emerge --sync --quiet

阅读新闻条目

当同步Portage ebuild 数据库时,Portage 可能会输出类似于下面的信息:

* IMPORTANT: 2 news items need reading for repository 'gentoo'.
* Use eselect news to read news items.

创建新闻条目是为了提供一个通信媒介,通过 Gentoo ebuild 数据库来给用户推送重要的消息。可以使用 eselect news 管理新闻条目。eselect 应用程序是一个Gentoo 特有的应用程序,它允许使用通用管理接口来管理系统。在这里,要用到 eselectnews 模块。

对于news模块,最常用的有三个操作:

  • 使用list显示一个可用新闻条目的预览。
  • 使用read来阅读新闻条目。
  • 使用purge将在新闻条目阅读后删除,并且不能再次阅读。
root #eselect news list
root #eselect news read

可以通过新闻阅读器手册页查看更多关于新闻阅读器的信息:

root #man news.eselect

选择正确的配置文件

提示
桌面配置文件并非只是 桌面环境。也可能是小的窗口管理器,例如 i3 或 sway。

配置文件是任何一个Gentoo系统的积木。它不仅指定USECFLAGS和其它重要变量的默认值,还会锁定系统的包版本范围。这些设定全是由Gentoo的Portage开发者们来维护。

运行 eselect 使用 profile模块,能看到当前系统正在使用什么配置文件:

root #eselect profile list
Available profile symlink targets:
  [1]   default/linux/amd64/23.0 *
  [2]   default/linux/amd64/23.0/desktop
  [3]   default/linux/amd64/23.0/desktop/gnome
  [4]   default/linux/amd64/23.0/desktop/kde
附注
命令的这个输出只是一个示例,并会随时间演变。
附注
若要使用 systemd, 请选择名称中包含 "systemd" 的配置文件。否则,选择相反的配置文件。

一些架构还会有桌面的子配置文件。

警告
升级 profile 不能掉以轻心。 选择初始 profile 时,使用与最初使用的 stage3 “相同的版本”(例如 23.0 )。 每个新的 profile 版本都通过新闻项目公布,新闻项目中包含了迁移说明。 在切换到较新的 profile 之前,请按照说明操作。

在看完框架的可用配置文件amd64之后,用户可以键入以下命令为系统选择一个不同的配置文件:

root #eselect profile set 2


No-multilib

若要选择没有 32 位应用和类库的纯 64 位环境,请使用 no-multilib 的配置文件:

root #eselect profile list
Available profile symlink targets:
  [1]   default/linux/amd64/23.0 *
  [2]   default/linux/amd64/23.0/desktop
  [3]   default/linux/amd64/23.0/desktop/gnome
  [4]   default/linux/amd64/23.0/desktop/kde
  [5]   default/linux/amd64/23.0/no-multilib

接下来选择no-multilib配置文件:

root #eselect profile set 5
root #eselect profile list
Available profile symlink targets:
  [1]   default/linux/amd64/23.0
  [2]   default/linux/amd64/23.0/desktop
  [3]   default/linux/amd64/23.0/desktop/gnome
  [4]   default/linux/amd64/23.0/desktop/kde
  [5]   default/linux/amd64/23.0/no-multilib *



附注
developer 子配置文件是专用于Gentoo Linux开发,也就是说不适用于普通用户。

Optional: Adding a binary package host

Since December 2023, Gentoo's Release Engineering team has offered an official binary package host (colloquially shorted to just "binhost") for use by the general community to retrieve and install binary packages (binpkgs).[1]

Adding a binary package host allows Portage to install cryptographically signed, compiled packages. In many cases, adding a binary package host will greatly decrease the mean time to package installation and adds much benefit when running Gentoo on older, slower, or low power systems.

Repository configuration

The repository configuration for a binhost is found in Portage's /etc/portage/binrepos.conf/ directory, which functions similarly to the configuration mentioned in the Gentoo ebuild repository section.

When defining a binary host, there are two important aspects to consider:

  1. The architecture and profile targets within the sync-uri value do matter and should align to the respective computer architecture (amd64 in this case) and system profile selected in the Choosing the right profile section.
  2. Selecting a fast, geographically close mirror will generally shorten retrieval time. Review the mirrorselect tool mentioned in the Optional: Selecting mirrors section or review the online list of mirrors where URL values can be discovered.

文件 /etc/portage/binrepos.conf/gentoobinhost.confCDN-based binary package host example
[binhost]
priority = 9999
sync-uri = https://distfiles.gentoo.org/releases/<arch>/binpackages/<profile>/x86-64/

Installing binary packages

Portage will compile packages from code source by default. It can be instructed to use binary packages in the following ways:

  1. The --getbinpkg option can be passed when invoking the emerge command. This method of for binary package installation is useful to install only a particular binary package.
  2. Changing the system's default via Portage's FEATURES variable, which is exposed through the /etc/portage/make.conf file. Applying this configuration change will cause Portage to query the binary package host for the package(s) to be requested and fall back to compiling locally when no results are found.

For example, to have Portage always install available binary packages:

文件 /etc/portage/make.confConfigure Portage to use binary packages by default
# Appending getbinpkg to the list of values within the FEATURES variable
FEATURES="${FEATURES} getbinpkg"
# Require signatures
FEATURES="${FEATURES} binpkg-request-signature"

Please also run getuto for Portage to set up the necessary keyring for verification:

root #getuto

Additional Portage features will be discussed in the the next chapter of the handbook.

配置 USE 变量

USE是Gentoo为用户提供的最具威力的变量之一。很多程序通过它可以选择编译或者不编译某些可选的支持。例如,一些程序可以在编译时加入对 GTK+或是对Qt的支持。其它的程序可以在编译时加入或不加入对于SLL的支持。有些程序甚至可以在编译时加入对framebuffer的支持(svgalib)以取代X11(X服务器)。

大多数的发行版会使用尽可能多的支持特性编译它们的软件包,这既增加了软件的大小也减慢了启动时间,而这些还没有算上可能会涉及到的大量依赖性问题。Gentoo可以让你自己定义软件编译的选项,而这正是USE要做的事。

USE变量里你可以定义关键字,它被用来对应相应的编译选项。例如,ssl将会把SSL支持编译到程序中以支持它。-X会移除其对于X服务器的支持(注意前面的减号)。gnome gtk -kde -qt5 将会以支持GNOME(和GTK+)但不支持KDE(和Qt)的方式编译软件,使系统为GNOME做完全调整(如果架构支持)。

默认的USE设置全放在了系统所使用的Gentoo配置文件的make.defaults文件中。Gentoo对它的配置文件们使用了一个(复杂的)继承系统,在这个阶段我们不去深入。最简单的检查当前活动的USE标记的办法是运行emerge --info并选择以USE开头的那一行:

root #emerge --info | grep ^USE
USE="X acl alsa amd64 berkdb bindist bzip2 cli cracklib crypt cxx dri ..."
附注
上面的示例被截断了,实际上的USE列表值是非常非常多的。

可以在系统的 /var/db/repos/gentoo/profiles/use.desc 中找到可用的USE标记的完整描述。

root #less /var/db/repos/gentoo/profiles/use.desc

less命令中,可以通过使用键来滚动,并且可以按q退出。

作为示例,我们展示一个支持DVD、ALSA,以及CD录制的基于KDE系统的USE设置:

root #nano /etc/portage/make.conf
文件 /etc/portage/make.conf为基于 KDE/Plasma 系统启用 DVD、ALSA 和 CD录制支持 flag
USE="-gtk -gnome qt5 kde dvd alsa cdr"

当在 /etc/portage/make.conf中定义一个 USE 值,会添加到系统 USE 标志中。USE 标志可以通过在列表内的值前面中添加 - 减号来全局移除。例如,禁用 X 图形化环境的支持,可以设置 -X

文件 /etc/portage/make.conf忽略默认USE标记
USE="-X acl alsa"
警告
尽管可以设置 -*(这将禁用除 make.conf 中指定的值之外的所有 USE 值),但这样做不推荐,也不理智。Ebuild 开发人员在 ebuild 中选择某些默认的 USE 标志值以防止冲突,增强安全性,避免错误,以及其他原因。禁用 所有 USE 标志将否定默认行为,并可能导致重大问题。

CPU_FLAGS_*

一些架构(包括 AMD64/X86、ARM、PPC)有称为 CPU_FLAGS_<ARCH>USE_EXPAND 变量,请将 <ARCH> 替换为相应的系统架构名称。

重要
不要糊涂! AMD64X86 系统有一些共同的架构,所以 AMD64 正确的变量名称是 CPU_FLAGS_X86

该变量用于构建编译特定的汇编代码或其他内置函数——通常是手写或其它的, 并且与要求编译器输出针对某个 CPU 功能的优化代码同。(例如 -march=

如果需要,用户除了配置他们的 COMMON_FLAGS ,还应该设置此变量。

需要几个步骤来设置它:

root #emerge --ask app-portage/cpuid2cpuflags

如果有疑问,请手动检查输出:

root #cpuid2cpuflags

然后复制输出到 package.use

root #echo "*/* $(cpuid2cpuflags)" > /etc/portage/package.use/00cpu-flags

VIDEO_CARDS

VIDEO_CARDS USE_EXPAND 变量应根据使用的 GPU 进行适当配置。仅控制台安装则不需​​要设置 VIDEO_CARDS。

Below is an example of a properly set VIDEO_CARDS variable. Substitute the name of the driver(s) to be used.

文件 /etc/portage/make.conf
VIDEO_CARDS="amdgpu radeonsi"

Details for various GPU(s) can be found at the AMDGPU, Intel, Nouveau (Open Source), or NVIDIA (Proprietary) articles.

可选:配置 ACCEPT_LICENSE 变量

Starting with Gentoo Linux Enhancement Proposal 23 (GLEP 23), a mechanism was created to allow system administrators the ability to "regulate the software they install with regards to licenses... Some want a system free of any software that is not OSI-approved; others are simply curious as to what licenses they are implicitly accepting."[2] With a motivation to have more granular control over the type of software running on a Gentoo system, the ACCEPT_LICENSE variable was born.

Portage 在 ACCEPT_LICENSE 中查找允许安装的软件包。打印当前系统范围的值运行:

user $portageq envvar ACCEPT_LICENSE
@FREE

在 Gentoo 仓库中定义的许可证组,由 Gentoo Licenses project 项目管理,有:

组别名称 描述
@GPL-COMPATIBLE 由自由软件基金会批准的 GPL 兼容许可 GPL [a_license 1]
@FSF-APPROVED 由 FSF 批准的自由软件许可证(包括@GPL-COMPATIBLE )
@OSI-APPROVED 由开放源代码促进会批准的许可证 [a_license 2]
@MISC-FREE Misc 许可证可能是自由软件,即遵循自由软件定义 [a_license 3] ,但不被 FSF 或 OSI 批准
@FREE-SOFTWARE 结合 @FSF-APPROVED,@OSI-APPROVED 和 @MISC-FREE
@FSF-APPROVED-OTHER 经 FSF 批准的“免费文档”和“除软件和文档外的实际使用作品”(包括字体)许可证
@MISC-FREE-DOCS 遵循自由定义的免费文档和其他作品(包括字体) [a_license 4]且没有在 @FSF-APPROVED-OTHE 中列出的杂项许可
@FREE-DOCUMENTS 结合 @FSF-APPROVED-OTHER 和 @MISC-FREE-DOCS
@FREE 所有许可证的 metaset,可以自由使用,共享,修改和共享修改。结合 @FREE-SOFTWARE 和 @FREE-DOCUMENTS
@BINARY-REDISTRIBUTABLE 至少允许以二进制形式自由分发软件的许可证。包括 @FREE
@EULA 试图剥夺您的权利的许可协议。与“保留所有权利”和需要明确的批准相比,@EULA 有更多的限制

Some common license groups include:

A list of software licenses grouped according to their kinds.
Name Description
@GPL-COMPATIBLE GPL compatible licenses approved by the Free Software Foundation [a_license 5]
@FSF-APPROVED Free software licenses approved by the FSF (includes @GPL-COMPATIBLE)
@OSI-APPROVED Licenses approved by the Open Source Initiative [a_license 6]
@MISC-FREE Misc licenses that are probably free software, i.e. follow the Free Software Definition [a_license 7] but are not approved by either FSF or OSI
@FREE-SOFTWARE Combines @FSF-APPROVED, @OSI-APPROVED, and @MISC-FREE.
@FSF-APPROVED-OTHER FSF-approved licenses for "free documentation" and "works of practical use besides software and documentation" (including fonts)
@MISC-FREE-DOCS Misc licenses for free documents and other works (including fonts) that follow the free definition [a_license 8] but are NOT listed in @FSF-APPROVED-OTHER.
@FREE-DOCUMENTS Combines @FSF-APPROVED-OTHER and @MISC-FREE-DOCS.
@FREE Metaset of all licenses with the freedom to use, share, modify and share modifications. Combines @FREE-SOFTWARE and @FREE-DOCUMENTS.
@BINARY-REDISTRIBUTABLE Licenses that at least permit free redistribution of the software in binary form. Includes @FREE.
@EULA License agreements that try to take away your rights. These are more restrictive than "all-rights-reserved" or require explicit approval

Currently set system wide acceptable license values can be viewed via:

user $portageq envvar ACCEPT_LICENSE
@FREE

As visible in the output, the default value is to only allow software which has been grouped into the @FREE category to be installed.

Specific licenses or licenses groups for a system can be defined in the following locations:

  • 整个系统的在已选择的 profile。
  • 整个系统的在 /etc/portage/make.conf
  • 每个软件包的在 /etc/portage/package.license 文件。
  • 每个软件包的在/etc/portage/package.license/ 目录 或文件。

可选择通过在配置文件中更改 /etc/portage/make.conf 覆盖系统范围默认接受项。

文件 /etc/portage/make.conf如何在系统范围接受 ACCEPT_LICENSE 许可证示例
ACCEPT_LICENSE="-* @FREE @BINARY-REDISTRIBUTABLE"

也可以选择为每个软件包定义接受的许可证,如下面的文件目录所示。需要注意的是,如果 package.license 目录不存在的话,需要创建该目录。

root #mkdir /etc/portage/package.license

Software license details for an individual Gentoo package are stored within the LICENSE variable of the associated ebuild. One package may have one or many software licenses, therefore it be necessary to specify multiple acceptable licenses for a single package.

文件 /etc/portage/package.license/kernel如何接受每个软件包的许可证示例
app-arch/unrar unRAR
sys-kernel/linux-firmware @BINARY-REDISTRIBUTABLE
sys-firmware/intel-microcode intel-ucode
重要
ebuild 中 的 LICENSE 变量仅是为 Gentoo 开发人员和用户准备的一份指南。它既不是法律声明,也不保证其真实性。因此不要过度依赖它,您需要深入检查软件包的本身,以及已经安装到系统的所有文件。

更新@world集合

当系统应用了任何升级,或从 任何profile 构建了stage3 后,应用了变化的 use 标记时,下一步是必要的。

  1. A profile target different from the stage file has been selected.
  2. Additional USE flags have been set for installed packages.

Readers who are performing an 'install Gentoo speed run' may safely skip @world set updates until after their system has rebooted into the new Gentoo environment.

Readers who are performing a slow run can have Portage perform updates for package, profile, and/or USE flag changes at the present time:

root #emerge --ask --verbose --update --deep --newuse @world

Removing obsolete packages

It is important to always depclean after system upgrades to remove obsolete packages. Review the output carefully with emerge --depclean --pretend to see if any of the to-be-cleaned packages should be kept if personally using them. To keep a package which would otherwise be depcleaned, use emerge --noreplace foo.

root #emerge --ask --pretend --depclean

If happy, then proceed with a real depclean:

root #emerge --ask --depclean
提示
如果选择了桌面环境配置文件,则此过程可能大大增加安装过程所需的时间量。时间紧迫的人可以通过这个“经验法则”工作:配置文件名称越短,系统 @world 集 越不具体; @world 集越不具体,系统将需要的软件包越少。换一种说法:
  • 选择 default/linux/amd64/23.0 将只有很少的包被重装或更新
  • 选择 default/linux/amd64/23.0/desktop/gnome/systemd 将需要安装许多软件包,因为 init 系统要从 OpenRC 更改为 systemd,并且将安装 GNOME 桌面环境框架。


时区

附注
这一步不适用于 musl libc 用户。不知道 musl libc 是什么的用户应该执行此步骤。

请避免使用 /usr/share/zoneinfo/Etc/GMT* 时区,它们的名字并不意味着想要的时区。例如,GMT-8 实际上是 GMT+8

为系统选择时区。在/usr/share/zoneinfo/中查找可用的时区,然后写进/etc/timezone文件。

root #ls /usr/share/zoneinfo
root #ls -l /usr/share/zoneinfo/Europe/
total 256
-rw-r--r-- 1 root root 2933 Dec  3 17:19 Amsterdam
-rw-r--r-- 1 root root 1742 Dec  3 17:19 Andorra
-rw-r--r-- 1 root root 1151 Dec  3 17:19 Astrakhan
-rw-r--r-- 1 root root 2262 Dec  3 17:19 Athens
-rw-r--r-- 1 root root 3664 Dec  3 17:19 Belfast
-rw-r--r-- 1 root root 1920 Dec  3 17:19 Belgrade
-rw-r--r-- 1 root root 2298 Dec  3 17:19 Berlin
-rw-r--r-- 1 root root 2301 Dec  3 17:19 Bratislava
-rw-r--r-- 1 root root 2933 Dec  3 17:19 Brussels
...

假设要选择的时区是 “Europe/Brussels”。

OpenRC

我们把时区名称写入 /etc/timezone 文件。

root #echo "Europe/Brussels" > /etc/timezone

接下来,重新配置 sys-libs/timezone-data 包,将会为我们基于 /etc/timezone 条目更新 /etc/localtime 文件。 /etc/localtime 文件用于让系统的 C 类库知道系统在什么时区。

root #emerge --config sys-libs/timezone-data
附注
The /etc/localtime file is used by the system C library to know the timezone the system is in.

systemd

当使用 systemd 时,采用一种稍有不同的方法。生成一个符号链接:

root #ln -sf ../usr/share/zoneinfo/Europe/Brussels /etc/localtime

之后当 systemd 运行时,时区和相关设置可以使用 timedatectl 命令配置。

配置区域设置

附注
这一步不适用于 musl libc 用户。不知道 musl libc 是什么的用户应该执行此步骤。

生成区域设置

大多数用户只想在他们的系统上使用一或两个地区。

Locale 不只是指定用户应该使用与系统进行交互的语言,同时也指定了字符串排序,日期和时间的显示等规则。Locale 是 "区分大小写" 的,必须完全按照描述的方式表示。完整的 locale 可用列表可以在 /usr/share/i18n/SUPPORTED 文件中找到。

系统支持的 locale 必须在 /etc/locale.gen 文件中定义。

root #nano /etc/locale.gen

下面的地区是一个示例,展示了同时使用英语(美国)和德语(德国)及附加字符格式(如 UTF-8)。

文件 /etc/locale.gen启用US和 DE 地区及附加字符格式
en_US ISO-8859-1
en_US.UTF-8 UTF-8
de_DE ISO-8859-1
de_DE.UTF-8 UTF-8
警告
许多应用程序需要至少有 UTF-8 区域设置才能正确构建。

下一步是运行 locale-gen 命令。此命令会生成 /etc/locale.gen 文件中所有指定的地区。

root #locale-gen

要验证当前所选择的 locale 可用,可以运行 locale -a

在 systemd 安装中,可以使用 localectl,例如 localectl set-locale ... 或者 localectl list-locales

选择区域设置

等完成后,我们就来设定系统级别的区域设置。我们再次使用 eselect 设定区域设置,现在使用 locale 模块。

通过 eselect locale list 可显示可用的目标:

root #eselect locale list
Available targets for the LANG variable:
  [1]  C
  [2]  C.utf8
  [3]  en_US
  [4]  en_US.iso88591
  [5]  en_US.utf8
  [6]  de_DE
  [7]  de_DE.iso88591
  [8]  de_DE.utf8
  [9] POSIX
  [ ]  (free form)

可以使用 eselect locale set <NUMBER> 选择正确的区域设置:

root #eselect locale set 9

还可以手动编辑 /etc/env.d/02locale 文件,在 systemd 是 /etc/locale.conf 文件,来选择区域设置:

文件 /etc/env.d/02locale手动设置系统区域设置 定义
LANG="de_DE.UTF-8"
LC_COLLATE="C.UTF-8"

设定区域设置可以避免在后面安装中,内核和软件汇编时的警告和错误。

现在重新加载环境:

root #env-update && source /etc/profile && export PS1="(chroot) ${PS1}"

更多有关区域设置选择过程的指导,另请参阅本地化指南UTF-8 指南。





可选:安装固件和微代码

固件

Linux Firmware

在开始配置内核部分之前,最好了解下,一些硬件设备需要先在系统上安装附加的固件,有时是非自由及开放源代码软件(FOSS)才能正常运行。我们经常网络接口上会使用附加的固件,特别是无线网络接口,常用于台式电脑和笔记本电脑。此外,来自 AMD,Nvidia 和 Intel 等供应商的现代视频芯片,要想使用完整的功能,通常也需要外部固件文件。现代硬件设备的大多数固件都可以在 sys-kernel/linux-firmware 软件包中找到。

为了在必要时提供固件,首次重启系统之前推荐安装 sys-kernel/linux-firmware 软件包:

root #emerge --ask sys-kernel/linux-firmware
附注
安装某些固件软件包时,经常需要接受固件关联的许可证。有需要的话,访问手册的许可证处理章节有助于你接受许可证。

注意,当内核以模块(M)加载内核符号时,将从文件系统中加载其相关的固件文件。对于以模块加载的符号,没有必要把设备固件文件加入内核二进制镜像中。

SOF Firmware

Sound Open Firmware (SOF) is a new open source audio driver meant to replace the proprietary Smart Sound Technology (SST) audio driver from Intel. 10th gen+ and Apollo Lake (Atom E3900, Celeron N3350, and Pentium N4200) Intel CPUs require this firmware for certain features and certain AMD APUs also have support for this firmware. SOF's supported platforms matrix can be found here for more information.

root #emerge --ask sys-firmware/sof-firmware

微码

除了独立显卡硬件和网络接口之外,CPU 可能也需要固件更新。通常这种固件被称为微码(microcode)。有时需要更新版本的微码来修补 CPU 硬件中的不稳定性、安全问题或其他复杂的错误。

AMD CPU 的微码更新在前面提到的 sys-kernel/linux-firmware 软件包内分发。Intel CPU 的微码可以在 sys-firmware/intel-microcode 包中找到,并且需要单独安装。更多有关如何更新微码的细节,请查看微码文章

内核配置和编译

现在是时候来配置和编译内核源代码了。将介绍三种用内核管理安装的方法,但是安装后,任何时候都可以采用新的方法。

由操作最少到操作最多排序:

全自动方法:Distribution 内核
Distribution 内核用来配置、自动构建安装 Linux 内核、相关模块和 initramfs 文件(可选,但是默认启用)。将来内核就像其他的系统软件包一样,通过使用包管理器全自动升级。如果需要定制内核,它可以提供自定义内核配置文件。这种过程操作最少,并且非常适合新 Gentoo 用户,因为它开箱即用,并且系统管理员最少参与。
混合方法:Genkernel
通过系统包管理器安装新内核源代码。系统管理员可以使用 Gentoo 的 genkernel 工具的配置,构建和安装 Linux 内核、相关模块和 initramfs 文件(可选,但默认 启用)。如果要自定义内核,它可以提供自定义内核配置文件。将来每次更新内核配置、编译和安装都需要系统管理员运行 eselect kernelgenkernel和其他可能的命令。
全手动方法
通过系统包管理器安装新的内核源代码。使用 eselect kernel 和一大堆 make 命令手动配置、构建和安装内核。将来内核升级需要重复手动配置、构建和安装内核文件的过程。这是操作做多的过程,但是在内核更新过程提供最大程度控制。

Linux内核是所有发行版的核心。它位于用户程序和系统硬件之间。尽管手册提供给用户一些可选的内核源码,但是在内核概述页面有更全面的列表,上面有着更多的细节描述。

提示
Kernel installation tasks such as, copying the kernel image to /boot or the EFI System Partition, generating an initramfs and/or Unified Kernel Image, updating bootloader configuration, can be automated with installkernel. Users may wish to configure and install sys-kernel/installkernel before proceeding. See the Kernel installation section below for more more information.

distribution 内核

Distribution 内核是涵盖了解包、配置、编译和安装内核完整过程的 ebuild。这种方法的主要优点是,内核可以作为 @world 的一部分通过包管理器升级到新版本。只需要运行 emerge 命令即可。Distribution 内核默认支持大多数硬件,但是有两种定制的机制:savedconfig 和 config snippets。查看项目页面获取更多配置细节。

安装 distribution 内核

Before installing the kernel package the dracut USE flag needs to be added for the package sys-kernel/installkernel in /etc/portage/package.use:

文件 /etc/portage/package.use/installkernelEnable dracut support
sys-kernel/installkernel dracut

Users may also wish to enable additional sys-kernel/installkernel USE flags at this stage. See the Installation/Kernel#Installkernel section for details.

如果用 Gentoo 补丁从源码构建一个内核 ,输入:

root #emerge --ask sys-kernel/gentoo-kernel

想要避免编译本地内核源代码的系统管理员可以使用预编译的内核镜像:

root #emerge --ask sys-kernel/gentoo-kernel-bin
Optional: Signed kernel modules

The kernel modules in the prebuilt distribution kernel (sys-kernel/gentoo-kernel-bin) are already signed. To sign the modules of kernels built from source enable the modules-sign USE flag, and optionally specify which key to use for signing in /etc/portage/make.conf:

文件 /etc/portage/make.confEnable module signing
USE="modules-sign"
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, to use custom signing keys.
MODULES_SIGN_KEY="/path/to/kernel_key.pem"
MODULES_SIGN_CERT="/path/to/kernel_key.pem" # Only required if the MODULES_SIGN_KEY does not also contain the certificate.
MODULES_SIGN_HASH="sha512" # Defaults to sha512.

If MODULES_SIGN_KEY is not specified the kernel build system will generate a key, it will be stored in /usr/src/linux-x.y.z/certs. It is recommended to manually generate a key to ensure that it will be the same for each kernel release. A key may be generated with:

root #openssl req -new -nodes -utf8 -sha256 -x509 -outform PEM -out kernel_key.pem -keyout kernel_key.pem
附注
The MODULES_SIGN_KEY and MODULES_SIGN_CERT may be different files. For this example the pem file generated by OpenSSL includes both the key and the accompanying certificate, and thus both variables are set to the same value.

OpenSSL will ask some questions about the user generating the key, it is recommended to fill in these questions as detailed as possible.

Store the key in a safe location, at the very least the key should be readable only by the root user. Verify this with:

root #ls -l kernel_key.pem
 -r-------- 1 root root 3164 Jan  4 10:38 kernel_key.pem 

If this outputs anything other then the above, correct the permissions with:

root #chown root:root kernel_key.pem
root #chmod 400 kernel_key.pem
Optional: Signing the kernel image (Secure Boot)

The kernel image in the prebuilt distribution kernel (sys-kernel/gentoo-kernel-bin) is already signed for use with Secure Boot. To sign the kernel image of kernels built from source enable the secureboot USE flag, and optionally specify which key to use for signing in /etc/portage/make.conf. Note that signing the kernel image for use with secureboot requires that the kernel modules are also signed, the same key may be used to sign both the kernel image and the kernel modules:

文件 /etc/portage/make.confEnable custom signing keys
USE="modules-sign secureboot"
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, to use custom signing keys.
MODULES_SIGN_KEY="/path/to/kernel_key.pem"
MODULES_SIGN_CERT="/path/to/kernel_key.pem" # Only required if the MODULES_SIGN_KEY does not also contain the certificate.
MODULES_SIGN_HASH="sha512" # Defaults to sha512.
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, to boot with secureboot enabled, may be the same or different signing key.
SECUREBOOT_SIGN_KEY="/path/to/kernel_key.pem"
SECUREBOOT_SIGN_CERT="/path/to/kernel_key.pem"
附注
The SECUREBOOT_SIGN_KEY and SECUREBOOT_SIGN_CERT may be different files. For this example the pem file generated by OpenSSL includes both the key and the accompanying certificate, and thus both variables are set to the same value.
附注
For this example the same key that was generated to sign the modules is used to sign the kernel image. It is also possible to generate and use a second separate key for signing the kernel image. The same OpenSSL command as in the previous section may be used again.

See the above section for instructions on generating a new key, the steps may be repeated if a separate key should be used to sign the kernel image.

To successfully boot with Secure Boot enabled, the used bootloader must also be signed and the certificate must be accepted by the UEFI firmware or Shim. This will be explained later in the handbook.

更新和清理

内核安装后,包管理器将自动更新到新版本。包管理器清理旧的软件包之前,将保留以前的版本。如果要回收磁盘空间,可以定期运行 emerge 加 --depclean 选项清理过时的软件包:

root #emerge --depclean

或者,专门清理旧版本的内核:

root #emerge --prune sys-kernel/gentoo-kernel sys-kernel/gentoo-kernel-bin

安装后/更新任务

Distribution 内核是一种能由其他软件包重新构建安装的内核模块。linux-mod.eclass 提供了 dist-kernel USE 标记来控制 virtual/dist-kernel 子槽的依赖。

在像 sys-fs/zfssys-fs/zfs-kmod 这样的软件包上启用 USE 标记后,将允许它们自动根据最新更新的内核重新编译,如果合适的话,还会相应地重新生成 initramfs!

手动重建 initramfs

需要的话, 可以手动重建 initramfs,内核更新后,执行:

root #emerge --ask @module-rebuild

如果任何内核模块(例如 ZFS)需要提前启动,需要之后通过下面命令重建 initramfs:

root #emerge --config sys-kernel/gentoo-kernel
root #emerge --config sys-kernel/gentoo-kernel-bin

安装内核源码

附注
本小节只有关于使用 genkernel(混合)或手动内核管理方法

The use of sys-kernel/installkernel is not strictly required, but highly recommended. When this package is installed, the kernel installation process will be delegated to installkernel. This allows for installing several different kernel versions side-by-side as well as managing and automating several tasks relating to kernel installation described later in the handbook. Install it now with:

root #emerge --ask sys-kernel/installkernel

当为基于 amd64 的系统安装和编译内核时,Gentoo 推荐使用 sys-kernel/gentoo-sources 软件包。

选择一个合适的内核并使用 emerge 来安装它。

root #emerge --ask sys-kernel/gentoo-sources

这将在 /usr/src/ 中安装路径中特定版本的 Linux 内核源码。如果没有在所选内核源码包内启用 USE=symlink,它将不会自行创建符号链接。

我们习惯性的维护 /usr/src/linux 符号链接。这样,它指向与当前运行的内核相对应的源代码。然而,默认情况下不会创建这个符号链接。有一个简单创建符号链接的方法是利用 eselect 的内核模块。

更多有关符号链接的信息以及如何管理符号链接,请参考升级内核

首先,列出所有已安装的内核:

root #eselect kernel list
Available kernel symlink targets:
  [1]   linux-6.6.21-gentoo

要创建一个名为 linux 的符号链接,使用:

root #eselect kernel set 1
root #ls -l /usr/src/linux
lrwxrwxrwx    1 root   root    12 Oct 13 11:04 /usr/src/linux -> linux-6.6.21-gentoo

备选:genkernel

附注
In case it was missed, this section requires the kernel sources to be installed. Be sure to obtain the relevant kernel sources, then return here for the rest of section.

如果全手动配置看起来太恐怖,系统管理员应该考虑使用 genkernel 混合方法来维护内核。

Genkernel 提供了通用的内核配置文件,并且会编译内核和 initramfs,然后将生成的二进制文件安装到适当的位置。它提供了系统首次启动时最小的常规硬件支持,并允许将来自定义内核配置以及对内核额外升级。

请注意:虽然使用 genkernel 维护内核可以让系统管理员对系统内核,initramfs 和其他选项更新时有更多的操作,但当新的源代码发行时,却投入更多的时间和精力去更新内核。所以那些希望不手动内核维护的人应该使用 distribution 内核

更进一步说,认为 genkernel 可以为正在运行的硬件自动生成自定义的内核配置 是一种误解;genkernel 使用了支持大多数通用硬件的预定的内核配置文件,并且可以自动处理 make 命令来汇编和安装内核、相关模块以及 initramfs 文件。

二进制可再分发软件许可证组

如果已经预先安装了 linux-firmware 软件包,那么跳转到安装章节

由于 sys-kernel/genkernel 软件包默认启用 firwmare USE 标志,所以包管理器还会尝试拉取 sys-kernel/linux-firmware 软件包。安装 linux-firmware 之前,需要接受二进制可再分发软件许可证。

通过在 /etc/portage/make.conf 文件为 ACCEPT_LICENSE 变量添加 @BINARY-REDISTRIBUTABLE 值,可以接受系统范围任何软件包的许可证组。通过在 /etc/portage/package.license/linux-firmware 文件添加特定内容,可以单独接受 linux-firmware 软件包的许可证组。

如果有必要,查看手册安装基本系统里的接受软件许可证的方法章节,然后修改一些可接受的软件许可证。

如果分析不出来,可以这样做:

root #mkdir /etc/portage/package.license
文件 /etc/portage/package.license/linux-firmware为 linux-firmware 软件包接受二进制可再分发许可证(binary redistributable)
sys-kernel/linux-firmware @BINARY-REDISTRIBUTABLE

安装

说明完以及准备好之后,安装 sys-kernel/genkernel 软件包:

root #emerge --ask sys-kernel/genkernel

常规

运行genkernel all来编译内核源码。值得注意的是,使用 genkernel 编译的内核适用于不同计算机体系结构的各种硬件,这可能使编译过程需要一阵子来完成。

附注
如果 root 分区/卷 不是使用 ext4 作为文件系统,它可能需要使用 genkernel --menuconfig all 来手动配置内核,并在内核中添加对这个具体文件系统的支持(比如:不是作为一个模块)。LVM2用户可能要作为参数来添加--lvm
附注
LVM2 用户应该在下面 genkernel命令添加 --lvm 参数 。
root #genkernel --mountboot --install all

genkernel 完成后,将在 /boot 目录生成并安装内核和初始化内存文件(initial ram filesystem,initramfs)。/lib/modules 目录将安装相关模块。initramfs 将在内核加载后立即启动,然后进行硬件自动检测(就像在 live 磁盘镜像环境中一样)。

root #ls /boot/vmlinu* /boot/initramfs*
root #ls /lib/modules

可选:手动配置

介绍

附注
In case it was missed, this section requires the kernel sources to be installed. Be sure to obtain the relevant kernel sources, then return here for the rest of section.

手动配置内核经常被 Linux 用户认为是最困难的步骤。但是事实并非如此──当您手动配置几次内核之后,你就不会再觉得它有多么难了!

无论如何,有一件事是真实的:当手动配置内核时,了解(硬件)系统是至关重要的。大多数信息可以通过安装包含lspci命令的sys-apps/pciutils来收集:

root #emerge --ask sys-apps/pciutils
附注
在chroot中,可以安全的忽略任何lspci可能抛出的关于pcilib的警告(比如pcilib: cannot open /sys/bus/pci/devices)。

另一个系统信息来源是运行lsmod来查看安装CD使用什么内核模块,它可能对启用什么提供了一个好的暗示。

现在进入内核源码目录并执行make menuconfig。这将启动一个菜单驱动的配置屏幕。

root #cd /usr/src/linux
root #make menuconfig

Linux 内核配置有很多很多的章节。我们先列出一些必须激活的选项(否则 Gentoo 将无法工作,或者离开附加的调整将无法正常工作)。我们同时在 Gentoo 维基上有一个 Gentoo 内核配置指南可能会在将来有帮助。

启用必要的选项

当使用 sys-kernel/gentoo-sources 时,强烈推荐启用 Gentoo 特有的配置选项。这些确保了系统可以正常运行所需的最少内核特性:

内核 启用 Gentoo 特有选项
Gentoo Linux --->
  Generic Driver Options --->
    [*] Gentoo Linux support
    [*]   Linux dynamic and persistent device naming (userspace devfs) support
    [*]   Select options required by Portage features
        Support for init systems, system and service managers  --->
          [*] OpenRC, runit and other script based systems and managers
          [*] systemd

当然,最后两行的选择取决于所选择的 init 系统 (OpenRC 对比 systemd)。但是两种 init 系统都启用也并没有害处。

当使用 sys-kernel/vanilla-sources时,不能使用额外的 init 系统。可以使用已启用的 init 系统,但这超出了手册的范围。

启用典型的系统组件支持

确保引导系统的每一个至关重要的驱动(比如 SATA 控制器,NVMe 块设备支持,文件系统支持,等等)是编译进内核而不是作为一个模块,否则系统将无法完全引导。

接下来选择最正确的CPU类型。同时建议启用MCE功能(如果可用)能在硬件出现问题时通知用户。在一些架构(比如x86_64),这些错误不会打印到 dmesg,但是会到/dev/mcelog。这需要app-admin/mcelog包。

同时选择Maintain a devtmpfs file system to mount at /dev来让重要的设备文件在引导过程的早期就已就绪(CONFIG_DEVTMPFS and CONFIG_DEVTMPFS_MOUNT):

内核 启用 devtmpfs 支持(CONFIG_DEVTMPFS
Device Drivers --->
  Generic Driver Options --->
    [*] Maintain a devtmpfs filesystem to mount at /dev
    [*]   Automount devtmpfs at /dev, after the kernel mounted the rootfs

验证 SCSI 磁盘支持是否已激活(CONFIG_BLK_DEV_SD):

内核 启用 SCSI 磁盘支持(CONFIG_SCSI, CONFIG_BLK_DEV_SD
Device Drivers --->
  SCSI device support  ---> 
    <*> SCSI device support
    <*> SCSI disk support
内核 启用基础 SATA 和 PATA 支持(CONFIG_ATA_ACPI, CONFIG_SATA_PMP, CONFIG_SATA_AHCI, CONFIG_ATA_BMDMA, CONFIG_ATA_SFF, CONFIG_ATA_PIIX
Device Drivers --->
  <*> Serial ATA and Parallel ATA drivers (libata)  --->
    [*] ATA ACPI Support
    [*] SATA Port Multiplier support
    <*> AHCI SATA support (ahci)
    [*] ATA BMDMA support
    [*] ATA SFF support (for legacy IDE and PATA)
    <*> Intel ESB, ICH, PIIX3, PIIX4 PATA/SATA support (ata_piix)

验证已经启用基础 NVMe 支持:

内核 启用 Linux 4.4.x 基础 NVMe 支持(CONFIG_BLK_DEV_NVME
Device Drivers  --->
  <*> NVM Express block device
内核 启用 Linux 5.x.x 基础 NVMe 支持(CONFIG_DEVTMPFS
Device Drivers --->
  NVME Support --->
    <*> NVM Express block device

也可以启用下列额外的 NVMe 支持:

内核 启用额外的 NVMe 支持(CONFIG_NVME_MULTIPATHCONFIG_NVME_MULTIPATHCONFIG_NVME_HWMONCONFIG_NVME_FCCONFIG_NVME_TCPCONFIG_NVME_TARGETCONFIG_NVME_TARGET_PASSTHRUCONFIG_NVME_TARGET_LOOPCONFIG_NVME_TARGET_FCCONFIG_NVME_TARGET_FCLOOPCONFIG_NVME_TARGET_TCP
[*] NVMe multipath support
[*] NVMe hardware monitoring
<M> NVM Express over Fabrics FC host driver
<M> NVM Express over Fabrics TCP host driver
<M> NVMe Target support
  [*]   NVMe Target Passthrough support
  <M>   NVMe loopback device support
  <M>   NVMe over Fabrics FC target driver
  < >     NVMe over Fabrics FC Transport Loopback Test driver (NEW)
  <M>   NVMe over Fabrics TCP target support

现在进入File Systems并选择系统将使用的文件系统。不要作为模块来编译根文件系统所使用的文件系统,否则系统可能不挂载这个分区。同时选择Virtual memory/proc file system根据系统的需要选择一个或多个以下选项:

内核 启用文件系统支持 (CONFIG_EXT2_FS, CONFIG_EXT3_FS, CONFIG_EXT4_FS, CONFIG_BTRFS_FS, CONFIG_XFS_FS, CONFIG_MSDOS_FS, CONFIG_VFAT_FS, CONFIG_PROC_FS, and CONFIG_TMPFS)
File systems --->
  <*> Second extended fs support
  <*> The Extended 3 (ext3) filesystem
  <*> The Extended 4 (ext4) filesystem
  <*> Btrfs filesystem support
  <*> XFS filesystem support
  DOS/FAT/NT Filesystems  --->
    <*> MSDOS fs support
    <*> VFAT (Windows-95) fs support
  Pseudo Filesystems --->
    [*] /proc file system support
    [*] Tmpfs virtual memory file system support (former shm fs)

如果使用PPPoE连接到互联网,或者是拨号调制解调器,则启用下面的选项(CONFIG_PPP, CONFIG_PPP_ASYNC, and CONFIG_PPP_SYNC_TTY):

内核 启用 PPPoE 所需要的驱动(PPPoECONFIG_PPPOECONFIG_PPP_ASYNCCONFIG_PPP_SYNC_TTY
Device Drivers --->
  Network device support --->
    <*> PPP (point-to-point protocol) support
    <*>   PPP support for async serial ports
    <*>   PPP support for sync tty ports

这两个压缩选项将是无害的,但是它们一定是不需要的,包括基于以太网的PPP选项也是一样,只有在配置内核模式PPPoE时才会需要。

不要忘记在内核中包括网(以太网或无线)卡。

大多数系统会有多核心处理,所以激活 Symmetric multi-processing support(对称多处理) 很重要 (CONFIG_SMP):

内核 激活 SMP 支持(CONFIG_SMP
Processor type and features  --->
  [*] Symmetric multi-processing support
附注
在多核系统中,每个核心视为一个处理器。

如果使用USB输入设备(比如键盘和鼠标)或其他USB设备,不要忘记启用那些:

内核 启用 USB 和人类输入设备支持(CONFIG_HID_GENERICCONFIG_USB_HIDCONFIG_USB_SUPPORTCONFIG_USB_XHCI_HCD,var>CONFIG_USB_EHCI_HCD,CONFIG_USB_OHCI_HCD(CONFIG_HID_GENERICCONFIG_USB_HIDCONFIG_USB_SUPPORTCONFIG_USB_XHCI_HCDCONFIG_USB_EHCI_HCDCONFIG_USB_OHCI_HCDCONFIG_USB4
HID support  --->
    -*- HID bus support
    <*>   Generic HID driver
    [*]   Battery level reporting for HID devices
      USB HID support  --->
        <*> USB HID transport layer
  [*] USB support  --->
    <*>     xHCI HCD (USB 3.0) support
    <*>     EHCI HCD (USB 2.0) support
    <*>     OHCI HCD (USB 1.1) support

Optional: Signed kernel modules

To automatically sign the kernel modules enable CONFIG_MODULE_SIG_ALL:

内核 Sign kernel modules CONFIG_MODULE_SIG_ALL
[*] Enable loadable module support  
  -*-   Module signature verification    
    [*]     Automatically sign all modules    
    Which hash algorithm should modules be signed with? (Sign modules with SHA-512) --->

Optionally change the hash algorithm if desired.

To enforce that all modules are signed with a valid signature, enable CONFIG_MODULE_SIG_FORCE as well:

内核 Enforce signed kernel modules CONFIG_MODULE_SIG_FORCE
[*] Enable loadable module support  
  -*-   Module signature verification    
    [*]     Require modules to be validly signed
    [*]     Automatically sign all modules
    Which hash algorithm should modules be signed with? (Sign modules with SHA-512) --->

To use a custom key, specify the location of this key in CONFIG_MODULE_SIG_KEY, if unspecified the kernel build system will generate a key. It is recommended to generate one manually instead. This can be done with:

root #openssl req -new -nodes -utf8 -sha256 -x509 -outform PEM -out kernel_key.pem -keyout kernel_key.pem

OpenSSL will ask some questions about the user generating the key, it is recommended to fill in these questions as detailed as possible.

Store the key in a safe location, at the very least the key should be readable only by the root user. Verify this with:

root #ls -l kernel_key.pem
 -r-------- 1 root root 3164 Jan  4 10:38 kernel_key.pem 

If this outputs anything other then the above, correct the permissions with:

root #chown root:root kernel_key.pem
root #chmod 400 kernel_key.pem
内核 Specify signing key CONFIG_MODULE_SIG_KEY
-*- Cryptographic API  ---> 
  Certificates for signature checking  --->  
    (/path/to/kernel_key.pem) File name or PKCS#11 URI of module signing key

To also sign external kernel modules installed by other packages via linux-mod-r1.eclass, enable the modules-sign USE flag globally:

文件 /etc/portage/make.confEnable module signing
USE="modules-sign"
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, when using custom signing keys.
MODULES_SIGN_KEY="/path/to/kernel_key.pem"
MODULES_SIGN_CERT="/path/to/kernel_key.pem" # Only required if the MODULES_SIGN_KEY does not also contain the certificate
MODULES_SIGN_HASH="sha512" # Defaults to sha512
附注
The MODULES_SIGN_KEY and MODULES_SIGN_CERT may be different files. For this example the pem file generated by OpenSSL includes both the key and the accompanying certificate, and thus both variables are set to the same value.

Optional: Signing the kernel image (Secure Boot)

When signing the kernel image (for use on systems with Secure Boot enabled) it is recommended to set the following kernel config options:

内核 Lockdown for secureboot
General setup  --->
  Kexec and crash features  --->   
    [*] Enable kexec system call                                                                                          
    [*] Enable kexec file based system call                                                                               
    [*]   Verify kernel signature during kexec_file_load() syscall                                                        
    [*]     Require a valid signature in kexec_file_load() syscall                                                        
    [*]     Enable ""image"" signature verification support
</div>  

<div lang="en" dir="ltr" class="mw-content-ltr">
[*] Enable loadable module support  
  -*-   Module signature verification    
    [*]     Require modules to be validly signed
    [*]     Automatically sign all modules
    Which hash algorithm should modules be signed with? (Sign modules with SHA-512) --->
</div>  

<div lang="en" dir="ltr" class="mw-content-ltr">
Security options  ---> 
[*] Integrity subsystem   
  [*] Basic module for enforcing kernel lockdown                                                                       
  [*]   Enable lockdown LSM early in init                                                                       
        Kernel default lockdown mode (Integrity)  --->
</div>            

  <div lang="en" dir="ltr" class="mw-content-ltr">
[*]   Digital signature verification using multiple keyrings                                                            
  [*]     Enable asymmetric keys support                                                                                     
  -*-       Require all keys on the integrity keyrings be signed                                                              
  [*]       Provide keyring for platform/firmware trusted keys                                                                
  [*]       Provide a keyring to which Machine Owner Keys may be added                                                        
  [ ]         Enforce Machine Keyring CA Restrictions

Where ""image"" is a placeholder for the architecture specific image name. These options, from the top to the bottom: enforces that the kernel image in a kexec call must be signed (kexec allows replacing the kernel in-place), enforces that kernel modules are signed, enables lockdown integrity mode (prevents modifying the kernel at runtime), and enables various keychains.

On arches that do not natively support decompressing the kernel (e.g. arm64 and riscv), the kernel must be built with its own decompressor (zboot):

内核 zboot CONFIG_EFI_ZBOOT
Device Drivers --->                                                                                                                           
  Firmware Drivers --->                                                                                                                       
    EFI (Extensible Firmware Interface) Support --->                                                                                               
      [*] Enable the generic EFI decompressor

After compilation of the kernel, as explained in the next section, the kernel image must be signed. First install app-crypt/sbsigntools and then sign the kernel image:

root #emerge --ask app-crypt/sbsigntools
root #sbsign /usr/src/linux-x.y.z/path/to/kernel-image --cert /path/to/kernel_key.pem --key /path/to/kernel_key.pem --out /usr/src/linux-x.y.z/path/to/kernel-image
附注
For this example the same key that was generated to sign the modules is used to sign the kernel image. It is also possible to generate and use a second sperate key for signing the kernel image. The same OpenSSL command as in the previous section may be used again.

Then proceed with the installation.

To automatically sign EFI executables installed by other packages, enable the secureboot USE flag globally:

文件 /etc/portage/make.confEnable Secure Boot
USE="modules-sign secureboot"
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, to use custom signing keys.
MODULES_SIGN_KEY="/path/to/kernel_key.pem"
MODULES_SIGN_CERT="/path/to/kernel_key.pem" # Only required if the MODULES_SIGN_KEY does not also contain the certificate.
MODULES_SIGN_HASH="sha512" # Defaults to sha512
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# Optionally, to boot with secureboot enabled, may be the same or different signing key.
SECUREBOOT_SIGN_KEY="/path/to/kernel_key.pem"
SECUREBOOT_SIGN_CERT="/path/to/kernel_key.pem"
附注
The SECUREBOOT_SIGN_KEY and SECUREBOOT_SIGN_CERT may be different files. For this example the pem file generated by OpenSSL includes both the key and the accompanying certificate, and thus both variables are set to the same value.
附注
When generating an Unified Kernel Image with systemd's ukify the kernel image will be signed automatically before inclusion in the unified kernel image and it is not necessary to sign it manually.


架构特有的内核配置

如果要支持32位程序,请确保选择IA32 Emulation(CONFIG_IA32_EMULATION)。Gentoo 默认会安装一个multilib 系统(混合 32 位/ 64 位计算),所以除非使用了一个 no-multilib 配置文件,否则这个选项是必需的。

内核 选择处理器类型和功能
Processor type and features  --->
   [ ] Machine Check / overheating reporting 
   [ ]   Intel MCE Features
   [ ]   AMD MCE Features
   Processor family (AMD-Opteron/Athlon64)  --->
      ( ) Opteron/Athlon64/Hammer/K8
      ( ) Intel P4 / older Netburst based Xeon
      ( ) Core 2/newer Xeon
      ( ) Intel Atom
      ( ) Generic-x86-64
Binary Emulations --->
   [*] IA32 Emulation

如果在分区时使用GPT分区标签,则启用对它的支持 (CONFIG_PARTITION_ADVANCED and CONFIG_EFI_PARTITION):

内核 启用对GPT的支持
-*- Enable the block layer --->
   Partition Types --->
      [*] Advanced partition selection
      [*] EFI GUID Partition support

如果使用 UEFI 来引导系统,则在内核中启用 EFI stub 支持,EFI 变量和 EFI 帧缓冲器。(CONFIG_EFICONFIG_EFI_STUBCONFIG_EFI_MIXEDCONFIG_EFI_VARSCONFIG_FB_EFI):

内核 启用对UEFI的支持
Processor type and features  --->
    [*] EFI runtime service support 
    [*]   EFI stub support
    [*]     EFI mixed-mode support
 
Device Drivers
    Firmware Drivers  --->
        EFI (Extensible Firmware Interface) Support  --->
            <*> EFI Variable Support via sysfs
    Graphics support  --->
        Frame buffer Devices  --->
            <*> Support for frame buffer devices  --->
                [*]   EFI-based Framebuffer Support
内核 Enabling SOF Firmware support (CONFIG_SND_SOC_SOF_TOPLEVEL, CONFIG_SND_SOC_SOF_PCI, CONFIG_SND_SOC_SOF_ACPI, CONFIG_SND_SOC_SOF_AMD_TOPLEVEL, CONFIG_SND_SOC_SOF_INTEL_TOPLEVEL)
Device Drivers --->
  Sound card support --->
    Advanced Linux Sound Architecture --->
      <M> ALSA for SoC audio support --->
        [*] Sound Open Firmware Support --->
            <M> SOF PCI enumeration support
            <M> SOF ACPI enumeration support
            <M> SOF support for AMD audio DSPs
            [*] SOF support for Intel audio DSPs


编译和安装

当配置完成,是时间来编译和安装内核了。退出配置并开始编译过程:

root #make && make modules_install
附注
还可以启用并行生成使用make -jXX是一个生成过程中所允许运行的并行任务的整数。这类似于早期有关/etc/portage/make.conf的中关于MAKEOPTS变量的介绍。

当内核完成编译,复制内核镜像到/boot/。这由make install命令来处理:

root #make install

这将复制内核镜像到/boot/,一起的还有System.map文件和内核配置文件。


Kernel installation

Installkernel

Installkernel may be used to automate, the kernel installation, initramfs generation, unified kernel image generation and/or bootloader configuration among other things. sys-kernel/installkernel implements two paths of achieving this: the traditional installkernel originating from Debian and systemd's kernel-install. Which one to choose depends, among other things, on the system's bootloader. By default systemd's kernel-install is used on systemd profiles, while the traditional installkernel is the default for other profiles.

If unsure, follow the 'Traditional layout' subsection below.

systemd-boot

When using systemd-boot (formerly gummiboot) as the bootloader, systemd's kernel-install must be used. Therefore ensure the systemd and the systemd-boot USE flags are enabled on sys-kernel/installkernel, and then install the relevant package for systemd-boot.

On OpenRC systems:

文件 /etc/portage/package.use/systemd-boot
sys-apps/systemd-utils boot kernel-install
sys-kernel/installkernel systemd systemd-boot
root #emerge --ask sys-apps/systemd-utils

On systemd systems:

文件 /etc/portage/package.use/systemd
sys-apps/systemd boot
sys-kernel/installkernel systemd-boot
root #emerge --ask sys-apps/systemd

GRUB

Users of GRUB can use either systemd's kernel-install or the traditional Debian installkernel. The systemd USE flag switches between these implementations. To automatically run grub-mkconfig when installing the kernel, enable the grub USE flag.

文件 /etc/portage/package.use/installkernel
sys-kernel/installkernel grub
root #emerge --ask sys-kernel/installkernel

Traditional layout, other bootloaders (e.g. lilo, etc.)

The traditional /boot layout (for e.g. LILO, etc.) is used by default if the grub, systemd-boot and uki USE flags are not enabled. No further action is required.


Building an initramfs

In certain cases it is necessary to build an initramfs - an initial ram-based file system. The most common reason is when important file system locations (like /usr/ or /var/) are on separate partitions. With an initramfs, these partitions can be mounted using the tools available inside the initramfs. The default configuration of the Project:Distribution Kernel requires an initramfs.

Without an initramfs, there is a risk that the system will not boot properly as the tools that are responsible for mounting the file systems require information that resides on unmounted file systems. An initramfs will pull in the necessary files into an archive which is used right after the kernel boots, but before the control is handed over to the init tool. Scripts on the initramfs will then make sure that the partitions are properly mounted before the system continues booting.

重要
If using genkernel, it should be used for both building the kernel and the initramfs. When using genkernel only for generating an initramfs, it is crucial to pass --kernel-config=/path/to/kernel.config to genkernel or the generated initramfs may not work with a manually built kernel. Note that manually built kernels go beyond the scope of support for the handbook. See the kernel configuration article for more information.

Installkernel can automatically generate an initramfs when installing the kernel if the dracut USE flag is enabled:

文件 /etc/portage/package.use/installkernel
sys-kernel/installkernel dracut

Alternatively, dracut may be called manually to generate an initramfs. Install sys-kernel/dracut first, then have it generate an initramfs:

root #emerge --ask sys-kernel/dracut
root #dracut --kver=6.6.21-gentoo

The initramfs will be stored in /boot/. The resulting file can be found by simply listing the files starting with initramfs:

root #ls /boot/initramfs*

Optional: Building an Unified Kernel Image

An Unified Kernel Image (UKI) combines, among other things, the kernel, the initramfs and the kernel command line into a single executable. Since the kernel command line is embedded into the unified kernel image it should be specified before generating the unified kernel image (see below). Note that any kernel command line arguments supplied by the bootloader or firmware at boot are ignored when booting with secure boot enabled.

An unified kernel image requires a stub loader, currently the only one available is systemd-stub. To enable it:

For systemd systems:

文件 /etc/portage/package.use/systemd
sys-apps/systemd boot

For OpenRC systems:

文件 /etc/portage/package.use/systemd-utils
sys-apps/systemd-utils boot kernel-install

Installkernel can automatically generate an unified kernel image using either dracut or ukify, by enabling the respective flag. The uki USE flag should be enabled as well to install the generated unified kernel image to the $ESP/EFI/Linux directory on the EFI system partition (ESP).

For dracut:

文件 /etc/portage/package.use/installkernel
sys-kernel/installkernel dracut uki
文件 /etc/dracut.conf
uefi="yes"
kernel_cmdline="some-kernel-command-line-arguments"

For ukify:

文件 /etc/portage/package.use/installkernel
sys-apps/systemd ukify          # For systemd systems
sys-apps/systemd-utils ukify    # For OpenRC systems
sys-kernel/installkernel dracut ukify uki
文件 /etc/kernel/cmdline
some-kernel-command-line-arguments

Note that while dracut can generate both an initramfs and an unified kernel image, ukify can only generate the latter and therefore the initramfs must be generated separately with dracut.

Generic Unified Kernel Image

The prebuilt sys-kernel/gentoo-kernel-bin can optionally install a prebuilt generic unified kernel image containing a generic initramfs that is able to boot most systemd based systems. It can be installed by enabling the generic-uki USE flag, and configuring installkernel to not generate a custom initramfs or unified kernel image:

文件 /etc/portage/package.use/generic-uki
sys-kernel/gentoo-kernel-bin generic-uki
sys-kernel/installkernel -dracut -ukify uki

Secure Boot

The generic Unified Kernel Image optionally distributed by sys-kernel/gentoo-kernel-bin is already pre-signed. How to sign a locally generated unified kernel image depends on whether dracut or ukify is used. Note that the location of the key and certificate should be the same as the SECUREBOOT_SIGN_KEY and SECUREBOOT_SIGN_CERT as specified in /etc/portage/make.conf.

For dracut:

文件 /etc/dracut.conf
uefi="yes"
kernel_cmdline="some-kernel-command-line-arguments"
uefi_secureboot_key="/path/to/kernel_key.pem"
uefi_secureboot_cert="/path/to/kernel_key.pem"

For ukify:

文件 /etc/kernel/uki.conf
[UKI]
SecureBootPrivateKey=/path/to/kernel_key.pem
SecureBootCertificate=/path/to/kernel_key.pem

Rebuilding external kernel modules

External kernel modules installed by other packages via linux-mod-r1.eclass must be rebuilt for each new kernel version. When the distribution kernels are used this may be automated by enabling the dist-kernel flag globally.

文件 /etc/portage/package.use/module-rebuild
*/* dist-kernel

External kernel modules may also be rebuilt manually with:

root #emerge --ask @module-rebuild

内核模块

列出可用的内核模块

附注
硬件模块手动列出是可选的。在大多数情况下,udev 通常将加载所有被检测为已连接的硬件模块。然而,列出自动检测到的模块并没有什么不良影响的。有时,一些奇特硬件需要帮助来加载其驱动程序。

可以在 /etc/modules-load.d/*.conf 文件中添加需要在每次启动时加载的模块,格式为每行一个模块。如果模块需要附加选项,可以在 /etc/modprobe.d/*.conf 文件内设置。

对于特定的内核版本,如果要查看所有可用模块,使用下面的 find 命令。不要忘记在查找时替换 “<kernel version>” 为对应的内核版本:

root #find /lib/modules/<kernel version>/ -type f -iname '*.o' -or -iname '*.ko' | less

强制加载特定内核模块

要强制加载内核 3c59x.ko 模块(3Com 网卡家族的特定驱动),编辑/etc/modules-load.d/network.conf 文件并在里面输入模块名字。

root #mkdir -p /etc/modules-load.d
root #nano -w /etc/modules-load.d/network.conf

注意,模块的 .ko 文件后缀对加载机制影响不大,所以配置文件中没有文件后缀:

文件 /etc/modules-load.d/network.conf强制加载 3c59x 模块
3c59x

继续到配置系统来安装。





文件系统信息

分区表和 UUID

MBR(BIOS)和GPT都支持“文件系统”标签和“文件系统”的UUID。 这些属性可以在尝试查找和挂载块设备时使用,作为 mount 命令的替代方法,在 /etc/fstab 中定义。文件系统标签和 UUID 由 LABELUUID 前缀标识,可以使用 blkid 命令查看:

root #blkid
警告
如果分区中的文件系统被擦除,则文件系统标签和UUID值将随后被更改或删除。

出于唯一性,建议使用 MBR 分区表的读者使用 UUID 来定义/etc/fstab 中的可挂载卷。

重要
LVM 卷文件系统的 UUID 与其 LVM 快照相同,因此应避免使用 UUID 挂载 LVM 卷。

分区卷标和 UUID

已经使用 GPT 磁盘的用户有一些更稳定的选项可用于在 /etc/fstab 中定义分区。分区卷标和分区 UUID 可以用来标识块设备的单独分区,而不管为分区本身选择了什么文件系统。分区卷标和 UUID 分别由 PARTLABEL PARTUUID 前缀标识,可以通过运行 blkid 命令在终端中很好地查看分区标签。EFI 系统的输出如下所示:

Output for an amd64 EFI system using the Discoverable Partition Specification UUIDs may like the following:

root #blkid
/dev/sr0: BLOCK_SIZE="2048" UUID="2023-08-28-03-54-40-00" LABEL="ISOIMAGE" TYPE="iso9660" PTTYPE="PMBR"
/dev/loop0: TYPE="squashfs"
/dev/sda2: UUID="12c37dea-624b-48b6-88b8-fba427f5f341" TYPE="swap" PARTLABEL="swap" PARTUUID="0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"
/dev/sda3: UUID="9bd83a8f-75c6-4a04-b155-6f6b748509a9" BLOCK_SIZE="512" TYPE="xfs" PARTLABEL="rootfs" PARTUUID="4f68bce3-e8cd-4db1-96e7-fbcaf984b709"
/dev/sda1: UUID="166F-2917" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="efi" PARTUUID="c12a7328-f81f-11d2-ba4b-00a0c93ec93b"

虽然对于分区表不总是正确的,但使用UUID来标识fstab 中的分区,即使文件系统更改,也可以保证在寻找某个卷时引导加载程序不会被混淆。对于经常重新启动并定期添加和删除SATA设备时,在 fstab 中定义分区,使用旧的默认分区文件 (/dev/sd*N非常危险) 。

块设备文件的命名取决于许多因素,包括磁盘如何以及以什么顺序加载到系统。它们也可能以不同的顺序显示,具体情况取决于在早期启动过程中内核首先检测到哪些设备。因此,除非系统管理员打算经常调整磁盘顺序,否则可以简单直接地使用默认块设备文件。

关于 fstab

在Linux系统下,系统所用到的所有分区都必须在 /etc/fstab 文件中指明。这个文件包含了这些分区的挂载点(在系统目录树中的位置)、挂载方法和特殊挂载选项(是否自动挂载,是否某个用户可以挂载它等)。

创建 /etc/fstab 文件

/etc/fstab 文件使用一种特殊语法格式。每行都包含六个字段。这些字段之间由空白键(空格键,tab键,或者两者混合使用)分隔。每个字段都有自己的含意:

  1. #第一个字段显示要挂载的特殊 block 设备或远程文件系统。 有几种设备标识符可用于特殊块设备节点,包括设备文件路径,文件系统标签,UUID,分区标签以及UUID。
  2. 第二个字段是分区挂载点,也就是分区应该挂载到的地方。
  3. 第三个字段给出分区所用的文件系统的类型。
  4. 第四个字段给出的是挂载分区时 mount 命令所用的挂载选项。由于每个文件系统都有自己的挂载选项,我们建议系统管理员阅读 mount 手册(man mount)以获得所有挂载选项的列表。多个挂载选项之间是用逗号分隔的。
  5. 第五个字段是给dump使用的,用以决定这个分区是否需要dump。一般情况下,你可以把该字段设为 0(零)。
  6. 第六个字段是给fsck使用的,用以决定系统非正常关机之后文件系统的检查顺序。根文件系统应该为 1,而其它的应该为 2(如果不需要文件系统自检的话可以设为 0)。
重要
Gentoo stage 文件默认提供的 /etc/fstab “不”是有效的fstab 文件,它只是提供了几个模板,可用于输入相关值。
root #nano /etc/fstab

DOS/传统 BIOS 系统

让我们来看看如何写下/boot/分区的选项。 这只是一个示例,应根据安装时的具体情况进行修改。 在amd64分区示例中, /boot/ 通常是/dev/sda1 xfs 作为文件系统。 它需要在启动期间进行检查,所以我们写下:

文件 /etc/fstab/etc/fstab 中 DOS/传统 BIOS 启动行的示例
# 调整与"准备磁盘"步骤的任何格式差异
/dev/sda1   /boot     xfs    defaults        0 2

有些用户不希望 /boot/ 分区自动挂载,以提高系统的安全性。 他们应该用noauto.代替 defaults。这意味着这些用户将需要在每次他们想要使用它时手动挂载这个分区。

增加符合你分区方案的规则,为你的光驱(当然,如果你有其他分区或者驱动器,也为它们加上)添加挂载规则。

下面是/etc/fstab文件的例子:

文件 /etc/fstab针对 DOS/传统 BIOS 系统 的完整 /etc/fstab 例子
# 调整与"准备磁盘"步骤的任何格式差异
/dev/sda1   /boot        xfs    defaults    0 2
/dev/sda2   none         swap    sw                   0 0
/dev/sda3   /            xfs    defaults,noatime              0 1
</div>

/dev/cdrom  /mnt/cdrom   auto    noauto,user          0 0

UEFI 系统

Below is an example of an /etc/fstab file for a system that will boot via UEFI firmware:

文件 /etc/fstabA full /etc/fstab example for an UEFI system
# Adjust for any formatting differences and/or additional partitions created from the "Preparing the disks" step
/dev/sda1   /efi        vfat    umask=0077     0 2
/dev/sda2   none         swap    sw                   0 0
/dev/sda3   /            xfs    defaults,noatime              0 1
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
/dev/cdrom  /mnt/cdrom   auto    noauto,user          0 0


DPS UEFI PARTUUID

Below is an example of an /etc/fstab file for a disk formatted with a GPT disklabel and Discoverable Partition Specification (DPS) UUIDs set for UEFI firmware:

文件 /etc/fstabDPS PARTUUID fstab example
# Adjust any formatting difference and additional partitions created from the "Preparing the disks" step.
# This example shows a GPT disklabel with Discoverable Partition Specification (DSP) UUID set:
PARTUUID=c12a7328-f81f-11d2-ba4b-00a0c93ec93b   /efi        vfat    umask=0077                   0 2
PARTUUID=0657fd6d-a4ab-43c4-84e5-0933c84b4f4f   none        swap    sw                           0 0
PARTUUID=4f68bce3-e8cd-4db1-96e7-fbcaf984b709   /           xfs     defaults,noatime             0 1


auto选项可以使mount 猜测文件系统(推荐对于可移动设备采用这个选项,因为它们可能采用很多不同的文件系统),而 user选项使得非root用户可以挂载光驱。

To improve performance, most users would want to add the noatime mount option, which results in a faster system since access times are not registered (those are not needed generally anyway). This is also recommended for systems with solid state drives (SSDs). Users may wish to consider lazytime instead.

提示
由于会使性能下降,所以不推荐在 /etc/fstab 定义 discard 挂载选项。一般来说,最好使用作业调度器(例如 cron 或 timer (systemd))定期丢弃块。查看定期 fstrim 作业来获取更多信息。

仔细检查/etc/fstab文件,保存并退出以继续。

网络信息

特别注意,以下部分可以帮助读者快速设置他们的系统接入局域网。

对于运行 OpenRC 的系统,更多网络设置的细节参考高级网络配置章节,手册末尾附近有介绍。有更具体网络需求的系统可能需要跳过此章节,然后返回此处继续后续的安装。

更多关于 systemd 具体的网络设置,请参考 systemd 文章的网络部分

主机名

系统管理员必须要做的事情之一就是命名他们的机器。尽管这看上去很容易,但是很多用户觉得很难起一个合适的主机名。为了加快进度,应该知道所有你所命名的名字都是可以在今后重新修改的。下面的示例使用 tux 作为主机名。

设置主机名(OpenRC 和 systemd)

root #echo tux > /etc/hostname

systemd

可以使用 hostnamectl 工具在正运行 systemd 的系统设置系统的主机名。但是,在安装过程中,应当使用 systemd-firstboot 命令(参见手册的后续部分)。

可以将设置主机名为 "tux":

root #hostnamectl hostname tux

运行 hostnamectl --helpman 1 hostnamectl 查看帮助。

网络

配置网络接口有许多可用的方法。本章节只介绍了其中一些方法。你可以选择一个看起来最合适的设置方法。

通过 dhcpcd 使用 DHCP (任何 init 系统)

大多数 LAN 网络都运行 DHCP 服务器。如果是这种情况,则建议使用 dhcpcd 程序获取 IP 地址。

安装:

root #emerge --ask net-misc/dhcpcd

在 OpenRC 系统中启用和开始服务:

root #rc-update add dhcpcd default
root #rc-service dhcpcd start

在 systemd 系统中启用服务:

root #systemctl enable dhcpcd

完成这些步骤后,下次系统启动时,dhcpcd 应该就会从 DHCP 服务器获取 IP 地址。 更多细节请查看 Dhcpcd 文章。

netifrc (OpenRC)

提示
在 OpenRC 使用 Netifrc 设置网络是一种特殊的方法。还有更简单的方法设置,比如 Dhcpcd

配置网络

在Gentoo Linux安装时,网络已经配置。然而,这是安装的 live 环境本身的配置,并不是新的系统环境的网络配置。现在你所要设置的是 Gentoo 系统的永久网络配置。

附注
更多关于网络配置的详细信息,包括网卡绑定、网桥、802.1Q VLANs和无线网络在内的高级配置会在高级网络配置这一部分介绍.

/etc/conf.d/net 当中收集了所有的网络信息。尽管这个文件采用直接易懂的语法,如果你还是因为觉得不够直观而完全不知道如何手动进行网络配置的话,请不用担心,我们将一一解释。在 /usr/share/doc/netifrc-*/net.example.bz2 中有一个详细注释过的例子,它涵盖了许多种类不同的配置。

首先安装net-misc/netifrc:

root #emerge --ask --noreplace net-misc/netifrc

系统默认使用DHCP。如果使用DHCP的话,你需要安装一个DHCP客户端。这个将在稍后的安装必要的系统工具部分介绍。

如果你需要配置你的网络连接,不管是因为你是需要指定DHCP选项还是你不使用DHCP,打开 /etc/conf.d/net:;

root #nano /etc/conf.d/net

设置 config_eth0routes_eth0 输入IP地址信息和路由信息:

附注
假定网络接口名称是 eth0。这个名称非常依赖于系统,如果安装介质足够新,则建议在从安装介质引导时重命名接口。更多信息可以在网络接口命名章节中找到。
文件 /etc/conf.d/net静态IP定义
config_eth0="192.168.0.2 netmask 255.255.255.0 brd 192.168.0.255"
routes_eth0="default via 192.168.0.1"

要使用DHCP,定义 config_eth0:

文件 /etc/conf.d/netDHCP 配置
config_eth0="dhcp"

请阅读 /usr/share/doc/netifrc-*/net.example.bz2 获取额外的配置文件选项的列表。如果你需要设定特殊的 DHCP 选项,请参考你的 DHCP 客户端的 man 手册页。

如果你有多个网络接口,那么重复之前对于config_eth1config_eth2等操作步骤。

现在保存配置并退出。

在启动时自动启用网络连接

为了在启动时自动激活网络接口,你必须添加这些到default运行级别。

root #cd /etc/init.d
root #ln -s net.lo net.eth0
root #rc-update add net.eth0 default

如果系统有多个网络接口,那么需要创建适当的net.* 文件,就像我们使用net.eth0一样。

如果在引导系统后,发现网络接口名称(目前写作 eth0)是错误的,那么执行以下步骤来纠正这一点:

  1. 用正确的名称更新 /etc/conf.d/net(比如说 enp3s0 或者 enp5s0 取代 eth0)。
  2. 创建符号链接(比如 /etc/init.d/net.enp3s0)。
  3. 删除旧的符号链接(rm /etc/init.d/net.eth0)。
  4. 创建新的默认运行级别。
  5. 删除旧的运行级别,使用 rc-update del net.eth0 default

hosts 文件

现在你需要告诉Linux有关你的网络的信息。这需要在/etc/hosts文件中定义,它将帮助你将那些无法被域名解析器解析的主机名解析成IP地址。

root #nano /etc/hosts
文件 /etc/hostsFilling in the networking information
# 定义当前系统
127.0.0.1     tux.homenetwork tux localhost
  
# 可选,定义网络上的其它系统
192.168.0.5   jenny.homenetwork jenny
192.168.0.6   benny.homenetwork benny

保存并退出编辑器,继续


系统信息

Root 密码

使用passwd命令设置root密码。

root #passwd

稍后将为日常操作创建其他常规用户帐户。

配置引导和启动

OpenRC

当在 Gentoo 中使用 OpenRC 时,系统使用/etc/rc.conf配置系服务,启动和关闭。 打开 /etc/rc.conf 并查看文件中的所有注释。查看设置并根据需要进行更改。

root #nano /etc/rc.conf

接下来,打开/etc/conf.d/keymaps 来处理键盘设置。编辑它就可以设置你的键盘。

root #nano /etc/conf.d/keymaps

要特别注意keymap这个变量 。如果你选择了错误的KEYMAP,在你敲击键盘的时候会有奇怪的结果。

完成 /etc/conf.d/hwclock 的配置之后,保存并退出。

root #nano /etc/conf.d/hwclock

如果你机器上的时钟不用UTC,你需要在配置文件加上clock="local"。否则,你的时钟就有可能出现偏差。

systemd

首先,建议运行 systemd-machine-id-setup ,然后运行 systemd-firstboot ,这将准备正确设置系统的各个组件,以便首次启动进入新的systemd环境。 传递以下选项将包括提示用户设置区域设置、时区、主机名、root 密码和 root shell 值。 它还会为安装分配一个随机机器 ID:

root #systemd-machine-id-setup
root #systemd-firstboot --prompt

接下来,用户应该运行 systemctl 来重置所有已安装工具的文件为预设的策略值:

root #systemctl preset-all --preset-mode=enable-only

虽然可以运行完整的预设更改,但这可能会重置在此过程中已配置的任何服务:

root #systemctl preset-all

这两个步骤将有助于确保从 live 环境平滑过渡到安装过程的首次启动。





系统日志工具

OpenRC

因为有一些工具提供给用户的功能比较类似,它们就没有包含在stage3当中。现在就是你选择安装哪一个的时候了。

首先需要决定的工具就是系统日志机制。Unix 和 Linux 在日志记录功能方面有良好的传统——如果愿意的话,可以把系统发生的所有事件都记录到日志文件中。

Gentoo提供了多种系统日志工具可供选择。包括:

  • app-admin/sysklogd -提供传统的系统日志记录守护程序。默认日志配置容易学习,这个包是初学者的好选择。
  • app-admin/syslog-ng -高级系统记录器。 需要额外配置很多东西, 更高级的用户可以根据它的日志潜力选择这个包; 注意额外的配置是任何种类的智能日志记录的必要条件。
  • app-admin/metalog -一个可以灵活配置的系统日志工具。

Gentoo ebuild 仓库内或许还有其他系统日志工具,因为可用软件包数量每天在增加。

提示
如果打算使用 syslog-ng ,建议安装并且配置 logrotate。syslog-ng 并没有提供系统日志文件的滚动功能。新版本(>= 2.0)的 sysklogd 会自己处理日志滚动。

要安装你所选择的系统日志工具,你可以用 emerge 命令安装它。在 OpenRC 中,使用 rc-update 将它加入默认运行级别。以下就是一个安装 app-admin/sysklogd 并作为系统 syslog 工具的例子:

root #emerge --ask app-admin/sysklogd
root #rc-update add sysklogd default

systemd

日志记录工具一般只有使用 OpenRC 的系统需要选择,systemd 已经自带了一个叫 systemd-journald 的服务作为日志记录工具。systemd-journald 服务通常能够支持并处理大部分上面提到的日志记录工具的功能。换而言之,使用 systemd 运行系统作为服务管理器可以安全跳过添加额外的系统日志记录工具。

更多关于使用 journalctl 审查系统的细节,请查看 man journalctl

对于因为某种原因需要将日志转发到中心主机的情况,在使用 systemd 的系统上支持冗余的系统记录机制可能很重要。但对于本手册的受众和目的来说,这是一个非典型常见且高阶的情况。因此本手册没有涵盖它。

可选:Cron守护进程

OpenRC

尽管这是可选的并且不是系统所必须的,但是最好能够安装一个 cron 守护进程。

cron守护程序执行计划中的命令。 如果某些命令需要定期执行(例如每天,每周或每月),这是非常方便的。

All cron daemons support high levels of granularity for scheduled tasks, and generally include the ability to send an email or other form of notification if a scheduled task does not complete as expected.

Gentoo 提供了三个可选的 cron 守护进程,包括:

  • sys-process/cronie - cronie is based on the original cron and has security and configuration enhancements like the ability to use PAM and SELinux.
  • sys-process/dcron - This lightweight cron daemon aims to be simple and secure, with just enough features to stay useful.
  • sys-process/fcron - A command scheduler with extended capabilities over cron and anacron.
  • sys-process/bcron - A younger cron system designed with secure operations in mind. To do this, the system is divided into several separate programs, each responsible for a separate task, with strictly controlled communications between parts.

cronie

下面的示例使用 sys-process/cronie

root #emerge --ask sys-process/cronie

添加 cronie 作为默认系统运行级别后,将会开机自启。

root #rc-update add cronie default

可选:dcron

root #emerge --ask sys-process/dcron

如果 dcron 是前置 cron agent,则需要执行额外的初始化命令:

root #crontab /etc/crontab

可选:fcron

root #emerge --ask sys-process/fcron

如果选择 fcron 作为任务调度器,则需要额外的 emerge 步骤:

root #emerge --config sys-process/fcron

可选:bcron

bcron 是一款新的内建特权分离的 cron agent。

root #emerge --ask sys-process/bcron

systemd

提示
systemd 不需要 cron 守护进程,因为它有 timers,但仍然可以运行 cron 守护程序。

可选:文件索引

如果你想索引你的系统文件使得你能够使用locate工具很快定位它们,你需要安装sys-apps/mlocate

root #emerge --ask sys-apps/mlocate

可选:远程 shell 访问

提示
opensshd 默认配置不允许远程登录 root 用户。如果有需要的话,请在安装后创建一个非 root 用户,并且配置合适的权限,或者调整 /etc/ssh/sshd_config,使其允许 root 远程登录。

要在安装后远程访问系统,必须配置为在启动时运行sshd

OpenRC

在 OpenRC 上将 sshd init 脚本添加到默认运行级别:

root #rc-update add sshd default

如果需要访问串行控制台(在远程服务器的情况下这是可能的),必须配置 agetty

/etc/inittab 中取消串行控制台部分的注释:

root #nano -w /etc/inittab
# SERIAL CONSOLES
s0:12345:respawn:/sbin/agetty 9600 ttyS0 vt100
s1:12345:respawn:/sbin/agetty 9600 ttyS1 vt100

systemd

运行以下命令来启用 SSH 服务端:

root #systemctl enable sshd

运行以下命令来启用串行控制台支持:

root #systemctl enable getty@tty1.service

可选:Shell 补全

Bash

Bash 是Gentoo系统里默认的shell,所以安装补全建议插件支持可以更有效率,更便捷。app-shells/bash-completion 将会提供Gentooo系统特有的和其他常用命令和工具的可用的建议补全

root #emerge --ask app-shells/bash-completion

完成安装后,可以使用eselect命令管理bash的补全支持的项目 。 查阅关于bash补全的文章获取更多详情:Shell completion integrations section

时间同步

使用一些同步系统时钟的方法很重要。通常使用 NTP 协议和软件。有一些 NTP 协议的其他实现,例如 Chrony

设置 Chrony,例如

root #emerge --ask net-misc/chrony

OpenRC

在 OpenRC 上,运行:

root #rc-update add chronyd default

systemd

在 systemd 上,运行:

root #systemctl enable chronyd.service

另外,systemd 用户可能希望使用默认安装的更简单的 systemd-timesyncd SNTP 客户端。

root #systemctl enable systemd-timesyncd.service

文件系统工具

根据你所使用的文件系统的不同,可能需要安装需要的文件系统工具(用于检查文件系统完整性、(重新)格式化文件系统等)。请注意,作为 @system 集一部分的,已经安装了 ext4 用户空间的工具 (sys-fs/e2fsprogs)。

以下的表格列出了特定文件系统所需要安装的工具。

文件系统 软件包
XFS sys-fs/xfsprogs
ext4 sys-fs/e2fsprogs
VFAT (FAT32, ...) sys-fs/dosfstools
Btrfs sys-fs/btrfs-progs
ZFS sys-fs/zfs
JFS sys-fs/jfsutils

It's recommended that sys-block/io-scheduler-udev-rules is installed for the correct scheduler behavior with e.g. nvme devices:

root #emerge --ask sys-block/io-scheduler-udev-rules
提示
获取更多关于Gentoo上文件系统的信息请看文件系统文章

网络工具

如果在之前配置系统步骤配置好网络,并且完成网络设置,那么本‘网络工具’章节可以安全地跳过。这样的话,继续配置引导程序章节。

安装DHCP客户端

重要
大多数用户需要 DHCP 客户端连接到他们网络。如果没有安装的话,则系统可能无法访问网络,从而使之后也无法下载 DHCP 客户端。

DHCP 客户端可以使用 netifrc 脚本自动获取一个或多个IP地址。我们建议使用 net-misc/dhcpcd(另请参阅 dhcpcd):

root #emerge --ask net-misc/dhcpcd

可选:安装PPPoE客户端

如果你需要ppp来连接网络,你需要安装它 net-dialup/ppp

root #emerge --ask net-dialup/ppp

可选:安装无线网络工具

如果系统将连接无线网络,请为开放网络或 WEP 网络安装 net-wireless/iw 包,为 WPA 或 WPA2 网络安装 net-wireless/wpa_supplicant 包。iw 也是一个有用的无线网络扫描的基本诊断工具

root #emerge --ask net-wireless/iw net-wireless/wpa_supplicant

现在继续配置引导启动程序






选择引导加载程序

完成配置Linux内核、安装系统工具和编辑配置文件之后,现在是时候去安装Linux安装的最后一个重要的部分:引导加载程序。

引导加载程序负责在引导过程中启动内核——若没有引导加载程序,系统将不知道按下电源键后将如何进行。

针对 amd64,我们将介绍如何在基于 DOS/传统 BIOS的系统上配置 GRUBLILO,以及针对 UEFI 系统如何配置 GRUBefibootmgr

在本手册的这一部分中,描述了 "emerging" 引导加载程序包和 "installing" 引导加载程序到系统磁盘之间的区别。 这里,术语 "emerging" 将用于请求 Portage 使软件包安装于系统。 术语 "installing" 将表示引导加载程序复制文件或物理地修改系统的磁盘驱动器的适当部分,以便在下一次开机时使引导加载程序“激活并准备好操作”。

默认:GRUB

默认情况下,Gentoo 系统现在主要依赖于 GRUB(在sys-boot/grub 包中),它是 GRUB Legacy 的继任者。无需额外配置,GRUB 就能支持旧的BIOS("pc") 系统。 在安装之前加上少量的配置,GRUB 可以支持超过一半的平台。 有关详细信息,请参阅位于 GRUB准备章节

Emerge

当使用只支持MBR分区表的旧版BIOS系统时,无需进行其他配置即可安装GRUB:

root #emerge --ask --verbose sys-boot/grub

UEFI用户注意:运行上述命令将在出现之前输出启用的GRUB_PLATFORMS 值。 当使用支持UEFI的系统时,用户需要确保启用 GRUB_PLATFORMS="efi-64" 参数(默认情况下是这样)。 如果设置不是这样,则需要在安装 GRUB 之前将 GRUB_PLATFORMS="efi-64"添加到/etc/portage/make.conf:

root #echo 'GRUB_PLATFORMS="efi-64"' >> /etc/portage/make.conf
root #emerge --ask sys-boot/grub

如果 GRUB 在未先添加 GRUB_PLATFORMS="efi-64"make.conf 时就已经 emerge 过了,可以添加这一行(像上面显示那样)然后可以通过 --update --newuse options to emerge 选项来重新计算 world package set

root #emerge --ask --update --newuse --verbose sys-boot/grub

GRUB 现在已经安装到系统中了,但是他还没有成为辅助引导加载程序(SBL)。

安装

接下来,通过grub-install命令安装 GRUB 所需的文件到/boot/grub/目录。假设第一块磁盘(引导系统的那块)是/dev/sda,将使用下面的一条命令:

DOS/传统 BIOS 系统

针对 DOS/传统 BIOS 系统:

root #grub-install /dev/sda

EFI 系统

重要
确保 EFI 系统分区在运行 grub-install 之前就已经挂载。否则它可能会把 grub-install 安装的 GRUB EFI 文件(grubx64.efi到错误的目录,并且不会提供任何辨识使用错误目录的信息。

针对 EFI 系统:

root #grub-install --target=x86_64-efi --efi-directory=/efi
Installing for x86_64-efi platform.
Installation finished. No error reported.

Upon successful installation, the output should match the output of the previous command. If the output does not match exactly, then proceed to Debugging GRUB, otherwise jump to the Configure step.

Optional: Secure Boot

The sys-boot/grub package does not recognize the secureboot USE flag, this is because the GRUB EFI executable is not installed by the package but is instead built and installed by the grub-install command. GRUB must therefore be manually signed after installation to the boot partition. Additionally, GRUB is a modular bootloader but loading modules is prohibited when Secure Boot is enabled. Therefore all necessary modules must be compiled into the GRUB EFI executable, below an example is shown including some basic modules, this may have to be adjusted for more advanced configurations:

root #emerge --noreplace sbsigntools
root #export GRUB_MODULES="all_video boot btrfs cat chain configfile echo efifwsetup efinet ext2 fat font gettext gfxmenu gfxterm gfxterm_background gzio halt help hfsplus iso9660 jpeg keystatus loadenv loopback linux ls lsefi lsefimmap lsefisystab lssal memdisk minicmd normal ntfs part_apple part_msdos part_gpt password_pbkdf2 png probe reboot regexp search search_fs_uuid search_fs_file search_label sleep smbios squash4 test true video xfs zfs zfscrypt zfsinfo"
root #grub-install --target=x86_64-efi --efi-directory=/efi --modules=${GRUB_MODULES} --sbat /usr/share/grub/sbat.csv
root #sbsign /efi/EFI/GRUB/grubx64.efi --key /path/to/kernel_key.pem --cert /path/to/kernel_key.pem --out /efi/EFI/GRUB/grubx64.efi

To successfully boot with secure boot enabled the used certificate must either be accepted by the UEFI firmware, or shim must be used as a pre-loader. Shim is pre-signed with the third-party Microsoft Certificate, accepted by default by most UEFI motherboards.

How to configure the UEFI firmware to accept custom keys depends on the firmware vendor, which is beyond the scope of the handbook. Below is shown how to setup shim instead:

root #emerge sys-boot/shim sys-boot/mokutil sys-boot/efibootmgr
root #cp /usr/share/shim/BOOTX64.EFI /efi/EFI/GRUB/shimx64.efi
root #cp /usr/share/shim/mmx64.efi /efi/EFI/GRUB/mmx64.efi

Shims MOKlist requires keys in the DER format, since the OpenSSL key generated in the example here is in the PEM format, the key must be converted first:

root #openssl x509 -in /path/to/kernel_key.pem -inform PEM -out /path/to/kernel_key.der -outform DER
附注
The path used here must be the path to the pem file containing the certificate belonging to the generated key. In this example both key and certificate are in the same pem file.

Then the converted certificate can be imported into Shims MOKlist:

root #mokutil --import /path/to/kernel_key.der

And finally we register Shim with the UEFI firmware. In the following command, boot-disk and boot-partition-id must be replaced with the disk and partition identifier of the EFI system partition:

root #efibootmgr --create --disk /dev/boot-disk --part boot-partition-id --loader '\EFI\GRUB\shimx64.efi' --label 'shim' --unicode
调试 GRUB

When debugging GRUB, there are a couple of quick fixes that may result in a bootable installation without having to reboot to a new live image environment.

In the event that "EFI variables are not supported on this system" is displayed somewhere in the output, it is likely the live image was not booted in EFI mode and is presently in Legacy BIOS boot mode. The solution is to try the removable GRUB step mentioned below. This will overwrite the executable EFI file located at /EFI/BOOT/BOOTX64.EFI. Upon rebooting in EFI mode, the motherboard firmware may execute this default boot entry and execute GRUB.

重要
如果 grub-install 返回了一个错误,类似 Could not prepare Boot variable: Read-only file system,那么为了成功安装,可能必须需要将 efivars 重新挂载为读写:
root #mount -o remount,rw,nosuid,nodev,noexec --types efivarfs efivarfs /sys/firmware/efi/efivars
root #mount -o remount,rw,nosuid,nodev,noexec --types efivarfs efivarfs /sys/firmware/efi/efivars

This is caused by certain non-official Gentoo environments not mounting the special EFI filesystem by default. If the previous command does not run, then reboot using an official Gentoo live image environment in EFI mode.

一些主板制造商似乎只支持EFI系统分区(ESP)中.EFI文件的 /efi/boot/目录。 GRUB安装程序可以使用 --removable选项自动执行此操作。 在运行以下命令之前验证是否已安装ESP。 假设ESP安装在/boot(如前所述),执行:

root #grub-install --target=x86_64-efi --efi-directory=/efi --removable

这将创建UEFI规范定义的默认目录,然后将 grubx64.efi 文件复制到由同一规范定义的“默认”EFI文件位置。

配置

接下来,基于用户在/etc/default/grub文件和/etc/grub.d中特别配置的脚本文件来生成 GRUB。在大多数场景中,不需要由用户来配置,GRUB 就可以自动检测出哪个内核用于引导(位于/boot/中最高的那一个)以及根文件系统是什么。也可以使用GRUB_CMDLINE_LINUX>变量在/etc/default/grub中附加内核参数。

要生成最终的 GRUB 配置,运行grub-mkconfig命令:

root #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-6.6.21-gentoo
Found initrd image: /boot/initramfs-genkernel-amd64-6.6.21-gentoo
done

需要注意至少找到一个Linux镜像在命令的输出中,它们是用来引导系统的。如果使用一个initramfs或用genkernel建立内核,同样会检测到正确的initrd 镜像。如果不是这样,进入到/boot/并使用ls命令检查内容。如果文件确实不存在,回到内核配置和安装的介绍。

提示
os-prober 工具可与 GRUB 配合使用,以检测所连接驱动器上的其他操作系统。可检测到 Windows 7, 8.1, 10 和其他 Linux 发行版。 那些希望双引导系统的应该出现 sys-boot/os-prober 包,然后重新运行 grub-mkconfig 命令(如上所示)。 如果遇到问题,请务必先阅读 GRUB 文章,然后再向Gentoo社区请求支持。

备选 1:LILO

Emerge

LILO (the LInuxLOader,) 是Linux引导程序的久经考验的主力。但是它缺少GRUB所拥有的一些特性。LILO仍旧在一些系统上被使用的原因是GRUB无法使用但LILO却可以。当然还因为一些人是先认识了LILO而且对它忠心不二。不管怎样,Gentoo可以支持它们两个启动器。

安装LILO是一件轻而易举的事,使用emerge就可以了。

root #emerge --ask sys-boot/lilo

配置

要配置LILO,首先要创建 /etc/lilo.conf:

root #nano -w /etc/lilo.conf

在配置文件中,小节(sections)被用于指向可引导的内核。请确保内核文件(与内核版本号一起)和initramfs文件都可以被知晓,因为它们都需要被这个配置文件所引用。

附注
如果根文件系统是JFS,请在每一个引导条目之后增加 append="ro"因为JFS在它被挂载为可读写之前需要重放它的日志。
文件 /etc/lilo.confLILO 配置样本
boot=/dev/sda             # 在 MBR 安装 LILO
prompt                    # 让用户有机会选择其他项
timeout=50                # 启动默认项前等待 5(五)秒
default=gentoo            # 超时后,启动 "gentoo" 项
compact                   # 这极大地减少了加载时间,并使 map 文件更小。在有些系统上可能会失效
  
image=/boot/vmlinuz-6.6.21-gentoo
  label=gentoo            # 给这个项目起的名字
  read-only               # 从只读 root 启动。不要修改这个!
  root=/dev/sda3          # 根文件系统位置
  
image=/boot/vmlinuz-6.6.21-gentoo
  label=gentoo.rescue     # 给这个项目起的名字
  read-only               # 从只读 root 启动。不要修改这个!
  root=/dev/sda3         # 根文件系统位置
  append="init=/bin/bb"   # 启动 Gentoo 静态救援 shell
  
# 下面两行用于与 Windows 系统双启动。
# 这个实例中,Windows 在 /dev/sda6。
other=/dev/sda6
  label=windows
附注
如果您使用不同的分区方案或内核文件,请根据需要进行调整。

如果initramfs是必须的,那么就更改配置文件以便引用这个initramfs文件,并且告诉initramfs根设备的所在位置。

文件 /etc/lilo.conf添加initramfs信息到引导条目
image=/boot/vmlinuz-6.6.21-gentoo
  label=gentoo
  read-only
  append="root=/dev/sda3"
  initrd=/boot/initramfs-genkernel-amd64-6.6.21-gentoo

如果额外的选项需要被传递到内核,使用append语句。例如增加 video 语句来使能framebuffer:

文件 /etc/lilo.conf添加视频参数到引导选项
image=/boot/vmlinuz-6.6.21-gentoo
  label=gentoo
  read-only
  root=/dev/sda3
  append="video=uvesafb:mtrr,ywrap,1024x768-32@85"

使用 genkernel的用户应该了解他们的内核使用与安装CD相同的引导选项。例如,如果对SCSI设备的支持需要被使能,就增加 doscsi到内核选项中。

现在保存这个文件并退出。

安装

为了彻底完成,运行 /sbin/lilo,这样 LILO 就会把 /etc/lilo.conf 中的设置应用到系统中(也就是说安装它自己到磁盘上)。要记住每一次一个新内核被安装或者 lilo.conf 文件被改变后,/sbin/lilo 都需要执行一次,以确保在内核文件名发生改变后系统仍然能够被引导起来。

root #/sbin/lilo

备选 2:efibootmgr

在基于UEFI的系统上,系统上的UEFI固件(换句话说,主引导加载程序)可以直接操作以查找UEFI引导条目。 这样的系统不需要具有额外的(也称为辅助)引导加载器,如GRUB,以帮助引导系统。 据说,基于EFI的引导加载程序(如GRUB)存在的原因是在引导过程中“扩展”UEFI系统的功能。 使用efibootmgr是真正的那些想要采取一个极简主义(虽然更僵硬的)方法来启动他们的系统; 使用GRUB(见上文)对于大多数用户更容易,因为它在引导UEFI系统时提供了灵活的方法。

System administrators who desire to take a minimalist, although more rigid, approach to booting the system can avoid secondary bootloaders and boot the Linux kernel as an EFI stub.

记住sys-boot/efibootmgr应用程序不是一个引导器,它是一个和UEFI固件相互作用并更新它的设置,因为之前安装的Linux内核可以通过额外的选项(如果需要)来引导,或允许多重引导条目。可以通过EFI变量(需要支持EFI变量的内核)来完成这个相互作用。

一定要阅读通过 EFI stub内核文章“'再继续。 内核必须具有能够被系统的UEFI固件直接引导的特定选项。 可能需要重新编译内核。 看看efibootmgr 文章,这也是一个好主意。

It is also a good idea to take a look at the efibootmgr article for additional information.

附注
要重申,efibootmgr 不是引导UEFI系统的要求。Linux内核本身就可以启动即引导,其他内核命令行选项可以内置到Linux内核(有一个内核配置选项 CONFIG_CMDLINE )允许用户指定启动参数作为命令行选项,甚至initramfs 可以“内置”到内核。

安装 efibootmgr 软件:

root #emerge --ask sys-boot/efibootmgr

创建 /efi/EFI/Gentoo 目录,并复制内核文件到这个位置,并命名为 bzImage.efi

root #mkdir -p /boot/efi/boot
root #cp /boot/vmlinuz-* /boot/efi/boot/bzImage.efi
附注
UEFI定义强制要求使用\作为目录分割符。

接下来,告诉UEFI固件创建一个叫作“Gentoo”的引导条目,它拥有全新编译的EFI stub内核:

root #efibootmgr --create --disk /dev/sda --part 2 --label "Gentoo" --loader "\efi\boot\bzImage.efi"

如果使用一个内存文件系统(initramfs),为它添加相应的引导选项:

root #efibootmgr -c -d /dev/sda -p 2 -L "Gentoo" -l "\efi\boot\bzImage.efi" initrd='\initramfs-genkernel-amd64-6.6.21-gentoo'

Note that the above command presumes an initramfs file was copied into the ESP inside the same directory as the bzImage.efi file.

完成这些变更后,当系统重新启动时,会有一个叫作“gentoo”的引导项。

Unified Kernel Image

If installkernel was configured to build and install unified kernel images. The unified kernel image should already be installed to the EFI/Linux directory on the EFI system partition, if this is not the case ensure the directory exists and then run the kernel installation again as described earlier in the handbook.

To add a direct boot entry for the installed unified kernel image:

root #efibootmgr --create --disk /dev/sda --part 1 --label "gentoo" --loader /efi/EFI/Linux/gentoo-x.y.z.efi

备选 3:Syslinux

Syslinux是 amd64架构的另一种引导加载程序替代方案。 它不仅支持MBR,从版本6.00开始,它开始支持EFI启动。 还支持PXE(网络)引导和鲜为人知的选项。 尽管Syslinux是许多流行的引导加载程序,但它并没有得到手册的支持。 读者可以在Syslinux文章中找到有关新兴然后安装此引导加载程序的信息。

备选 4: systemd-boot

Another option is systemd-boot, which works on both OpenRC and systemd machines. It is a thin chainloader and works well with secure boot.

To install systemd-boot:

root #bootctl install
重要
Make sure the EFI system partition has been mounted before running bootctl install.

When using this bootloader, before rebooting, verify that a new bootable entry exists using:

root #bootctl list

If no new entry exists, ensure the sys-kernel/installkernel package has been installed with the systemd-boot USE flag enabled, and re-run the kernel installation.

For the distribution kernels:

root #emerge --ask --config sys-kernel/gentoo-kernel

For a manually configured and compiled kernel:

root #make install
重要
When installing kernels for systemd-boot, no root= kernel command line argument is added by default. On systemd systems that are using an initramfs users may rely instead on systemd-gpt-auto-generator to automatically find the root partition at boot. Otherwise users should manually specify the location of the root partition by setting root= in /etc/kernel/cmdline as well as any other kernel command line arguments that should be used. And then reinstalling the kernel as described above.

Optional: Secure Boot

When the secureboot USE flag is enabled, the systemd-boot EFI executable will be signed automatically. bootctl install will automatically install the signed version.

To successfully boot with secure boot enabled the used certificate must either be accepted by the UEFI firmware, or shim must be used as a pre-loader. Shim is pre-signed with the third-party Microsoft Certificate, accepted by default by most UEFI motherboards.

How to configure the UEFI firmware to accept custom keys depends on the firmware vendor, which is beyond the scope of the handbook. A postinst hook to automatically update systemd-boot and set it up with shim instead is provided on the systemd-boot wiki page. However the first time this should be done manually by following the steps below:

root #emerge --ask sys-boot/shim sys-boot/mokutil sys-boot/efibootmgr
root #cp /usr/share/shim/BOOTX64.EFI /efi/EFI/BOOT/BOOTX64.EFI
root #cp /usr/share/shim/mmx64.efi /efi/EFI/BOOT/mmx64.efi
root #cp /efi/EFI/systemd/systemd-bootx64.efi /efi/EFI/BOOT/grubx64.efi
附注
Shim is hardcoded to load grubx64.efi. As such the systemd-boot bootloader must be named as if it were GRUB.

Shims MOKlist requires keys in the DER format, since the OpenSSL key generated in the example here is in the PEM format, the key must be converted first:

root #openssl x509 -in /path/to/kernel_key.pem -inform PEM -out /path/to/kernel_key.der -outform DER
附注
The path used here must be the path to the pem file containing the certificate belonging to the generated key. In this example both key and certificate are in the same pem file.

Then the converted certificate can be imported into Shims MOKlist:

root #mokutil --import /path/to/kernel_key.der

And finally we register Shim with the UEFI firmware. In the following command, boot-disk and boot-partition-id must be replaced with the disk and partition identifier of the EFI system partition:

root #efibootmgr --create --disk /dev/boot-disk --part boot-partition-id --loader '\EFI\BOOT\BOOTX64.EFI' --label 'shim' --unicode



重启系统

退出chroot环境并unmount全部已持载分区。然后敲入一条有魔力的命令来初始化最终的、真实的测试:reboot

(chroot) livecd #exit
livecd~#cd
livecd~#umount -l /mnt/gentoo/dev{/shm,/pts,}
livecd~#umount -R /mnt/gentoo
livecd~#reboot

当别忘了移除 live 镜像,否则可能再次从 live 镜像启动,而不是新安装的 Gentoo 系统!

当重启进全新安装的 Gentoo 环境后,最好进行完成Gentoo安装





用户管理

添加一个日常使用的用户

在 Unix/Linux 系统中,用 root 进行工作是一件危险的事情,应该尽量避免。因此我们强烈推荐您为日常使用添加一个或更多的标准用户账户。

用户所属的组定义了其可以执行的活动。下表中列出了许多您可能希望使用的重要组:

Group Description
audio 允许使用声音设备
cdrom 允许直接使用光驱设备
floppy 允许直接使用软驱
games 允许运行游戏
portage 能够访问portage受限资源。
usb 允许使用USB设备
video 允许使用视频采集设备和硬件加速
wheel 可以使用su.

比如,创建一个叫作 larrywheelusersaudio 组的成员用户,首先使用 root 登录(只有 root 能创建用户)并运行 useradd

Login:root
Password: (Enter the root password)

When setting passwords for standard user accounts, it is good security practice to avoid using the same or a similar password as set for the root user.

Handbook authors recommended to use a password at least 16 characters in length, with a value fully unique from every other user on the system.

root #useradd -m -G users,wheel,audio -s /bin/bash larry
root #passwd larry
Password: (Enter the password for larry)
Re-enter password: (Re-enter the password to verify)

如果一个用户仍需要以 root 身份做一些任务,他们可以使用 su - 来临时得到 root 权限。另一种方式是使用 sudoapp-admin/sudo)或者 doas (app-admin/doas) 工具,如果配置正确的话,非常安全。

磁盘清理

删除安装后的残留文件

如果 Gentoo 安装完毕并且系统已经重启过,并且所有事情都完成好了,那么我们现在就要从硬盘上删除下载的 stage3 的 tar 包。记住它们下载在 / 目录。

The files are located in the / directory and can be removed with the following command:

root #rm /stage3-*.tar.*

下一步该做什么?

不知道接下来该做什么?现在有许多途径可以探索……Getoo 为用户提供了大量的可能性,因此也已经在 wiki 和其他与 Gentoo 相关的子域名上提供大量的文档(少量没有),可以通过这些文档来进行探索(参见下面的 Gentoo 在线章节)。

额外的文档

需要注意的是,由于 Gentoo 中可用的选项数量众多,手册提供的文档范围有限——它主要侧重于启动和运行 Gentoo 系统的基础知识以及基本的系统管理活动。该手册有意排除了有关图形环境的说明、有关强化的详细信息以及其他重要的管理任务。话虽如此,本手册还有更多部分可以帮助读者使用更多基本功能。

读者一定要看一下手册的下一章节使用 Gentoo,讲述了如何保障软件是最新的、如何安装额外的软件包、USE 标记的更多细节、OpenRC init 系统,以及与 Gentoo 系统安装后,管理相关的各种其他信息话题。

除了这本手册,也鼓励读者去探索Gentoo维基的其他角落来寻找更多的、社区提供的文档。Gentoo wiki 组同时提供一个文档概述,其中按照类别列出了一系列的维基文档。比如,它指向的本地化指南能使系统更有家的感觉(特别适用于以英语为第二语言的用户)。

大多数使用桌面的用户将设置图形环境。有许多社区维护支持桌面环境(DEs)窗口管理器 (WMs)的 'meta' 文章。读者应该知道,每个桌面环境设置有略微不同的步骤,这会增加 Bootstrap 的复杂性。

许多其他的 Meta 文章 为我们的读者提供了对 Gentoo 中可用软件的高级概述。

Gentoo 在线

重要
读者应注意,所有在线的 Gentoo 官方网站均受Gentoo的 行为准则约束。活跃于 Gentoo 社区是一种特权,而不是一种权利,用户应该知道行为准则的存在是有原因的。

除了 Libera.Chat 托管的因特网中继聊天( internet relay chat IRC)网络和邮件列表之外,大多数Gentoo 网站要求每个站点都有一个帐户,以便提问、展开讨论或上报 Bug。

论坛 和 IRC

欢迎每个用户来我们的 Gentoo 论坛 或我们的 Gentoo 的 因特网中继聊天( internet relay chat IRC)频道。有很多以前发现的新 Gentoo 安装遇到的问题,在获得一些反馈后得以解决,这些问题的经验可以在论坛中轻松地搜索查看。其他用户第一次使用 Gentoo 遇到安装问题的可能性非常令人惊讶。建议用户在 Gentoo 支持频道寻求帮助之前搜索论坛和 wiki。

邮件列表

这些是提供给社区成员的一些邮件列表,他们更愿意通过电子邮件请求支持或反馈,而不是在论坛或IRC上创建用户帐户。用户需要按照说明进行操作,以便订阅特定的邮件列表。

Bug

有时,在查看 wiki、搜索论坛、在IRC频道或邮件列表中寻求支持之后,并没有问题已知的解决方案。一般来说,这是在 Gentoo 的 Bugzilla 网站 上报告 bug 的信号。

开发指南

希望了解更多有关开发 Gentoo 的读者可以查看开发指南。该指南提供了有关编写 ebuild、使用 eclass 的说明,并提供了 Gentoo 开发中许多基本概念的定义。

结语

Gentoo 是一个健壮、灵活、维护良好的发行版。开发者社区很高兴听到关于如何使Gentoo 成为一个“更好的”发行版的反馈。

在此提醒,任何关于 “本手册”的反馈应该按照如何改进手册?章节中开头的详细准则。

我们期待看到我们的用户将如何选择使用 Gentoo 满足他们独特的用例和需求。



Warning: Display title "Gentoo Linux 手册:安装Gentoo" overrides earlier display title "手册:AMD64/完整/安装".