This page provides a guide on increasing AWS EC2 root EBS volume size when EC2 is running.
When launching an EC2 instance on AWS console, an 8 GiB EBS volume will be attached as default, aka root volume. Suppose, your EC2 instance is running for a long time, and the root volume is full. You want to expand the volume size without stopping running instance. You have two options:
- Expand the root EBS volume size
- Attach a new EBS volume to EC2 instance
There are some concepts you need to keep in mind before we go through the details.
- Data volume availability – To make the data volume available, you need to format and mount the data volume.
- File system – To take advantage of the increased size of the data volume, the file system needs to be extended.
Expand the root EBS volume size
Here is a screenshot before modification. The EC2 instance is running with an 8 GiB root EBS volume attached, which is the default size of attached EBS volume when launching an EC2 instance from AWS console.
SSH login to instance, and run df -hT to show the information about the file system. The Filesystem */dev/xvda1 * is what we need to expand. Currently it's 8.0G and has 1.6G used.
Use the following process when expanding a volume root size:
- (Optional) Before modifying a volume that contains valuable data, it is a best practice to create a snapshot of the volume in case you need to roll back your changes. For more information, see Create Amazon EBS snapshots.
Request a volume modification.
From AWS console, select the volume to modify and choose Actions, Modify volume. The Modify volume screen displays the volume ID and the volume's current configuration, including type, size, IOPS, and throughput. Enter a new value, for example 20 for Size. After you have finished changing the volume settings, choose Modify. The volume size changes to 20 as follows.
Extend a Linux file system after resizing a volume
After you increase the size of an EBS volume, you must use the file system–specific commands to extend the file system to the new, larger size. You can do this as soon as the volume enters the optimizing state.
To extend a file system on Linux, you need to:
- Extend the partition, if your volume has one
- Extend the file system
Use lsblk command to view your available disk devices and their mount points to help you determine the correct device name to use. The root device is /dev/xvda, which has three partitions named xvda1, xvda127 and xvda128.
Use the lsblk -f command to get information about all of the devices attached to the instance. The FSTYPE column shows the file system type for each device. Device partition xvda1 is formatted using XFS file system, while partition xvda128 is VFAT file system. If the column is empty for a specific device, it means that the device does not have a file system.
We choose partition xvda1 to extend. As the partition file system type is XFS, use the growpart command to extend. Note the space between the device name (xvda) and the partition number (1) for partition xvda1.
Run lsblk -f command to verify the partition xvda1 size has been extended to 20 GiB.
Now, extend the file system. Get the name, size, type, and mount point for the file system that you need to extend. Use the df -hT command.
The commands to extend the file system differ depending on the file system type. Choose the following correct command based on the file system type that you noted in the previous step. For the XFS file system, use the xfs_growfs command and specify the mount point of the file system that you noted in the previous step.
Verify that the file system has been extended using the df -hT command.
Attach a new EBS volume to EC2 instance
AWS EC2 instance is allowed to attach multiple EBS volumes. An alternative to increasing the EBS volumes is to attach additional EBS volumes.
Create an EBS volume with 100GiB on AWS console
From AWS console, go to Volume tab and click Create Volume. The Create Volume screen shows the volume's default configuration, including type, size, IOPS, and throughput. You can keep these fields as default, and create a volume with 100 GiB available.
Attach the newly created volume to EC2 instance
Select the volume to attach and choose Actions, Attach volume. Select the instance attached to, and give it a device name, for example /dev/sdf. See Device names on Linux instances - Amazon Elastic Compute Cloud for more information about device name recommendations from AWS.
The new volume state is in-use after attached.
Make the data volume available for use
Now we have a new volume with 100 GiB attached to instance. Use the following procedure to make the data volume available for use.
Firstly, SSH into the instance, format and mount the data volume.
In the terminal window, use the following command to view your available disk devices and their mount points. The output of lsblk removes the /dev/ prefix from full device paths. The output shows that there are two devices attached to the instance - the root device /dev/xvda and the newly attached volume /dev/xvdf. In the demo, you can see the column FSTYPE is empty for the new volume /dev/xvdf which means the new volume does not have a file system.
To create a file system, use the following command with the data volume named /dev/xvdf. As the root volume has an XFS file system, you should format the new volume with XFS as well.
For another file system, find the details from Extend a Linux file system after resizing a volume - Amazon Elastic Compute Cloud.
To create a mount point directory for the new volume, use the sudo mkdir /data command. The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume. The following example creates a directory named /data. And mount the data volume in the directory we created, use the following mount command with the new volume name (/dev/xvdf) and the mount point directory name (/data). Verify that you have formatted and mounted your data volume.
The following example output shows that the data volume /dev/xvdf has an XFS file system and is mounted at the /data mount point.
Now, we have an additional EBS volume attached to EC2 successfully. The instance can make use of 100 GiB volume.
Summary
- Newly created EBS volume is a block volume, and must have file system initialized after attached to instance.
- To make the data volume available, you need to format and mount the data volume. Format means creating a file system on volume.
- To take advantage of the increased size of the data volume, the file system needs to be extended.
- Use command sudo mkfs -t xfs /dev/xdaf to create a file system. xfs is the type of file system.
- Use command sudo mount /dev/nvme1n1 /data to mount data volume to a mount point directory.
- The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume.
- Useful command:
- sudo lsblk -f : shows the devices, file system type and mount point attached to the instance
- df -hT : shows the file system information
- sudo xfs_growfs -d /data : extend the XFS file system that is mounted on the /data mount point.
Thanks for reading and appreciate your comments on content and grammar!
Top comments (0)