Decreasing Root Disk Size of an "EBS Boot" AMI on EC2

So I have followed Eric's wonderful article here: http://alestic.com/2009/12/ec2-ebs-boot-resize

This was the code basically that helped me increase the default size of the AMI:

 ec2-run-sintances ami-ID -n 1 --key keypair.pem --block-device-mapping "/dev/sda1=:250"

Running Ubuntu 11.10 I didn't even have to re-size the disk afterwards, it was immediately a 250GB drive.

How do I go about decreasing the default size of the AMI???

I tried:

 ec2-run-sintances ami-ID -n 1 --key keypair.pem --block-device-mapping "/dev/sda1=:100"

Obviously... but I was told:

Client.InvalidBlockDeviceMapping: Volume of size 100GB is smaller than snapshot ####### <250>


The best way to go about reducing the size is to create an empty new (small) volume and copy the file system over from an old (big) volume.

EBS boot AMIs are based on snapshots. If you own the AMI, then you can create a volume from the snapshot to copy it. Attach both volumes to a running instance for the copy.

My favorite rsync options on Ubuntu are:

rsync -PaSHAX /bigvol/ /smallvol/

These options preserve things like ownership, permissions, links, and much more.

Detach the volumes and create a snapshot of the new smaller one.

Register the new (small) snapshot as an AMI, making sure to specify the same architecture and AKI (kernel) as the original AMI.


The approach I take to decreasing an EBS root volume is as follows:

Stop (not terminate) the target instance, and detach the root EBS volume. Alternatively, you can snapshot the root volume (or use an existing snapshot) and create a new EBS volume from that. (e.g. /dev/xvda1)

Note: the volume you use from the step above will be altered - so you may want to take a snapshot if you didn't already.

  1. Create a new EBS volume that is the desired size (e.g. /dev/xvdg)
  2. Launch an instance, and attach both EBS volumes to it

  3. Check the file system (of the original root volume): (e.g.) e2fsck -f /dev/xvda1

  4. Maximally shrink the original root volume: (e.g. ext2/3/4) resize2fs -M -p /dev/xvda1

  5. Copy the data over with dd:

    1. Choose a chunk size (I like 16MB)
    2. Calculate the number of chunks (using the number of blocks from the resize2fs output): blocks*4/(chunk_size_in_mb*1024) - round up a bit for safety

    3. Copy the data: (e.g.) dd if=/dev/xvda1 ibs=16M of=/dev/xvdg obs=16M count=80

  6. Resize the filesystem on the new (smaller) EBS volume: (e.g.) resize2fs -p /dev/xvdg

  7. Check the file system (of the original root volume): (e.g.) e2fsck -f /dev/xvdg

  8. Detach your new EBS root volume, and attach it to your original instance