-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-vm.sh
More file actions
executable file
·104 lines (93 loc) · 2.44 KB
/
start-vm.sh
File metadata and controls
executable file
·104 lines (93 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#
# This starts the given VM
#
# gnd @ gnd.sk, 2017 - 2019
#
####################################################################
# Check if LIMA_ROOT set
if [ -z $LIMA_ROOT ]; then
echo "Cant find LIMA. Please check if the install finished correctly."
echo "Exiting. Reason: LIMA_ROOT not set."
exit
fi
# Define globals
source $LIMA_ROOT/vms/settings
### waits until conection to new VM established
connect-ssh() {
local ip=${1}
local hostname=${2}
con=0
tries=0
draw_tries() { for ((i=0; i<try; i=i+1)); do printf "▇"; done }
clean_line() { printf "\r"; }
while [[ "$con" == "0" ]]; do
check=`ssh -q -o ConnectTimeout=1 -o StrictHostKeyChecking=no $ip hostname`
sleep 1
if [[ ! "$check" == "$hostname" ]]; then
clean_line
tries=$((tries+1))
draw_tries=$((tries % 100))
for (( try=1; try<=draw_tries; try=try+1 )); do
printf "Waiting for VM: "$tries"s "; draw_tries
clean_line
done
else
con=1
printf "\n\nVM up ! "
fi
done
}
usage() {
printf "\n"
printf "This starts the given VM\n"
printf "Usage: \n"
printf "$0 <name NAME> [quiet] \n\n"
}
### Check if VM_NAME is unique and existing, otherwise exit
case "$1" in
'name')
VM_NAME=$2
LINS=`cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"|wc -l`
if [[ $LINS -lt 1 ]]; then
printf "\n$0: No such name $VM_NAME found\n\n"
exit
fi
if [[ $LINS -gt 1 ]]; then
printf "\n$0: More names like '$VM_NAME' found, please be specific:\n"
cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"
printf "\n"
exit
fi
;;
*)
usage
exit
;;
esac
### Make sure the VM is not running
CHECK=`virsh list --all|grep " $VM_NAME "`
if [[ ! -z $CHECK ]]; then
printf "\n$0: $VM_NAME is already running.\n"
else
### Determine VM parameters
VM_TYPE=`cat $VM_LIST | awk {'print $2" "$5;'}|grep "^$VM_NAME "|awk {'print $2;'}`
VM_IP=`cat $VM_LIST | awk {'print $2" "$3;'}|grep "^$VM_NAME "|awk {'print $2;'}`
if [[ $VM_TYPE == "dyn" ]]; then
VM_TYPE_DIR="dynamic"
fi
if [[ $VM_TYPE == "sta" ]]; then
VM_TYPE_DIR="static"
fi
### Start the VM
echo "Starting $VM_NAME"
virsh create $VM_DIR/$VM_TYPE_DIR/$VM_NAME/vm.xml
### Wait for the VM to come up
if [[ $3 != "quiet" ]]; then
connect-ssh $VM_IP $VM_NAME
else
sleep 1
fi
### Print VM is up
echo "$VM_NAME is up."
fi