gPT fdisk

来自 Arch Linux 中文维基

GPT fdisk 是由 Rod Smith 编写的一套用于硬盘分区的文本模式工具集,由 gdiskcgdisksgdiskfixparts 组成。它适用于 GUID(Globally Unique Identifier)分区表(GPT)硬盘,而不是过去常用的 MBR(Master Boot Record)分区表。

gdiskcgdisksgdisk 在功能上一致,但提供了不同类型的用户界面。gdisk 提供文本交互模式,sgdisk 提供命令行模式,而 cgdisk 有基于 curses 的用户界面。该篇文章涵盖了 gdisk(8)sgdisk(8) 工具的使用。

提示:

安装[编辑 | 编辑源代码]

安装 gptfdisk

列出分区[编辑 | 编辑源代码]

要显示块设备上的分区表和分区信息,可以使用如下命令,其中设备名类似于 /dev/sda/dev/nvme0n1/dev/mmcblk0 等:

# gdisk -l /dev/sda

或者使用 sgdisk 命令:

# sgdisk -p /dev/sda

备份和恢复分区表[编辑 | 编辑源代码]

修改磁盘前,请先备份磁盘的分区表,可以使用备份将相同的分区布局复制到多个设备上。

sgdisk 可以创建一个二进制备份,包含 MBR, GPT 主表头,GPT 备份表头和分区表副本。下面示例将 /dev/sda 的分区表信息备份到 sgdisk-sda.bin:

# sgdisk -b=sgdisk-sda.bin /dev/sda

通过下面命令恢复备份:

# sgdisk -l=sgdisk-sda.bin /dev/sda

如果要复制分区到其它磁盘,例如从 /dev/sda 复制到 /dev/sdc:

# sgdisk -R=/dev/sdc /dev/sda

如果两个磁盘位于同一个计算机,使用下面命令设置随机的分区 GUID:

# sgdisk -G /dev/sdc

创建分区表和分区[编辑 | 编辑源代码]

分区操作的第一步是创建一个分区表。创建分区表后,就可以根据目标分区方案来创建分区了。

在开始之前,建议先备份当前的分区表和分区布局。

下面的步骤演示了如何使用 gdisk 来创建分区表和分区。另外,你也可以使用基于 curses 的 cgdisk,但下面的操作步骤就不再适用于你了。对应的用法可参考 cgdisk(8)

gdisk 自动将分区按 2048 个 512 字节扇区(1 MiB)对齐,这一设置兼容所有先进格式化 HDD 和大部分的 SSD

使用 gdisk时, 将要编辑的块设备作为命令参数。以 /dev/sda 为例:

# gdisk /dev/sda

创建新分区表[编辑 | 编辑源代码]

警告: 在已经保存数据的磁盘上创建新分区,会删除磁盘上的所有数据。
提示:在分区前,先确认你的 NVMe 硬盘和先进格式化硬盘是否使用了最佳逻辑扇区大小

在提示中输入 o 来创建新的 GPT 分区表并清除当前所有的分区数据。如果分区已经创建了你需要的分区表,请跳过此步骤。

创建分区[编辑 | 编辑源代码]

在提示中输入 n 命令来创建新分区。你需要提供分区编号,起始扇区位置,末端扇区位置及分区类型。

注意: 对于分区大小和位置的相关问题可参考分区#分区方案

分区编号[编辑 | 编辑源代码]

分区编号是分配给分区的一个数字,例如:磁盘 /dev/sda 上编号为 1 的分区是 /dev/sda1,类似的有 /dev/nvme0n1 上的 /dev/nvme0n1p1/dev/mmcblk0 上的 /dev/mmcblk0p1。命名方案的细节可参考 Device file#Partition。分区编号不一定与分区在硬盘上的顺序匹配,在这种情况下你可以对它们进行排序

建议使用 gdisk 提出的默认编号。

起始和末端扇区[编辑 | 编辑源代码]

