AWS EC2 Expanding linux root partition

Posted By : Mohd Adnan | 31-Jul-2018

AWS EC2 EBS

Image Credits: Daniel Lopez

 

If the partition you want to expand is not root partition, then you can simply unmount the volume and resize it from the instance itself. There is a problem when you want to expand a root partition. You cannot directly expand the root volume from your instance. There are steps involved that you need to follow.

 

Preparing your instance for root partition expansion

 

Note: Create a snapshot of your root volume (EBS) before you attempt to expand root volume. The methods are destructive and cannot be reverted back. 

1. run lsblk and record device name that the root volume is attached to. The value is likely to be /dev/sda1 or /dev/xvdf

 

2. You cannot expand your root partition from your running instance. You need to stop your primary instance from AWS EC2 dashboard

 

3. Detach the root volume of your primary EC2 instance

 

4. Create a secondary instance - you can use nano instance and start it. You've to start the instance before you can begin the process. Having two root partitions while booting the instance can corrupt the file system. 

 

5. Once your secondary instance is started, attach a root volume of your primary instance to it. 

 

 

Expanding a Linux Partition using parted

 

The parted utility is a partition editing tool that is available on most of the Linux distributions. It can create & edit both MBR partition tables,GPT partition table.

Here are the steps to expand your root partition using parted utility.

1. Identify the parition names using lsblk

[ec2-user ~]$ lsblk
NAME    MAJ:MIN RM  SIZE RO MOUNTPOINT
xvdf    202:80   0  50G  0
xvdf1   202:81   0  10G  0 /mnt
xvda1   202:1    0   30G  0 /

 

In the above example, a xvdf device has 50 GiB of storage available and it contains a partition xvdf1 of 10 GiB

 

2. Unmount the parition if it is mounted
[ec2-user ~]$
sudo umount /mnt

 

3. Run parted command on the device
[ec2-user ~]$
sudo parted /dev/xvdf
GNU Parted 2.1
Using /dev/
xvdf
Welcome to GNU Parted! Type 'help' to view a list of commands.

 

4. Change the parted units of measure to sectors.
(parted) unit s

 

5. Run the print command to list the partitions on the device.
If you receive messages regarding backup of GPT table and to fix GPT - you should ignore it

 

6. Examine the output for the total size of the disk, the partition table type, the number of the partition, the start point of the partition, and any flags, such as boot
Model: Xen Virtual Block Device (
xvd)
Disk /dev/
xvdg: 104857600s
Sector size (logical/physical): 512B/512B
Partition Table:
msdos

Number  Start  End        Size       Type     File system  Flags
 1      2048s  35649535s  35647488s  primary  ext3

 

7. Delete the partition entry for the partition using the number (1) from the previous step.
(parted) rm 1

 

8. Create a new partition that extends entire volume.
(parted)
mkpart primary 2048s 100%

 

9. Run the print command to verify your partition.
(parted) print
Model: Xen Virtual Block Device (
xvd)
Disk /dev/
xvdg: 104857600s
Sector size (logical/physical): 512B/512B
Partition Table:
msdos

Number  Start  End         Size        Type     File system  Flags
 1      2048s  104857599s  104855552s  primary  ext3

 

10. Check to see that any flags that were present earlier are still present for the partition that you expanded
 (parted) set 1 boot on

 

11. Run the quit command to exit parted.
 (parted) quit

 

 

Returning an Expanded Partition to Its Original Instance

 

Now that you've expanded the partition.

1. Detach the expanded volume from the secondary instance


2. Re-attach it to the primary instance


3. Start your primary instance and your root partition should be expanded. You can run lsblk command to verify.


4. You can terminate your secondary instance now

About Author

Author Image
Mohd Adnan

Adnan, an experienced Backend Developer, boasts a robust expertise spanning multiple technologies, prominently Java. He possesses an extensive grasp of cutting-edge technologies and boasts hands-on proficiency in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases such as MySQL and PostgreSQL. Adnan consistently delivers invaluable contributions to a variety of client projects, including Vision360 (UK) - Konfer, Bitsclan, Yogamu, Bill Barry DevOps support, enhedu.com, Noorisys, One Infinity- DevOps Setup, and more. He exhibits exceptional analytical skills alongside a creative mindset. Moreover, he possesses a fervent passion for reading books and exploring novel technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..