How do I convert my EC2 Ubuntu instance's default MBR partitioning scheme to GPT in order to bypass the 2 TiB limit for MBR partitions on my EBS volume?

Last updated: 2020-06-03

My Amazon Elastic Compute Cloud (Amazon EC2) instance is running Ubuntu 16.04, 18.04, or 20.04. How can I convert the default MBR partitioning scheme on my Amazon Elastic Block Store (Amazon EBS) volume to the GPT partitioning scheme to bypass the 2 TiB limit for MBR partitions?

Resolution

Warning: Before stopping and starting your instance, be sure you understand the following:

  • Instance store data is lost when you stop and start an instance. If your instance is instance store-backed or has instance store volumes containing data, the data is lost when you stop the instance. For more information, see Determining the root device type of your instance.
  • If your instance is part of an Amazon EC2 Auto Scaling group, stopping the instance may terminate the instance. If you launched the instance with Amazon EMR, AWS CloudFormation, or AWS Elastic Beanstalk, your instance might be part of an AWS Auto Scaling group. Instance termination in this scenario 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 starting the resolution steps.
  • Stopping and starting the instance changes the public IP address of your instance. It's a best practice to use an Elastic IP address instead of a public IP address when routing external traffic to your instance. If you are using Route 53, 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, the instance is terminated when stopped. You can change the instance shutdown behavior to avoid this.

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

1.    Open the Amazon EC2 console.

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

3.    Launch a second instance in the same Availability Zone as the first instance and 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 and then attach it to the instance you created in step 2 as /dev/sdf.

6.    Connect to the running instance (created in step 2) using SSH.

7.    Use the lsblk command to view the root partition of /dev/sdf. The root partition of /dev/sdf is only 2 TiB size, as shown in the following example:

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

8.    Use the gdisk tool to convert the partition table from MBR to GPT.

# 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.    Enter the following commands at the prompts to enter Expert mode and set the sector alignment value.

Note: Make sure you change the sector alignment value from the default 8 to 1. Using the 8 bytes alignment might cause issues when you're creating 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   

10.    Enter the following commands at the prompts to create a GPT partition. 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 the preceding example, ef02 is the BIOS boot partition number.

11.    Enter the following commands at the prompts to delete the root partition:

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

12.    Enter the following commands at the prompts to recreate the root partition to 3 TB. 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'

13.    Enter the following commands at the prompts to save the GPT partition table:

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.

Run the lsblk command to view the new volume information:

# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf    202:80   0   3T  0 disk 
└─xvdf1 202:81   0   3T  0 part 

14.    Use the fsck tool to check that the file system of device /dev/xvdf1 is correct. For more information on the fsck tool, see fsck on the Ubuntu man website.

# sudo e2fsck -f /dev/xvdf1
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

15.    Run the resize command to resize the file system in order to extend it to 3TB.

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

# sudo resize2fs /dev/xvdf1
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.

16.    Install Grub on the device /dev/xvdf1 and configure it using the following commands:

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

17.    Detach the volume /dev/xvdf from the running instance.

18.    Attach the volume /dev/xvdf back to its original instance as /dev/sda1.

19.    Start the original instance using SSH.

20.    Run the lsblk command to verify that the root volume on your original instance now has 3 TiB of space.


Did this article help you?

Anything we could improve?


Need more help?