Blog / Others/ How to Extend the File System After Expanding an AWS EC2 Root EBS Volume

How to Extend the File System After Expanding an AWS EC2 Root EBS Volume

Problem Description

When creating an EC2 instance using an AWS AMI, you may allocate a larger size for the root EBS volume (e.g., 20GB). However, after SSHing into the instance and running df -h, the /dev/xvda1 partition may still show only the default 8GB of available space.

Root Cause

AWS AMI images are typically pre-configured with a smaller root partition. While the underlying EBS volume has been resized, the operating system's file system has not been automatically extended to use the new capacity. You can verify the actual block device size (e.g., /dev/xvda) using commands like fdisk -l or lsblk.

Solution: Extend the Root File System

To utilize the additional space on the EBS volume, you must extend the file system on the root partition. For common Linux distributions (Amazon Linux 2, Ubuntu, CentOS) using ext2/ext3/ext4 or XFS, follow these steps.

Step 1: Confirm Device and File System Type

sudo fdisk -l /dev/xvda
sudo lsblk -f

Step 2: Extend the File System

Use the appropriate command for your file system type. For ext2/ext3/ext4:

sudo resize2fs /dev/xvda1

For XFS (common on newer AMIs like Amazon Linux 2023):

sudo xfs_growfs /

Step 3: Verify the Result

Run df -h again to confirm the /dev/xvda1 partition now reflects the full capacity.

df -h

Alternative: Attach a New EBS Volume

If the root volume is full or you prefer to separate data, you can attach an additional EBS volume.

  1. Create and attach a new EBS volume to your instance via the AWS Console.
  2. Identify the new block device (e.g., /dev/xvdf) using lsblk.
  3. Partition (using fdisk or parted), format (e.g., sudo mkfs -t ext4 /dev/xvdf1), and mount the new volume.
  4. Add an entry to /etc/fstab for automatic mounting at boot.

Important: It is recommended to create an EBS snapshot backup before performing these operations. Test the procedure on a non-production instance first.

Post a Comment

Your email will not be published. Required fields are marked with *.