Expand a Volume in an EC2 Linux Instance

Thomas Gallagher
FAUN — Developer Community 🐾
4 min readDec 31, 2020

--

From @oskaryil on Unsplash

Intro

If you’ve ever read any of my previous Medium articles, you’ll know that I primarily work on DevOps in AWS, building and learning with Terraform as my playground. During my time working with AWS, I’ve worked with all sorts of operating systems and environments, so I have picked up quite a few tips and tricks that can help save you time on your projects!

Please feel free to send me a message and let me know if this works for you, or how you would change it up for my articles in the future!

Expanding Drives on your Linux Instances

One of my favorite features of AWS is how easy they make expanding your volumes inside the console. You just open EC2, open Volumes, select your volume, then click Modify. You can add as much space as you need, up to 16TB, then click apply, and boom, you have all the space you need. In EC2 Linux instances, however, the volumes are not automatically expanded. In order to properly expand the drive space, you will need to connect to your instance and run a few commands, which are detailed below.

The first thing you’ll want to do after adding space is grabbing some information about your instance’s block devices. This can be done by running the below command:

sudo lsblk

The lsblk command, in this format, lists information about all of your instance’s block devices. For the purposes of this article, I’ll be using a Linux Instance living in one of my AWS environments, so it may not match what you see when you run lsblk.

Taken from inside one of my Linux Instances

After running the sudo lsblk command, you will want to follow up by running the df command, which will list the remaining available space in your volumes, like below.

sudo df -H
Taken from inside one of my Linux Instances

Now that we have gotten our “discovery” out of the way, we can move onto the actual practice of expanding the drive.

In this exercise, we would want to expand partition 1 on nvme0n1, which would be named /dev/nvme0n1p1. Once you have expanded your volume in the AWS console, you would connect to your Linux instance and run the below.

sudo growpart /dev/nvme0n1 1

Because the disk will automatically be expanded, you use growpart to claim the space that you have created with the expansion. The growpart command is useful here, as it will expand the selected partition to fill any available space. Let’s say you expanded your drive from 100GB to 200GB, when you run sudo lsblk, you will see /dev/nvme0n1 listed at 200GB, with the /dev/nvme0n1/nvme0n1p1 partition listed at 100GB. Once you run growpart, you would run sudo lsblk to verify that your partition has claimed the space. Upon running sudo lsblk, you will see that /dev/nvme0n1/nvme0n1p1 will have claimed the space, and is now listed at 200GB.

Now that you have done this, you are done, right? Well, not quite. Sure, you have expanded the volume and the partition, and your space is now available for use, so in essence, your task should be complete. However, in order to make the space usable by the operating system, you need to extend your filesystem. For this exercise, I used resize2fs, which will resize the ext2, ext3, and ext4 filesystem types. The resize2fs command can be used on unmounted volumes to both shrink and expand the filesystem. On mounted volumes, you can only use resize2fs to expand the filesystem.

Since you have done the work to gather the name of your disk and partition, running resize2fs is as simple as the below!

sudo resize2fs /dev/nvme0n1p1

Once you do this, your filesystem will expand to claim any available space, and the space will now be usable by the OS. You can verify the space has been claimed by running the df -H command again, which will output your available space in percentage format.

Conclusion

While expanding volumes in AWS is super easy and convenient, there is just a little more work required if you want to expand the volume properly in your Linux instances. The best part about Linux is also the worst part: it won’t do anything that you do not tell it to do. If you followed this article and it worked for you, please feel free to drop me a line and let me know!

If you enjoyed reading this article, please feel free to check out my other Medium articles, and to visit my LinkedIn, where I am active and always willing to chat about all things AWS, and I’d never say no to a beer or a coffee.

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

AWS Cloud Architect specializing in migration & Terraform/CloudFormation-based scalable cloud solutions. Passionate about agile, efficient cloud adoption.