How to Reduce the Size of an EBS Volume

EBS Size ReductionWith the world of the cloud growing at exponential rates, the demand for persistent storage is also increasing. AWS EBS offers persistent storage for Amazon EC2. EBS is a cost effective, plug and play device that can be attached to one instance at a time. EBS also offers a backup and recovery mechanism with the help of snapshots. With growing storage needs, users may have to think about increasing the size of their storage. Users may also shrink large EBS volumes to save on unutilized volume costs.

All things considered, there is a real demand to reduce the size of EBS volumes. However, it’s no easy task. It may require you to check certain conditions such as the total utilized storage compared to the desired size of your new EBS volume. You also need to learn your data’s growth rate and choose the size of your EBS volume accordingly.

In this how-to guide, we will cover how to create a smaller sized data volume (in addition to a root device) using Linux and Windows.

A few points to keep in mind:

  • It’s impossible to reduce the size of an original volume, so you need to create a new volume and migrate the existing data.
  • Always remember to back-up your data before migrating.
  • Remove the large (old) volume so you don’t continue to pay for storage you’re not using.

 

Get 24/7 Data Access and 100% Uptime with AWS + N2WS

Step-by-Step: Create a Smaller EBS Volume (Linux)

In the steps below, we will show you how to create a smaller EBS volume using an Ubuntu Linux instance with an 8GB data volume attached in addition to the root volume.

Locate the target EBS volume:

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 describe -volumes

How to Reduce the Size of an EBS Volume

Mount the additional 8GB data volume to a directory called ‘new’.

How to Reduce the Size of an EBS Volume

The new directory (the data volume) is not empty, as seen below:

How to Reduce the Size of an EBS Volume

Since the volume is not completely full, having consumed less than 1GB of storage, you can create a smaller sized volume and copy the data from the original volume. As mentioned above, the new volume size must be bigger than the amount of space that was consumed by the original volume.

The steps below can help you create your new, smaller volume:

1. Create a 5GB volume in the same zone as your running instance.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 create-volume –size 5 –region ap-southeast-1 –availability-zone ap-southeast-1a –volume-type gp2

How to Reduce the Size of an EBS Volume

2. Now, attach the new volume as ‘/dev/sdf’ to your running instance where the old EBS external volume is mounted.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 attach-volume –volume-id vol-e2a37a0a –instance-id i-5d3b2b90 –device /dev/sdf

How to Reduce the Size of an EBS Volume

3. Since the new volume behaves like a raw unformatted block volume, you need to first format it with a file system and mount it to the instance.

See the steps below to mount the volume:

In this case, we used ext3 as a file system, but that’s not mandatory. You can select whichever file system you need.

a. mkfs -t ext3 /dev/sdf

How to Reduce the Size of an EBS Volume

b. echo “/dev/sdf /newone ext3 noatime 0 0” >> /etc/fstab

How to Reduce the Size of an EBS Volume

c. mkdir /newone

How to Reduce the Size of an EBS Volume

d. mount /newone

How to Reduce the Size of an EBS Volume

e. df -h

Now you can see that the new 5GB external EBS volume is successfully mounted in the directory as ‘newone’

How to Reduce the Size of an EBS Volume

4. Copy the data from the 8GB volume to the 5GB volume using the following command:

sudo rsync -aHAXxSP /new /newone

How to Reduce the Size of an EBS Volume

In the command With option aHAXxSP makes a full backup of system root directory.

We recommend ensuring that there are no writes on the disk in order to achieve a consistent file system. You can temporarily freeze IO during copies. Some file systems, such as XFS, allow you to freeze and unfreeze IO activity in order to successfully and consistently copy data. Learn more about consistent snapshots and freezing applications.

As you can see, all of the data has been copied to the new 5GB volume mounted on the ‘newone’ directory.

How to Reduce the Size of an EBS Volume

You can find the difference between original volume and new volume directory with below command :

diff -r /new /newone

How to Reduce the Size of an EBS Volume

