Posted on

Shrinking An Amazon EBS Volume

Now copy your data recursively from your source volume to your newly created volume using the following command(Assuming your old volume is mounted at /mnt/ebs)

sudo cp -r –preserve=all /mnt/ebs/. Amazon’s Elastic Block Store Volumes are easy to use and expand but notoriously hard to shrink once their size has grown. Here are the steps for Shrinking any mounted EBS volume on EC2 Instances.

For various reasons you may need to expand or shrink the size of your EBS volume. /mnt/ebs1 –verbose
Copying data recursively preserves the file attributes such as mode,ownership,timestamps and security contexts, if possible additional attributes such as links.

5. /mnt/ebs1 –verbose
sudo cp -r –preserve=all /mnt/ebs/. After copying the data detach your old volume and mount your new volume at the same mount point using the following command

sudo umount -l /mnt/ebs
sudo umount -l /mnt/ebs

sudo mount /dev/sdi /mnt/ebs
sudo mount /dev/sdi /mnt/ebs
Automating the process by Shell Script
In order to automate all this process here is the Script which will automate the process

Download the zip from this page https://github.com/cloudthat/ebs-shrink

Extract the zip and navigate to the extracted folder

Step 1: Place the script on the instance with permissions to execute

scp -i /ebsreducescript @:/home/ec2-user/.
scp -i /ebsreducescript @:/home/ec2-user/.
Now SSH into the instance using the following command and Update file permissions

ssh -i ec2-user@ip-address
ssh -i ec2-user@ip-address

sudo chmod 500 ebsreducescript
sudo chmod 500 ebsreducescript
Step 2: Configure AWS Cli

Also before executing the script you need to configure awscli use the following commands to install

sudo yum install python-pip
sudo yum install python-pip

sudo pip install awscli
sudo pip install awscli
Run aws configure at the command line to set up your credentials and settings.

$ aws configure
AWS Access Key ID [None]: ANKITIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: ANKITXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-1
Default output format [None]: json

$ aws configure
AWS Access Key ID [None]: ANKITIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: ANKITXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-1
Default output format [None]: json
This Script Will Create a New Volume of User Defined,Mount it to /mnt/ebs1 Copy all the data from the attached EBS Volume recursively that need to be Migrated preserving all the user permissions,then Unmount it and Mount the Newly Created EBS Volume at the Same Mount Point

Note:The migrated volume is not deleted it is just unmounted So if any error occurs we can reattach it. Because you are only charged for the space currently allocated to your EBS volumes, it is also cost efficient to allocate only your approximate short term need and expand the volume as the need arises (this should be considered during the planning phase of your projects and/or during formulation of growth strategies for your service).

The below steps have been tested on an Amazon Linux instance to resize an EBS Volume using the AWS console

Create a new EBS Voume of your desired size from the console and attach it to your instance
Now login to your instance using ssh and format your new attached volume using the following command (Assuming that new volume is attached at /dev/sdi)

ssh -i ec2-user@ip-address
ssh -i ec2-user@ip-address

sudo mkfs -t ext4 /dev/sdi
sudo mkfs -t ext4 /dev/sdi
3.Then make a directory at /mnt/ebs1 and mount the new volume using following commands

sudo mkdir /mnt/ebs1
sudo mkdir /mnt/ebs1

sudo mount /dev/sdi /mnt/ebs1
sudo mount /dev/sdi /mnt/ebs1
4. This Script can only use for Attached(Mounted) EBS Volumes not Root Volume.

Leave a Reply