/ linux

Extending the disk space on an EC2 instance

When you create an EC2 instance or EBS instance with the default configuration the allocated disk space is 8GB unless you explicitly configure this. This would be more than enough for most of the applications that are running on a t2.micro instance. But at times, you will need some extra space on your server. You can either add that when you spin up a new instance or can even extend your existing disk on the server and add more space.

I am going to focus on how to extend the disk space on a running instance in this article.

Let's say we want to increase the size of our boot drive from the default 8GB to a 100GB. It can be done by following three steps:

  1. Resize EBS Volume
  2. Resize partition
  3. Resize file system

Resize Volume

The first step is to modify the Volume to add more space. To do that:

  • Login to your AWS Web console
  • Go to EBS
  • Select the volume that you want to extend
  • Click Modify Volume
  • And change the size field to whatever value you need(100 in our case)
  • And finally click the Modify button to apply the changes.

Once that is done, we need to resize the partition on the OS level. To make the change, we need to ssh into your instance and run some Linux commands.

First of all let’s list block devices attached to our box: lsblk

lsblk

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

As you can see, even though we extended our volume, /dev/xvda1 is still the default 8GB partition on a 100 GB device and there are no other partitions on the volume. We now need to resize the partition on the disk.

Resize partition

Let’s use growpart to resize 8G partition up to 100G:

growpart /dev/xvda1

Let’s check the result (you can see /dev/xvda1 is now 100G):

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

Awesome.!

Resize Filesystem

The last thing to do is the resize the file system to grow all the way to fully utilized the newly added space.

Let's check the current file system size using the df -h command:

df -h

Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M  5.6M   94M   6% /run
/dev/xvda1      7.7G  2.0G  5.7G  26% /
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

Now let's resize filesystem using resize2fs command

resize2fs /dev/xvda1

Let's recheck the file system size to confirm our changes:

df -h

Filesystem      Size  Used Avail Use%  Mounted on
/dev/xvda1      100G  2.0G 97.9G 2%    /

Known gotchas

AWS will only allow you to resize the Volume once per day. So if you want to resize the volume again, you will have to wait a day. So be sure to do your math and get it right the first time. :)

Docs

Hope someone finds it useful. Thanks for reading 🖖🏻

Manu S Ajith

Manu S Ajith

Tech Entrepreneur, dating Elixir, in long-term ❤️ w/ Ruby, had multiple one night stands w/ Go. Into functional paradigms DDD/CQRS/EventSourcing architecture these days. @manusajith on the interwebs

Read More