I want to extend my Linux file system after I increase my Amazon Elastic Block Store (Amazon EBS) volume on my Amazon Elastic Compute Cloud (Amazon EC2) instance.
Resolution
Important: The following steps apply only to file systems that use the entire disk device. You can't use these steps for partitions, the root file system, RAID devices, or Logical Volume Manager (LVM). You must wait at least 6 hours between modifications of an Amazon EBS volume, and the file system must be in the optimizing state before you resize it.
The following example extends an 8 GB ext4 file system to fully use a 16 GB volume.
Create a snapshot and connect to your instance
Create a snapshot of your volume, and then use SSH to connect to your instance.
Verify that the file system size matches the volume size
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
-
To check the size and usage percentage of the file systems, run the following df -h command:
df -h
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 7.7G 7.7G 0 100% /
/dev/xvdf 16G 7.1G 8.0G 48% /home/ubuntu/test
Note: In the example output, the /dev/xvdf file system size is 16 G and is 48% full. On Nitro instances, disk devices use the naming format /dev/nvmeXn1 and partitions use the naming format /dev/nvmeXn1pY. In the file system names, X identifies the disk and Y identifies the partition.
-
To check the size of the xvdf volume, run the following lsblk command:
lsblk
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 16G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdf 202:80 0 16G 0 disk /home/ubuntu/test
Note: In the example output, the size of the xvda volume is 16 GB.
Extend the partition
-
In the lsblk command's output, compare the partition size and the volume size, and then take one of the following actions:
If the partition size is smaller than the volume size, then proceed to step 2.
If the partition size is equal to the volume size, then proceed to step 3.
Note: If a partition contains the file system, then extend the partition before you expand the file system. If your operating system (OS) doesn't detect the volume size change, then run the following command:
echo 1 | sudo tee /sys/class/block/xvda/device/rescan >/dev/null
Note: Replace xvda with your volume ID. If the size still doesn't update, then reboot the OS.
-
To extend the partition, run the following growpart command and specify the device name and the partition number:
sudo growpart /dev/xvda 1
Example output:
CHANGED: partition=1 start=2048 old:
Note: The partition number is the number after the device name. For example, for xvda1, the partition number is 1.
-
To verify that you extended the partition, run the lsblk command:
lsblk
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 16G 0 disk
└─xvda1 202:1 0 16G 0 part /
xvdf 202:80 0 16G 0 disk /home/ubuntu/test
Note: The partition size must be equal to the volume size.
Extend an ext3 or ext4 file system
If you use an ext3 or ext4 file system, then run the following resize2fs command:
sudo resize2fs /dev/xvda1
Note: In the resize2fs command, include the device name from the output of the df -h command.
Example output:
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/xvda1 is mounted on /;
on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/xvda1 is now 4194304 (4k) blocks long
Note: You can run the resize2fs command only for ext3 and ext4 file systems. If you receive a "resize2fs: Device or resource busy while trying to open /dev/xvdf" error, then you applied the resize to a disk partition. If you receive a "resize2fs: Bad magic number in super-block while trying to open /dev/xvda1:" error, then the file system isn't ext4. To check the file system type, run the df -hT command. If you receive an "open: No such file or directory while opening /dev/xvdb1:" error, then you specified an incorrect partition. To check the partition, run the df -hT command.
Extend an XFS file system
If you use an XFS file system, then run the xfs_growfs command and specify the mount point of the file system. For example, to extend a file system a file system mounted on / (root), run the following xfs_growfs command:
sudo xfs_growfs -d /
Note: If you receive an "xfs_growfs: /data is not a mounted XFS filesystem" error, then you specified the incorrect mount point or the file system isn't XFS. To check the mount point and file system type, run the df -hT command. If you receive a "data size unchanged, skipping:" error, then the file system already extends the entire volume. If the volume has no partitions, then confirm that the volume modification succeeded.
Verify the file system extension
To verify that you extended the file system, run the following df -h command:
df -h
In the output, confirm that the file system size is equal to the volume size.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 16G 7.7G 8.3G 48% /
/dev/xvdf 16G 7.1G 8.0G 48% /home/ubuntu/test
Note: In the preceding example output, the /dev/xvdf file system is 16 G in size and 48% full.
Related information
Extend the file system after resizing an Amazon EBS volume
Modify an Amazon EBS volume using Elastic Volumes operations
Make an Amazon EBS volume available for use