准备磁盘

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Handbook:AMD64/Installation/Disks and the translation is 100% complete.
AMD64 手册
安装
关于安装
选择安装媒介
配置网络
准备磁盘
安装 stage3
安装基础系统
配置内核
配置系统
安装系统工具
配置引导程序
安装收尾
使用 Gentoo
Portage 介绍
USE 标记
Portage 功能特性
Initscript 系统
环境变量
使用 Portage
文件和目录
变量
混合使用不同的软件分支
额外的工具
自定义软件包仓库
高级特性
配置网络
开始
高级配置
模块化网络
无线网络
添加功能
动态管理


块设备简介

块设备

让我们来好好看看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: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: HGST HTS721010A9
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xf163b576

现在这块硬盘已准备好创建新的分区。

创建引导分区

首先,创建一个将挂载到 /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-1953525167, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1953525167, default 1953525167): +1G

<div lang="en" dir="ltr" class="mw-content-ltr">
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 安装文件