Daniel López Azaña

Theme

Social Media

Blog

GNU/Linux, Open Source, Cloud Computing, DevOps and more...

How to enlarge the size of an EBS volume in AWS and extend an ext4 partition

Logo AWS EBSWhen we completely fill up an ext4 filesystem mounted on a partition hosted in an EBS volume of Amazon Web Services and we can not do anything to free space because we do not want to lose any of the stored data, the only solution is to grow up the volume and extend the associated partition up to 100% of its capacity to obtain free space again.

We start in our example with a 50 GB volume full to 100%. We want to extend it to double the size, 100 GB:

~# df -h /var/respaldo
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvdg1       50G   47G     0 100% /var/respaldo
~# lsblk | grep xvdg
xvdg    202:96   0   50G  0 disk 
└─xvdg1 202:97   0   50G  0 part

Here are the steps to achieve it:

1. Unmount the filesystem

The first thing to do will be to unmount the filesystem or filesystems present in the EBS volume:

~# sync
~# umount /var/respaldo

2. Detach the volume from its instance

Next locate in the AWS console the volume and detach it from its EC2 instance:

Desvincular volumen de una instancia

You can also do the same from the command line (AWS CLI):

~# aws ec2 describe-instances --region eu-west-1 --filters Name=tag-key,Values="Name" Name=tag-value,Values="*gnadmin*" | grep InstanceId
                    "InstanceId": "i-02ed7d0bda0768883",
~# aws ec2 describe-volumes --region eu-west-1 --filters Name=attachment.instance-id,Values=i-02ed7d0bda0768883 --filters Name=tag-key,Values="Name" Name=tag-value,Values="*respaldo*" --query 'Volumes[*].{ID:VolumeId,Tag:Tags}'
[
    {
        "Tag": [
            {
                "Key": "Name",
                "Value": "gnadmin11 - /var/respaldo"
            }
        ],
        "ID": "vol-0ca0db0a8d60f1aa5"
    }
]
~# aws ec2 detach-volume --volume-id vol-0ca0db0a8d60f1aa5
{
    "AttachTime": "2016-12-01T18:19:50.000Z",
    "InstanceId": "i-02ed7d0bda0768883",
    "VolumeId": "vol-0ca0db0a8d60f1aa5",
    "State": "detaching",
    "Device": "/dev/sdg"
}

As you can see, the volume status when you launch the previous command is detaching. You will have to wait until it is available in order to continue with this procedure, but don’t worry, it is almost instantaneous:

~# aws ec2 describe-volumes --region eu-west-1 --volume-id vol-0ca0db0a8d60f1aa5 | grep State
            "State": "available",

3. Create a volume snapshot

Now you will take a snapshot of your volume and all its contents:

You can get the same result with the aws-cli create-snapshot command:

~# aws ec2 create-snapshot --region eu-west-1 --volume-id vol-0ca0db0a8d60f1aa5 --description "Backup temporal /var/respaldo" | grep SnapshotId

In this case you will have to wait for a long time to finish, especially in large volumes. In the meantime you can check the progress with the following command:

~# aws ec2 describe-snapshots --region eu-west-1 --snapshot-id snap-008c4162b8fb6b76d | egrep "Progress|State"
            "State": "pending",
            "Progress": "27%",

4. Create a new larger volume from the snapshot

As I indicated at the beginning, now you will create a larger volume of 100 GB from the snapshot you took in the previous section. The volume type in the example is magnetic , but you can select the type that best suits your needs:

To do the same from the command line you will execute the following:

~# aws ec2 create-volume --size 100 --region eu-west-1 --availability-zone eu-west-1a --snapshot-id snap-008c4162b8fb6b76d --volume-type standard 
{ 
    "Size": 100, 
    "SnapshotId": "snap-008c4162b8fb6b76d", 
    "Encrypted": true, 
    "VolumeType": "standard", 
    "AvailabilityZone": "eu-west-1a", 
    "VolumeId": "vol-0b87a40a0ff017f69", 
    "CreateTime": "2017-05-23T14:01:25.890Z", 
    "State": "creating" 
}

5. Attach the new volume to your instance

Once the volume is created and becomes available, you can attach it again to your instance from the AWS console:

Or from the command line:

~# aws ec2 attach-volume --volume-id vol-0b87a40a0ff017f69 --instance-id i-02ed7d0bda0768883 --device /dev/sdg 
{ 
    "VolumeId": "vol-0b87a40a0ff017f69", 
    "InstanceId": "i-02ed7d0bda0768883", 
    "Device": "/dev/sdg", 
    "AttachTime": "2017-05-23T14:35:26.200Z", 
    "State": "attaching" 
}

6. Extend partition to grow it up to 100% of available space

After attaching the volume to its instance, you can now see the volume has a size of 100GB with the lsblk command, but the partition is still 50GB:

~# lsblk | grep xvdg 
xvdg    202:96   0  100G  0 disk  
└─xvdg1 202:97   0   <strong>50G</strong>  0 part

