Increasing AWS EC2 Instance’s Volume size without Data Loss

Increasing AWS EC2 Instance’s Volume size without Data Loss

NOTE: Take snapshot of EBS volume before doing any changes to ebs volume

Create set of files

cd /

Execute below command to create 10 text files

for i in {1..10}; do sudo touch file$i.txt && echo "This is some text for file$i.txt" | sudo tee file$i.txt > /dev/null; done
ls /f*

Your machine’s root device is attached as /dev/xvda

Let’s see how operating system treats attached volume

$ lsblk

Let's add extra 5 GB of capacity to the root disk volume

Go to ebs volumes, select ec2 instance root volume, click on modify

Default ec2 root volume is 8 GB

Increase volume size to 15 GB

Let's check the root volume size on console

lsblk | grep xvda

Extending the Partition

To extend the partition, you’ll need to use the growpart command.

Replace /dev/xvda and 1 with your actual disk and partition numbers if they differ.

Run the following command

sudo growpart /dev/xvda 1

Extending the File System

Now comes the file system extension part.

Depending on your file system type (XFS or Ext4)

use one of the following commands:

For EXT4 File System:

sudo resize2fs /dev/xvda1

For XFS File System:

sudo xfs_growfs -d /

Verify the data

ls /f*

cat /file1.txt