分区的起始和末端扇区可以用扇区数或是以千字节(K),兆字节(M),吉字节(G),太字节(T)或是皮字节(P)为单位的位置进行指定。

位置可以以用下方式进行指定:

  • 从硬盘起始端开始计算的绝对值,例如:40M 起始端意味着扇区从硬盘的第 40 MiB 开始。
  • +size-size 为格式的相对位置,例如:+2G 代指默认起始扇区后 2 GiB 的位置,而 -200M 代指最后一个可用扇区往前 200 MiB 的位置。

Pressing the Enter key with no input specifies the default value, which is the start of the largest available block for the first sector and the end of the same block for the last sector.

注意: 确保使用 +size{M,G,T,P} 格式的相对值指定分区大小,且不使用小于 1 MiB 的值,这样分区就能始终和设备属性对齐。
提示:为了防备需要创建 BIOS 启动分区的情况发生,建议在硬盘前 2 TiB 的任意位置保留一个 1 MiB 大小的空余空间(例如使用 +1M 参数创建首个分区)。

分区类型[编辑 | 编辑源代码]

可以通过输入 gdisk 内置类型编码或手动指定分区类型 GUID 来选择分区类型。默认的 Linux filesystem(GUID 0FC63DAF-8483-4772-8E79-3D69D8477DE4,gdisk 内置编码 8300)类型适用于大多数用例。

提示:
常见分区类型
分区类型 挂载点 gdisk 编码 分区类型 GUID
Linux 文件系统 Any 8300 0FC63DAF-8483-4772-8E79-3D69D8477DE4
EFI 系统分区 Any1 ef00 C12A7328-F81F-11D2-BA4B-00A0C93EC93B
BIOS 启动分区 None ef02 21686148-6449-6E6F-744E-656564454649
Linux x86-64 根分区 (/) / 8304 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
Linux swap [SWAP] 8200 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
Linux /home /home 8302 933AC7E1-2EB4-4F13-B844-0E14E2AEF915
Linux /srv /srv 8306 3B8F8425-20E0-4F3B-907F-1A25A76F98E8
Linux /var /var1 8310 4D21B016-B534-45C2-A9FB-5C16E091FD2D
Linux /var/tmp /var/tmp1 8311 7EC6F557-3BC5-4ACA-B293-16EF5DF639D1
Linux LVM Any 8e00 E6D6D379-F507-44C2-A23C-238F2A3DF928
Linux RAID Any fd00 A19D880F-05FC-4D3B-A006-743F0F84911E
Linux LUKS Any 8309 CA7D7CCB-63ED-4C53-861C-1742536059CC
Linux dm-crypt Any 8308 7FFEC5C9-2D00-49B7-8941-3EA10A5586B7
  1. systemd-gpt-auto-generator(8) 只会在满足特定条件的情况下自动挂载分区,详细信息可参考 systemd#GPT分区自动挂载

重复这一步骤,直到创建完所有需要的分区。

写入更改到硬盘[编辑 | 编辑源代码]

提示:为了方便区分,可以使用 c 命令修改分区名称(分区标签)。

使用 w 命令来向硬盘写入分区表并退出。

提示和技巧[编辑 | 编辑源代码]

在 MBR 和 GPT 之间转换[编辑 | 编辑源代码]

提示:更多信息请阅读 Rod Smith 的 MBR 与 GPT 互转换说明

gdisksgdiskcgdisk 能将 MBR 和 BSD 盘符 无损转换为 GPT。转换时,所有 MBR 主分区和逻辑分区都会变成 GPT 分区,并为每个分区生成正确的分区类型 GUID 和唯一分区 GUID。

转换之后,需要重新安装并配置引导加载程序才能正常从 GPT 启动。

警告:
  • GPT 在硬盘末端存放了第二分区表。该数据结构默认消耗 33 个 512 字节(16.5 KiB)扇区大小。MBR 在硬盘末端没有类似的数据结构,也意味着如果 MBR 硬盘上的最后一个分区占用了硬盘的最末端,会导致转换无法完成。如果你遇到了这种情况,需要放弃转换,并调整最后一个分区的大小。

