Skip to content

How do I convert the default MBR partitioning scheme to GPT for my EC2 Ubuntu instance?

7 minute read
1

Ubuntu versions 22.04 and later use GPT partitioning by default. However, you must manually set up GPT partitioning for Ubuntu versions 16.04, 18.04, and 20.04.

Resolution

Important: Before you stop and start your instance, take the following actions:

Note: When you stop and start an instance, the instance's public IP address changes. It's a best practice to use an Elastic IP address to route external traffic to your instance instead of a public IP address. If you use Amazon Route 53, then you might need to update the Route 53 DNS records when the public IP address changes.

To convert your Ubuntu instance's default MBR partitioning scheme to GPT, complete the following steps:

  1. Open the Amazon EC2 console.

  2. Launch an instance from an Amazon Machine Image (AMI) that runs Ubuntu.

  3. Launch a second instance from the same Ubuntu AMI with a 3 TiB root volume in the same Availability Zone .

  4. Stop the second instance.

  5. Detach the /dev/xvda or /dev/sda1 root volume from the second instance. Then, attach the root volume as /dev/sdf to the first instance.

  6. Use SSH to connect to the first instance.

  7. To view the root partition of /dev/sdf, run the following lsblk command:

    sudo lsblk

    Example output:

    NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    (snip)
    xvdf    202:80    0   3T  0 disk 
    └─xvdf1 202:81    0   2T  0 part 
    (snip)

    Note: In the preceding example, the root partition of /dev/sdf is 2 terabytes (TB). On Nitro instance types, the block device name is similar to /dev/nvme1n1.

  8. To convert the partition table from MBR to GPT, use the gdisk tool to run the following command:

    sudo gdisk /dev/xvdf

    Note: If you enter incorrect commands in gdisk, then press q or Ctrl-C to quit from gdisk and not save the changes.
    Example output:

    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. To enter Expert mode and set the sector alignment value, enter the following values at the prompts:

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

    Note: Press Enter after each prompt. The preceding configuration changes the sector alignment value from the default 8 to 1. If you use the 8-byte (B) alignment, then you might experience issues when you create the GPT partition.

  10. To create a GPT partition, enter the following values at the prompts:

    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                                                     

    Note: Press Enter after each prompt. When you press Enter for the Last sector prompt, you receive the Current type is 'Linux filesystem' output. In the preceding example, ef02 is the BIOS boot partition number.
    Example output:

    Changed type of partition to 'BIOS boot partition'
  11. To delete the root partition, enter the following values at the prompts:

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

    Note: Press Enter after each prompt.

  12. To recreate the root partition to 3 TiB, enter the following values at the prompts:

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

    Note: Press Enter after each prompt. When you press Enter for the Last sector prompt, you receive the Current type is 'Linux filesystem' output. To use the default settings, enter Enter for the First sector, Last sector, and Hex code or GUID prompts.
    Example output:

    Changed type of partition to 'Linux filesystem'
  13. To save the GPT partition table, enter the following values at the prompts:

    Command (? for help): w                                                                                                        
    Do you want to proceed? (Y/N): y

    Important: This action overwrites existing partitions.
    Example output:

    OK; writing new GUID partition table (GPT) to /dev/xvdf.
    The operation has completed successfully.
  14. To view the new volume information, run the following lsblk command:

    sudo lsblk

    Example output:

    NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvdf    202:80   0   3T  0 disk 
    └─xvdf1 202:81   0   3T  0 part
  15. To check whether the file system of the /dev/xvdf1 device is correct, use the fsck tool to run the following command:

    sudo e2fsck -f /dev/xvdf1

    Note: To respond yes to all questions, add a -y switch to the e2fsck command. For more information about the fsck tool, see fsck.8.gz on the Ubuntu website.
    Example output:

    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
  16. To resize the file system to 3 TiB, run the following resize command:

    sudo resize2fs /dev/xvdf1

    Note: It might take 10-20 seconds to resize the file system.
    Example output:

    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.
  17. Install Grub on the /dev/xvdf1 device, and then use the following commands to configure Grub based on your Ubuntu version:
    Ubuntu 16.04 or 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
  18. Detach the /dev/xvdf volume from the running instance.

  19. Attach the /dev/xvdf volume back to the first instance as /dev/xvda or /dev/sda1.

  20. Use SSH to connect to the first instance, and then start the instance.

  21. To verify that the root volume on your original instance now has 3 TiB of space, run the lsblk command:

    sudo lsblk

Related information

Amazon EBS volume constraints

AWS OFFICIALUpdated a year ago
2 Comments

Dear support,
on point 10 Note: Make sure that you change the sector alignment value from the default 8 to 1. If you use the 8-bytes alignment, then this might cause issues when you create the GPT partition.

when I put number 1 for partition, I get the below message:
Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): 1
Warning: Setting alignment to a value that does not match the disk's
physical block size! Performance degradation may result!
Physical block size = 4096
Logical block size = 512
Optimal alignment = 8 or multiples thereof.

But after that I continued the rest instructions and it worked for me as GPT without any issues. I was doing the above steps on my dev server but now I want to apply on Production server same specs like the dev server so for sure I will have the same warning message.

It is a normal warning message, I should skip it and it won't affect the disk performance as mentioned in the warning or no there is something wrong here?

Please confirm to know how I should proceed.

I'm doing the above on Ubuntu 20.04 TLS

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT

replied 2 years ago