如何将我的 EC2 Ubuntu 实例的默认 MBR 分区方案转换为 GPT,以绕过 EBS 卷上的 MBR 分区的 2TiB 限制?
上次更新时间:2023 年 2 月 13 日
我的 Amazon Elastic Compute Cloud (Amazon EC2) 实例正在运行 Ubuntu 16.04、18.04 或 20.04。MBR 分区有 2 TiB 的限制。我想要将我的 Amazon Elastic Block Store (Amazon EBS) 卷上的默认 MBR 分区方案转换为 GPT 分区方案,以绕过此限制。
解决方法
警告:在停止并启动实例之前,请考虑以下事项:
- 如果您的实例受实例存储支持或具有包含数据的实例存储卷,则在实例停止时数据将丢失。有关更多信息,请参阅确定实例的根设备类型。
- 如果您的实例是 Amazon EC2 Auto Scaling 组的一部分,停止实例可能会终止实例。如果您使用 Amazon EMR、AWS CloudFormation 或 AWS Elastic Beanstalk 启动实例,则您的实例可能是 AWS Auto Scaling 组的一部分。如果情况如此,则是否会发生实例终止取决于您的自动扩缩组的实例缩减保护设置。如果您的实例是自动扩缩组的一部分,则在开始执行解决步骤之前,请暂时从自动扩缩组中删除该实例。
- 停止和启动实例会更改实例的公共 IP 地址。在将外部流量路由到您的实例时,最佳做法是使用弹性 IP 地址,而不是公有 IP 地址。如果您使用 Amazon Route 53,那么您可能必须在公有 IP 更改时更新 Route 53 DNS 记录。
- 如果实例的关闭行为设置为终止,则实例停止时将会终止。为避免出现此问题,请更改实例关闭行为。
注意:最佳实践是在开始任何解决步骤前创建 EBS 卷的备份。
1. 打开 Amazon EC2 控制台。
2. 从运行 Ubuntu 的 Amazon 系统映像 (AMI) 中启动实例。
3. 在与第一个实例相同的可用区中用具有 3TiB 根卷的相同 Ubuntu AMI 启动第二个实例。
4. 停止具有 3TiB 根卷的实例。
5. 从已停止的实例分离根卷(/dev/xvda 或 /dev/sda1)。然后, 将其作为 /dev/sdf 附加到您在步骤 2 中创建的实例。
6. 使用 SSH 连接到正在运行的实例(在步骤 2 中创建)。
7. 使用 lsblk 命令查看 /dev/sdf 的根分区。/dev/sdf 根分区的大小仅为 2TiB,如下面的示例所示:
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
(snip)
xvdf 202:80 0 3T 0 disk
└─xvdf1 202:81 0 2T 0 part
(snip)
注意:在 Nitro 实例类型上,区块设备名称与以下示例类似:/dev/nvme1n1。
8. 使用 gdisk 工具将分区表从 MBR 转换为 GPT。
注意:如果在 gdisk 中输入的命令不正确,则使用 q 退出 gdisk 而不保存更改,或者按 Ctrl+C。
# sudo gdisk /dev/xvdf
GPT fdisk (gdisk) version 1.0.1
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY
DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
9. 在提示中输入以下命令,以进入专家模式并设置扇区对齐值。
注意:确保将扇区对齐值从默认的 8 更改为 1。在创建 GPT 分区时使用 8 字节对齐可能会导致出现问题。
Command (? for help): x
Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): 1
Expert command (? for help): m
10. 在提示中输入以下命令,以创建 GPT 分区。在最后一个扇区提示中键入 enter,以使用默认扇区编号 2047。
Command (? for help): n
Partition number (2-128, default 2): 128
First sector (34-6291455966, default = 4294967296) or {+-}size{KMGTP}: 34
Last sector (34-2047, default = 2047) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): ef02
Changed type of partition to 'BIOS boot partition'
注意:在前述示例中,ef02 是 BIOS 启动分区编号。
11. 在提示中输入以下命令,以删除根分区:
Command (? for help): d
Partition number (1-128): 1
12. 在提示中输入以下命令,以将根分区重新创建为 3-TiB。在第一个扇区、最后一个扇区和十六进制代码或 GUID 提示中,键入 enter 以使用默认设置。
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (2048-6291455966, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
13. 在提示中输入以下命令,以保存 GPT 分区表:
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/xvdf.
The operation has completed successfully.
运行 lsblk 命令以查看新的卷信息:
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf 202:80 0 3T 0 disk
└─xvdf1 202:81 0 3T 0 part
14. 使用 fsck 工具来检查设备 /dev/xvdf1 的文件系统是否正确。有关 fsck 工具的更多信息,请参阅 Ubuntu 网站上的 fsck。
注意:在 e2fsck 命令中添加 -y 开关将会假定所有问题的答案均为 yes。
# sudo e2fsck -f /dev/xvdf1
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
cloudimg-rootfs: 57524/262144000 files (0.0% non-contiguous), 16648272/536870655 blocks
15. 运行 resize 命令调整文件系统的大小,以将其扩展至 3-TiB。
注意:调整文件系统的大小可能需要 10-20 秒。
# sudo resize2fs /dev/xvdf1
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/xvdf1 to 786431739 (4k) blocks.
The filesystem on /dev/xvdf1 is now 786431739 (4k) blocks long.
16. 在设备 /dev/xvdf1 上安装 Grub,并使用以下命令对其进行配置:
Ubuntu 16.04 或 18.04:
sudo mount /dev/xvdf1 /mnt
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo mount --bind /dev /mnt/dev
sudo chroot /mnt /bin/bash
grub-install /dev/xvdf
grub-mkdevicemap
update-grub
exit
sudo umount -l /mnt/dev
sudo umount -l /mnt/sys
sudo umount -l /mnt/proc
sudo umount -l /mnt
Ubuntu 20.04:
sudo mount /dev/xvdf1 /mnt
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo mount --bind /dev /mnt/dev
sudo chroot /mnt /bin/bash
grub-install /dev/xvdf
grub-mkdevicemap
echo "GRUB_DISABLE_OS_PROBER=true" > /etc/default/grub
echo "GRUB_FORCE_PARTUUID=" > /etc/default/grub.d/40-force-partuuid.cfg
update-grub
exit
sudo umount -l /mnt/dev
sudo umount -l /mnt/sys
sudo umount -l /mnt/proc
sudo umount -l /mnt
17. 将卷 /dev/xvdf 与正在运行的实例分离。
18. 将卷 /dev/xvdf 作为 /dev/xvda 或 /dev/sda1 附加回它的原始实例。
19. 使用 SSH 启动原始实例。
20. 运行 lsblk 命令,以验证原始实例上的根卷现在具有 3-TiB 的空间。