PureDevOps Community

AWS: Extend a Linux file system (volume) of EC2 Instance

  • Increase the volume to the desired size either using 1) console ->ec2 Instances → volume → select the volume and choose the option to increase in the right side buttons or 2) cli command aws ec2 modify-volume --size 150 --volume-id vol-1234567890abcdef0

Pre-Requisite

Extend the file system of EBS volumes

To extend the file system of EBS volumes
  1. Connect to your instance.

  2. Resize the partition, if needed. To do so:
    a. Check whether the volume has a partition. Use the lsblk command.
    In the following example output, the root volume (xvda) has a partition (xvda1), while the additional
    volume (xvdf) has no partition.

     ```
       [ec2-user ~]$ sudo lsblk                
       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  24G  0 disk  
    

    If the volume has a partition, continue the procedure from the following step (2b). If the volume has no partitions, skip steps 2b, 2c, and 2d, and continue the procedure from step 3.

    b. Check whether the partition needs to be extended. In the lsblk command output from the previous step, compare the partition size and the volume size.

    If the partition size is smaller than the volume size, continue to the next step. If the partition size is equal to the volume size, the partition can’t be extended.

    c. Extend the partition. Use the growpart command and specify the partition to extend.

    [ec2-user ~]$ sudo growpart /dev/xvda 1
    

    d. Verify that the partition has been extended. Use the lsblk command. The partition size should now be equal to the volume size.

    [ec2-user ~]$ sudo lsblk               
    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  24G  0 disk
    
    
  3. Extend the file system.

         a.  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.

  • [XFS file system] Use the xfs_growfs command and specify the mount point of the file system that you noted in the previous step.

    [ec2-user ~]$ sudo xfs_growfs -d /
    

    [Ext4 file system] Use the resize2fs command and specify the name of the file system that you noted in the previous step.

      [ec2-user ~]$ sudo resize2fs /dev/xvda1