So now it is necessary to extend it to increase its size to 100 GB so that 100% of the available space is used and not wasted. It may be necessary to unmount the file system again, since the operating system automatically mount it after attaching the volume to the instance if it finds the partition necessary information in the /etc/fstab file:

~# umount /var/respaldo
~# parted /dev/xvdg resizepart 1 100%
Warning: Not all of the space available to /dev/xvdg appears to be used, you can fix the GPT to use all of the space (an extra 104857600 blocks) or continue with the current setting?  
Fix/Ignore? Fix
Information: You may need to update /etc/fstab.
~# lsblk | grep xvdg 
xvdg    202:96   0  100G  0 disk  
└─xvdg1 202:97   0  <strong>100G</strong>  0 part /var/respaldo

7. Resize the ext4 filesystem to 100% of the partition

In the same way that it was necessary to resize the partition so that it occupies 100% of the volume, now it is necessary to do the same with the filesystem with which the partition is formatted, ext4 in our example. You can do this using the resize2fs command:

~# resize2fs /dev/xvdg1 
resize2fs 1.42.13 (17-May-2015) 
Filesystem at /dev/xvdg1 is mounted on /var/respaldo; on-line resizing required 
old_desc_blocks = 4, new_desc_blocks = 7 
The filesystem on /dev/xvdg1 is now 26214139 (4k) blocks long.

8. Optionally check filesystem’s integrity

Although the resizing performed by the resize2fs command is very secure and data corruption is unlikely to occur, it is a good idea to perform a complete filesystem check before you start using it again. Previously it may be necessary to unmount it, since it will probably be automatically mounted after resize2fs command finishes:

~# fsck.ext4 -f /dev/xvdg1 
e2fsck 1.42.13 (17-May-2015) 
Pass 1: Checking inodes, blocks, and sizes 
Pass 2: Checking directory structure 
Pass 3: Checking directory connectivity 
Pass 4: Checking reference counts 
Pass 5: Checking group summary information 
/dev/xvdg1: 610218/6553600 files (0.2% non-contiguous), 12654908/26214139 blocks

9. Mount the filesystem

Finally you will mount the filesystem and you will be able to see how now its occupation has fallen to 50% and you have almost 50 GB of additional space available, which was the initial goal:

~# mount /dev/xvdg1
~# df -h /dev/xvdg1
Filesystem      Size  Used Avail Use% Mounted on 
/dev/xvdg1       99G   47G   <strong>48G  50%</strong> /var/respaldo
Daniel López Azaña

About the author

Daniel López Azaña

Tech entrepreneur and cloud architect with over 20 years of experience transforming infrastructures and automating processes.

Specialist in AI/LLM integration, Rust and Python development, and AWS & GCP architecture. Restless mind, idea generator, and passionate about technological innovation and AI.

Related articles

terraform-and-route53-logos

How to quickly import all records from a Route53 DNS zone into Terraform

The terraform import command allows you to import into HashiCorp Terraform resources that already existed previously in the provider we are working with, in this case AWS. However, it only allows you to import those records one by one, with one run of terraform import at a time. This, apart from being extremely tedious, in some situations becomes impractical. This is the case for the records of a Route53 DNS zone. The task can become unmanageable if we have multiple DNS zones, each one with tens or hundreds of records. In this article I offer you a bash script that will allow you to import in Terraform all the records of a Route53 DNS zone in a matter of seconds or a few minutes.

February 8, 2022
Script to automatically change all gp2 volumes to gp3 with aws-cli

Script to automatically change all gp2 volumes to gp3 with aws-cli

Last December Amazon announced its new EBS gp3 volumes, which offer better performance and a cost saving of 20% compared to those that have been used until now (gp2). Well, after successfully testing these new volumes with multiple clients, I can do nothing but recommend their use, because they are all advantages and in these 2 and a half months that have passed since the announcement I have not noticed any problems or side effects.

February 16, 2021
AWS security groups

How to automatically update all your AWS EC2 security groups when your dynamic IP changes

One of the biggest annoyances when working with AWS and your Internet connection has a dynamic IP is that when it changes, you immediately stop accessing to all servers and services protected by an EC2 security group whose rules only allow traffic to certain specific IP’s instead of allowing open connections to everyone (0.0.0.0.0/0).Certainly the simplest thing to do is always allowing traffic on a given port to everyone, so that even if you have a dynamic IP on your Internet connection you will always be able to continue accessing even if it changes. But opening traffic on a port to everyone is not the right way to proceed from a security point of view, because then any attacker will be able to access that port without restrictions, and that is not what you want.

January 12, 2021

Comments

Ultimate September 15, 2020
Huh. This sounds way too complicated.. Is there no other way :(
Daniel October 14, 2020
Yes, some time ago Amazon added feature to modify EBS volume size, so you can increase it to the desired size, and then run the growpart and resize2fs commands to resize partition and filesystem respectively with 0 downtime.

Submit comment