-
Notifications
You must be signed in to change notification settings - Fork 1
/
VMMgr_Launcher
407 lines (398 loc) · 17.7 KB
/
VMMgr_Launcher
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#! /bin/bash
#
# Script Name: VMMgr_Launcher
# Autor: Sylvain Girod ( uubu at gmx dot fr )
# Version: 0.05
# Description: VMMgr_Launcher main entry for VM management
#
# This is the entry point for VMMGR, type
MYID=$(id -u)
### Install/uninstall only with root ###
if [ "$MYID" == "0" ]; then
if [ "$1" == "install" ] || [ "$1" == "uninstall" ]; then
echo "${1^}ing VMMGR..."
else
echo "Error, except 'install' and 'uninstall' commands, I refuse to run as root."; exit -1
fi
# load local lib if exists
[ -r $(dirname $0)/VMMgr_.conf ] && . $(dirname $0)/VMMgr_.conf
### all other command never has root ###
else
if [ "$1" == "install" ] || [ "$1" == "uninstall" ]; then
echo "Error, '$1' commands need root privileges"; exit -1
fi
if [ -r /etc/VMMGR/VMMgr_.conf ]; then
. /etc/VMMGR/VMMgr_.conf
elif [ -r $(dirname $0)/VMMgr_.conf ]; then
. $(dirname $0)/VMMgr_.conf
else
echo "No config VMMgr_.conf found, abording..."; exit -1
fi
fi
# Loading Libs
if [ -r $VMMGR_LIB_PATH/VMMgr_Functions ]; then
. $VMMGR_LIB_PATH/VMMgr_Db
. $VMMGR_LIB_PATH/VMMgr_Functions
. $VMMGR_LIB_PATH/VMMgr_Networks
. $VMMGR_LIB_PATH/VMMgr_Disks
. $VMMGR_LIB_PATH/VMMgr_CPUs
. $VMMGR_LIB_PATH/VMMgr_Install
. $VMMGR_LIB_PATH/VMMgr_Group
. $VMMGR_LIB_PATH/VMMgr_Dbm
. $VMMGR_LIB_PATH/VMMgr_Physical
. $VMMGR_LIB_PATH/VMMgr_RemoteAccess
. $VMMGR_LIB_PATH/VMMgr_Devices
fi
### If DB not exists ###
if [ $MYID != 0 ]; then Set_Db $VMMGR_DB_PATH; fi
case "$1" in
"check")
Check_Requirements
Check_System
;;
"install")
. $VMMGR_LIB_PATH/VMMgr_Install
Install_VMMGR
;;
"uninstall")
. $VMMGR_LIB_PATH/VMMgr_Install
Uninstall_VMMGR
;;
"create")
# Args:
# $2: Vm Name
# $3: Template Name
# Steps:
# 1) Check Vars, template, if vm and config file already exists
# 2) Declare VM in DB, copy template, and customize Vars
if [ ! "$2" ] || [ ! "$3" ]; then
echo "Usage $0 <vm name> <template>"
elif [ "$(echo $2 | grep "-")" ]; then
echo "Error, VM name contain '-' character, please use '_' instead"
elif [ -r $VMMGR_DATA_PATH/Templates/$3 ] || [ -r $VMMGR_CONFIG_PATH/Templates/$3 ]; then
VMMGR_LOG "VMMGR" "CREATE" "Creating vm with name: $2 from template $3"
VMCONF=$(Check_Existing_VM $2)
[ -r $VMMGR_DATA_PATH/Templates/$3 ] && TEMPLATE=$VMMGR_DATA_PATH/Templates/$3
[ -r $VMMGR_CONFIG_PATH/Templates/$3 ] && TEMPLATE=$VMMGR_CONFIG_PATH/Templates/$3
if [ "$VMCONF" == "" ]; then VMCONF="$2.conf"; fi
if [ -r $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF ]; then
echo "Warning, config file $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF already exists"
VMMGR_LOG "VMMGR" "CREATE" "Error, config for $2 already exists"
else
cp $TEMPLATE $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
Custom_VM_Config $2 $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
fi
GROUP=$(cat $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF | grep "^GROUP=" | cut -d\" -f2)
VMMGR_LOG "VMMGR" "CREATE" "Setting group: $GROUP for new vm: $2"
VMID=$(Db_Create_VM $VMMGR_DB_PATH $2 $VMCONF $(cat /proc/sys/kernel/random/uuid) $GROUP)
Db_Set_Group $VMMGR_DB_PATH $2 $GROUP
else
echo "Error, template $3 doesn't exists"
fi
;;
"delete")
VMMGR_LOG "$2" "DELETE" "Deleting vm $2"
if [ ! "$2" ]; then
echo "Usage $0 delete <vm name> [disks[-conf[-db]]]"
else
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "VM $2 not found in DB $VMCONF"
else
VM_Delete $2 $VMCONF $3
fi
fi
;;
"start")
Check_System
if [ "$($VMMGR_PATH/VMMgr_Launcher status $2 | grep running)" != "" ]; then
echo "VM $1 already running"
else
if [ ! "$2" ]; then
echo "Usage $0 start <vm name>"
elif [ "$3" == "regen" ]; then
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ ! -r $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF ]; then
echo "VM $2 not found"
else
Start_VM_From_Config $2 $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
fi
elif [ -r /etc/systemd/system/$2.service ]; then
systemctl start $2
else
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ ! -r $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF ]; then
echo "VM $2 not found"
else
Start_VM_From_Config $2 $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
fi
fi
fi
fi
;;
"enable")
VMMGR_LOG "$2" "ENABLE" "Enabling $2.service"
Customize_Unit $2 $VMMGR_DATA_PATH/VM_CONFIGS/$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
UNITEXIST=$(systemctl list-unit-files $2.service | grep 1)
if [ "$UNITEXIST" ]; then
UNITPATH=$(systemctl status $2.service | grep "Loaded:" | cut -d\( -f2 ); UNITPATH="${UNITPATH%%.service*}.service"
if [ "$(sha1sum $VMMGR_DATA_PATH/UNITS/$2.service | awk '{print $1}')" == "$(sha1sum $UNITPATH | awk '{print $1}')" ]; then
echo "VM $2 already enabled"
else
sudo systemctl disable $2
sudo cp $VMMGR_DATA_PATH/UNITS/$2.service /etc/systemd/system/$2.service
sudo chmod 744 /etc/systemd/system/$2.service
sudo systemctl daemon-reload
fi
else
sudo cp $VMMGR_DATA_PATH/UNITS/$2.service /etc/systemd/system/$2.service
sudo systemctl daemon-reload
fi
sudo systemctl enable $2
Db_Enable_VM $VMMGR_DB_PATH $2
;;
"disable")
VMMGR_LOG "$2" "DISABLE" "disabling $2.service"
sudo systemctl disable $2
Db_Disable_VM $VMMGR_DB_PATH $2
;;
"stop")
VMMGR_LOG "$2" "STOP" "Stopping vm $2"
if [ "$(systemctl is-active $2)" == "active" ]; then
systemctl stop $2
else
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
{ echo " system_powerdown"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
{ echo { echo '{ "execute": "qmp_capabilities" } { "execute": "system_powerdown" } { "execute": "quit" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
fi
fi
;;
"group")
Check_System
Manage_Group $2 $3 $4
;;
"status")
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
{ echo " info status"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|} 2>/dev/null
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
{ echo '{ "execute": "qmp_capabilities" } { "execute": "query-status" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
fi
;;
"kill")
VMMGR_LOG "$2" "KILL" "killing VM $2"
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
{ echo " quit"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
{ echo { echo '{ "execute": "qmp_capabilities" } { "execute": "quit" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
fi
;;
"reset")
VMMGR_LOG "$2" "RESET" "VM Hard Reset"
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
{ echo " system_reset"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
{ echo { echo '{ "execute": "qmp_capabilities" } { "execute": "system_reset" } { "execute": "quit" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
fi
;;
"ipmistart")
;;
"ipmistop")
;;
"ipmistatus")
;;
"db")
VM_DB_Manage $2 $3
;;
"connect")
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
case "$3" in
"spice")
nohup remote-viewer $VMMGR_DATA_PATH/R-SPICE/$2 &
;;
"telnet")
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
;;
"qmp")
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
;;
"vnc")
nohup remote-viewer $VMMGR_DATA_PATH/R-VNC/$2 &
;;
"ssh")
IPNAME=$(Db_Get_SSH $VMMGR_DB_PATH $2)
ssh ${IPNAME#*|}
;;
esac
fi
;;
"load")
VMMGR_LOG $2 "DVD change" "Trying to load $3 into DVD drive"
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
if [ "$3" ]; then
case "$3" in
"virtiowin")
ISO=$VMMGR_ISO_PATH/virtio-win.iso
;;
*)
if [ -f "$3" ]; then
ISO=$3
elif [ -f "$VMMGR_ISO_PATH/$3" ]; then
ISO="$VMMGR_ISO_PATH/$3"
fi
if [ "${3#http}" != $3 ]; then
ISO=$3
fi
;;
esac
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
{ echo " change DVD $ISO"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
if [ "$4" == "reset" ]; then
{ echo " system_reset"; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
{ echo '{ "execute": "qmp_capabilities" } { "execute": "change DVD $ISO" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
if [ "$4" == "reset" ]; then
{ echo { echo '{ "execute": "qmp_capabilities" } { "execute": "system_reset" } { "execute": "quit" }'; sleep 1; } | telnet $(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}') ${IPPORT#*|}
fi
fi
fi
fi
;;
"list")
List_All_VM $2
;;
"usb") ### add DEVICE:VENDOR USB Device at runtime
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ ! -r $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF ]; then
echo "VM $2 not found"
else
. $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
if [ "$TELNET" == "on" ]; then
IPPORT=$(Set_Service_Listen $TELNETIP $TELNETIPVER $TELNETTYPE $NAME telnet $TELNETPORT)
TELNETTPL="-monitor telnet:$(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}'):${IPPORT#*|},server,nowait"
TIP=${TELNETTPL##*monitor telnet:}
TIP=${TIP%%,server,nowait}
{ echo " $(SET_DIF_USB $3)"; sleep 1; } | telnet ${TIP%:*} ${TIP##*:}
elif [ "$QMP" == "on" ]; then
IPPORT=$(Set_Service_Listen $QMPIP $QMPIPVER $QMPTYPE $NAME qmp $QMPPORT)
QMPTPL="-chardev socket,id=qmp01,host=$(ip -br a s | grep ${IPPORT%|*} | awk '{print $1}'),port=${IPPORT#*|},server,nowait -mon chardev=qmp01,mode=control"
TIP=${QMPTPL##*chardev socket,id=qmp01,host=}
TIP=${TIP%%,server,nowait*}
{ echo '{ "execute": "qmp_capabilities" } { "execute": "$(SET_DIF_USB $3)" }'; sleep 1; } | telnet ${TIP%%,*} ${TIP##*port=}
fi
fi
;;
"usb-daemon") ### monitor usb device for disconnection
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ ! -r $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF ]; then
echo "VM $2 not found"
else
nohup $VMMGR_PATH/VMMgr_USB-daemon $2 $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF &
fi
;;
"wintools")
VMMGR_LOG "VMMGR" "WINTOOLS" "Downloading wintools..."
### Virtio Win
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso --directory-prefix=$VMMGR_ISO_PATH/
### QEMU Guest Agent
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-qemu-ga/qemu-ga-win-100.0.0.0-3.el7ev/qemu-ga-x64.msi --directory-prefix=$VMMGR_ISO_PATH/
### Spice Guest Tools
wget https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-latest.exe --directory-prefix=$VMMGR_ISO_PATH/
;;
"ssh")
VMMGR_LOG "VMMGR" "SSH" "Setting SSH param"
Set_SSH $3 $2 $4 $5
;;
"edit")
VMCONF=$(Db_Get_VM_Config $VMMGR_DB_PATH $2)
if [ "$VMCONF" == "" ]; then
echo "$2 not found in DB"
else
vi $VMMGR_DATA_PATH/VM_CONFIGS/$VMCONF
fi
;;
*)
echo "Usage: VMMgr_Launcher <command> <options>
install/uninstall: Install or uninstall on your system instead of running all in the current folder
check: helper to allow you to configure correctly your system
create <vm name> <template>: declare un new vm based on a template
start <vm name> <regen>: Start you vm. regen bypass existing qemu-config and use you vm config
stop <vm name>: stop the vm
kill <vm name>: force stop vm
reset <vm name>: reset vm
status <vm name>: is the vm running?
edit <vm name>: edit vm config
connect <vm name> <spice|vnc|telnet|qmp|ssh> connect to your vm
load <vmname> <fullpath iso|rel to iso path|virtiowin> <reset>: load iso into DVD drive, optionally reset immediatly after
wintools: download Windows drivers and tools in your iso path (actually: $VMMGR_ISO_PATH)
enable: Install unit systemd and enable
disable: Disable unit systemd
delete: [disks[-conf[-db]]] delete VM
group: <list|start|stop|enable|disable> group management
list <all|running|stopped|enabled|disabled> list vms
db <reconf|todb|import|export|backup list|restore|purge>
ssh <set|update|delete> <vmname> <IP> [ <ssh hostname> ]: register IP and hostname for ssh access
usb <DEVICE:VENDOR>: connect usb device at runtime
usb-daemon <vm name>: monitor missing usb devices and re-add it once plugged again
"
;;
esac
if [ "$MYID" != "0" ]; then
### Auto Backup DB ###
if [ "$1" == "install" ] || [ "$1" == "check" ]; then
VMMGR_LOG "VMMGR" "SYSTEM" "action $1 done!"
else
Auto_Backup_Db
fi
### Recheck Perms ###
sudo chown -R qemu:qemu $VMMGR_DATA_PATH && sudo chmod -R 770 $VMMGR_DATA_PATH
fi