Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | baseImage="Ubuntu1604LTS" |
| 4 | iVmName="vInstaller" |
| 5 | iVmNetwork="default" |
| 6 | shutdownTimeout=5 |
| 7 | ipTimeout=20 |
| 8 | |
| 9 | lBlue='\033[1;34m' |
| 10 | green='\033[0;32m' |
| 11 | orange='\033[0;33m' |
| 12 | NC='\033[0m' |
| 13 | red='\033[0;31m' |
| 14 | yellow='\033[1;33m' |
| 15 | dGrey='\033[1;30m' |
| 16 | lGrey='\033[1;37m' |
| 17 | lCyan='\033[1;36m' |
| 18 | |
| 19 | wd=`pwd` |
| 20 | # Update the XML file with the VM information |
| 21 | echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 22 | cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml |
| 23 | |
| 24 | # Copy the vm image to the default storage pool |
| 25 | echo -e "${lBlue}Creating the storage for the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 26 | # Copy the vm image to the installer directory |
| 27 | virsh pool-create-as installer --type dir --target `pwd` |
| 28 | virsh vol-create-from default ${iVmName}_volume.xml $iVmName.qcow2 --inputpool installer |
| 29 | virsh pool-destroy installer |
| 30 | |
| 31 | # Create the VM using the updated xml file and the uploaded image |
| 32 | virsh define tmp.xml |
| 33 | |
| 34 | rm tmp.xml |
| 35 | |
| 36 | # Start the VMm, if it's already running just ignore the error |
| 37 | echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 38 | virsh start $iVmName > /dev/null 2>&1 |
| 39 | |
| 40 | # Get the VM's IP address |
| 41 | ctr=0 |
| 42 | ipAddr="" |
| 43 | while [ -z "$ipAddr" ]; |
| 44 | do |
| 45 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 46 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 47 | sleep 3 |
| 48 | if [ $ctr -eq $ipTimeout ]; then |
| 49 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 50 | exit |
| 51 | fi |
| 52 | ctr=`expr $ctr + 1` |
| 53 | done |
| 54 | |
| 55 | # Log into the vm |
| 56 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr |