DEV Community

Mohammed Mithlaj
Mohammed Mithlaj

Posted on • Updated on

Reduce size of the EBS volume of your Ec2

Resizing an EBS volume to a larger size is a straightforward process in the AWS console. However, reducing the size of an EBS volume is not supported directly. Unlike the resizing to larger size functionality, there is no built-in feature that allows you to decrease the size of an EBS volume through the AWS console.

Resizing and Reducing EBS Volumes - Step-by-Step Guide

Resizing and reducing EBS volumes is not a simple task, but by following these steps, you can successfully accomplish it while ensuring data integrity and system stability. This guide will walk you through the process.

Tested Environment : Ubuntu 18LTS

Note: Before proceeding, please exercise caution and ensure you have backups of your data to avoid any potential data loss.

  1. Stop all traffic and connections, except for one SSH connection for your use.

  2. Take a snapshot of the volume (backup):

    • Go to the AWS Management Console.
    • Access the EC2 service and navigate to the EBS section.
    • Locate the volume you want to resize and create a snapshot.
  3. Create a new EBS volume with a smaller size:

    • Choose the same volume type as your old volume (e.g., gp2, gp3).
    • Enter the desired size (e.g., 70GB).
    • Select the same availability zone as the old volume.
    • Add a tag for the new volume, e.g., NAME: new-vol.
  4. Attach and format the new volume:

    • Go to the EBS section and locate the newly created volume.
    • Click "Attach" and select the EC2 instance.
    • Specify the Device Name as /dev/sdf.
    • Click "Attach" to attach the new volume to the EC2 instance.
    • Use the command lsblk to list the current disks and verify that the new disk is listed . eg : /dev/nvme1n1 , /dev/xvdf etc will be present reduced volume size.
    • From now we are assuming new volume name as nvme1n1 . Check if the new volume has any data using the command sudo file -s /dev/nvme1n1.`.
      • If it displays "/dev/nvme1n1: data," the volume is empty and can be formatted.
      • If it shows any other output (have some data), do not format the volume.
  5. Format the new volume:

    • Format the new volume with the following command: sudo mkfs -t ext4 /dev/nvme1n1
  6. Mount the new volume:

    • Create a directory to mount the new volume: sudo mkdir /mnt/new-vol
    • Mount the new volume to the directory: sudo mount /dev/nvme1n1 /mnt/new-vol
    • Verify that the new volume is mounted using the df -h command.

Image description

7.Copy data from the old volume to the new volume:

  • Use the rsync command to copy data from the old volume to the new volume: sudo rsync -axv / /mnt/new-vol/.
  • Please note that the time taken for the copy process will depend on the size of your data.

8.Make the new volume mountable:

  • Install GRUB on the new volume using the following command: sudo grub-install --root-directory=/mnt/new-vol/ --force /dev/nvme1n1
  • Unmount the new volume: sudo umount /mnt/new-vol

9.Update UUID and labels:

  • Check the UUID of the old volume using the blkid command.

Image description

  • Copy the UUID value for backup purposes.
  • Replace the UUID of the new volume with the copied UUID using the tune2fs command: sudo tune2fs -U COPIED_UUID /dev/nvme1n1
  • If you encounter an error like this "This operation requires a freshly checked filesystem."

run sudo e2fsck -f /dev/nvme1n1 to check the filesystem.

  • Check the system label of the old volume using the command: sudo e2label /dev/nvme0n1p1
    • Replace the label of the new volume with the old volume's label: sudo e2label /dev/nvme1n1 cloudimg-rootfs

10.Detach the new volume and complete the process:
- Stop the EC2 instance.
- Detach the old volume and the new volume from the EBS section in the AWS console.
- Attach the new volume to /dev/sda1.
- Start the EC2 instance.
- SSH into the EC2 instance and verify that everything is functioning correctly.

Once your system is working as expected, you may consider deleting the old volume and its snapshot after a week, if no issues arise. However, it's recommended to keep them as backups for added security.

By following these steps carefully, you can resize and reduce your EBS volumes while minimizing risks and ensuring the integrity of your data and system.

Top comments (1)

Collapse
 
devadikhagesh profile image
devadikhagesh

this is not working with already checked filesystem at the ninth step. After I detached both the EBS volumes and attached with new one. it says /bin/bash: permission denied while trying to ssh the ec2 server.