如何将我的 EC2 Ubuntu 实例的默认 MBR 分区方案转换为 GPT,以绕过 EBS 卷上 MBR 分区的 2-TiB 限制?

4 分钟阅读
0

我的 Amazon Elastic Compute Cloud(Amazon EC2)实例运行 Ubuntu 16.04、18.04 或 20.04。MBR 分区有 2-TiB 的限制。我想要将我的 Amazon Elastic Block Store(Amazon EBS)卷上的分区方案转换为 GPT 分区方案,以绕过此限制。

解决方法

**警告:**在停止并启动实例之前,请考虑以下事项:

  • 如果您的实例是由实例存储备份,或者具有包含数据的实例存储卷,则数据会在实例停止时删除。有关详细信息,请参阅确定实例的根设备类型
  • 如果您的实例是 Amazon EC2 自动扩缩组的一部分,则停止该实例可能会导致其终止。如果您使用 Amazon EMR、AWS CloudFormation 或 AWS Elastic Beanstalk 启动该实例,则您的实例可能位于 AWS 自动扩缩组中。在这种情况下,实例终止取决于您的自动扩缩组的实例横向缩减保护设置。如果您的实例是自动扩缩组的一部分,在开始执行解决步骤之前,请暂时从此自动扩缩组中删除该实例
  • 停止和启动实例会更改实例的公共 IP 地址。当您将外部流量路由到您的实例时,最佳做法是使用弹性 IP 地址而不是公有 IP 地址。如果您使用的是 Amazon Route 53,则在公共 IP 发生变化时可能需要更新 Route 53 DNS 记录
  • 如果实例的关闭行为设置为终止,则该实例会在停止时终止。为避免出现此问题,请更改实例关闭行为

**注意:**最佳做法是在开始任何解决步骤前创建 EBS 卷的备份

1.    打开 Amazon EC2 控制台

2.    从运行 Ubuntu 的亚马逊机器映像(AMI)中启动实例

3.    在与第一个实例相同的可用区中从具有 3-TiB 根卷的相同 Ubuntu AMI 启动第二个实例。

4.    停止具有 3-TiB 根卷的实例

5.    从已停止的实例分离根卷/dev/xvda/dev/sda1)。然后,将其作为 /dev/sdf 附加到您在步骤 2 中创建的实例。

6.    使用 SSH 连接到正在运行的实例(在步骤 2 中创建)。

7.    使用 lsblk 命令查看 /dev/sdf 的根分区。/dev/sdf 根分区的大小仅为 2-TiB,如下面的示例所示:

# 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 的空间。

相关信息

针对 EBS 卷的大小和配置的限制

AWS 官方
AWS 官方已更新 8 个月前