With 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 an EBS volume.
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.
Fortify your data backup strategy across every critical dimension—from security to disaster recovery to cost savings.
- Efficiency + Optimization
- Security + Control
- Orchestration + Visibility
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:
Using the AWS CLI:
aws ec2 describe -volumes
Mount the additional 8GB data volume to a directory called ‘new’.
df -h
The new directory (the data volume) is not empty, as seen below:
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:
Create a 5GB volume in the same zone as your running instance.
Using the AWS CLI, write the following command:
aws ec2 create-volume --size 5 --region ap-southeast-1 --availability-zone ap-southeast-1a --volume-type gp2
Now, attach the new volume as ‘/dev/sdf’ to your running instance where the old EBS external volume is mounted.
Using the AWS CLI, run the command:
aws ec2 attach-volume --volume-id vol-e2a37a0a --instance-id i-5d3b2b90 --device /dev/sdf
Mounting the volume
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.
mkfs -t ext3 /dev/sdf
echo "/dev/sdf /newone ext3 noatime 0 0" >> /etc/fstab
mkdir /newone
mount /newone
df -h
Now you can see that the new 5GB external EBS volume is successfully mounted in the directory as ‘newone’
Copying the data
Copy the data from the 8GB volume to the 5GB volume using the following command:
sudo rsync -aHAXxSP /new /newone
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.
You can find the difference between original volume and new volume directory with below command:
diff -r /new /newone
Detaching the old volume and stop the instance
You can now detach the old 8GB volume from the instance, but first stop the instance to prevent inconsistencies.
Using the AWS CLI command:
aws ec2 stop-instances --instance-ids i-5d3b2b90
Detaching the external volume
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.
Using the AWS CLI:
aws ec2 detach-volume --volume-id vol-bc69b754
Starting the instance
Start the instance.
Using the AWS CLI:
aws ec2 start-instances --instance-ids i-5d3b2b90
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.
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.
Below are the contents of volume ‘D’:
Creating the smaller volume
Now, in order to reduce the size of an EBS volume and 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.
Using the AWS CLI:
aws ec2 create-volume --size 5 --region ap-southeast-1 --availability-zone ap-southeast-1a --volume-type gp2
Attaching the new volume
Attach the new volume as ‘/dev/sdf’ to the running instance where the previous EBS external volume is also mounted.
Using the AWS CLI:
aws ec2 attach-volume --volume-id vol-e2a37a0a --instance-id i-a79a896a --device /dev/sdf
Mounting the new volume on the instance
Mount the new volume on the instance:
Since the new volume is raw and unformatted, you need to first format it. Go to ‘My Computer’ and select ‘Manage’.
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.
Create a new drive (volume) and label it ‘E’.
The new 5GB volume is now available as an additional drive.
Copying your data
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:\
Once copying is complete, check the contents of drive ‘E’ to make sure that all of your data was successfully copied over.
Detaching the old volume
In order to detach the old 10GB volume from your instance, you need to first stop the instance.
Using the AWS CLI:
aws ec2 stop-instances --instance-ids i-a79a896a
Afterwards, go to the ‘Volumes’ section of your AWS EC2 console and detach the external 10GB EBS volume that was mounted on drive ‘D’.
Using the AWS CLI:
aws ec2 detach-volume --volume-id vol-3dbf7d5
There you have it! We’ve shown you two ways to create a more appropriately sized EBS volume and efficiently and easily reduce the size of your EBS volume so you can save on costs. Remember to back up your data using EBS snapshots BEFORE performing any of the steps outlined above.
Top-rated tool for AWS – N2WS Backup & Recovery
N2WS gives you the abilitity to eliminated downtime and achieve truly high availiability for your critical AWS cloud workloads. Third-party products can perfectly complement existing AWS capabilities. N2WS Backup & Recovery was built specifically for AWS. By leveraging native AWS technologies to utilize block-level and incremental snapshots, it allows for very efficient backups which can be easily automated.
With N2WS Backup & Recovery for AWS, you can eliminate downtime and achieve truly high availability for your critical AWS cloud workloads.
Ezra is the Sr. Director of Alliances and Partners at N2WS with more than 10 years of experience. Ezra enjoys long bike rides, cultivating rare semi-succulents, and building complex aquatic ecosystems.
Try N2WS Backup & Recovery free after 30-days it automatically converts to our Forever Free edition (no credit card needed).
Read Also