Sometimes we attach large volumes while launching AWS EC2 instance, later to realise only that we don’t need large size and need to decrease it.
There’s no option in AWS console to perform the same but with a small trick we can achieve the same.
In this article, we’ll see detailed steps to do that. The task follow below steps.
- Take snapshot the current volume (let’s say it’s 9 GB for this article)
- Create a new smaller EBS volume (let’s say it’s 7 GB for this article)
- Attach the new (smaller) volume
- Mount the new (smaller) volume
- Copy data from old (larger) volume to the new (smaller) volume
- Detach and unmount old (larger) volume
In below screen, we can see that we already have volume attached to instance and size is 9 GB.
You can see that volume is mounted in a server under /mnt/Big_Volume and have some dummy files in it also.
Showing fstab entry for this volume.
NOW WE WANT TO REDUCE THE SIZE FROM 9 GB TO 7 GB.
Create another volume of 7 GB and attach it to the instance. It’s needless to say that you have to create another volume in the same region.
Attach this volume to an already running instance.
Now login into the instance and execute below command :
- $ sudo fdisk -l
Next step is to create the filesystem. Use below command.
- $ sudo mkfs -t xfs /dev/xvdg
Create any Directory where you want to mount this Volume. i.e. : /mnt/New_Small_Volume
Using blkid and /etc/fstab complete your mount process.
- $ sudo blkid
Copy the UUID of xvdg and edit your /etc/fstab.
Next is to prepare the volume for mount : $ sudo mount -a
And using lsblk command we can verify our smaller volume.
Next step is to copy the date from larger volume to smaller volume.
- $ sudo rsync -HAXxSPa /source/ /Target
- i.e : sudo rsync -HAXxSPa /mnt/Big_Volume/ /mnt/New_Small_Volume
you can see data is available in smaller volume.
Now we need to delete the /etc/fstab entry for larger volumer and then we can detach is from console and delete it.
Make sure you delete the entry from the fstab otherwise instance will not be able to start again once it’s stopped.
We cannot decrease the size of EBS volume directly but we can create a new smaller volume and move the data.
Hope this article will help! Please write your suggestions/feedback in comment section.