I recently shifted all my images and videos data from EBS volume to S3 as it was cheaper. After completing the shifting I had plenty of unused GBs of space left. I decided to reduce my EBS volume size assuming it would be as simple as it was to increase volume size. However that did not turn out to be the case and I end up spending two weeks figuring it out how to do it!
Thus, I have created the guide below for someone trying to do something similar, hoping to save their time.
Before doing anything else, take a snapshot of your volume. So in case of any blunder, your data is safe.
Create a new EC2 instance and volume. Make sure this instance is identical your current instance (same operating system and everything else) or else it won’t boot. I made this mistake and wasted 8 extra days stressing over it. This instance itself is not required but the volume created with it will be used. You may delete this instance once volume is created with it.
Create another new Amazon EC2 instance same as your existing instance (Very important). A micro or small instance should do. We only need this temporarily to run some commands and will delete it once our volume is reduced.
Detach the old, over-sized volume from its instance and attach it to the instance you created in step #3 above.
Mount the old volume as /dev/sdf/ (it will become /dev/xvdf).
Mount the new, smaller volume (created in step #2) as /dev/sdg (it will become /dev/xvdg).
Run this command:
sudo e2fsck -f /dev/xvdf1
If the last command runs without errors, go ahead and run this command:
sudo resize2fs -M -p /dev/xvdf1
The last line from the resize2fs command above will tell you how many 4k blocks are on your file system. Now, you need to calculate the number of 16MB blocks you need.
Use this formula:
blockcount * 4 / (16 * 1024)
Where blockcount is from the last line of the resize2fs command.
Round up this number to give yourself a small buffer. Save this result. We will use it soon.
Now run this command:
sudo dd bs=16M if=/dev/xvdf1 of=/dev/xvdg1 count=SavedResultFromPreviousStep
This may take a while to complete depending on how much data you have. This step performs the actual copying of data from the old, over-sized drive to the new, smaller drive.
When the copy finishes, run the following two commands:
sudo resize2fs -p /dev/xvdg1
sudo e2fsck -f /dev/xvdg1
These commands will resize and check that everything is good with your new file system.
If everything is good, shut down the temporary instance. Detach both the old and new volumes. Attach the new, small volume to your original EC2 instance. This time, mount the drive to your boot device (/dev/sda1).
Boot up your original EC2 instance and test that everything works well with the new, small volume.
I hope the above article provided you with helpful giude and if you still run into problems, you can contact me. I would love to help you, if I could. :)