You might have launched an EC2 instance with EBS volume but with large storage size than what is actually required.
Amazon’s Elastic Block storage (EBS) is very easy to upgrade the storage size when required.
But When it comes to reduce the size of the root EBS volumes , We need to follow the below steps so that we can save some money and also preserve the data.
In the blog post , I have explained how to reduce AWS EBS root volume size.
- Always take a backup of existing volume before performing any changes.
- As the root EBS volume cannot be reduced directly , We need to create new volume and migrate data over there.
- Remove any unused volumes to avoid paying extra.
- In this tutorial , I will show you how to reduce the root EBS volume of the Ubuntu EC2 instance.
- The current root volume size is 50GB , We will reduce it to 25GB.
Reducing Root EBS Volume size
First We need to stop the EC2 instance.For that ,
Login to EC2 Management console.In the navigation pane , Choose Instances.
Under Actions , Hover to Instance State and Choose Stop and then click Yes, Stop
Then we need to make a note of the root volume’s block device and the availability zone.
For that , Choose the Instance , Under Description , Click the Root Device ,
and then choose the EBS ID , It will take us to the Volumes page.
Here you can find the details of volume such as Attachment information , Availability zone, Volume Size , Volume Type etc.
Creating an Empty EBS Volume
Now We need to create a new empty EBS volume in the same availability zone.
For this tutorial , As we have planned to reduce the root volume from 50GB to 25 GB , Hence the new EBS volume size should be 25GB.
In the navigation pane , Choose Volumes , Click Create Volume
Provide the volume size and choose the availability zone and then click Create Volume
As a result , Now , We will be having 2 EBS volumes.
- Root EBS Volume of size 50GB (Already attached with instance).
- New empty EBS volume of size 25GB
Attaching volumes to the Instance
We have to attach the new volume to the instance.
To attach a volume , Select the volume , Under Actions , Click Attach Volume
Choose the instance and then assign a Block Device and then click Attach
Make a note of Device (Attach information) of all the EBS Volumes.
Starting EC2 Instance & Mounting Volumes
We have performed all the above actions with the instance in the stopped state.
Now start the EC2 instance and SSH into it.
After logging in to the instance , Run the below command to check the volumes attached with the instance.
From the above screenshot , You can see that the root volume xvda1 is mounted on /.
But the volumes xvdf (new volume) is not mounted.Because before mounting the volumes we need to create filesystem for them.
We will use ext4 file system for the volumes.
sudo mkfs.ext4 /dev/xvdf
To mount the volumes we need to create folders.
sudo mkdir /mnt/newvol
sudo mount /dev/xvdf /mnt/newvol
Now all the volumes are mounted.
Now We need to copy all the data from the root volume to the new volume
sudo rsync -avx / /mnt/newvol/
It takes time depending on the size of the volume.
We need to install grub on the new volume.
grub-install --root-directory=/mnt/newvol/ --force /dev/xvdf
Next step is to use the UUID of the root volume to the new volume.
Before that , Unmount the new volume.
sudo umount /mnt/newvol
Then check the filesystem for any errors.
sudo e2fsck -f /dev/xvdf
To get the UUID of the root volume , Run the below command.
Make a note of UUID of root volume and using tune2fs command we will set this UUID to the new volume.
tune2fs -U 2ce321d3-087f-4a58-8f97-ed6135e77fee /dev/xvdf
After changing the UUID and the labelling the new volume , We need to check the filesystem.
sudo e2fsck -f /dev/xvdf
We need to check the file system label of the root volume as we need to use the same for the new volume.
You will get the output as cloudimg-rootfs.
Replace the new volume label with the root volume label using the below command.
e2label /dev/xvdf cloudimg-rootfs
Log out of Instance and Detach Volumes
Now sign out of EC2 instance and then Login to EC2 management console and then stop the instance.
We need to detach all the volumes from the EC2 instance.
To detach the volumes from the instance , Go to Volumes page
Select the Volume and then click Detach Volume
Click Yes, Detach.
Now we are at the final phase.
We need to attach new volume (25GB) at /dev/sda1.
Select the Volume , Under Actions , Click Attach Volume
Choose the instance and enter the device as /dev/sda1.
and click Attach
Start the Instance & Verify
We have successfully completed all the required steps to reduce the size of the root EBS volume from 50GB to 25GB.
Start the EC2 Instance and SSH into it.
If all the above things are executed properly , Then you can see that the size of the root EBS volume is reduced from 50GB to 25GB.
Finally , Delete the snapshot and the unused volumes.
Thanks for reading. Hope you find this article helpful.
Please do check out my other blogs.