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

7 分钟阅读
0

我的 Amazon Elastic Compute Cloud (Amazon EC2) 实例通过 centos.org 运行来自 AWS Marketplace 的 CentOS 7。MBR 分区有 2 TiB 的限制。我想要将我的 Amazon Elastic Block Store (Amazon EBS) 卷上的分区方案转换为 GPT 分区方案,以绕过此限制。

解决方法

**重要事项:**在停止和启动实例之前,请务必查看以下内容:

  • 当您停止和启动实例时,实例存储数据会丢失。如果您的实例是由实例存储备份,或者具有包含数据的实例存储卷,则实例停止时数据会丢失。有关详细信息,请参阅 Amazon EC2 实例的根卷
  • 如果您的实例是 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 卷的备份

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

  1. 打开 Amazon EC2 控制台

  2. 从运行 CentOS 7 的 AWS Marketplace 亚马逊机器映像 (AMI) 启动实例。

  3. 在与第一个实例相同的可用区域中,从具有 3 TiB 根卷的同一 CentOS AMI 启动第二个实例。
    **注意:**如果已有 CentOS 7 实例,则无需启动新的 CentOS 7 实例。对于 CentOS 7,请修改根卷,将其大小扩展到 2 TiB 以上。

  4. 停止具有 3 TiB 根卷的实例,然后停止在步骤 2 中创建的实例。
    **注意:**由于 CentOS 7 来自 AWS Marketplace AMI,因此必须先停止实例,然后才能附加带有 AWS Marketplace 代码的卷。如不停止实例,会出现以下错误: "Error attaching volume: Cannot attach volume 'vol-#################' with Marketplace codes as the instance 'i-################' is not in the 'stopped' state."

  5. 从已停止的实例中分离根卷/dev/xvda/dev/sda1)。然后,将其作为 /dev/sdf 附加到在步骤 2 中创建的实例。
    **注意:**如果使用 Xen 实例类型,则设备名称会显示为 /dev/sdf/dev/sda。如果使用 Nitro 实例类型,则设备名称会显示为 /dev/nvme0n1/dev/nvme1n1

  6. 启动您在步骤 2 中启动的实例,然后使用 SSH 连接到该实例。

  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)

    注意:****/dev/xvdf1 可能作为根文件系统“/”挂载,如以下示例所示。如果发生这种情况,请停止并启动该实例,直到 /dev/xvda1 挂载为根文件系统为止。

    # lsblk
    NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0   8G  0 disk
    └─xvda1 202:1    0   8G  0 part /
    xvdf    202:80   0   3T  0 disk
    └─xvdf1 202:81   0   2T  0 part
  8. 要将分区表从 MBR 转换为 GPT,请使用 gdisk 工具:

    # 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!

    **注意:**要安装 gdisk 工具,请运行以下命令:

    sudo yum install gdisk -y
  9. 要创建 GPT 分区,请在提示符处运行以下命令。在 Last sector(最后一个扇区)提示符处输入 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 启动分区编号。

  10. 要删除根分区,请在提示符处运行以下命令:

Command (? for help): d
Partition number (1-128): 1
  1. 要将根分区重新创建为 3 TiB,请在提示符处运行以下命令。在 First sector(第一个扇区)、Last sector(最后一个扇区)和 Hex code(十六进制代码)或 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'
  1. 要保存 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.

要查看新分区信息,请运行以下命令:

sudo gdisk -l /dev/xvdf
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/xvda: 6291456000 sectors, 2.9 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 35D6B819-1C79-4EC0-920F-4D1191609505
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6291455966
Partitions will be aligned on 8-sector boundaries
Total free space is 0 sectors (0 bytes)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      6291455966   2.9 TiB     8300  Linux filesystem
 128              34            2047   1007.0 KiB  EF02  BIOS boot partition
  1. 要检查设备 /dev/xvdf1 的文件系统是否正确,请运行文件系统检查和修复工具命令。
    CentOS 7:
# sudo xfs_repair /dev/xvdf1
Phase 1 - find and verify superblock...
        - reporting progress in intervals of 15 minutes
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - 05:27:07: scanning filesystem freespace - 1025 of 1025 allocation groups done
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - 05:27:07: scanning agi unlinked lists - 1025 of 1025 allocation groups done
        - process known inodes and perform inode discovery...
        - agno = 960
        - agno = 0
        - agno = 240
        - agno = 480

(snip)
        - agno = 238
        - agno = 239
        - 05:27:08: process known inodes and inode discovery - 25856 of 25856 inodes done
        - process newly discovered inodes...
        - 05:27:08: process newly discovered inodes - 1025 of 1025 allocation groups done
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - 05:27:08: setting up duplicate extent list - 1025 of 1025 allocation groups done
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - agno = 4

(snip)
        - agno = 1021
        - agno = 1022
        - agno = 1023
        - agno = 1024
        - 05:27:08: check for inodes claiming duplicate blocks - 25856 of 25856 inodes done
Phase 5 - rebuild AG headers and trees...
        - 05:27:08: rebuild AG headers and trees - 1025 of 1025 allocation groups done
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
        - 05:27:08: verify and correct link counts - 1025 of 1025 allocation groups done
done

CentOS 6:

# sudo e2fsck -f /dev/xvdf1
e2fsck 1.41.12 (17-May-2010)
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
/dev/xvdf1: 18734/524288 files (0.2% non-contiguous), 284948/2096896 blocks
  1. 要调整文件系统的大小,将其扩展到 3 TiB,请运行 xfs_growfsresize2fs 命令。
    CentOS 7:
# sudo mount -o nouuid /dev/xvdf1 /mnt
# sudo xfs_growfs /dev/xvdf1
meta-data=/dev/xvdf1             isize=512    agcount=1025, agsize=524224 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=536870656, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 536870656 to 786431739

CentOS 6:

# sudo resize2fs /dev/xvdf1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/xvdf1 to 786431739 (4k) blocks.

The filesystem on /dev/xvdf1 is now 786431739 blocks long.
  1. 在设备 /dev/xvdf1 上安装 Grub,然后使用以下命令进行配置。
    **注意:**CentOS 6 不需要安装 Grub。如果您使用的是 CentOS 6,请跳到步骤 15。
    CentOS 7:
sudo mount --bind /proc /mnt/procsudo mount --bind /sys /mnt/sys
sudo mount --bind /dev /mnt/dev
sudo chroot /mnt /bin/bash
grub2-install /dev/xvdf
exit
sudo umount -l /mnt/dev
sudo umount -l /mnt/sys
sudo umount -l /mnt/proc
sudo umount -l /mnt
  1. 停止实例。
  2. 分离卷 /dev/xvdf,使其与已停止的实例分离。
  3. 附加卷 /dev/xvdf,使其作为 /dev/sda1 附加回它的原始实例。
  4. 启动原始实例,然后使用 SSH 连接到该实例。
  5. 要验证目前原始实例上的根卷是否具有 3 TiB 的空间,请运行 lsblk 命令:
lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   3T  0 disk
└─xvda1 202:1    0   3T  0 part /

相关信息

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

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