|TABLE OF CONTENT|
An Amazon EBS volume is a block-level storage device that can be attached to your instances. You can use it as you would use a physical hard drive. Moreover, EBS volumes are flexible and you can dynamically increase size, modify the provisioned input/output operations per second (IOPS) capacity and change volume type on live production volumes.
This blog illustrates how to increase and decrease the size of EBS volume with a step-by-step instructions.
Increase the Size of EBS Volume
If you are familiar with Amazon EC2 and EBS volume, you might have experience modifying the size of EBS volume. AWS provides an easy way to increase the size of your EBS volume. In fact, we could increase it without detaching the volume or restarting the instance. That’s quite nice work done there because we would not need to have downtime for our instance.
Step 1: Go to the EC2 service in the AWS console and click on the server-specific volume by selecting the EC2 server > Storage > volume ID.
Step 2: Select the Volume > Actions > Modify volume
Step 3: Modify the volume size by changing the Size value > click Modify.
Extending OS file system
After you finish modifying the volume, you need to extend the OS file system to see your increased volume size. The example below is the command I used for Ubuntu OS.
Step 4: SSH into the server and check whether the storage disk space is allocated by using “lsblk” command. Your increased volume will be shown just above your current volume, e.g., xvda1 is your current volume with 10GB size and xvda with 15GB size.
Step 5: Extend the partition by typing: sudo growpart /dev/xvda 1.
Note: Here dev/xvda denotes the partition name and 1 denotes the partition number.
Further, extend the volume by typing: sudo resize2fs /dev/xvda1.
Now let us check the EBS size allocated to our server by using the “lsblk” command.
Hence, we have increased the EBS volume from 10GB to 15GB.
Decrease the Size of EBS Volume
In the previous task we have learned about increasing our EBS volume size. You might be wondering, can we decrease EBS volume size? The answer is NO. It is impossible to decrease EBS volume size. When you have 100GB EBS and you decide to modify it into 30GB you will get error: The size of a volume can only be increased, not decreased. That’s terrible …
But don’t worry. We could also do the trick to decrease EBS volume size. However, it is not easy task as it is not straightforward.
Here is the trick:
- Snapshot the volume
- Create a new smaller EBS volume
- Attach the new volume
- Format the new volume
- Mount the new volume
- Copy data from old volume to the new volume
- Prepare the new volume
- Detach and unmount old volume
Assume we have:
- Instance named “my-instance” in “ap-south-1a” zone
- 30GB EBS volume size named “old-volume”
- We wish to decrease into 10GB and name it “new-volume.”
We would need to stop the instance to prevent inconsistencies.
Step 1: Stop the EC2 Instance and create a new EBS volume with desired storage (In our case it is 10GB) in the same availability zone. Name it as “new-volume.”
Step 2: Attach the new EBS volume to the Instance, start the Instance and SSH into the instance.
Step 3: mount the new volume.
- Create filesystem for new EBS volume by using the command: sudo mkfs -t ext4 /dev/xvdf
- Create a directory to mount using the command: sudo mkdir /mnt/new-volume
- Mount new volume into the directory using the command: sudo mount /dev/xvdf /mnt/new-volume
- Check volume with the command: “df -h”
- The new volume should be successfully mounted now.
Step 4: Copy data from the old volume to the new volume.
Use rysnc to copy from the old volume to the new volume “sudo rsync -axv / /mnt/new-volume/”
Relax and wait until it is finished. Get a coffee!
Step 5: Prepare the new volume.
- Install grub on new-volume using the command: sudo grub-install –root-directory=/mnt/new-volume/ –force /dev/xvdf
- Unmount new-volume with the following command sudo umount /mnt/new-volume
- Check UUID using command: blkid
- Copy UUID from /dev/xvda1 (paste anywhere to backup this UUID); This is your old UUID.
- Use tune2fs to replace UUID “sudo tune2fs -U COPIED_UUID /dev/xvdf” COPIED_UUID is the string value from point 4.
- Check the system label from old-volume using command “sudo e2label /dev/xvda1” It will display string like cloudimg-rootfs
- Replace new-volume label with old-volume label using command “sudo e2label /dev/xvdf cloudimg-rootfs”
- We can logout SSH now.
Step 6: Detach old volume
- Stop instance my-instance
- Detach old-volume
- Detach new-volume
- Attach new-volume to /dev/sda1
- Start instance my-instance
After we finish all steps above, we could check our instance by login into SSH.
We could delete old-volume and snapshot-old-volume if our system works correctly. I prefer to keep snapshot for a week / month before deleting it.
AWS provide a straightforward method to increase the volume size of EBS. We need to extend the OS file system to see the changes. We cannot straightforward decrease the size of EBS volume. All we can do is to create a new smaller volume and transfer all the data from old volume to new volume and make new volume as root volume.
Hope it’s help!
CloudThat is a pioneer in the cloud-computing sphere in India with expertise in training and consulting services since 2012. The hallmark of our quality is evident with the strategic partnerships we have with major public cloud providers like AWS, Azure, GCP (Google Cloud Platform), and VMware.
We are a Microsoft Gold Partner, AWS Advanced Tier Partner, AWS DevOps Services Competency Partner, Authorized AWS Training Partner, Google Cloud Partner, and Authorized VMware Training Reseller. We are accoladed with Microsoft Partner of the Year Finalist-2022, Microsoft Learning Partner of the Year Finalist-2020, Winner of Microsoft Learning Partner of the Year Award-2017 a rare accomplishment for any organization.
Renowned as the first Cloud training & consulting organization from India, in our 10+ years of illustrative journey, we have delivered 200+ projects by serving clients from across the globe spanning 28+ countries and trained 5 Lakh professionals.
- What is EBS volume in AWS? An Amazon EBS volume is a durable, block-level storage device that you can attach to your instances
- Which EBS volume is best? AWS Elastic Block Store (EBS) is Amazon’s block-level storage solution used with the EC2 cloud service to store persistent data. That means the data is stored on the AWS EBS servers even if the EC2 instances are shut down.