Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 15 | |
| 16 | baseImage="Ubuntu1604LTS" |
| 17 | iVmName="vInstaller" |
| 18 | iVmNetwork="default" |
| 19 | shutdownTimeout=5 |
| 20 | ipTimeout=20 |
| 21 | |
| 22 | lBlue='\033[1;34m' |
| 23 | green='\033[0;32m' |
| 24 | orange='\033[0;33m' |
| 25 | NC='\033[0m' |
| 26 | red='\033[0;31m' |
| 27 | yellow='\033[1;33m' |
| 28 | dGrey='\033[1;30m' |
| 29 | lGrey='\033[1;37m' |
| 30 | lCyan='\033[1;36m' |
| 31 | |
| 32 | wd=`pwd` |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 33 | |
| 34 | # Check if a specific network was specified on the command line. |
| 35 | # This is used mostly for testing. |
| 36 | if [ $# -eq 1 ]; then |
| 37 | iVmNetwork=$1 |
| 38 | fi |
| 39 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 40 | # Update the XML file with the VM information |
| 41 | echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 42 | cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml |
| 43 | |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 44 | # Check that the default storage pool exists and create it if it doesn't |
| 45 | poolCheck=`virsh pool-list --all | grep default` |
| 46 | if [ -z "$poolCheck" ]; then |
| 47 | virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images/ |
| 48 | virsh pool-autostart default |
| 49 | virsh pool-start default |
| 50 | else |
| 51 | poolCheck=`virsh pool-list | grep default` |
| 52 | if [ -z "$poolCheck" ]; then |
| 53 | virsh pool-start default |
| 54 | fi |
| 55 | fi |
| 56 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 57 | # Copy the vm image to the default storage pool |
| 58 | echo -e "${lBlue}Creating the storage for the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 59 | # Copy the vm image to the installer directory |
| 60 | virsh pool-create-as installer --type dir --target `pwd` |
| 61 | virsh vol-create-from default ${iVmName}_volume.xml $iVmName.qcow2 --inputpool installer |
| 62 | virsh pool-destroy installer |
| 63 | |
| 64 | # Create the VM using the updated xml file and the uploaded image |
| 65 | virsh define tmp.xml |
| 66 | |
| 67 | rm tmp.xml |
| 68 | |
| 69 | # Start the VMm, if it's already running just ignore the error |
| 70 | echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 71 | virsh start $iVmName > /dev/null 2>&1 |
| 72 | |
| 73 | # Get the VM's IP address |
| 74 | ctr=0 |
| 75 | ipAddr="" |
| 76 | while [ -z "$ipAddr" ]; |
| 77 | do |
| 78 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 79 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 80 | sleep 3 |
| 81 | if [ $ctr -eq $ipTimeout ]; then |
| 82 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 83 | exit |
| 84 | fi |
| 85 | ctr=`expr $ctr + 1` |
| 86 | done |
| 87 | |
| 88 | # Log into the vm |
| 89 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr |