AWS convert Paravirtual to HVM Virtualization

By | November 18, 2014

There are scenarios where one needs to move away from PV to HWM in which situation its sometimes hard to reconfigure the whole system. Below we have a script that does this automatically.

#!/bin/bash

# ——————————————— #
# Standard Variables
# ——————————————— #

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
tmp=”/tmp”
logfile=”$tmp/$0.$$.log”
accountid=”$(curl http://169.254.169.254/latest/dynamic/instance-identity/document -s -m 5 | \grep -i “accountId” | awk -F “\”” ‘{ print $4 }’)”
region=”$(curl http://169.254.169.254/latest/dynamic/instance-identity/document -s -m 5 | \grep -i “region” | awk -F “\”” ‘{ print $4 }’)”
az=”$(curl http://169.254.169.254/latest/curl http://169.254.169.254/latest/dynamic/instance-identity/document -s -m 5 | \grep -i “availabilityZone” | awk -F “\”” ‘{ print $4 }’)”
AWS_DEFAULT_REGION=”$region”

# ——————————————— #
# END Standard Variables
# ——————————————— #

# ——————————————— #
# Variable Variables
# ——————————————— #

workinginstanceid=”$(curl http://169.254.169.254/latest/meta-data/instance-id -m 5 -s)”
localami=”$(curl http://169.254.169.254/latest/meta-data/ami-id -s -m 5)”
localinstancejson=”$tmp/local-instance”
sysroot=”/mnt”

# ——————————————— #
# END Variable Variables
# ——————————————— #

# ——————————————— #
# Standard Functions
# ——————————————— #

MINIPROGRESS(){
for f in $(seq 2); do
echo -ne “.”;
sleep .5;
done
}

PROGRESS(){
for f in $(seq 10); do
echo -ne “.”;
sleep .5;
done
}

MAXPROGRESS(){
for f in $(seq 10); do
echo -ne “.”;
sleep 3;
done
}

CHECK_ROOT(){
if [[ $EUID -ne 0 ]]; then
echo -e “”;
echo -e “\e[31mERROR:\e[0m This script must be run as root.”;
echo -e ” 1. Re-run this script as root.”;
echo -e ” 2. Ensure aws-cli is configured for the root user.”;
echo -e “”;
exit 1
fi
}

CHECK_OS(){
LOGGING “”;
LOGGING “—————————-“;
LOGGING “Checking working instance OS”;
LOGGING “—————————-“;
LOGGING “”;
if [[ $(file /etc/*_version) = *ERROR* ]] ||\
[[ $(grep -i “Ubuntu” /etc/*_version) != *Ubuntu* ]] &&\
[[ $(file /etc/*-release) != *ERROR* ]]; then
LOGGING “Working instance – RHEL Based”;
os=”rhel”;
elif [[ $(file /etc/*-release) = *ERROR* ]] ||\
[[ $(grep -i “Ubuntu” /etc/*-release) = *Ubuntu* ]] &&\
[[ $(file /etc/*_version) != *ERROR* ]]; then
LOGGING “Working instance – Ubuntu”;
os=”ubuntu”;
else
LOGGING “Unsupported distributions”;
os=”unsupported”;
echo -e “Only Debian & RHEL based Distributions currently supported”;
exit 1
fi
}

CHECK_AWSCLI(){
if [[ -z $os ]]; then
CHECK_OS
fi
LOGGING “”;
LOGGING “———————————-“;
LOGGING “Checking awscli tools installation”;
LOGGING “———————————-“;
LOGGING “”;
if hash aws 2>/dev/null; then
LOGGING “AWSCLI installed”
aws=true
else
LOGGING “AWSCLI not installed – installing”;
aws=false
echo -e “Installing aws-cli”;
if [[ $os = “ubuntu” ]]; then
sudo apt-get update > /dev/null 2>&1 && sudo apt-get install -y python-pip > /dev/null 2>&1 && sudo pip install –upgrade awscli > /dev/null 2>&1
elif [[ $os = “rhel” ]]; then
sudo yum install python-pip -y > /dev/null 2>&1 && sudo pip install –upgrade awscli > /dev/null 2>&1
fi
if hash aws 2>/dev/null; then
aws=true
else
LOGGING “Failed to install AWSCLI”;
echo -e “Cannot install awscli”;
exit
fi
fi
}

CHECK_JQ(){
if [[ -z $os ]]; then
CHECK_OS
fi
LOGGING “”;
LOGGING “————————“;
LOGGING “Checking jq installation”;
LOGGING “————————“;
LOGGING “”;
if hash jq 2>/dev/null; then
LOGGING “JQ installed”
jq=true
else
LOGGING “JQ not installed – installing”;
echo -e “Installing jq”;
if [[ $os = “ubuntu” ]]; then
sudo apt-get update > /dev/null 2>&1 && sudo apt-get install -y jq > /dev/null 2>&1
elif [[ $os = “rhel” ]]; then
if [[ $(grep -i “Amazon” /etc/*-release) = *Amazon* ]]; then
yum install -y jq > /dev/null 2>&1
else
sudo yum install https://s3-eu-west-1.amazonaws.com/scripts-prerequisite/jq-latest.rpm -y > /dev/null 2>&1
fi
fi
if hash jq 2>/dev/null; then
jq=true
else
LOGGING “Failed to install JQ”;
echo -e “Cannot install jq”;
exit
fi
fi
}

AWSCLI_CREDS(){
LOGGING “”;
LOGGING “———————————“;
LOGGING “Checking required AWS credentials”;
LOGGING “———————————“;
LOGGING “”;
if [[ -f $HOME/.aws/config ]]; then
export AWS_CONFIG_FILE=$HOME/.aws/config
LOGGING “Found $HOME/.aws/config”;
elif [[ $(curl http://169.254.169.254/latest/meta-data/iam/info/ -s -m 5) =~ “Success” ]]; then
LOGGING “IAM Role found – checking basic permissions:”;
if [[ $(aws ec2 describe-instances –region $region 2>&1) =~ “UnauthorizedOperation” ]]; then
LOGGING “Failed to do basic aws ec2 describe-instances call using IAM role. Configuring awscli config file”;
echo -e “Configuring aws-cli”;
aws configure
export AWS_CONFIG_FILE=$HOME/.aws/config
fi
else
LOGGING “$HOME/.aws/config not found. IAM role not found. Configuring awscli config file”;
echo -e “Configuring aws-cli”;
aws configure
export AWS_CONFIG_FILE=$HOME/.aws/config
fi
}

CHKPERMISSIONS(){
LOGGING “Checking $1″;
permcheck=”$($2 2>&1)”
if [[ $permcheck =~ “UnauthorizedOperation” ]] || [[ $permcheck =~ “Unable to locate credentials” ]]; then
printf “%-25s%-25s\n” “$1” “$RED[FAIL]$NORMAL”
echo $permfail >> $logfile
PERMFAIL
else
printf “%-25s%-25s\n” “$1” “$GREEN[OK]$NORMAL”
fi
}

PERMFAIL(){
echo -e “”;
echo -e “”;
if [[ -f $HOME/.aws/config ]]; then
echo -e “Ensure access key: \e[32m\e[1m`grep -A 4 “default” $HOME/.aws/config | grep -i “aws_access_key_id” | awk ‘{ print$3 }’`\e[0m has the correct permissions.”;
LOGGING “^^^ FAILED ^^^”;
LOGGING “*FATAL ERROR*”;
LOGGING “”;
LOGGING “Ensure access key: \e[32m\e[1m`grep -A 4 “default” $HOME/.aws/config | grep -i “aws_access_key_id” | awk ‘{ print$3 }’`\e[0m has the correct permissions.”;
elif [[ $(curl http://169.254.169.254/latest/meta-data/iam/info/ -s -m 5) =~ “Success” ]]; then
echo -e “Ensure that role: \e[32m\e[1m$(curl http://169.254.169.254/latest/meta-data/iam/info/ -s -m 5 | grep InstanceProfileArn | awk -F”\”” ‘{ print $4 }’)\e[0m has the correct permissions.”;
LOGGING “^^^ FAILED ^^^”;
LOGGING “*FATAL ERROR*”;
LOGGING “”;
LOGGING “Ensure that role: \e[32m\e[1m$(curl http://169.254.169.254/latest/meta-data/iam/info/ -s -m 5 | grep InstanceProfileArn | awk -F”\”” ‘{ print $4 }’)\e[0m has the correct permissions.”;
else
echo -e “Ensure that the IAM user or role has the correct permissions.”;
LOGGING “Ensure that the IAM user or role has the correct permissions.”;
fi
FAILMESSAGE
exit $?
}

LOGGING(){
echo $1 >> $logfile
}

LOGGINGVAR(){
sed -i “s/$1″/”$2″/g $logfile
}

# ——————————————— #
# END Standard Functions
# ——————————————— #

# ——————————————— #
# Variable Functions
# ——————————————— #

PERMISSIONS(){
LOGGING “”;
LOGGING “———————————“;
LOGGING “Checking required IAM permissions”;
LOGGING “———————————“;
LOGGING “”;

### Getting working instance details to use in permission testing
aws ec2 describe-instances –instance-id $workinginstanceid –output json 2>/dev/null > $localinstancejson
localvol=$(cat $localinstancejson | jq -r ‘.Reservations[].Instances[0].BlockDeviceMappings[].Ebs.VolumeId’)
localami=$(cat $localinstancejson | jq -r ‘.Reservations[].Instances[0].ImageId’)

# IAM permission checks – format: CHKPERMISSIONS “display message” “aws command with –dry-run”
CHKPERMISSIONS “describe-instances” “aws ec2 describe-instances –instance-id $workinginstanceid –dry-run”;
CHKPERMISSIONS “start-instances” “aws ec2 start-instances –instance-id $workinginstanceid –dry-run”;
CHKPERMISSIONS “stop-instances” “aws ec2 stop-instances –instance-id $workinginstanceid –dry-run”;
CHKPERMISSIONS “run-instances” “aws ec2 run-instances –image-id $localami –instance-type m3.medium –dry-run”;
CHKPERMISSIONS “describe-volumes” “aws ec2 describe-volumes –volume-ids $localvol –dry-run”;
CHKPERMISSIONS “attach-volumes” “aws ec2 attach-volume –volume-id $localvol –instance-id $workinginstanceid –device /dev/sdf –dry-run”;
CHKPERMISSIONS “create-volumes” “aws ec2 create-volume –size 1 –availability-zone $az –dry-run”;
CHKPERMISSIONS “detach-volumes” “aws ec2 detach-volume –volume-id $localvol –dry-run”;
CHKPERMISSIONS “delete-volumes” “aws ec2 delete-volume –volume-id $localvol –dry-run”;
CHKPERMISSIONS “describe-snapshots” “aws ec2 describe-snapshots –snapshot-id snap-testing –dry-run”;
CHKPERMISSIONS “create-snapshot” “aws ec2 create-snapshot –volume-id $localvol –description createsnapshot.$$ –dry-run”;
CHKPERMISSIONS “register-image” “aws ec2 register-image –name registername.$$ –description registerimage.$$ –architecture x86_64 –root-device-name /dev/sda1 –virtualization-type hvm –dry-run”;
CHKPERMISSIONS “create-image” “aws ec2 create-image –instance $workinginstanceid –name createimage.$$ –dry-run”;
CHKPERMISSIONS “describe-images” “aws ec2 describe-images –image-id $localami –dry-run”;

LOGGING “”;
LOGGING “————————–“;
LOGGING “Permission check completed”;
LOGGING “————————–“;
LOGGING “”;
}

FAILMESSAGE(){
echo -e “”;
echo -e “\e[1mSample IAM Policy:\e[0m”;
echo -e “{“;
echo -e ” \”Statement\”: [“;
echo -e ” {“;
echo -e ” \”Sid\”: \”Stmt1394176741257\”,”;
echo -e ” \”Action\”: [“;
echo -e ” \”ec2:DescribeInstances\”,”;
echo -e ” \”ec2:RunInstances\”,”;
echo -e ” \”ec2:StartInstances\”,”;
echo -e ” \”ec2:StopInstances\”,”;
echo -e ” \”ec2:DescribeVolumes\”,”;
echo -e ” \”ec2:AttachVolume\”,”;
echo -e ” \”ec2:CreateVolume\”,”;
echo -e ” \”ec2:DetachVolume\”,”;
echo -e ” \”ec2:DeleteVolume\”,”;
echo -e ” \”ec2:DescribeImages\”,”;
echo -e ” \”ec2:RegisterImage\”,”;
echo -e ” \”ec2:DescribeSnapshots\”,”;
echo -e ” \”ec2:CreateSnapshot\”,”;
echo -e ” \”ec2:CreateImage\””;
echo -e ” ],”;
echo -e ” \”Effect\”: \”Allow\”,”;
echo -e ” \”Resource\”: \”*\””;
echo -e ” }”;
echo -e ” ]”;
echo -e “}”;
echo -e “”;
echo -e “See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html”;
echo -e “”;
LOGGING “”;
LOGGING ‘{‘;
LOGGING ‘ “Statement”: [‘;
LOGGING ‘ {‘;
LOGGING ‘ “Sid”: “Stmt1394176741257”,’;
LOGGING ‘ “Action”: [‘;
LOGGING ‘ “ec2:DescribeInstances”,’;
LOGGING ‘ “ec2:RunInstances”,’;
LOGGING ‘ “ec2:StartInstances”,’;
LOGGING ‘ “ec2:StopInstances”,’;
LOGGING ‘ “ec2:DescribeVolumes”,’;
LOGGING ‘ “ec2:AttachVolume”,’;
LOGGING ‘ “ec2:CreateVolume”,’;
LOGGING ‘ “ec2:DetachVolume”,’;
LOGGING ‘ “ec2:DeleteVolume”,’;
LOGGING ‘ “ec2:DescribeImages”,’;
LOGGING ‘ “ec2:RegisterImage”,’;
LOGGING ‘ “ec2:DescribeSnapshots”,’;
LOGGING ‘ “ec2:CreateSnapshot”,’;
LOGGING ‘ “ec2:CreateImage”‘;
LOGGING ‘ ],’;
LOGGING ‘ “Effect”: “Allow”,’;
LOGGING ‘ “Resource”: “*”‘;
LOGGING ‘ }’;
LOGGING ‘ ]’;
LOGGING ‘}’;
LOGGING “See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html”;
}

BANNER(){
echo -e “\e[1m\e[31m#########################################################################\e[0m”;
echo -e “\e[1m\e[31m# #\e[0m”;
echo -e “\e[1m\e[31m#\e[0m \e[1m\e[4mPV to HVM\e[0m \e[1m\e[31m#\e[0m”;
echo -e “\e[1m\e[31m#########################################################################\e[0m”;
}

# ——————————————— #
# END Variable Functions
# ——————————————— #

### ——————————————— ###
### ——————————————— ###
# Main script start
### ——————————————— ###
### ——————————————— ###

# ——————————————— #
# Standard Script
# ——————————————— #

# Start logging
rm -rf $logfile;
touch $logfile;

LOGGING ” $0 script log:”;
LOGGING “——————————- “;
LOGGING ” “;
LOGGING “Resources used:”;
LOGGING “vara”;
LOGGING “varb”;
LOGGING “varc”;
LOGGING “vard”;
LOGGING “vare”;
LOGGING “varf”;
LOGGING “varg”;
LOGGING “——————————–“;
LOGGING ” “;
LOGGING “Work log:”;
LOGGING “——–“;
LOGGING ” “;

# Checking prerequisites
LOGGING “———————-“;
LOGGING “Checking prerequisites”;
LOGGING “———————-“;
LOGGING ” “;
BANNER;
echo -e “”;
echo -e “Checking script prerequisite”;
echo -e “”;
# Checking if script is executed as root
CHECK_ROOT

# Checking if this is a supported OS
CHECK_OS

# Checking is required packages are installed
CHECK_AWSCLI
CHECK_JQ

# Checking awscli credentials (config file or IAM role)
AWSCLI_CREDS

# Checking required IAM permissions
PERMISSIONS

LOGGING “—————————“;
LOGGING “Required prerequisites met”;
LOGGING “—————————“;
LOGGING ” “;

clear;
BANNER;

# ——————————————— #
# END Standard Script
# ——————————————— #

# ——————————————— #
# Custom script
# ——————————————— #
#exit $?

# Determine local instance id – to ensure everything is taking place in the same AZ

if [ -z $workinginstanceid ]; then
check_response=”false”
echo -e “\e[1mCannot determine (this) instance id. Enter instance id:\e[0m”;
while [[ ${check_response} == “false” ]]; do
read -p “> “;
if [[ $REPLY =~ ^(i-[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9])$ ]]; then
check_response=”true”
workinginstanceid=$REPLY
LOGGING “*ERROR* Could not determine this instance id by using ec2 metadata – checking manually.”;
echo -ne “Checking instance”;
PROGRESS
if [[ `ip address show eth0 | grep -i “inet ” | awk -F ” ” ‘{ print$2 }’ | awk -F “/” ‘{ print$1 }’` != `cat $localinstancejson | jq -r ‘.Reservations[].Instances[0].PrivateIpAddress’` ]]; then
echo;
echo “Instance id is incorrect. Determine correct instance id and try again.”;
echo;
LOGGING “*FATAL ERROR* Instance id does NOT match. Exiting…”;
exit $?
fi
echo -ne “[OK]!”;
LOGGING “*WARN* Manually determined working instance id: $workinginstanceid”;
else
echo “Invalid instance id. Try again: “;
fi
done;
echo -e “”;
else
LOGGING “*SUCCESS* Determined instance id: $workinginstanceid”;
LOGGINGVAR “vara” “Working instance id: $workinginstanceid”;
fi

jacquecp
az=`cat $localinstancejson | jq -r ‘.Reservations[].Instances[].Placement.AvailabilityZone’`
LOGGING “*SUCCESS* Determined Availability Zone: $az”;
LOGGINGVAR “varb” “Availability Zone: $az”
LOGGINGVAR “varg” “Region: $region”
# Define / check source instance

srcinstancejson=”$tmp/srcdescribe-instances”
sourcevolumes=”$tmp/srcvol-list”
sourcesnapshots=”$tmp/srcsnap-list”
srcvolumejson=”$tmp/srcdescribe-volumes”
srcimagejson=”$tmp/srcimage”
check_response=”false”

clear;
BANNER
echo -e “”;
sourceinstance=$1
if [[ -n $sourceinstance ]]; then
if [[ $sourceinstance =~ ^(i-[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9])$ ]]; then
if [[ $sourceinstance == $workinginstanceid ]]; then
echo -e “”;
echo -e “ERROR: Script executed on source PV instance.”;
echo -e ” Script cannot be executed from source PV instance.”;
echo -e ” Launch a temoprary Amazon Linux instance to run the script.”;
echo -e ” Exiting script”;
echo -e “”;
exit 1;
fi
else
echo -e “\e[1m$1 is not a valid instance id. Please provide a valid source PV instance id:\e[0m”;
while [[ ${check_response} == “false” ]]; do
read -p “> “;
if [[ $sourceinstance =~ ^(i-[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9])$ ]]; then
if [[ $sourceinstance == $workinginstanceid ]]; then
echo -e “”;
echo -e “ERROR: Script executed on source PV instance.”;
echo -e ” Script cannot be executed from source PV instance.”;
echo -e ” Launch a temoprary Amazon Linux instance to run the script.”;
echo -e ” Exiting script”;
echo -e “”;
exit 1;
fi
check_response=”true”
echo $sourceinstance
else
echo -e “\e[31m***Incorrect format***\e[0m (Format: i-xxxxxxxx)”;
echo;
echo -e “\e[1mSource PV instance:\e[0m”;
fi
done;
fi
else
echo -e “\e[1mSource PV instance:\e[0m”;
while [[ ${check_response} == “false” ]]; do
read -p “> “;
if [[ $REPLY =~ ^(i-[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9])$ ]]; then
if [[ $REPLY == $workinginstanceid ]]; then
echo -e “”;
echo -e “ERROR: Script executed on source PV instance.”;
echo -e ” Script cannot be executed from source PV instance.”;
echo -e ” Launch a temoprary Amazon Linux instance to run the script.”;
echo -e ” Exiting script”;
echo -e “”;
exit 1;
fi
check_response=”true”
sourceinstance=$REPLY
else
echo -e “\e[31m***Incorrect format***\e[0m (Format: i-xxxxxxxx)”;
echo;
echo -e “\e[1mSource PV instance:\e[0m”;
fi
done;
fi
echo
aws ec2 describe-instances –instance-ids $sourceinstance > /dev/null 2>&1
status=$?
if [[ “$status” != 0 ]] ; then
echo -e “”;
echo -e “The instance ID $sourceinstance does not exist in account accountid.”;
echo -e “Confirm instance id and / or IAM credentials and try again.”;
echo -e “”;
exit 1;
fi
(aws ec2 describe-instances –instance-ids $sourceinstance > $srcinstancejson) | tee -a $logfile > /dev/null 2>&1
LOGGING “Source instance – $sourceinstance – json saved to $srcinstancejson”;
cat $srcinstancejson | tee -a $logfile > /dev/null 2>&1 &
LOGGINGVAR “varc” “Source Instance: $sourceinstance”

# Determine source volume(s) to create snapshots
cat $srcinstancejson | jq -r ‘.Reservations[].Instances[].BlockDeviceMappings[] | select(.Ebs) | .DeviceName, “-“, .Ebs.VolumeId, “,”‘ | tr ‘\n’ ‘ ‘ | sed -e ‘s*\ ,*\n*g’ -e ‘s* \/*\/*g’ > $sourcevolumes
LOGGING “Source volumes / devices”
cat $sourcevolumes | tee -a $logfile > /dev/null 2>&1 &

# Create temporary AMI from source instance.
if [[ $(aws ec2 describe-instances –instance-ids $sourceinstance | jq -r ‘.Reservations[].Instances[].State.Name’) != stopped ]]; then
echo -e “Source instance is running.”
check_response=”false”
echo -e “”;
echo -e “Reboot instance to create temporary AMI? (y/n)”;
while [[ $check_response == “false” ]]; do
read -p “> “;
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
tempami=$(aws ec2 create-image –instance-id $sourceinstance –name “Temp-Image-PV-HVM-Conversion.$$” –description “Temporary AMI created from source instance $sourceinstance” –reboot | jq -r ‘.ImageId’)
check_response=”true”
elif [[ $REPLY =~ ^([nN][oO]|[nN])$ ]]; then
echo -e “”;
echo -e “Filesystem integrity cannot be guaranteed. Creating temporary AMI without reboot…”;
tempami=$(aws ec2 create-image –instance-id $sourceinstance –name “Temp-Image-PV-HVM-Conversion.$$” –description “Temporary AMI created from source instance $sourceinstance” –no-reboot | jq -r ‘.ImageId’)
check_response=”true”
else
echo -e “Yes or No”;
fi
done
else
echo -e “”;
echo -e “Creating temporary AMI from instance: \e[31m$sourceinstance\e[0m”;
tempami=$(aws ec2 create-image –instance-id $sourceinstance –name “Temp-Image-PV-HVM-Conversion.$$” –description “Temporary AMI created from source instance $sourceinstance” –no-reboot | jq -r ‘.ImageId’)
fi
echo -e “”;
echo -e “This process may take a very long time if the instance has large volumes attached.”;
echo -e “Please be patient and allow this process to finish”;
echo -e “”;
echo -ne “Creating AMI”;
MINIPROGRESS
while [[ $(aws ec2 describe-images –image-ids $tempami | jq -r ‘.Images[].State’) != “available” ]]; do
PROGRESS
while [[ $(aws ec2 describe-images –image-ids $tempami | jq -r ‘.Images[].State’) == “pending” ]]; do
MAXPROGRESS
done;
while [[ $(aws ec2 describe-images –image-ids $tempami | jq -r ‘.Images[].State’) == “failed” ]]; do
aws ec2 deregister-image –image-id $tempami > /dev/null 2>&1;
sleep 5;
tempami=$(aws ec2 create-image –instance-id $sourceinstance –name “Temp-Image-PV-HVM-Conversion.$$” –description “Temporary AMI created from source instance $sourceinstance” –no-reboot | jq -r ‘.ImageId’)
done;
done;
aws ec2 describe-images –image-ids $tempami > $srcimagejson
cat $srcimagejson | jq -r ‘.Images[].BlockDeviceMappings[] | select(.Ebs) | .DeviceName, “*”, .Ebs.SnapshotId, “,”‘| tr ‘\n’ ‘ ‘ | sed -e ‘s*\ ,*\n*g’ -e ‘s* \/*\/*g’ -e ‘s*\ **g’ > $sourcesnapshots
for completed in $(cat $srcimagejson | jq -r ‘.Images[].BlockDeviceMappings[] | select(.Ebs) | .Ebs.SnapshotId’); do
MINIPROGRESS
LOGGING “P – tempami=$tempami\n srcimagejson=$srcimagejson\n completed=$completed\n”
while [[ $(aws ec2 describe-snapshots –snapshot-ids $completed | jq -r ‘.Snapshots[].State’) != “completed” ]]; do
PROGRESS
done;
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;

LOGGING “Temprorary AMI – $tempami – created from instance $sourceinstance.”
LOGGINGVAR “vard” “Temporary AMI – $tempami”
LOGGING $(cat $srcimagejson)

# Creating source root volume for conversion

srcrootsnap=$(cat $sourcesnapshots | grep -i ‘sda\|xvde’ | awk -F “*” ‘{ print $2 }’)
srcvolsize=$(aws ec2 describe-snapshots –snapshot-ids $srcrootsnap | jq -r .’Snapshots[].VolumeSize’)
srcvolume=$(aws ec2 create-volume –snapshot-id $srcrootsnap –size $srcvolsize –availability-zone $az –volume-type gp2 | jq -r ‘.VolumeId’)
LOGGING “Created temporary source volume – $srcvolume from snapshot – $srcrootsnap with size – $srcvolsize in AZ – $az”
LOGGINGVAR “vare” “Source volume: $srcvolume”

echo -e “”;
echo -ne “Created temporary source volume to use in conversion1”;
MINIPROGRESS
while [[ $(aws ec2 describe-volumes –volume-ids $srcvolume | jq -r ‘.Volumes[].State’) != “available” ]]; do
PROGRESS
done;
echo -e “\e[92m$srcvolume\e[0m”;
# Create destination root volume for conversion

dstvolsize=$(expr $srcvolsize + 1)
dstvolume=$(aws ec2 create-volume –size $dstvolsize –availability-zone $az –volume-type gp2 | jq -r ‘.VolumeId’)
LOGGING “Created temporary destination volume – $dstvolume with size – $dstvolsize in AZ – $az”
LOGGINGVAR “varf” “Desitnation volume – $dstvolume”

echo -e “”;
echo -ne “Created temporary destination volume to use in conversion”;
MINIPROGRESS
while [[ $(aws ec2 describe-volumes –volume-ids $dstvolume | jq -r ‘.Volumes[].State’) != “available” ]]; do
PROGRESS
done;
echo -e “\e[92m$dstvolume\e[0m”;
echo -e “”;

# Attaching & mounting volumes
LOGGING “————————————-“;
LOGGING “Mounting source / destination volumes”;
LOGGING “————————————-“;
LOGGING “”;

sourcedev=”/dev/sdj”
sourcedevicemapping=$(cat $localinstancejson | jq -r ‘.Reservations[].Instances[].BlockDeviceMappings[].DeviceName’)
if [[ -z $(echo $sourcedevicemapping | grep “$sourcedev”) ]]; then
echo -ne “Attaching source volume \e[31m$srcvolume\e[0m as \e[31m$sourcedev\e[0m”;
LOGGING “Attaching source volume $srcvolume as $sourcedev”;
(aws ec2 attach-volume –volume-id $srcvolume –instance-id $workinginstanceid –device $sourcedev) | tee -a $logfile > /dev/null 2>&1 &
MINIPROGRESS
while [[ $(aws ec2 describe-volumes –volume-ids $srcvolume | jq -r ‘.Volumes[].State’) != in-use ]]; do
PROGRESS
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
else
echo -e “Default device name, \e[31m$sourcedev\e[0m in use. Select another device: “;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $sourcedevicemapping | sed -e ‘s/ /\\\|/g’)”
check_response=”false”
while [[ ${check_response} == “false” ]]; do
read -p “> “;
if [[ $REPLY =~ ^([\/]dev[\/]sd[ghijklmnop])$ ]]; then
if [[ -z $(echo $sourcedevicemapping | grep -o “$REPLY”) ]]; then
check_response=”true”
sourcedev=$REPLY
else
check_response=”false”
echo -e “”;
echo -e “Device \e[31m$REPLY\e[0m already in use. Select a different device:”;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $sourcedevicemapping | sed -e ‘s/ /\\\|/g’)”;
fi
else
check_response=”false”
echo -e “”;
echo -e “\e[31m$REPLY\e[0m is not a valid selection. Select from the list of devices below:”;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $sourcedevicemapping | sed -e ‘s/ /\\\|/g’)”
fi
done;
echo -ne “Attaching source volume \e[31m$srcvolume\e[0m as \e[31m$sourcedev\e[0m”;
LOGGING “Attaching source volume $srcvolume as $sourcedev”;
(aws ec2 attach-volume –volume-id $srcvolume –instance-id $workinginstanceid –device $sourcedev) | tee -a $logfile > /dev/null 2>&1 &
PROGRESS
while [[ $(aws ec2 describe-volumes –volume-ids $srcvolume | jq -r ‘.Volumes[].State’) != in-use ]]; do
MINIPROGRESS
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
fi

destdev=”/dev/sdk”
destdevicemapping=$(cat $localinstancejson | jq -r ‘.Reservations[].Instances[].BlockDeviceMappings[].DeviceName’)
if [[ -z $(echo $destdevicemapping | grep “$destdev”) ]]; then
echo -ne “Attaching destination volume \e[31m$dstvolume\e[0m as \e[31m$destdev\e[0m”;
LOGGING “Attaching destination volume $dstvolume as $destdev”;
(aws ec2 attach-volume –volume-id $dstvolume –instance-id $workinginstanceid –device $destdev) | tee -a $logfile > /dev/null 2>&1 &
while [[ $(aws ec2 describe-volumes –volume-ids $dstvolume | jq -r ‘.Volumes[].State’) != in-use ]]; do
PROGRESS
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
else
echo -e “”;
echo -e “Default device name, \e[31m$destdev\e[0m in use. Select another device: “;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $destdevicemapping | sed -e ‘s/ /\\\|/g’)”
check_response=”false”
while [[ ${check_response} == “false” ]]; do
read -p “> ”
if [[ $REPLY =~ ^([\/]dev[\/]sd[ghijklmnop])$ ]]; then
if [[ -z $(echo $destdevicemapping | grep -o “$REPLY”) ]]; then
check_response=”true”
destdev=$REPLY
else
check_response=”false”
echo -e “”;
echo -e “Device \e[31m$REPLY\e[0m already in use. Select a different device:”;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $destdevicemapping | sed -e ‘s/ /\\\|/g’)”
fi
else
check_response=”false”
echo -e “”;
echo -e “\e[31m$REPLY\e[0m is not a valid selection. Select from the list of devices below:”;
echo -e ” \e[1m\e[92m/dev/sdf
\e[1m\e[92m/dev/sdg
\e[1m\e[92m/dev/sdh
\e[1m\e[92m/dev/sdi
\e[1m\e[92m/dev/sdj
\e[1m\e[92m/dev/sdk
\e[1m\e[92m/dev/sdl
\e[1m\e[92m/dev/sdm
\e[1m\e[92m/dev/sdn
\e[1m\e[92m/dev/sdo
\e[1m\e[92m/dev/sdp\e[0m” | grep -v “$(echo $destdevicemapping | sed -e ‘s/ /\\\|/g’)”
fi
done;
echo -ne “Attaching destination volume \e[31m$dstvolume\e[0m as \e[31m$destdev\e[0m”;
LOGGING “Attaching destination volume $dstvolume as $destdev”;
(aws ec2 attach-volume –volume-id $dstvolume –instance-id $workinginstanceid –device $destdev) | tee -a $logfile > /dev/null 2>&1 &
PROGRESS
while [[ $(aws ec2 describe-volumes –volume-ids $dstvolume | jq -r ‘.Volumes[].State’) != in-use ]]; do
MINIPROGRESS
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
fi

### Create HVM root device

# Get xen device names
clear;
BANNER
echo -e “”;
echo -e “\e[1m\e[4mPreparing devices:\e[0m”;
echo -e “”;
#set -x
LOGGING “————————————–“;
LOGGING “Preparing source & destination volumes”;
LOGGING “————————————–“;
LOGGING “”;
sourcedev1=”$(echo “$sourcedev” | awk -F ‘d’ ‘{print $NF}’)”
destdev1=”$(echo “$destdev” | awk -F ‘d’ ‘{print $NF}’)”
LOGGING “Attached source device: $sourcedev”;
LOGGING “Attached destination device: $destdev”;

if [[ $(file “/dev/xvd$sourcedev1” | \grep -o block) == “block” ]]; then
source=”$(\echo $sourcedev | \sed -e ‘s/s/xv/’)”
LOGGING “Internal source device: $source”;
else
echo -e “Cannot determine source device.”;
LOGGING “Cannot determine source device”;
exit &?
fi
if [[ $(file /dev/xvd$destdev1 | \grep -o block) == “block” ]]; then
destination=”$(\echo $destdev | \sed -e ‘s/s/xv/’)”
LOGGING “Internal destination device: $destination”;
sleep 5;
else
echo -e “Cannot determine desitnation device”;
LOGGING “Cannot determine desitnation device”;
exit &?
fi

echo -e “This process will take a very long time (\e[31mUp to a couple of hours\e[0m) if”;
echo -e “checking large source volumes. Please be patient and allow this process to finish.”;
echo -e “”;
LOGGING “Checking source volume”;
echo -e “Checking \e[31m$source\e[0m:”;
(e2fsck -fp “$source”) | tee -a $logfile
# Check to see if fsck was successful
status=$?
if [[ “$status” = 0 ]] ; then
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “Reducing \e[31m$source\e[0m size:”;
LOGGING “Reducing source device size”;
(resize2fs -M “$source”) | tee -a $logfile
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
else
echo -e “fsck failed – skip resizing $source.”;
LOGGING “fsck failed – skip resizing $source.”;
fi
echo -e “Dumping \e[31m$source\e[0m info:”;
dump=$(dumpe2fs -h “$source”)
block_size=$(awk -F’:[ \t]+’ ‘/^Block size:/ {print $2}’ <<<“$dump”)
block_count=$(awk -F’:[ \t]+’ ‘/^Block count:/ {print $2}’ <<<“$dump”)
LOGGING “source device dump2fs output:”;
LOGGING “$dump”;
LOGGING “source device block size: $block_size”;
LOGGING “source device block count: $block_count”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “Partitioning \e[31m$destination\e[0m:”;
LOGGING “Partitioning $destination”;
(parted $destination –script ‘mklabel msdos mkpart primary 1M -1s print quit’) | tee -a $logfile > /dev/null 2>&1
(partprobe $destination) | tee -a $logfile
(udevadm settle) | tee -a $logfile
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo;
echo -e “Duplicating \e[31m$source\e[0m to \e[31m$destination\e[0m:”;
LOGGING “Duplicating source to destination device”;
(dd if=$source of=${destination}1 bs=$block_size count=$block_count) | tee -a $logfile
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “Resizing \e[31m$destination\e[0m to fill volume:”;
echo -e “”;
LOGGING “Resizing $destination”;
(resize2fs ${destination}1) | tee -a $logfile
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “Mount \e[31m$destination\e[0m to \e[31m$sysroot\e[0m:”;
echo -e “”;
LOGGING “Mounting $destination to $sysroot:”;
(mount ${destination}1 $sysroot) | tee -a $logfile
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “Preparing \e[31m$destination\e[0m for chroot environment:”;
(cp -a $destination ${destination}1 $sysroot/dev/) | tee -a $logfile > /dev/null 2>&1
LOGGING “copy $destination and ${destination}1 $sysroot/dev/ (Preparing for chroot)”;
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
LOGGING “————————————-“;
LOGGING “Source & destination devices prepared”;
LOGGING “————————————-“;
LOGGING “”;
#set +x
### Making changes to destination volume
clear;
BANNER
echo -e “”;
echo -e “\e[1m\e[4mConverting $dstvolume to HVM:\e[0m”;
echo -e “”;
LOGGING “————————–“;
LOGGING “Converting $dstvolume to HVM”;
LOGGING “————————–“;
LOGGING “”;

# Installing grub
(rm -f $sysroot/boot/grub/*stage*) | tee -a $logfile > /dev/null 2>&1 &
(cp $sysroot/usr/*/grub/*/*stage* $sysroot/boot/grub/) | tee -a $logfile > /dev/null 2>&1 &
#(cp $sysroot/usr/lib/grub/*stage* $sysroot/boot/grub/) | tee -a $logfile > /dev/null 2>&1 &
(rm -f $sysroot/boot/grub/device.map) | tee -a $logfile > /dev/null 2>&1 &
echo -e “Installing grub”;
LOGGING “Installing grub”;
echo -e “”;
cat <<EOF | chroot $sysroot grub –batch
device (hd0) $destination
root (hd0,0)
setup (hd0)
EOF
(rm -f $sysroot/dev/$destination $sysroot/dev/${destination}1) | tee -a $logfile > /dev/null 2>&1 &
echo -e “”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;

# Fixing grub.conf / menu.lst
echo -e “Fixing grub config”;
LOGGING “Fixing grub config”;
(e2label ${destination}1 root) | tee -a $logfile > /dev/null 2>&1 &
sleep 10; # Give everything time to setting before hacking grub.conf
for grub in grub.conf menu.lst menu.lst~; do
sed -i ‘s/(hd0)/(hd0,0)/’ $sysroot/boot/grub/$grub > /dev/null 2>&1
sed -i ‘s/root=\([^ ]*\)/root=LABEL=root/’ $sysroot/boot/grub/$grub > /dev/null 2>&1
sed -i ‘s/console*\([^ ]*\)/console=ttyS0/’ $sysroot/boot/grub/$grub > /dev/null 2>&1
done
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo;

# Fix fstab
echo -e “Fixing fstab config”;
LOGGING “Fixing fstab config”;
sed -i ‘s,^.*\ / ,LABEL=root\ \ \/,’ $sysroot/etc/fstab
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo;
LOGGING “$(cat $sysroot/boot/grub/grub.conf )”;
LOGGING “$(cat $sysroot/boot/grub/menu.lst )”;
LOGGING “”;
LOGGING “$(cat $sysroot/etc/fstab)”;
LOGGING “”;

# Unmount $sysroot
(umount $sysroot) | tee -a $logfile > /dev/null 2>&1
LOGGING “Unmounted $dstvolume”;
LOGGING “————————————–“;
LOGGING “$dstvolume successfully converted to HVM”;
LOGGING “————————————–“;
LOGGING “”;

# Creating snapshot and registering AMI
LOGGING “—————————————“;
LOGGING “Regisering snapshot and registering AMI”;
LOGGING “—————————————“;
LOGGING “”;
sleep 5;
echo -ne “Creating a snapshot of \e[31m$dstvolume\e[0m”;
MINIPROGRESS
finalsnap=$tmp/final-snapshots
rm -f $finalsnap
for snap in $(cat $sourcesnapshots | grep -v ‘/dev/sda\|/dev/xvde’); do
region=$region
srcdev=$(echo $snap | awk -F “*” ‘{ print $1 }’);
srcsnap=$(echo $snap | awk -F “*” ‘{ print $2 }’);
tempsnap=$(aws ec2 copy-snapshot –source-region $region –destination-region $region –source-snapshot-id $srcsnap –description “converted-HVMAMI-additionalvolume” | jq -r ‘.SnapshotId’);
echo -e “$tempsnap*$srcdev” >> $finalsnap;
LOGGING “Create $tempsnap from $srvdev”;
MINIPROGRESS
done;
rootsnap=$(aws ec2 create-snapshot –volume-id $dstvolume –description “converted-HVMAMI-root” | jq -r ‘.SnapshotId’)
echo -e “$rootsnap*/dev/sda1” >> $finalsnap;
LOGGING “creating snapshot of root $destvol – $snapshot”;
while [[ $(aws ec2 describe-snapshots –snapshot-ids $(cat $finalsnap | awk -F “*” ‘{ print $1 }’ | tr “\n” ” “) | jq -r ‘.Snapshots[].State’ | grep -i completed | wc -l) != “$(cat $finalsnap | wc -l)” ]]; do
MAXPROGRESS
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;

### Create block device mapping file
mapping=$tmp/mapping
echo -e “[\n” > $mapping;
for snap in $(cat $finalsnap); do snapshot=$(echo $snap | awk -F “*” ‘{ print $1 }’); device=$(echo $snap | awk -F “*” ‘{ print $2 }’); size=$(aws ec2 describe-snapshots –snapshot-ids $snapshot | jq -r ‘.Snapshots[].VolumeSize’); echo -e ” {\n \”VirtualName\”: \”ebs\”,\n \”DeviceName\”: \”$device\”,\n \”Ebs\”: {\n \”SnapshotId\”: \”$snapshot\”,\n \”VolumeSize\”: $size,\n \”VolumeType\”: \”gp2\”\n }\n },” >> $mapping; done;
head -n -1 $mapping > $tmp/tmpmap;
\mv $tmp/tmpmap $mapping;
echo -e ” }\n]” >> $mapping;

### Register AMI
echo -ne “Registering AMI”;
MINIPROGRESS
ami=$(aws ec2 register-image –name “Converted HVM – $sourceinstance.$$” –description “HVM AMI created from source instance $sourceinstance” –architecture x86_64 –root-device-name “/dev/sda1” –virtualization-type hvm –block-device-mappings file://$mapping | jq -r ‘.ImageId’)
MINIPROGRESS
while [[ $(aws ec2 describe-images –image-id $ami | jq -r ‘.Images[].State’) != “available” ]]; do
PROGRESS
done;
LOGGING “AMI: $ami created successfully”;
echo -e “\e[1m\e[92m[OK]\e[0m”;
echo -e “”;
echo -e “HVM AMI creation complete. AMI = \e[31m$ami\e[0m”;
echo -e “”;

### Cleanup
clear;
BANNER
echo -e “”;
echo -e “\e[1m\e[4mClean up temporary resources:\e[0m”;
echo -e “”;
echo -e “Continue with clean up of temprary resources? It’s suggested to test \e[31m$ami\e[0m first,”;
echo -e “before removing temporary resource. (y/n)”;
check_response=”false”
while [[ $check_response == “false” ]]; do
read -p “> “;
if [[ $REPLY =~ ^([yY][eE][sS]|[yY])$ ]]; then
check_response=”true”;
echo -e “”;
echo -ne “Removing temporary resources”;
LOGGING “Removing resources:”;
MINIPROGRESS
(aws ec2 deregister-image –image-id $tempami) | tee -a $logfile > /dev/null 2>&1
MINIPROGRESS
for cleanup in $(cat $sourcesnapshots | awk -F “*” ‘{ print $2 }’); do
(aws ec2 delete-snapshot –snapshot-id $cleanup) |tee -a $logfile > /dev/null 2>&1 &
done;
(aws ec2 detach-volume –volume-id $srcvolume) | tee -a $logfile > /dev/null 2>&1 &
while [[ $(aws ec2 describe-volumes –volume-ids $srcvolume | jq -r ‘.Volumes[].State’) != “available” ]]; do
PROGRESS
done;
(aws ec2 detach-volume –volume-id $dstvolume) | tee -a $logfile > /dev/null 2>&1 &
while [[ $(aws ec2 describe-volumes –volume-ids $dstvolume | jq -r ‘.Volumes[].State’) != “available” ]]; do
PROGRESS
done;
(aws ec2 delete-volume –volume-id $srcvolume) | tee -a $logfile > /dev/null 2>&1 &
(aws ec2 delete-volume –volume-id $dstvolume) | tee -a $logfile > /dev/null 2>&1 &
rm -f jacquecp
for delete in $localinstancejson $srcinstancejson $sourcevolumes $sourcesnapshots $srcvolumejson $srcimagejson $finalsnap $mapping; do
(rm -f $delete) | tee -a $logfile > /dev/null 2>&1
done;
echo -e “\e[1m\e[92m[OK]\e[0m”;
elif [[ $REPLY =~ ^([nN][oO]|[nN])$ ]]; then
check_response=”true”;
echo -e “”;
echo -e “Manually remove the following resource once you have successfully tested \e[31m$ami\e[0m:”;
echo -e ” 1. Deregister temporary AMI of source instance: \e[31m$tempami\e[0m”;
echo -e ” 2. Delete the following snapshots used by \e[31m$tempami\e[0m:”;
for cleanup in $(cat $sourcesnapshots | awk -F “*” ‘{ print $2 }’); do
echo -e ” \e[31m$cleanup\e[0m”;
done
echo -e ” 3. Unmount \e[31m$dstvolume\e[0m from mount point /mnt”;
echo -e ” 4. Detach and delete temporary volumes used during the conversion process:”;
echo -e ” \e[31m$srcvolume\e[0m”;
echo -e ” \e[31m$dstvolume\e[0m”;
echo -e ” 5. Delete the following temporary files:”;
for delete in $localinstancejson $srcinstancejson $sourcevolumes $sourcesnapshots $srcvolumejson $srcimagejson $finalsnap $mapping; do
echo -e ” \e[31m$delete\e[0m”;
done;
LOGGING “Manual cleanup selected. Ensure the removal of the following resource:”;
LOGGING “Temporary AMI: $tempami”;
LOGGING “Snapshots: $(cat $sourcesnapshots | awk -F “*” ‘{ print $2 }’)”;
LOGGING “Volumes: $srcvolume, $dstvolume”;
LOGGING “Delete the following files manually:”;
LOGGING “$localinstancejson”;
LOGGING “$srcinstancejson”;
LOGGING “$sourcevolumes”;
LOGGING “$sourcesnapshots”;
LOGGING “$srcvolumejson”;
LOGGING “$srcimagejson”;
LOGGING “$finalsnap”;
LOGGING “$mapping”;
LOGGING “Done”;
else
echo -e “\e[92myes\e[0m or \e[31mno\e[0m?”
fi
done;

### Finished
clear;
BANNER
echo -e “”;
echo -e “\e[1m\e[4mConversion script completed.\e[0m”;
echo -e “”;
echo -e “New HVM AMI created from instance $sourceinstance:”;
echo -e ” \e[31m$ami\e[0m”;
echo -e “”;
echo -e “Log file located at:\e[31m$logfile\e[0m.”;
echo -e “”;

Сomments аrchive