Yogaya Logo

Shrink AWS EBS volumes using EC2 ubuntu

Image of Abhishek Sharma

Abhishek Sharma

November 13, 2022


  1. Launch an instance
  2. Create a new volume
  3. Attach the new 3GB volume
  4. Format the new volume
  5. Mount the new volume
  6. Copy data from the old volume to the new one
  7. Using Elastio, back up the data to s3 bucket
  8. Mount the volume as a device on your instance
  9. Clean up your resources in the AWS Account

1. Launching an instance

alt_text

2. Creating new volume

alt_text

3. Attaching new 8GB vol to instance making it the two volumes

a. The original volume is /dev/sda1 b. The new-vol of 3G to the will be attached as /dev/sdg

alt_text

4. Format the new volume

a. Login to the EC2 instance via SSH b. List all available volumes: lsblk

alt_text

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.

alt_text

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.

alt_text

alt_text

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

alt_text

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

alt_text

7. Using Elastio, back up the data to s3 bucket (very fast process

alt_text

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.