Published December 14, 2018 by

How to Reduce EBS volume size of AWS EC2 Linux Instance

In this tutorial, I will show you how to reduce EBS volume size in AWS using an Ubuntu Linux instance. In this example, I have 25GB root volume and try to decrease it to 8GB.

1. Login to AWS management consoles and stop the instance.
2. Select the volume attached to this instance and detach it.

3. Select the detached volume and create a snapshot.

4. Create a new volume in the same availability zone of your instance of the preferred size. This volume will eventually replace your current volume.

5. Create another new volume from a snapshot which has been we have created in Step 3. Make sure you need to create in same availability zone.
So finally we have 3 volume now. one is the original volume, the second one is new created (reduce size) volume and, the third one is created from a snapshot.

6. Now Reattach the three volumes to an instance as described below.

Original volume at /dev/sda1
new volume at /dev/sdf
snapshot at /dev/sdg

7. Restart the instance and sign in with SSH. Create a filesystem on the new volume using mkfs.ext4
 sudo mkfs.ext4 /dev/xvdf  

8. it generates the following output

9. Create mount points for snapshot, new volume and mount them using below commands.
 sudo mkdir /mnt/newvol  
 sudo mkdir /mnt/snapshot  
 sudo mount /dev/xvdf /mnt/newvol  
 sudo mount -t ext4 /dev/xvdg1 /mnt/snapshot  

10. Find the filesystem label of the original volume, so as to change the same for a new volume.
 sudo e2label /dev/xvda1  
It produces output like this cloudimg-rootfs

11. Set this as the label for the new volume.
 sudo e2label /dev/xvdf cloudimg-rootfs  

12. Now, copy all data from the snapshot to new volume using rsync command.
 sudo rsync -avx /mnt/snapshot/ /mnt/newvol/  
It takes while depends on your data size.

13. Install grub on a new volume using below command
 sudo grub-install --root-directory=/mnt/newvol/ --force /dev/xvdf  

14. Unmount the new volume
 sudo umount /mnt/newvol  

15. Freshly check filesystem
 sudo e2fsck -f /dev/xvdf  

16. Change UUID insert below command.
It produces output like below

/dev/xvda1: LABEL="cloudimg-rootfs" UUID="bbf64c6d-bc15-4ae0-aa4c-608fd9820d95" TYPE="ext4" PARTUUID="9f3e4931-01"

Copy the UUID string from the result of blkid (the one for original volume /dev/xvda) and use it with tune2fs, replacing <uuid> below,
 sudo tune2fs -U <uuid> /dev/xvdf  
Change <uuid> with real UUID.

17. Freshly check again filesystem
 e2fsck -f /dev/xvdf  

18. Notes: if tune2fs fails to change the UUID, you may need to unset uninit_bg flag* on the new volume, before trying the same command again.
 sudo tune2fs -O ^uninit_bg /dev/xvdf  
and also repeat Step 17.

19. Now Sign out from SSH and Stop Instance from AWS management console

20. Detach All three volume from the instance

21. Attach new volume (reduced size) at /dev/sda1 

22. Start the instance and check.

23. if all things are good, then delete remaining two EBS volumes, which are no need more. Also, delete snapshot which we have taken in Step 3.