Howto extend EBS volume on Amazon Linux 2?
AWS Elastic Block Store (or EBS) is Amazon's highly available, low-latency block storage solution. The service is used together with EC2 to persist data on the instances. EBS provides multiple settings that allow fine-tuning your application to meet your performance, storage size, and cost requirements.
One of the best advantages of EBS is the fact that it is elastic. It means you can always add more gigabytes to your existing volumes if the current ones run out of space.
In the following article, I will provide the exact steps to achieve it on Amazon Linux 2 that I usually use to run my workloads. Also, please remember that my servers run on the Amazon Nitro platform. You can verify if you run on Nitro or an elder Xen-based virtualization platform by running the following command.
$ aws ec2 describe-instance-types --instance-type t3.small --query "InstanceTypes[].Hypervisor"
[
"nitro"
]
My demo instance uses the t3.small instance type on the us-east-1 region. As you can see from the output, it runs on the Nitro virtualization platform.
Let's check how much disk space I use.
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 960M 0 960M 0% /dev
tmpfs tmpfs 969M 0 969M 0% /dev/shm
tmpfs tmpfs 969M 348K 968M 1% /run
tmpfs tmpfs 969M 0 969M 0% /sys/fs/cgroup
/dev/nvme0n1p1 xfs 30G 1.7G 29G 6% /
My attached EBS volume is 30 Gb. Let's extend it to 40 Gb. First, you need to modify the EBS volume itself. I found it handy to use AWS CLI to do the operation, but it is also doable from the AWS Management Console. DO NOT FORGET TO TAKE EBS SNAPSHOT BEFORE YOU CONTINUE WITH THE NEXT STEPS!
$ aws ec2 modify-volume --size 40 --volume-id vol-0c87855625e650f4d
{
"VolumeModification": {
"VolumeId": "vol-0c87855625e650f4d",
"ModificationState": "modifying",
"TargetSize": 40,
"TargetIops": 120,
"TargetVolumeType": "gp2",
"TargetMultiAttachEnabled": false,
"OriginalSize": 30,
"OriginalIops": 100,
"OriginalVolumeType": "gp2",
"OriginalMultiAttachEnabled": false,
"Progress": 0,
"StartTime": "2022-10-21T15:30:44+00:00"
}
}
vol-0c87855625e650f4d is my EBS vloume ID. You can find yours on the EC2 instance's details page in AWS Management Console. Now you need to wait till the volume is ready. You should wait till your EBS volume's Volume status is Okay, and the size is your new size (40 Gb in my example) on the EBS Volumes page in the Management Console.
Now, log in to your EC2 instance and list partitions.
sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 30G 0 part /
└─nvme0n1p128 259:2 0 1M 0 part
My nvme0n1p1 is 30 Gb only, but I want to extend it to 40 Gb. Use growpart command to extend the nvme0n1p1 partition in a partition table to fill available space.
$ sudo growpart /dev/nvme0n1 1
CHANGED: partition=1 start=4096 old: size=62910431 end=62914527 new: size=83881951 end=83886047
Verify the successful completion of the operation.
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 40G 0 part /
└─nvme0n1p128 259:2 0 1M 0 part
But the fact that you extended the partition does not mean that you increased the size of the existing file system. For example, my root ("/") is still 30 Gb.
df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 960M 0 960M 0% /dev
tmpfs tmpfs 969M 0 969M 0% /dev/shm
tmpfs tmpfs 969M 348K 968M 1% /run
tmpfs tmpfs 969M 0 969M 0% /sys/fs/cgroup
/dev/nvme0n1p1 xfs 30G 1.7G 29G 6% /
Since my EC2 instance uses XFS, I can use xfs_growfs command to increase the size of the file system.
$ sudo xfs_growfs -d /
Finally, verify the successful completion of the operation and my root is 40 Gb.
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 960M 0 960M 0% /dev
tmpfs tmpfs 969M 0 969M 0% /dev/shm
tmpfs tmpfs 969M 348K 968M 1% /run
tmpfs tmpfs 969M 0 969M 0% /sys/fs/cgroup
/dev/nvme0n1p1 xfs 40G 1.7G 39G 5% /
If you use a Xen-based instance, follow a deeper tutorial from AWS documentation.