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 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.
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’. 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: 1. Create a 5GB volume in the same zone as your running instance. Using the AWS CLI:
aws ec2 create-volume –size 5 –region ap-southeast-1 –availability-zone ap-southeast-1a –volume-type gp2 2. Now, attach the new volume as ‘/dev/sdf’ to your running instance where the old EBS external volume is mounted. Using the AWS CLI:
aws ec2 attach-volume –volume-id vol-e2a37a0a –instance-id i-5d3b2b90 –device /dev/sdf 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 b. echo “/dev/sdf /newone ext3 noatime 0 0” >> /etc/fstab c. mkdir /newone d. mount /newone e. df -h
Now you can see that the new 5GB external EBS volume is successfully mounted in the directory as ‘newone’4. 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 5. You can now detach the old 8GB volume from the instance, but first stop the instance to prevent inconsistencies. Using the AWS CLI:
aws ec2 stop-instances –instance-ids i-5d3b2b90 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. Using the AWS CLI:
aws ec2 detach-volume –volume-id vol-bc69b754 7. 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’: 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. Using the AWS CLI:
aws ec2 create-volume –size 5 –region ap-southeast-1 –availability-zone ap-southeast-1a –volume-type gp2 2. 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 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’. 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. c. Create a new drive (volume) and label it ‘E’.4. The new 5GB volume is now available as an additional drive. 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:\ Once copying is complete, check the contents of drive ‘E’ to make sure that all of your data was successfully copied over. 6. 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 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’. 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. Remember to back up your data using EBS snapshots BEFORE performing any of the steps outlined above. Try N2WS Backup & Recovery (CPM) for FREE!Read Also
Get the ultimate peace-of-mind with our automated backup solution that allows you to restore in 1-click. It’s simple, secure, and “just works” so you can focus on what you do best. Start with our “Forever Free” Plan!