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