On AWS: Shrink An EBS Data Volume

cloud-hard-drive-thumbnail-120x120Shrinking an AWS EBS volume is straightforward, if it is an ordinary data volume. However, shrinking an EBS volume cannot be done directly by making the volume itself smaller.  You must instead create a new (empty) volume which is the desired smaller size, and then migrate the larger volume’s contents to the smaller volume.

Note that this procedure assumes working with a Linux-type file system volume, e.g., ext4.  The procedure below is not useful for bootable-sysroot volumes. If you use the procedure below in that case, you may produce a volume which contains the same files as the larger volume, but it will not boot.

Fair Warning: this is not a beginner’s guide. You should already be an experienced user of AWS EC2 and EBS services.  You should be proficient at using either the AWS Console EC2 Dashboard, or the AWS CLI, to create and wrangle EC2 instances and EBS volumes. You must be able to login to an EC2 instance over ssh.  You should also have a basic understanding of the GNU/Linux system, and how to do command line entry at a BASH prompt.

To perform the procedure below, we’ll use an EC2 instance created specifically as a disposable worker host. The BASH CLI commands below assume using an Ubuntu AMI as the EC2 instance. The Free Tier t2.micro instance type is a fine choice for this EC2 instance.

In the following example, let’s assume we wish to create a 4GB volume from an 8GB volume.

Let’s begin:

1. Launch an EC2 instance, then set up for later steps.

Use the EC2 Dashboard, or the AWS CLI, to create the worker host.

After it has been created, login to the EC2 worker host instance over an ssh terminal.

Enter “sudo su” to become the root user.

2. Attach the larger volume to the running EC2 worker host instance.  As an example, let’s assume the larger volume we are starting with is an 8GB volume.

Using the EC2 Dashboard Volumes Screen, or the AWS CLI, attach the 8GB (larger) volume as /dev/sdf.  (If necessary, detach it from an EC2 instance to which it may already be attached.)

Note that some Linux kernels may rename /dev/sdf to /dev/xvdf during startup. This is the case shown in Step 3, next.

3. Mount the 8GB (larger) volume in preparation for a later copy operation.

mkdir /media/xvdf
mount /dev/xvdf /media/xvdf

4. Create a new EBS volume the desired size.

This is the smaller volume you wish to be the new size.  As an example, let’s use 4GB as our volume’s desired size.

Use the EC2 Dashboard Volumes Screen, or the AWS CLI, to create the new 4GB (smaller) volume.

5. Attach the new 4GB (smaller) volume to the running EC2 instance.

Using the EC2 Dashboard Volumes Screen, or the AWS CLI, attach the 4GB (smaller) volume as /dev/sdg.

Note that some Linux kernels may rename /dev/sdg to /dev/xvdg during startup. This is the case shown in Step 6, next.

6. Create a file system on the new 4GB (smaller) volume.

mkfs -t ext4 /dev/xvdg

ext4 is an example here, but you may use a different file system type, compatible with your larger volume.

7. Mount the new 4GB (smaller) volume.

mkdir /media/xvdg
mount /dev/xvdg /media/xvdg

8. Copy the data files from the 8GB, (larger) volume to the 4GB, (smaller) one.

cp -pr /media/xvdf/* /media/xvdg/

9. Do a file by file comparison of all files on each volume.

diff -qr --no-dereference /media/xvdf /media/xvdg

Note that diff may warn about “special” files, (e.g., “is a socket”, “is a fifo”, “is a block special file”, or “is a  character special file”).  This is normal, but other types of warnings, and certainly anything declared as an error, should be investigated.  Also, the message “Only in /media/xvdg: lost+found” may be shown.  The directory “lost+found” is created during creation of the new file-system on the smaller volume.  If you have no use for it, you may delete the “lost+found” directory.

10. Stop the EC2 worker host instance.

11. Detach both 8GB, (larger), and 4GB, (smaller), volumes, using the EC2 Dashboard Volumes Screen or the AWS CLI.

The volumes are now available for attachment and use by other EC2 instances.

Remember to dispose of the worker host EC2 instance.  You may wish to delete your original larger volume later, after you’ve ensured your new smaller volume is trustworthy.

“If you can’t be lucky, be good.”


Leave a Reply