How do I convert the default MBR partitioning scheme to GPT of my EC2 Ubuntu instance so that I can bypass the 2-TiB limit for MBR partitions on my EBS volume?

7 minute read
0

My Amazon Elastic Compute Cloud (Amazon EC2) instance runs Ubuntu 16.04, 18.04, or 20.04. There's a 2-TiB limit for MBR partitions. To bypass this limit, I want to convert the partitioning scheme on my Amazon Elastic Block Store (Amazon EBS) volume to a GPT partitioning scheme.

Resolution

Warning: Before you stop and start your instance, consider the following:

  • If your instance is instance store-backed or has instance store volumes that contain data, then the data is deleted when you stop the instance. For more information, see Determine the root device type of your instance.
  • If your instance is part of an Amazon EC2 Auto Scaling group, then an instance stop might terminate the instance. If you launched the instance with Amazon EMR, AWS CloudFormation, or AWS Elastic Beanstalk, then your instance might be in an AWS Auto Scaling group. In this case, instance termination depends on the instance scale-in protection settings for your Auto Scaling group. If your instance is part of an Auto Scaling group, then temporarily remove the instance from the Auto Scaling group before you start the resolution steps.
  • When you stop and start an instance, this changes the public IP address of your instance. When you route external traffic to your instance, it's a best practice to use an Elastic IP address instead of a public IP address. If you use Amazon Route 53, then you might have to update the Route 53 DNS records when the public IP changes.
  • If the shutdown behavior of the instance is set to Terminate, then the instance terminates when you stop it. To avoid this issue, change the instance shutdown behavior.

Note: It's a best practice to create a backup of your EBS volume before you begin any resolution steps.

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

  1. Open the Amazon EC2 console.

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

  3. In the same Availability Zone as the first instance, launch a second instance from the same Ubuntu AMI with a 3-TiB root volume.

  4. Stop the instance with the 3-TiB root volume.

  5. Detach the root volume (/dev/xvda or /dev/sda1) from the stopped instance. Then, attach the root volume to the instance that you created in step 2 as /dev/sdf.

  6. Use SSH to connect to the running instance (created in step 2).

  7. To view the root partition of / dev/sdf, use the lsblk command. The root partition of /dev/sdf is only 2-TiB size, as the following example shows:

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

    Note: On Nitro instance types, the block device name looks similar to the following example: /dev/nvme1n1.

  8. To convert the partition table from MBR to GPT, use the gdisk tool:
    Note: If you enter incorrect commands in gdisk, then use q to quit from gdisk without saving changes or press Ctrl+C.

    # sudo gdisk /dev/xvdfGPT 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 commands at the prompts:

  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.

    Command (? for help): x                                            
    Expert command (? for help): l                                     
    Enter the sector alignment value (1-65536, default = 2048): 1      
    Expert command (? for help): m
  11. To create a GPT partition, enter the following commands at the prompts. Type enter at the Last sector prompt to use the default sector number 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'

    Note: In this example, ef02 is the BIOS boot partition number.

  12. To delete the root partition, enter the following commands at the prompts:

    Command (? for help): d                                                                                                         
    Partition number (1-128): 1
  13. To recreate the root partition to 3-TiB, enter the following commands at the prompts. In the First sector, Last sector, and Hex code or GUID prompts, type enter to use the default settings:

    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'
  14. To save the GPT partition table, enter the following commands at the prompts:

    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.

    To view the new volume information, run the lsblk command:

    # lsblkNAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvdf    202:80   0   3T  0 disk 
    └─xvdf1 202:81   0   3T  0 part
  15. To check that the file system of device /dev/xvdf1 is correct, use the fsck tool. For more information on the fsck tool, see fsck on the Ubuntu website:
    Note: If you add a -y switch to the e2fsck command, then it assumes yes to all questions.

    # sudo e2fsck -f /dev/xvdf1e2fsck 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 extend it to 3-TiB, run the resize command:
    Note: It might take 10-20 seconds to resize the file system.

    # sudo resize2fs /dev/xvdf1resize2fs 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 device /dev/xvdf1, and then use the following commands to configure it:
    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 volume /dev/xvdf from the running instance.

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

  20. Start the original instance with SSH.

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

Related information

Constraints on the size and configuration of an EBS volume

AWS OFFICIAL
AWS OFFICIALUpdated 3 days ago