如何将我的 EC2 Ubuntu 实例的默认 MBR 分区方案转换为 GPT?

4 分钟阅读
0

我的 Amazon Elastic Compute Cloud (Amazon EC2) 16.04、18.04 或 20.04 Ubuntu 实例的 MBR 分区的最大配额为 2 太字节 (TiB)。为了绕过此配额,我想在我的 Amazon Elastic Block Store (Amazon EBS) 卷上转换为 GPT 分区方案。

简短描述

Ubuntu 版本 22.04 及更高版本默认使用 GPT 分区。但是,必须为 Ubuntu 版本 16.04、18.04 和 20.04 手动设置 GPT 分区。

解决方法

**重要事项:**在停止和启动实例之前,请执行以下操作:

**注意:**当停止或启动某个实例时,该实例的公共 IP 地址将发生变化。最佳实践是使用弹性 IP 地址而不是公共 IP 地址将外部流量路由到您的实例。如果您使用的是 Amazon Route 53,则在公共 IP 地址发生变化时可能需要更新 Route 53 DNS 记录

要将您的 Ubuntu 实例的默认 MBR 分区方案转换为 GPT,请完成以下步骤:

  1. 打开 Amazon EC2 控制台

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

  3. 从位于同一个可用区中具有 3 TiB 根卷的同一 Ubuntu AMI 启动第二个实例

  4. 停止第二个实例

  5. 将 /dev/xvda 或 /dev/sda1 根卷与第二个实例分离。然后,将根卷作为 /dev/sdf 连接到第一个实例。

  6. 使用 SSH 连接到第一个实例

  7. 要查看 /dev/sdf 的根分区,请运行以下 lsblk 命令:

    sudo 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)

    **注意:**在前面的示例中,/dev/sdf 的根分区为 2 太字节 (TB)。在 Nitro 实例类型上,块设备名称类似于 /dev/nvme1n1

  8. 要将分区表从 MBR 转换为 GPT,请使用 gdisk 工具运行以下命令:

    sudo gdisk /dev/xvdf

    **注意:**如果您在 gdisk 中输入了错误的命令,请按 qCtrl-C 退出 gdisk 而不保存更改。
    输出示例:

    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. 要进入 Expert(专家)模式并设置扇区对齐值,请在提示符处输入以下值:

    Command (? for help): x                                            
    Expert command (? for help): l                                     
    Enter the sector alignment value (1-65536, default = 2048): 1      
    Expert command (? for help): m

    **注意:**在每个提示符后按 Enter。前面的配置将 sector alignment(扇区对齐)值从默认的 8 更改为 1。如果您使用 8 字节 (B) 对齐方式,则在创建 GPT 分区时可能会遇到问题。

  10. 要创建 GPT 分区,请在提示符处输入以下值:

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}:                                              
Hex code or GUID (L to show codes, Enter = 8300): ef02                                                     

**注意:**在每个提示符后按 Enter。当您为 Last sector(最后一个扇区)提示符按 Enter 时,您会收到 Current type is 'Linux filesystem'(当前类型为“Linux 文件系统”)输出。在上述示例中,ef02 是 BIOS 启动分区编号。
输出示例:

Changed type of partition to 'BIOS boot partition'
  1. 要删除根分区,请在提示符处输入以下值:
Command (? for help): d                                                                                                         
Partition number (1-128): 1

**注意:**在每个提示符后按 Enter。 要将根分区重新创建为 3 TiB,请在提示符处输入以下值:

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}:                       
Hex code or GUID (L to show codes, Enter = 8300):

**注意:**在每个提示符后按 Enter。当您为 Last sector(最后一个扇区)提示符按 Enter 时,您会收到 Current type is 'Linux filesystem'(当前类型为“Linux 文件系统”)输出。要使用默认设置,请为 First sector(第一个扇区)、Last sector(最后一个扇区)和 Hex code or GUID(十六进制代码或 GUID)提示符按 Enter
输出示例:

Changed type of partition to 'Linux filesystem'
  1. 要保存 GPT 分区表,请在提示符处输入以下值:
Command (? for help): w                                                                                                        
Do you want to proceed? (Y/N): y

**重要事项:**此操作会覆盖现有分区。
输出示例:

OK; writing new GUID partition table (GPT) to /dev/xvdf.
The operation has completed successfully.
  1. 要查看新的卷信息,请运行 lsblk 命令:
sudo lsblk

输出示例:

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf    202:80   0   3T  0 disk
└─xvdf1 202:81   0   3T  0 part
  1. 要检查 /dev/xvdf1 设备的文件系统是否正确,请使用 fsck 工具运行以下命令:
sudo e2fsck -f /dev/xvdf1

**注意:**要对所有问题回答 yes(是),请在 e2fsck 命令中添加 -y 开关。有关 fsck 工具的详细信息,请参阅 Ubuntu 网站上的 fsck.8.gz
输出示例:

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
  1. 要将文件系统大小调整为 3 TiB,请运行以下 resize 命令:
sudo resize2fs /dev/xvdf1

**注意:**调整文件系统大小可能需要 10~20 秒。
输出示例:

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.
  1. /dev/xvdf1 设备上安装 Grub,然后使用以下命令根据您的 Ubuntu 版本配置 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-l /mnt/dev sudo umount -l /mnt/sys sudo umount -l /mnt/proc sudo umount -l /mnt`
  1. 将 /dev/xvdf 卷与正在运行的实例分离
  2. 将 /dev/xvdf 卷作为 /dev/xvda/dev/sda1 连接回第一个实例。
  3. 使用 SSH 连接到第一个实例,然后启动实例
  4. 要验证目前原始实例上的根卷是否具有 3 TiB 的空间,请运行 lsblk 命令:
sudo lsblk

相关信息

Amazon EBS 数量约束

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