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` |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 20 | |
| 21 | # Check if a specific network was specified on the command line. |
| 22 | # This is used mostly for testing. |
| 23 | if [ $# -eq 1 ]; then |
| 24 | iVmNetwork=$1 |
| 25 | fi |
| 26 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 27 | # Update the XML file with the VM information |
| 28 | echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 29 | cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml |
| 30 | |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 31 | # Check that the default storage pool exists and create it if it doesn't |
| 32 | poolCheck=`virsh pool-list --all | grep default` |
| 33 | if [ -z "$poolCheck" ]; then |
| 34 | virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images/ |
| 35 | virsh pool-autostart default |
| 36 | virsh pool-start default |
| 37 | else |
| 38 | poolCheck=`virsh pool-list | grep default` |
| 39 | if [ -z "$poolCheck" ]; then |
| 40 | virsh pool-start default |
| 41 | fi |
| 42 | fi |
| 43 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 44 | # Copy the vm image to the default storage pool |
| 45 | echo -e "${lBlue}Creating the storage for the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 46 | # Copy the vm image to the installer directory |
| 47 | virsh pool-create-as installer --type dir --target `pwd` |
| 48 | virsh vol-create-from default ${iVmName}_volume.xml $iVmName.qcow2 --inputpool installer |
| 49 | virsh pool-destroy installer |
| 50 | |
| 51 | # Create the VM using the updated xml file and the uploaded image |
| 52 | virsh define tmp.xml |
| 53 | |
| 54 | rm tmp.xml |
| 55 | |
| 56 | # Start the VMm, if it's already running just ignore the error |
| 57 | echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 58 | virsh start $iVmName > /dev/null 2>&1 |
| 59 | |
| 60 | # Get the VM's IP address |
| 61 | ctr=0 |
| 62 | ipAddr="" |
| 63 | while [ -z "$ipAddr" ]; |
| 64 | do |
| 65 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 66 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 67 | sleep 3 |
| 68 | if [ $ctr -eq $ipTimeout ]; then |
| 69 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 70 | exit |
| 71 | fi |
| 72 | ctr=`expr $ctr + 1` |
| 73 | done |
| 74 | |
| 75 | # Log into the vm |
| 76 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr |