Skip to content

How do I extend my Linux file system after I increase my Amazon EBS volume on my Amazon EC2 instance?

5 minute read
0

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

Resize the file system when it's in the optimizing state.

Note: The following steps apply only to file systems that use the entire disk device. You can't use the steps to increase file systems on partitions, the root file system, RAID devices, or Logical Volume Manager (LVM). The following example extends an 8 GB ext4 file system to fully use a 16 GB volume.

To extend your Linux file system, complete the following steps:

  1. Create a snapshot of your volume.

  2. Use SSH to connect to your instance.

  3. To check the size and the percentage that the file systems uses, 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 preceding example output, the /dev/xvdf file system size is 16 G and is 48% full. On Nitro instances, disk devices are named /dev/nvmeXn1 and partitions are named /dev/nvmeXn1pY. In the file system names, X identifies the disk and Y identifies the partition.

  4. To check the size of the xvdf volume, run the following lsblk command:

    ubuntu@ip-172-31-32-114:~ 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 preceding example output, the size of the xvda volume is 16 GB.

  5. 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 6.
    If the partition size is equal to the volume size, then you don't need to extend the partition. Proceed to step 7.
    Note: If there's a partition in the volume that contains a file system, then extend the partition before you expand the file system.

  6. Run the following growpart command to extend the partition 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.

  7. To verify that you extended the partition, run the lsblk command:

    ubuntu@ip-172-31-32-114:~ 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: The partition size must be equal to the volume size.

  8. Run the following resize2fs command to automatically extend the size of the /dev/xvda file system to the full space on the volume. Include the device name from the output of the df -h command:

    ubuntu@ip-172-31-32-114:~ sudo resize2fs /dev/xvda1

    Note: You can run the resize2fs command only for ext3 and ext4 filesystems.
    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: In the preceding example output, the volume uses an ext4 file system. 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 a "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.

  9. Run the xfs_growfs command and specify the mount point of the file system.
    For example, to extend a file system mounted on a forward slash (/), run the following command:

    ubuntu@ip-172-31-32-114:~ sudo xfs_growfs -d /

    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 is successful.

  10. To verify that you extended the file system, run the following df -hT command and then confirm that the file system size is equal to the volume size:

    ubuntu@ip-172-31-32-114:~ 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 preceding example output, the /dev/xvdf file system is 16 G in size and 48% full.

Related information

View information about an Amazon EBS volume

Make an Amazon EBS volume available for use

AWS OFFICIALUpdated 5 months ago