这篇文章的某些内容需要扩充。

原因: 提供该问题的相关引用。 (在 Talk:GPT fdisk 中讨论)
  • 已知基于 Intel 芯片组并在 RAID 模式下运行的笔记本存在备份 GPT 表损坏问题,解决方法是尽可能使用 AHCI 替代 RAID。

要用 sgdisk 将 MBR 分区表转换为 GPT,使用 -g/--mbrtogpt 选项:

# sgdisk -g /dev/sda

要将 GPT 转换为 MBR,使用 -m/--gpttombr 选项,注意最多只能转换四个分区。

# sgdisk -m /dev/sda

Sort partitions[编辑 | 编辑源代码]

This applies for when a new partition is created in the space between two partitions or a partition is deleted. /dev/sda is used in this example.

# sgdisk -s /dev/sda

After sorting the partitions if you are not using Persistent block device naming, it might be required to adjust the /etc/fstab and/or the /etc/crypttab configuration files.

注意: The kernel must read the new partition table for the partitions (e.g. /dev/sda1) to be usable. Reboot the system or tell the kernel to reread the partition table.

Recover GPT header[编辑 | 编辑源代码]

In case main GPT header or backup GPT header gets damaged, you can recover one from the other with gdisk. /dev/sda is used in this example.

# gdisk /dev/sda

choose r for recovery and transformation options (experts only). From there choose either

  • b: use backup GPT header (rebuilding main)
  • d: use main GPT header (rebuilding backup)

When done write the table to disk and exit via the w command.

Expand a GPT disk[编辑 | 编辑源代码]

After enlarging a disk (e.g. in hardware RAID or a virtual machine disk) the newly added free space will not be immediately usable since GPT keeps data at the end of the disk. You must relocate the backup GPT header to the new end of the disk.

Run sgdisk with the option -e/--move-second-header, e.g.:

# sgdisk -e /dev/sda

Afterwards print the partition table; the total free space should now be increased.

Prevent GPT partition automounting[编辑 | 编辑源代码]

systemd-gpt-auto-generator(8) will automount partitions following the Discoverable Partitions Specification. Sometimes that may not be desirable.

The automounting can be disabled by setting the partition attribute 63 "do not automount" on the partition.

Start gdisk, e.g.:

# gdisk /dev/sda

Press p to print the partition table and take note of the partition number(s) of the for which you want to disable automounting.

Press x extra functionality (experts only).

Press a set attributes. Input the partition number and set the attribute 63. Under Set fields are: it should now show 63 (do not automount). Press Enter to end attribute changing. Repeat this for all partitions you want to prevent from automounting.

When done write the table to disk and exit via the w command.

Alternatively using sgdisk, the attribute can be set using the -A/--attributes= option; see sgdisk(8) for usage. For example, to set partition attribute 63 "do not automount" on /dev/sda2 run:

# sgdisk -A 2:set:63 /dev/sda

gdisk EFI application[编辑 | 编辑源代码]

本文内容或本节内容已经过期。

原因: gdisk_x64.efi does not seem to work anymore and is apparently unmaintained. (在Talk:GPT fdisk讨论)

There is no package for the EFI version of gdisk, but Rod Smith provides a prebuilt gdisk-1.04 EFI binary on SourceForge. Download gdisk-efi-*.zip and extract the archive. To use it, copy gdisk_x64.efi to the EFI system partition and launch it from your boot loader or UEFI Shell.

gdisk_x64.efi allows you to edit the partition table before the operating system is even booted. It is used the same way as the gdisk binary on Linux.

注意: gdisk_x64.efi cannot access the file system, thus it cannot backup the partition table to a file or restore it from a backup file.

See README-efi.txt for more information.

参阅[编辑 | 编辑源代码]