- Create new Ubuntu Micro instance (e.g., Name = Tools)
- Stop instance you want to lower EBS volume.
- Create AMI image as backup of your instance.
- Create an empty X GB Amazon EBS volume in the same availability zone where X is smaller size you desire.
- Detach the volume you wish to resize from the stopped instance from step 2 above.
- Attach the original volume to the Tools instance (e.g., as /dev/xvdf)
- Attach new downsized volume to the Tools instance (e.g., as /dev/xvdg)
- SSH into Tools instance and run these steps:
- Verify newly attached partitions via
cat /proc/partitions
- To ensure that the existing original file system is in order, run
e2fsck -f /dev/xvdf
. - If the e2fsck command ran without errors, now run
resize2fs -M -p /dev/xvdf
. - The last line from the resize2fs command should tell you how many 4k blocks the filesystem now is. To calculate the number of 16MB blocks you need, use the following formula: blockcount * 4 / (16 * 1024). Round this number up to nearest integer and use this rounded number to give yourself a little buffer.
- If you dont yet have a partition on your new volume (/dev/xvdg1), use fdisk as follows:
1.
fdisk /dev/xvdg
1. p (print to view partitions) 1. n (new partition)- p (primary partition)
- Press enter to accept default first sector
- Press enter to accept default last sector
- w (write out changes)
- Execute the following command, using the number you came up with in the previous step.
dd bs=16M if=/dev/xvdf of=/dev/xvdg count=numberfrompreviousstep
. Depending on how large your volume is this may take several minutes to run -- let it finish. - After the copy finishes, resize and check and make sure that everything is in order with the new filesystem by running:
1.
resize2fs -p /dev/xvdg
1.e2fsck -f /dev/xvdg
- After this step is complete, time to wrap this up:
- Detach downsized volume from the Tools instance you created.
- Attach the shrunken volume to the old EC2 instance as /dev/sda1 (your boot device)
- Restart your old instance.
- Ensure Instance boots up correctly and is working well.
- If all is well, cleanup: 1. Save the previous, larger volume until you've validated that everything is working properly. 1. When you've verified things are working well, feel free to delete the new EC2 Tools instance you created, plus the larger volume and any backup AMI's and/or snapshots you created.
- Stop instance you want to lower EBS volume.
- Create an empty X GB Amazon EBS volume in the same availability zone where X is smaller size you desire.
- Attach new volume to the instance and again note all device name details.
- Start instance.
- SSH into instance.
- Run these commands:
cat /proc/partitions
cat /etc/fstab
mkfs -t ext4 /dev/xvdg (assuming xvdg is the new device you just attached)
- Create mount directory and mount new volume
mkdir /mnt/small
mount /dev/xvdg /mnt/small
- Sync the files to smaller volume (assumes /mnt/original/ is where your original volume you want to resize - substitute as appropriate)
rsync -aHAXxSP /mnt/original/ /mnt/small > rsync-aHAXxSP.out 2>&1 &
diff -qr /mnt/original/ /mnt/small
- Unmount smaller volume
umount /dev/xvdg
- Stop the instance
- Detach both old and newly resized volumes
- Attach just the newly resized volume now and make sure to use same /dev/… as detailed in step 3 of original volume you are replacing (this ensures that nothing bad will happen in case of auto-mounting scripts - e.g., referenced by rc0.d startup scripts referencing /etc/fstab).
- Start instance
- SSH into instance
- Verify new volume is auto-mounted and working properly as expected.
- review ownership and permissions
- Create new fresh instance from AMI you want to modify.
- Follow Steps to Lower EBS Volume Size on an Instance (see above)
- [Optional] Rename large sized AMI to prior AMI name -old or -large
- Create new AMI from running instance to snapshot with lowered volume size
- [Optional] Delete old/larger AMI
- TODO
- Launch new instance from AMI
- Increase storage size from default AMI
- [Optional] Copy small sized AMI to prior AMI name -old or -small
- [Optional] Delete prior AMI to make room for replacement
- Wait for instance to be running
- Resize the file system so that it fills up the entire newly increased EBS volume
df -h
resize2fs /dev/xvdf # (where xvdf is the device representing increased EBS volume)
df -h
- Create new AMI from running instance to snapshot with increased volume size
- https://matt.berther.io/2015/02/03/how-to-resize-aws-ec2-ebs-volumes/
- http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html#migrate-data-larger-volume
- http://cloudacademy.com/blog/amazon-ebs-shink-volume/
- http://www.n2ws.com/how-to-guides/how-to-reduce-the-size-of-an-ebs-volume.html
- https://alestic.com/2009/12/ec2-ebs-boot-resize/
- http://superuser.com/questions/594203/how-to-copy-entire-linux-root-filesystem-to-new-hard-drive-on-with-ssh-and-tar
- https://wiki.archlinux.org/index.php/full_system_backup_with_rsync