Increasing Root Volume Size of an EBS Backed EC2 Instance

  • by Emre Yilmaz
  • Sep 24, 2017
  • AWS
  • Istanbul

When you start an EC2 instance, you define an EBS volume size. However, you may need more disk space later for some reason. In this blog post, I will demonstrate an example on how you can increase the root volume size of an EC2 Linux instance.

There are three components of this operation:

  • AWS Elastic Block Storage (EBS) volumes attached as block devices to our EC2 instance such as /dev/xvda.
  • Partions on block devices, such as /dev/xvda1.
  • Mounted filesystem on the operating system such as "/dev/xvda1" mounted on "/".

How to check block devices and filesystem

First of all let’s connect to our EC2 instance using SSH. For demonstration, I will be using a t2.micro instance backed by 8GB EBS initially and running Ubuntu 16.04LTS.

Once we connect, let’s list block devices available:

~$ sudo lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /

Here we see that, there is 8G size on xvda block device and all 8GB is allocated to xvda1 partition as we expect.

Let’s display the amount of disk space used and available on Linux filesystem.

~$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M  3.1M   97M   4% /run
/dev/xvda1      7.7G  965M  6.8G  13% /
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000

As can be seen, we have a 7.7G disk space available on /dev/xvda1 which we will increase by 2G. AWS allocated remaining nearly 0.3G space to udev and tmpfs automatically.

One more thing to go, we need to determine whether the disk use ext3-ext4 or xfs file system:

~$ sudo file -sL /dev/xvda1
/dev/xvda1: Linux rev 1.0 ext4 filesystem data, UUID=3e13556e-d28d-407b-bcc6-97160eafebe1, volume name "cloudimg-rootfs" (needs journal recovery) (extents) (large files) (huge files)

We checked initial state of our EC2 instance; let’s continue by resizing the volume using AWS Management Console.

Modifying Volume on AWS Management Console

I will show you how to modify the root volume using AWS Management Console. On our EC2 dashboard, click on Volumes under ELASTIC BLOCK STORE section on the left. Here, we need to select the volume attached to our instance and click on Modify from the Actions. You can see the screenshot below.

Modify Volume

Simply, increase the size in the modal window opened. In my case I will resize it to 10G.

Modify Volume

After clicking on Modify, AWS will display an approval window to avoid unintended actions, click Yes on the modal. A success message will be displayed which you can ignore by clicking Close.

After getting volume increase request, AWS will resize the EBS volume in a few seconds. You can see the process finished after clicking refresh button after a while on the top-right.

Modify Volume

As our volume is resized. However, now we need to extend OS file system for it to take effect.

Extending OS Filesystem (ext3, ext4)

Remember that we listed block devices and saw that all 8GB of xvda was allocated on xvda1. Now, let’s display after resizing the volume and see what it shows us:

~$ sudo lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  10G  0 disk
└─xvda1 202:1    0   8G  0 part /

Please, see how the size of xvda increased from 8G to 10G. Unfortunately, only 8G is used by xvda1. We need to allocate all 10G to xvda1.

We do this using growpart as our filesystem is ext4.

~$ sudo growpart /dev/xvda 1
CHANGED: partition=1 start=2048 old: size=16775135 end=16777183 new: size=20969439,end=20971487

We can confirm that all the storage is allocated to xvda1 by listing block storages again:

~$ sudo lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  10G  0 disk
└─xvda1 202:1    0  10G  0 part /

We increased block storage, but we also need to increase filesystem:

~$ sudo resize2fs /dev/xvda1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/xvda1 is now 2621179 (4k) blocks long.

Let’s confirm it by redisplaying filesystem disk space usage.

~$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M  3.1M   97M   4% /run
/dev/xvda1      9.7G  964M  8.7G  10% /
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000

As we can see, the size of /dev/xvda1 became 9.7G with a net 2G increase. Now, our resizing of EBS volume is complete.

Conclusion

In the lifetime of our EBS backed EC2 instances, we may need to increase root or other volumes of our instances. In this blog post, I tried to demonstrate how you can do this on an Ubuntu instance with ext4 filesystem. For ext3, the process is also the same. If your instance is running Amazon Linux or Centos with ext3 or ext4 filesystem; you can apply the same process again.

If your filesystem is xfs, you will need to do the filesystem extension part using xfsprog. You can find the details in AWS documentation in the references section.

Thanks for reading!

References

...

AWS Consultant, Instructor | AWS Certified Solutions Architect - Professional & DevOps Engineer

CEO @ Shikisoft

Follow

Would you like to start learning AWS CloudFormation?

Our new course AWS CloudFormation Step by Step: Beginner to Intermediate is live on Udemy!

Enroll now!
RSS

Subscribe to this blog's RSS feed

Categories