AWS EBS Root volume size reduction
Posted By : Amit Solanki | 14-Jan-2021
Amazon’s Elastic Block Storage (EBS) is very easy to increment in size. But When it comes to reducing the size of the root EBS volumes, it can only be increased and not decreased as it contains Boot files and information.
In the article, we will learn how to reduce AWS EBS root volume size.
- Remember to take the backup of the existing volume before making any changes.
- Since we cannot reduce the root EBS Volume directly, we have to create a new volume and transfer data over there.
1) Reducing Root EBS Volume size:-
The inaugural step is to stop the EC2 instance. To do that, log in to the EC2 Management console.
In the navigation pane. Choose Instances.
Under Actions, Hover to Instance State and choose to Stop and then click Yes, Stop.
Then, we have to note down 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. You will find details related to the volume such as Attachment information, Availability zone, Volume Size, Volume Type, etc.
2) Creating an Empty EBS Volume:-
The next step is creating a new empty EBS volume in the same availability zone. Since we have planned to shrink the root volume from 50GB to 25GB in this tutorial, the new EBS volume size will be 25GB.
Choose Volumes in the navigation pane. Then click on Create Volume. Enter the volume size, pick the availability zone, and click on Create Volume.
3) Attaching volumes to the Instance:-
To perform this activity, Select the volume under the Actions and click Attach Volume.
Pick the instance, assign it a Block Device, and then click Attach.
Note down the Device (Attach information) of all the EBS Volumes.
4) Starting EC2 Instance & Mounting Volumes:-
We have completed every above-mentioned action with the instance in the stopped state. Now, we will start the EC2 instance and SSH into it.
Log into the Instance and run the following command to check the volumes attached with the instance
But the volumes xvdf (new volume) is not mounted. Because before mounting the volumes we need to create a filesystem for them. We will use the 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 sync -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
The next step is to use the UUID of the root volume for 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.
# sudo blkid
Make a note of the UUID of the root volume and using the tune2fs command we will set this UUID to the new volume.
# sudo tune2fs -U <UUID> /dev/xvdf
After changing the UUID and labeling 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.
# sudo e2label /dev/xvda1
You will get the output as clouding-rootfs.
Replace the new volume label with the root volume label using the below command.
# sudo e2lable /dev/xvdf cloudimg-rootfs
Also Read: Integrate AWS Amplify Cognito in Android
5) Log out of the instance and detach volume
Now sign out of the EC2 instance and then log in to the 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 the Volumes page, select the Volume and then click Detach Volume
Click Yes, Detach.
Now we are in 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/xvda1. and click Attach
6) Start the Instance & Verify
This marks the successful completion of all the 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.
Why Choose Oodles Technologies For DevOps Solutions?
We are seasoned DevOps solutions and service providers with vast experience in providing full-scale DevOps solutions for varied business requirements. Our team of DevOps professionals formulates effective strategies to strengthen your enterprise IT infrastructure and enhance operational efficiency. Our 360-degree DevOps solutions and services accelerate the software development lifecycle and ensure faster delivery with continuous deployment. For project related queries, reach us out at email@example.com