Shrink AWS EBS volumes using EC2 ubuntu
Abhishek Sharma
November 13, 2022
- Launch an instance
- Create a new volume
- Attach the new 3GB volume
- Format the new volume
- Mount the new volume
- Copy data from the old volume to the new one
- Using Elastio, back up the data to s3 bucket
- Mount the volume as a device on your instance
- Clean up your resources in the AWS Account
1. Launching an instance
- In the AWS dashboard (EC2 service), create an instance of 8GB
2. Creating new volume
- Create Volume of 3G and name it new-volume
3. Attaching new 8GB vol to instance making it the two volumes
- Attach the new 3GB volume to the instance (from 1. above) to make two volumes (8GB and 3GB)
a. The original volume is /dev/sda1 b. The new-vol of 3G to the will be attached as /dev/sdg
4. Format the new volume
a. Login to the EC2 instance via SSH b. List all available volumes: lsblk
c. Run the $ df -hT command: of the 8GB EBS volume space, only 2.2 GB is used and the rest is free space. Next, the free space will be eliminated.
d. Reminder: the original volume is at /dev/xvda1 and the new volume at**/dev/xvdg.** Create a partition on /dev/xvdg using the $ fdisk command and format the partition.
- The new partition is xvdg1
e. To format, check if the volume has any data or not by the following command: sudo file -s /dev/xvdg1
If the output is /dev/xvdg1: data, it means the volume is empty and it can be formatted.
f. Format the volume with the following command: sudo mkfs.ext4 /dev/xvdg1
5. Mounting the new volume
Mount the new volume to the EC2 instance.
a. Create a directory to mount the new volume: $ sudo mkdir /new-volume
b. Mount the new volume into this directory: $ sudo mount /dev/xvdg1 /new-volome
c. Verify with: $ df -hT
6. Copying data from the old volume to the new one
Copy the data from the original volume to the new volume
sudo rsync -axv / /dev/root /new-volume
7. Using Elastio, back up the data to s3 bucket (very fast process
- Using Elastio, back up the data to s3 bucket (very fast process
8. Mount the volume as a device on your instance
sudo modprobe nbd sudo -E elastio mount rp --id r-dged6mmwk3sq5ssfpwlweqol
Note: r-... is the recovery point
9. The same process can be followed for multiple partitions on a volume
10. Clean up your resources in the AWS Account.
NB: You can use cloud-nuke to cleean up your aws resources.