blob: 0532613ac3eaead2bf008f957dcc195d8bd719ba [file] [log] [blame]
Sergio Slobodrianc5477712017-06-07 11:56:56 -04001#!/bin/bash
2
3baseImage="Ubuntu1604LTS"
4iVmName="vInstaller"
5iVmNetwork="default"
6shutdownTimeout=5
7ipTimeout=20
8
9lBlue='\033[1;34m'
10green='\033[0;32m'
11orange='\033[0;33m'
12NC='\033[0m'
13red='\033[0;31m'
14yellow='\033[1;33m'
15dGrey='\033[1;30m'
16lGrey='\033[1;37m'
17lCyan='\033[1;36m'
18
19wd=`pwd`
Sergio Slobodrian7c483622017-06-13 15:51:34 -040020
21# Check if a specific network was specified on the command line.
22# This is used mostly for testing.
23if [ $# -eq 1 ]; then
24 iVmNetwork=$1
25fi
26
Sergio Slobodrianc5477712017-06-07 11:56:56 -040027# Update the XML file with the VM information
28echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
29cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
30
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -040031# Check that the default storage pool exists and create it if it doesn't
32poolCheck=`virsh pool-list --all | grep default`
33if [ -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
37else
38 poolCheck=`virsh pool-list | grep default`
39 if [ -z "$poolCheck" ]; then
40 virsh pool-start default
41 fi
42fi
43
Sergio Slobodrianc5477712017-06-07 11:56:56 -040044# Copy the vm image to the default storage pool
45echo -e "${lBlue}Creating the storage for the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
46# Copy the vm image to the installer directory
47virsh pool-create-as installer --type dir --target `pwd`
48virsh vol-create-from default ${iVmName}_volume.xml $iVmName.qcow2 --inputpool installer
49virsh pool-destroy installer
50
51# Create the VM using the updated xml file and the uploaded image
52virsh define tmp.xml
53
54rm tmp.xml
55
56# Start the VMm, if it's already running just ignore the error
57echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
58virsh start $iVmName > /dev/null 2>&1
59
60# Get the VM's IP address
61ctr=0
62ipAddr=""
63while [ -z "$ipAddr" ];
64do
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`
73done
74
75# Log into the vm
76ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr