Created
January 23, 2017 17:26
-
-
Save grenade/51228c5e57a99a43efcf51d8dfaf53fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ec2_profile="taskcluster" | |
ec2_region="us-east-1" | |
target_instance_id="i-00000000000000000" | |
shrink_instance_id="i-99999999999999999" | |
# determine the device name of the root volume (usually "/dev/sda1") | |
target_instance_device=$(aws ec2 describe-instance-attribute --profile ${ec2_profile} --region ${ec2_region} --instance-id ${target_instance_id} --attribute rootDeviceName | jq -r '.RootDeviceName.Value') | |
# determine the ec2 volume id of the root volume | |
large_volume_id=$(aws ec2 describe-instance-attribute --profile ${ec2_profile} --region ${ec2_region} --instance-id ${target_instance_id} --attribute blockDeviceMapping | jq -r '.BlockDeviceMappings[] | select(.DeviceName == "${target_instance_device}") | .Ebs.VolumeId') | |
# detach the large volume from the target instance | |
aws ec2 detach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${large_volume_id} | |
# determine the availability zone of the target host | |
target_instance_availability_zone=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${target_instance_id} | jq -r '.Reservations[0].Instances[0].Placement.AvailabilityZone') | |
# create a new small volume in the availability zone of the target host | |
small_volume_id=$(aws ec2 create-volume --profile ${ec2_profile} --region ${ec2_region} --availability-zone ${target_instance_availability_zone} --size 30 --volume-type gp2 | jq -r '.VolumeId') | |
# attach the large volume to the shrink host as /dev/sdf | |
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${large_volume_id} --instance-id ${shrink_instance_id} --device /dev/sdf | |
# attach the small volume to the shrink host as /dev/sdg | |
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id} --instance-id ${shrink_instance_id} --device /dev/sdg | |
# determine the public dns name of the shrink host | |
shrink_instance_dns_name=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${shrink_instance_id} | jq -r '.Reservations[0].Instances[0].PublicDnsName') | |
# determine the ssh key name of the shrink host | |
shrink_instance_key_name=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${shrink_instance_id} | jq -r '.Reservations[0].Instances[0].KeyName') | |
# ssh to the shrink host and copy the large volume to the small one | |
ssh -i ~/.ssh/${shrink_instance_key_name} ec2-user@${shrink_instance_dns_name} sudo dd if=/dev/sdf of=/dev/sdg bs=1M | |
# detach the small volume from the shrink host | |
aws ec2 detach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id} | |
# attach the small volume to the target host | |
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id} --instance-id ${target_instance_id} --device ${target_instance_device} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on http://nybbl.blogspot.co.uk/2013/08/shrinking-windows-boot-drive-in-aws.html