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

6 分钟阅读
0

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

解决方法

**警告:**停止并启动实例前,确保了解以下内容:

  • 当您停止和启动实例时,实例存储数据丢失。如果您的实例受实例存储支持或具有包含数据的实例存储卷,则在实例停止时数据将丢失。有关更多信息,请参阅确定实例的根设备类型

  • 如果您的实例是 Amazon EC2 Auto Scaling 组的一部分,停止实例可能会终止实例。如果您使用 Amazon EMR、AWS CloudFormation 或 AWS Elastic Beanstalk 启动实例,您的实例可能是 AWS Auto Scaling 组的一部分。在这种情况下,是否会发生实例终止取决于您的 Auto Scaling 组的实例缩减保护设置。如果您的实例是 Auto Scaling 组的一部分,则在开始执行解决步骤之前,暂时从 Auto Scaling 组中删除该实例

  • 停止和启动实例会更改实例的公共 IP 地址。在将外部流量路由到您的实例时,最佳做法是使用弹性 IP 地址而不是公有 IP 地址。如果您使用 Route 53,您可能必须在公有 IP 更改时更新 Route 53 DNS 记录

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

1.    打开 Amazon EC2 控制台

2.    从运行 CentOS 7 的 AWS Marketplace Amazon 系统映像 (AMI) 中启动实例。

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

**注意:**如果您已经拥有 CentOS 7 实例,则无需启动新的 CentOS 7 实例。对于 CentOS 7,修改根卷以将其大小扩展至超过 2TiB。

4.    停止具有 3TiB 根卷的实例,并停止在步骤 2 中创建的实例。

**注意:**CentOS 7 来自 AWS Marketplace AMI,因此您需要先停止实例,然后再附加具有 AWS Marketplace 代码的卷。如果您不停止实例,则会出现以下错误:

“附加卷时出错:无法附加具有 Marketplace 代码的‘vol-xxxxxxxxxxxxxxxxx’,因为实例‘i-xxxxxxxxxxxxxxxx’未处于‘已停止’状态。”

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

**注意:**请使用 Xen 平台实例类型,如 t2。如果您使用 Nitro 平台(如 t3),则设备名称将更改为 nvmeXn1

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

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)

注意:****/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.    使用 gdisk 工具将分区表从 MBR 转换为 GPT。

**注意:**如果尚未安装 gdisk 工具,您可以使用命令 sudo yum install gdisk -y 来安装它。

# 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.    在提示中输入以下命令,以创建 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 启动分区编号。

10.    在提示中输入以下命令,以删除根分区:

Command (? for help): d                                                                                                         
Partition number (1-128): 1

11.    在提示中输入以下命令,以将根分区重新创建为 3TB。在第一个扇区最后一个扇区十六进制代码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'

12.    在提示中输入以下命令,以保存 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.

运行 gdisk 命令以查看新分区信息:

$ 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

13.    使用文件系统检查和修复工具来检查设备 /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

14.    运行 xfs_growfsresize2fs 命令调整文件系统的大小,以将其扩展至 3TB。

**注意:**调整文件系统的大小可能需要几分钟。

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.

15.    在设备 /dev/xvdf1 上安装 Grub,并使用以下命令对其进行配置。

**注意:**S 6 不需要安装 Grub。如果您使用的是 CentOS 6,请跳到步骤 16。

CentOS 7:

sudo mount --bind /proc /mnt/proc
sudo 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

16.    停止该实例。

17.    将卷 /dev/xvdf 与已停止的实例分离。

18.    将卷 /dev/xvdf 作为 /dev/sda1 附加回它的原始实例。

19.    启动原始实例并使用 SSH 连接到它。

20.    运行 lsblk 命令,以验证原始实例上的根卷现在具有 3TiB 的空间。

$ 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 分区的 2TiB 限制?

AWS 官方
AWS 官方已更新 4 年前