Wondering how to convert the EC2 Ubuntu instance’s default partitioning scheme to GPT? We can help you!
Here, at Bobcares, we often receive similar requests from our AWS customers as a part of our AWS Support Services.
Today, let’s see the steps followed by our Support Techs to help our customers to convert the EC2 Ubuntu instance’s default partitioning scheme to GPT.
Convert the EC2 Ubuntu instance’s default partitioning scheme to GPT
Now let’s see the steps to convert the Amazon EC2 Ubuntu instance’s default partitioning scheme to a GPT partitioning scheme.
- At first, log in to the AWS Management Console and open the AWS EC2 console.
2. Then launch an instance from an Amazon Machine Image running Ubuntu.
3. Then we need to launch another instance from the same AMI and in the same Availablity zone of the first instance with 3 TiB root volume.
4. Stop the instance with 3 TiB root volume.
5. Then detach the root volume from this stopped instance (/dev/xvda or /dev/sda1).
6. And attach that volume as /dev/sdf to the running instance that we launched first.
7. Then access the running instance via SSH.
8. Run the lsblk command to see the root partition of /dev/sdf.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
(snip)
xvdf 202:80 0 3T 0 disk
└─xvdf1 202:81 0 2T 0 part
The root partition is only 2T.
9. Now, to convert the partition table from MBR to GPT we can use gdisk tool.
# 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!
10. We need to enter the following commands at the command prompt to enter Expert mode and set the sector alignment value.
Command (? for help): x
Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): 1
Expert command (? for help): m
For the sector alignment value, make sure to change the value from 8 to 1 as the 8 bytes alignment might cause issues when we create the GPT partition.
11. Then we need to enter the following commands to create a GPT partition. Type enter at the Last sector prompt to use the default sector number 2047. Also, note that ef02 is the BIOS boot partition number.
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'
12. Now enter the following commands to delete the root partition:
Command (? for help): d
Partition number (1-128): 1
13. Then enter the following commands to recreate the root partition to 3 TB.
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'
Type enter to use the default settings in the First sector, Last sector, and Hex code or GUID.
14. Give the following commands at the prompt 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.
15. Use the lsblk command to see the new volume details:
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdf 202:80 0 3T 0 disk
└─xvdf1 202:81 0 3T 0 part
16. To check that the file system of device /dev/xvdf1 is correct we can use the fsck tool.
# 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
17. Use the resize command to resize the file system in order to expand the partition to 3TB.
# 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.
18. Then 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
19. Now we can detach the volume /dev/xvdf from the running instance.
20. And attach the volume /dev/xvdf back to its original instance as /dev/xvda or /dev/sda1.
21. Then start the original instance using SSH.
22. Use the lsblk command to check and verify that the root volume on the original instance has 3 TiB of space.
Important points to be noted:
Here in this task, we need to stop and start our instance, so before doing this, be sure to understand the following points:
- The data will be lost while stoping the instance if our instance is instance store-backed or has instance store volumes containing data. So make sure to back up any data that want to keep on the instance store volume.
- Also, note that stopping and restarting the instance changes the public IP address of the instance. So it is always better to use an Elastic IP address instead of a public IP address when routing external traffic to the instance.
- If the instance is part of an Amazon EC2 Auto Scaling group then stopping the instance could terminate the instance. Also, if the instance is launched by services that use AWS Auto Scaling, such as Amazon EMR, AWS CloudFormation, etc., then stopping the instance could lead to its termination. In these cases, the instance termination depends on the instance scale-in protection settings for the Auto Scaling group. So If the instance is a part of an Auto Scaling group, remove the instance temporarily from the Auto Scaling group first. Then we can proceed with the recovery.
- Stopping the instance will terminate the instance if the shutdown behavior is set to Terminate.
[Need help with more AWS queries? We’d be happy to assist]
Conclusion
To conclude, today we discussed the steps followed by our Support Engineers to help our customers to convert the EC2 Ubuntu instance’s default partitioning scheme to GPT.
0 Comments