5. You can now detach the old 8GB volume from the instance, but first stop the instance to prevent inconsistencies.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 stop-instances –instance-ids i-5d3b2b90

How to Reduce the Size of an EBS Volume

6. After stopping the instance, go to the ‘Volumes’ section of your AWS EC2 console and detach the external 8GB EBS volume that is mounted on the ‘new’ directory.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 detach-volume –volume-id vol-bc69b754

How to Reduce the Size of an EBS Volume

7. Start the instance.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 start-instances –instance-ids i-5d3b2b90

How to Reduce the Size of an EBS Volume

Now you can see that the external 8GB EBS volume has been successfully detached and the only remaining external data volume is your new 5GB volume.

How to Reduce the Size of an EBS Volume

Be sure to delete the old 8GB volume, however, or else you will continue to be charged for it.

Now that we’ve shown you how to create a more appropriately sized volume attached to a Linux instance, we’ll show you how to use a 10GB external EBS data volume to create a 5GB volume on a WIndows instance. As shown below, the data volume is attached as drive ‘D’.

Assuming that less than 5GB of the original 10GB volume’s total disk space is being consumed by the ‘D’ drive, it makes sense to make the new volume 5GB.

How to Reduce the Size of an EBS Volume

Below are the contents of volume ‘D’:

How to Reduce the Size of an EBS Volume

Now, in order to create a smaller volume than the one shown above, follow the steps outlined below:

1. Create a 5GB volume in the same zone as the running instance.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 create-volume –size 5 –region ap-southeast-1 –availability-zone ap-southeast-1a –volume-type gp2

How to Reduce the Size of an EBS Volume

2. Attach the new volume as ‘/dev/sdf’ to the running instance where the previous EBS external volume is also mounted.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 attach-volume –volume-id vol-e2a37a0a –instance-id i-a79a896a –device /dev/sdf

How to Reduce the Size of an EBS Volume

3. Mount the new volume on the instance:

a. Since the new volume is raw and unformatted, you need to first format it. Go to ‘My Computer’ and select ‘Manage’.

How to Reduce the Size of an EBS Volume

b. The volume might be attached as raw unformatted disk to a Windows instance, so you have to first initialize the disk. Make the new disk volume 5GB.

How to Reduce the Size of an EBS Volume

c. Create a new drive (volume) and label it ‘E’.

How to Reduce the Size of an EBS Volume

4. The new 5GB volume is now available as an additional drive.

How to Reduce the Size of an EBS Volume

5. With the following command, you can copy all of your data from the 10GB external volume is mounted on drive ‘D’ to the new 5GB external EBS volume that is now mounted onto drive ‘E’.

xcopy /s D:\*.* E:\

How to Reduce the Size of an EBS Volume

Once copying is complete, check the contents of drive ‘E’ to make sure that all of your data was successfully copied over.

How to Reduce the Size of an EBS Volume

6. In order to detach the old 10GB volume from your instance, you need to first stop the instance.

How to Reduce the Size of an EBS Volume

Using the AWS CLI:

aws ec2 stop-instances –instance-ids i-a79a896a

How to Reduce the Size of an EBS Volume

7. Afterwards, go to the ‘Volumes’ section of your AWS EC2 console and detach the external 10GB EBS volume that was mounted on drive ‘D’.

How to Reduce the Size of an EBS Volume

Using the AWS CLI

aws ec2 detach-volume –volume-id vol-3dbf7d5

How to Reduce the Size of an EBS Volume

There you have it! We’ve shown you two ways to create a more appropriately sized EBS volume. Remember to back up your data using EBS snapshots BEFORE performing any of the steps outlined above.

N2W Software is the maker of Cloud Protection Manager (CPM), an enterprise-class backup, recovery and disaster recovery solution for Amazon EC2, based on AWS native snapshots for EBS volumes, RDS databases and Redshift clusters. See otheur CPM datasheet for a full list of features.

Share this post →

You might also like: