如何將 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 console (Amazon EC2 主控台)。

  2. 從執行 Ubuntu 的 Amazon Machine Image (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. 若要進入專家模式並設定磁區對齊值,請在提示處輸入以下值:

    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 鍵。上述組態會將磁區對齊值從預設的 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 鍵。當您按下 Enter 鍵以顯示最後一個磁區提示時,您會收到**目前類型為「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 鍵。當您按下 Enter 鍵以顯示最後一個磁區提示時,您會收到目前類型為「Linux 檔案系統」的輸出。若要使用預設設定,請在第一個磁區最後一個磁區十六進位程式碼或 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

注意:若要對所有問題都回答,請在 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 官方已更新 3 個月前