-
Notifications
You must be signed in to change notification settings - Fork 1
/
qvm
1499 lines (1343 loc) · 43.3 KB
/
qvm
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Filename: qvm
# Autor: Michael Mair-Keimberger (m DOT mairkeimberger AT gmail DOT com)
# Date: 31.05.2020
# Copyright (C) 2020 Michael Mair-Keimberger
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Discription:
# qvm: script for working with kvm guests
MAX_QTAP=30
DRYRUN=false
TAP_DELETE=true
AUTOSTART_SPICY=false
[ -e ~/.config/qvm/qvm.conf ] && source ~/.config/qvm/qvm.conf
# programs / dependencies
BRIDGE="/sbin/bridge" # sys-apps/iproute2
IP="/bin/ip" # sys-apps/iproute2
NC="/usr/bin/nc" # net-analyzer/openbsd-netcat
QGA="/usr/bin/qemu-ga-client" # app-emulation/qemu[pyhton]
LSOF="/usr/bin/lsof" # sys-process/lsof
VFSD="/usr/libexec/virtiofsd" # app-emulation/virtiofsd
SPICY="/usr/bin/spicy" # net-misc/spice-gtk
# log settings
NOCOLOR='\e[0m' # no color
YELLOW='\e[1;33m' # yellow color
LIGHTRED='\e[1;31m' # lightred color
GREEN='\e[1;32m' # green color
loginfo() { while IFS='' read -r line; do >&2 echo -e "${GREEN} * ${NOCOLOR}${line}"; done }
loginfon() { while IFS='' read -r line; do >&2 echo -en "${GREEN} * ${NOCOLOR}${line}"; done }
logwarn() { while IFS='' read -r line; do >&2 echo -e "${YELLOW} * ${NOCOLOR} Warning: ${line}"; done }
logerr() { while IFS='' read -r line; do >&2 echo -e "${LIGHTRED} * ${NOCOLOR}${line}"; done }
# check PID_DIR and TMP_DIR
PID_DIR="/run/kvm"
if [ $(id -u) = 0 ]; then
[ -d ${PID_DIR} ] || mkdir ${PID_DIR}
TMP_DIR="/tmp"
else
PID_DIR="/run/user/$(id -u)"
if [ -n "${TMP_DIR}" ]; then
[ -w ${TMP_DIR} ] || TMP_DIR="/run/user/$(id -u)"
else
TMP_DIR="/run/user/$(id -u)"
fi
fi
# check if the qemu guest agent is available
QGA_AVAILABLE=false
if $(command -v ${QGA} > /dev/null) ; then
QGA_AVAILABLE=true
fi
# check if spicy is available
SPICY_AVAILABLE=false
if $(command -v ${SPICY} > /dev/null) ; then
SPICY_AVAILABLE=true
fi
_dry_run_check(){
if ${DRYRUN}; then
return 0
else
return 1
fi
}
_vm_edit(){
local _cmd="${1}"
local _value="${2}"
local _output
_output="$(echo "${_cmd} ${_value}" | ${NC} -U -q1 ${VMSOCK})"
# catch some errors and print them
if $(echo ${_output}|grep -q "Migration is disabled") || $(echo ${_output}|grep -q "Error:"); then
echo "${_output}" | head -n+3 | tail -n1 | (logerr)
fi
}
_vm_edit_snapshot_output(){
local _cmd="${1}"
local _output
_output="$(echo "${_cmd}" | ${NC} -U -q1 ${VMSOCK})"
echo "${_output}"|tail -n+4|head -n-1
}
_check_remote_access_port(){
# remote access checks
local range=${1}
local ip=${2}
local port=${3}
# check with netstat if a port is used
if [ -z "${port}" ]; then
local port_nr=0
while $(netstat -nat | grep -E "${ip}:${range}${port_nr}[^0-9]" >/dev/null); do
port_nr=$(expr ${port_nr} + 1)
done
local access_port="${range}${port_nr}"
else
local access_port="${port}"
fi
echo ${access_port}
}
_cfg_network_check(){
# network checks/default settings
declare -a _tmp_vm_interfaces=("${VM_NETWORK}" "${VM_NETWORK0}" "${VM_NETWORK1}" "${VM_NETWORK2}" "${VM_NETWORK3}" \
"${VM_NETWORK4}" "${VM_NETWORK5}" "${VM_NETWORK6}" "${VM_NETWORK7}" "${VM_NETWORK8}" "${VM_NETWORK9}")
declare -a _vm_interfaces
local i
for i in "${_tmp_vm_interfaces[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_interfaces+=("${i}")
done
local _net_user_set=false
local _net_tap_set=false
local _net_id=0
local _random_mac=false
local _vhost_set=""
if ${VM_ENABLE_VHOST:-false}; then
_vhost_set=",vhost=on"
fi
if [ -n "${_vm_interfaces}" ]; then
for single_net in "${_vm_interfaces[@]}"; do
local net_typ="$(echo ${single_net}|cut -d';' -f1)"
local net_mac="$(echo ${single_net}|cut -sd';' -f2)"
local net_drv="$(echo ${single_net}|cut -sd';' -f3)"
local net_brd="$(echo ${single_net}|cut -sd';' -f4)"
local net_qtp="$(echo ${single_net}|cut -sd';' -f5)"
# set default network driver
net_drv=${net_drv:-rtl8139}
# check for mac address
if [ -z "${net_mac}" ]; then
local _random_mac=true
# set random mac address
net_mac="$(printf '56:6E:17:00:%02X:%02X\n' \
$(($(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -d' ' -f1) % 256)) \
$(($(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -d' ' -f1) % 256)))"
fi
case ${net_typ} in
tap)
# if a tap device is set, set BRDEV to the first bridge device to get
# a ip for spice/vnc remote access
if ! ${_net_tap_set}; then
_net_tap_set=true
BRDEV=${net_brd}
fi
# check bridge device
if [ -n "${net_brd}" ]; then
if ! [ -d /sys/class/net/${net_brd}/bridge/ ]; then
echo "${net_brd} is not a bridge device. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
else
echo "No bridge device setup! ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
# check for max_vms
if [ ${_net_id} -ge ${MAX_QTAP} ]; then
echo "Maximum number of network connections reached. Please increase the MAX_QTAP variable." | (logerr)
_dry_run_check || return 1
fi
# user defined interfaces
if [ -n "${net_qtp}" ]; then
# check if interface exists, other create it (NET_INT)
if ! [ -f /sys/class/net/${net_qtp}/tun_flags ]; then
NET_INT+=( ${net_brd},${net_qtp} )
else
# add the Interface net list
if [ "$(cat /sys/class/net/${net_qtp}/operstate)" = "up" ]; then
echo "\"${net_qtp}\" already used. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
else
echo "${net_qtp} " >> ${VMNET}
fi
fi
if ${_random_mac}; then
echo "MAC address \"${net_mac}\" for ${net_qtp} is random and will be lost after shutdown" | (logwarn)
fi
VM_NET="${VM_NET} \
-netdev tap,id="${VMNAME}_${_net_id}",ifname=${net_qtp},script=no,downscript=no${_vhost_set} \
-device ${net_drv},netdev="${VMNAME}_${_net_id}",mac=${net_mac}"
# random interfaces
else
# get the id nummber for qtap...
while (${BRIDGE} link | grep -Eoq qtap${_net_id}); do
_net_id=$(expr ${_net_id} + 1)
done
if ${_random_mac}; then
echo "MAC address \"${net_mac}\" for qtap${_net_id} is random and will be lost after shutdown" | (logwarn)
fi
VM_NET="${VM_NET} \
-netdev tap,id="${VMNAME}_${_net_id}",ifname=qtap${_net_id},script=no,downscript=no${_vhost_set} \
-device ${net_drv},netdev="${VMNAME}_${_net_id}",mac=${net_mac}"
# save interfaces to create later
NET_INT+=( ${net_brd},qtap${_net_id} )
fi
;;
user)
if (${_net_user_set}); then
echo "Network typ user can only be used once. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
_net_user_set=true
if ${_random_mac}; then
echo "MAC address \"${net_mac}\" for usernet is random and will be lost after shutdown" | (logwarn)
fi
VM_NET="${VM_NET} \
-netdev user,id=${VMNAME}_${_net_id} \
-device ${net_drv},netdev="${VMNAME}_${_net_id}",mac=${net_mac}"
;;
bridge)
# if a tap device is set, set BRDEV to the first bridge device to get
# a ip for spice/vnc remote access
if ! ${_net_tap_set}; then
_net_tap_set=true
BRDEV=${net_brd}
fi
# check the mac address
if ${_random_mac}; then
echo "MAC address \"${net_mac}\" for qtap${_net_id} is random and will be lost after shutdown" | (logwarn)
fi
# check bridge device
if [ -n "${net_brd}" ]; then
if ! [ -d /sys/class/net/${net_brd}/bridge/ ]; then
echo "${net_brd} is not a bridge device. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
else
echo "No bridge device setup! ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
VM_NET="${VM_NET} \
-netdev bridge,br=${net_brd},id=${VMNAME}_${_net_id} \
-device ${net_drv},netdev=${VMNAME}_${_net_id},mac=${net_mac}"
;;
*)
echo "Unknow Network Typ. Network will be disabled" | (logwarn)
VM_NET="${VM_NET} -net none"
continue
;;
esac
_net_id=$(expr ${_net_id} + 1)
done
else
VM_NET="-net none"
fi
}
_cfg_9p_check() {
# 9P directory checks
declare -a _tmp_vm_9pshare=("${VM_9P_SHARE}" "${VM_9P_SHARE0}" "${VM_9P_SHARE1}" "${VM_9P_SHARE2}" "${VM_9P_SHARE3}" \
"${VM_9P_SHARE4}" "${VM_9P_SHARE5}" "${VM_9P_SHARE6}" "${VM_9P_SHARE7}" "${VM_9P_SHARE8}" "${VM_9P_SHARE9}")
declare -a _vm_9pshare
local i
for i in "${_tmp_vm_9pshare[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_9pshare+=("${i}")
done
local _9p_id=0
if [ -n "${_vm_9pshare}" ]; then
for single_9p in "${_vm_9pshare[@]}"; do
local share_name="$(echo ${single_9p}|cut -d';' -f1)"
local share_path="$(echo ${single_9p}|cut -sd';' -f2)"
local share_secm="$(echo ${single_9p}|cut -sd';' -f3)"
local share_read="$(echo ${single_9p}|cut -sd';' -f4)"
if [ -z ${share_name} ] || ! [ -e ${share_path} ]; then
echo "No name given or Path does not exist. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
# default security model none
share_secm=${share_secm:-none}
share_read=${share_read:-off}
VM_9P="${VM_9P} \
-fsdev local,id="${VMNAME}_9p_${_9p_id}",path="${share_path}",security_model="${share_secm}",readonly="${share_read}" \
-device virtio-9p-pci,fsdev="${VMNAME}_9p_${_9p_id}",mount_tag="${share_name}""
# print out 9p settings
echo "Adding 9P mount-tag: \"${share_name}\" sharing \"${share_path}\" with security \"${share_secm}\"" | (loginfo)
_9p_id=$(expr ${_9p_id} + 1)
done
fi
}
_cfg_vfsd_check() {
# virtiofsd directory checks
declare -a _tmp_vm_vfsdshare=("${VM_VFSD_SHARE}" "${VM_VFSD_SHARE0}" "${VM_VFSD_SHARE1}" "${VM_VFSD_SHARE2}" "${VM_VFSD_SHARE3}" \
"${VM_VFSD_SHARE4}" "${VM_VFSD_SHARE5}" "${VM_VFSD_SHARE6}" "${VM_VFSD_SHARE7}" "${VM_VFSD_SHARE8}" "${VM_VFSD_SHARE9}")
declare -a _vm_vfsdshare
local i
for i in "${_tmp_vm_vfsdshare[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_vfsdshare+=("${i}")
done
local _vfsd_id=0
if [ -n "${_vm_vfsdshare}" ]; then
for single_vfsd in "${_vm_vfsdshare[@]}"; do
local share_name="$(echo ${single_vfsd}|cut -d';' -f1)"
local share_path="$(echo ${single_vfsd}|cut -sd';' -f2)"
local share_cache="$(echo ${single_vfsd}|cut -sd';' -f3)"
if [ -z ${share_name} ] || ! [ -e ${share_path} ]; then
echo "No name given or Path does not exist. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
# default settings
share_cache=${share_cache:-auto}
VM_VFSD="${VM_VFSD} \
-chardev socket,id="${VMNAME}_vfsd_${_vfsd_id}",path=${VMVFSDPID}${share_name} \
-device vhost-user-fs-pci,queue-size=1024,chardev="${VMNAME}_vfsd_${_vfsd_id}",tag=${share_name}"
VM_VFSD_START+=( "--socket-path=${VMVFSDPID}${share_name} --shared-dir ${share_path} --cache ${share_cache} --syslog" )
# print out virtiofsd settings
echo "Adding virtiofsd mount-tag: \"${share_name}\" sharing \"${share_path}\"" | (loginfo)
_vfsd_id=$(expr ${_vfsd_id} + 1)
done
fi
}
_cfg_harddisk_check() {
# image checks/default settings
declare -a _tmp_vm_harddisk=("${VM_HARDDISK}" "${VM_HARDDISK0}" "${VM_HARDDISK1}" "${VM_HARDDISK2}" "${VM_HARDDISK3}" \
"${VM_HARDDISK4}" "${VM_HARDDISK5}" "${VM_HARDDISK6}" "${VM_HARDDISK7}" "${VM_HARDDISK8}" "${VM_HARDDISK9}")
declare -a _vm_harddisk
local i
for i in "${_tmp_vm_harddisk[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_harddisk+=("${i}")
done
if [ -n "${_vm_harddisk}" ]; then
for single_hd in "${_vm_harddisk[@]}"; do
local hd_path="$(echo ${single_hd}|cut -d';' -f1)"
local hd_if="$(echo ${single_hd}|cut -sd';' -f2)"
local hd_cache="$(echo ${single_hd}|cut -sd';' -f3)"
local hd_aio="$(echo ${single_hd}|cut -sd';' -f4)"
if [ -z "${hd_path}" ] || ! [ -e "${hd_path}" ]; then
echo "${hd_path} not found. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
# check if image is already running
if $(${LSOF} -c qemu 2>/dev/null | grep -q ${hd_path} >/dev/null); then
echo "\"${VMNAME}\" is already running!" | (logerr)
_dry_run_check || return 1
fi
# set default settings for hd if nothing is set
hd_cache=${hd_cache:-writethrough}
hd_if=${hd_if:-ide}
hd_aio=${hd_aio:-threads}
# set image format to raw if lvm partition is used or file extensions end with "raw"
[ -b "${hd_path}" ] && hd_path="${hd_path},format=raw"
[ "${hd_path##*.}" = "raw" ] && hd_path="${hd_path},format=raw"
VM_HD="${VM_HD} \
-drive file=${hd_path},if=${hd_if},cache=${hd_cache},aio=${hd_aio}"
done
fi
}
_cfg_remote_access_check() {
# get interface ip
if ${_net_tap_set}; then
local local_ip="$(${IP} address show ${BRDEV} \
| grep -Eo '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' \
| head -n1)"
else
local local_ip="0.0.0.0"
fi
# remote access spice
VM_ENABLE_SPICE="$(echo ${VM_REMOTE_ACCESS_SPICE:-false}|cut -d';' -f1)"
VM_ENABLE_VNC="$(echo ${VM_REMOTE_ACCESS_VNC:-false}|cut -d';' -f1)"
if ${VM_ENABLE_SPICE}; then
spice_password="$(echo ${VM_REMOTE_ACCESS_SPICE}|cut -sd';' -f2)"
spice_ip="$(echo ${VM_REMOTE_ACCESS_SPICE}|cut -sd';' -f3)"
[ -z "${spice_ip}" ] && spice_ip="${local_ip}"
spice_port="$(echo ${VM_REMOTE_ACCESS_SPICE}|cut -sd';' -f4)"
spice_port="$(_check_remote_access_port 580 "${spice_ip}" "${spice_port}")"
echo "Spice access on ${spice_ip}:${spice_port} (Password: \"${spice_password}\")" | (loginfo)
fi
if ${VM_ENABLE_VNC}; then
vnc_password="$(echo ${VM_REMOTE_ACCESS_VNC}|cut -sd';' -f2)"
vnc_ip="$(echo ${VM_REMOTE_ACCESS_VNC}|cut -sd';' -f3)"
[ -z "${vnc_ip}" ] && vnc_ip="${local_ip}"
vnc_port="$(echo ${VM_REMOTE_ACCESS_VNC}|cut -sd';' -f4)"
vnc_port="$(expr $(_check_remote_access_port 590 "${vnc_ip}" "${vnc_port}") - 5900)"
echo " VNC access on ${vnc_ip}:$(expr ${vnc_port} + 5900) (Password: \"${vnc_password}\")" | (loginfo)
fi
if ! ${VM_ENABLE_SPICE} && ! ${VM_ENABLE_VNC} && [ "${VM_DISPLAY_MODE}" = "none" ] && ${VM_ENABLE_DAEMONIZE}; then
echo "No remote access or sdl enabled. At least one of them needs to be enabled!" | (logerr)
_dry_run_check || return 1
fi
}
_cfg_audio_check() {
declare -a _tmp_vm_audio=("${VM_AUDIO}" "${VM_AUDIO0}" "${VM_AUDIO1}" "${VM_AUDIO2}" "${VM_AUDIO3}" \
"${VM_AUDIO4}" "${VM_AUDIO5}" "${VM_AUDIO6}" "${VM_AUDIO7}" "${VM_AUDIO8}" "${VM_AUDIO9}")
declare -a _vm_audio
local i
for i in "${_tmp_vm_audio[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_audio+=("${i}")
done
local _audio_id=0
if [ -n "${_vm_audio}" ]; then
for single_audio in "${_vm_audio[@]}"; do
local audio_dev="$(echo ${single_audio}|cut -d';' -f1)"
local audio_hw="$(echo ${single_audio}|cut -sd';' -f2)"
if [ -z "${audio_dev}" ]; then
audio_dev="-audiodev sdl,id=${VMNAME}_s_${_audio_id},driver=sdl"
else
case ${audio_dev} in
sdl|alsa|oss|none)
audio_dev="-audiodev ${audio_dev},id=${VMNAME}_s_${_audio_id},driver=${audio_dev}"
;;
*)
# not supported
echo "Audiodev \"${audio_dev}\" is not supported. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
;;
esac
fi
if [ -z "${audio_hw}" ]; then
audio_hw="-device ich9-intel-hda -device hda-output,audiodev=${VMNAME}_s_${_audio_id}"
else
case "${audio_hw}" in
ich9-intel-hda)
audio_hw="-device ${audio_hw} -device hda-output,audiodev=${VMNAME}_s_${_audio_id}"
;;
usb-audio)
audio_hw="-device ${audio_hw},audiodev=${VMNAME}_s_${_audio_id},multi=on"
;;
*)
audio_hw="-device ${audio_hw},audiodev=${VMNAME}_s_${_audio_id}"
;;
esac
fi
VM_SOUND="${VM_SOUND} ${audio_dev} ${audio_hw}"
_audio_id=$(expr ${_audio_id} + 1)
done
fi
}
_cfgcheck(){
# default settings
VM_ENABLE_GL=$(echo ${VM_ENABLE_GL:-false})
VM_DISPLAY_MODE=$(echo ${VM_DISPLAY_MODE:-none})
declare -a _tmp_vm_dev=("${VM_DEVICES}" "${VM_DEVICES0}" "${VM_DEVICES1}" "${VM_DEVICES2}" "${VM_DEVICES3}" \
"${VM_DEVICES4}" "${VM_DEVICES5}" "${VM_DEVICES6}" "${VM_DEVICES7}" "${VM_DEVICES8}" "${VM_DEVICES9}")
declare -a _vm_dev
local i
for i in "${_tmp_vm_dev[@]}"; do
if [ -z "${i}" ]; then
continue
fi
_vm_dev+=("${i}")
done
# directly add all vm_devices additions to the config. Nothing to check since simply anything can be added.
for single_dev in "${_vm_dev[@]}"; do
VM_DEV="${VM_DEV} ${single_dev}"
done
if (${VM_ENABLE_SNAPSHOTMODE:-false}); then
echo "SNAPSHOT MODE! Changes to the guest system won't be saved!" | (logwarn)
fi
# enable OpenGL only for the virtio display driver
if ${VM_ENABLE_GL}; then
if ! [ "${VM_VGA}" = "virtio" ] && ! [ "${VM_DISPLAY_MODE}" = "none" ]; then
echo "Virtio GPU-Driver and SDL/GTK must be enabled for OpenGL" | (logwarn)
VM_ENABLE_GL=false
fi
fi
# basic tests/settings
#
# if script runs as root respect VM_USER setting from config file
# otherwise use current running user
SET_USER="true"
if [ $(id -u) = 0 ]; then
if [ -n "${VM_USER}" ]; then
if ! $(getent passwd ${VM_USER} >/dev/null 2>&1); then
echo "User \"${VM_USER}\" not found. User set to nobody" | (logwarn)
VM_USER="nobody"
VM_ENABLE_GL=false
VM_DISPLAY_MODE="none"
fi
else
echo "No user set. User nobody will be used" | (logwarn)
VM_USER="nobody"
VM_ENABLE_GL=false
VM_DISPLAY_MODE="none"
fi
else
# if run as non root, set SET_USER to false to avoid using -runas
SET_USER="false"
fi
# check if SDL can be enabled (needs .Xauthority file)
_check_xauth() {
if [ "${VM_DISPLAY_MODE}" == "sdl" ]; then
if ! [ -e /home/${1}/.Xauthority ]; then
echo "No Xauthority file for user \"${1}\". Disabling SDL output. You might want to use GTK instead" | (logwarn)
VM_ENABLE_GL=false
VM_DISPLAY_MODE="none"
fi
fi
}
if [ $(id -u) = 0 ]; then
_check_xauth "${VM_USER}"
else
_check_xauth "$(id -un)"
fi
# guests can't boot from other devices if vm_kernel/vm_initrd is used
if [ -n "${VM_KERNEL}" ] && [ -n "${VM_INITRD}" ]; then
VM_BOOT_DEV=""
else
VM_KERNEL=""
fi
# guest wont start with "host" cpu if kvm is disabled
if [ "${VM_CPU}" = "host" ] && [ -z "${VM_ACCEL}" ]; then
VM_ACCEL="kvm"
fi
# cdrom checks
if [ -n "${VM_CDROM}" ]; then
if ! [ -e "${VM_CDROM}" ]; then
echo "Image dosn't exist. ${DEFAULT_MSG}" | (logerr)
_dry_run_check || return 1
fi
fi
_cfg_harddisk_check || return 1
_cfg_network_check || return 1
_cfg_9p_check || return 1
_cfg_vfsd_check || return 1
_cfg_remote_access_check || return 1
_cfg_audio_check || return 1
}
_myconfig(){
# basic config
myconfig=(
-name ${VMNAME}
-monitor unix:${VMSOCK},server=on,wait=off
-pidfile ${VMPID}
-device virtio-balloon
-m ${VM_MEMORY:-1G}
-smp ${VM_SMP:-2}
-machine ${VM_MACHINE:-pc}
-k ${VM_LC:-de}
-cpu ${VM_CPU:-host}
-accel ${VM_ACCEL:-kvm}
)
# set runas user
${SET_USER} && myconfig+=(-runas ${VM_USER})
if [ -n "${VM_KERNEL}" ]; then
myconfig+=(
-kernel ${VM_KERNEL}
-initrd ${VM_INITRD}
)
[ -n "${VM_APPEND}" ] && myconfig+=(-append "${VM_APPEND}")
fi
# enable/disable usb support
(${VM_ENABLE_USB:-true}) && myconfig+=(-usb)
# daemonize true/false
(${VM_ENABLE_DAEMONIZE:-true}) && myconfig+=(-daemonize)
# snapshotmode on/off
(${VM_ENABLE_SNAPSHOTMODE:-false}) && myconfig+=(-snapshot)
# spice virtual serial port (copy/paste)
# there are two possiblities for clipboard sharing
# qemu-vdagent and spicevmc
# if display mode is set to gtk we choose to use qemu-vdagent for clipboard
# sharing. this however disables it for spice connections
if $(echo ${VM_DISPLAY_MODE}| grep -q ^gtk); then
(${VM_ENABLE_VIRTSERIAL:-true}) && myconfig+=(
-chardev qemu-vdagent,id=vdagent,name=vdagent,clipboard=on,mouse=off \
-device virtio-serial,max_ports=2 \
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0
)
else
(${VM_ENABLE_VIRTSERIAL:-true}) && myconfig+=(
-chardev spicevmc,id=vdagent,name=vdagent
-device virtio-serial
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0
)
fi
# qemu guest agent (qga)
(${VM_ENABLE_QGA:-false}) && myconfig+=(
-device virtio-serial
-chardev socket,id=${VMNAME}-qga,path=${VMQGASOCK},server=on,wait=off
-device virtserialport,chardev=${VMNAME}-qga,name=org.qemu.guest_agent.0
)
# qemu machine protocol (qmp)
(${VM_ENABLE_QMP:-false}) && myconfig+=(
-chardev socket,id=${VMNAME}-qmp,path=${VMQMPSOCK},server=on,wait=off
-mon chardev=${VMNAME}-qmp,mode=control,pretty=on
)
[ -n "${VM_VFSD}" ] && VM_ENABLE_VPMEM=true
# virtio pmem device
(${VM_ENABLE_VPMEM:-false}) && myconfig+=(
-object memory-backend-file,id=mem1,size=${VM_MEMORY:-1G},mem-path=/dev/shm/,share=on
-numa node,memdev=mem1
)
# enable pcie port
local _pcie_ports="${VM_PCIE_PORTS:-0}"
while [ ${_pcie_ports} -gt 0 ]; do
myconfig+=(
-device pcie-root-port,id=pcie-${_pcie_ports},slot=${_pcie_ports}
)
_pcie_ports=$(expr ${_pcie_ports} - 1)
done
# sdl/gtk output
if ! [ ${VM_DISPLAY_MODE} = "none" ]; then
case ${VM_DISPLAY_MODE} in
gtk*|sdl*)
if (${VM_ENABLE_GL}); then
myconfig+=(-display ${VM_DISPLAY_MODE},gl=on)
else
myconfig+=(-display ${VM_DISPLAY_MODE})
fi
;;
*)
echo "Error: ${VM_DISPLAY_MODE} not supported" | (logerr)
exit 1
;;
esac
fi
# enable (multiple) usb redir devices
local _usb_redir="${VM_USB_REDIR:-0}"
while [ ${_usb_redir} -gt 0 ]; do
myconfig+=(
-chardev spicevmc,name=usbredir,id=usbredirchardev_${_usb_redir}
-device usb-redir,chardev=usbredirchardev_${_usb_redir},id=usbredirdev_${_usb_redir},debug=3
)
_usb_redir=$(expr ${_usb_redir} - 1)
done
# remote access settings
# passwords are set after a vm started
if ${VM_ENABLE_SPICE}; then
if [ -z "${spice_password}" ]; then
myconfig+=(-spice port=${spice_port},addr=${spice_ip},disable-ticketing=on)
else
myconfig+=(-spice port=${spice_port},addr=${spice_ip})
fi
fi
if ${VM_ENABLE_VNC}; then
if [ -n "${vnc_password}" ]; then
myconfig+=(-vnc ${vnc_ip}:${vnc_port},password)
else
myconfig+=(-vnc ${vnc_ip}:${vnc_port})
fi
fi
if ! ${VM_ENABLE_SPICE} && ! ${VM_ENABLE_VNC} && ! ${VM_ENABLE_DAEMONIZE}; then
! [ ${VM_DISPLAY_MODE} = "none" ] || myconfig+=(-nographic)
fi
# enable/disable harddisks
[ -z "${VM_HD}" ] || myconfig+=(${VM_HD})
# network settings
[ -z "${VM_NET}" ] || myconfig+=(${VM_NET})
# 9p shares
[ -z "${VM_9P}" ] || myconfig+=(${VM_9P})
# virtiofsd shares
[ -z "${VM_VFSD}" ] || myconfig+=(${VM_VFSD})
# display settings
[ -z "${VM_VGA}" ] || myconfig+=(-vga ${VM_VGA})
# audio settings
[ -z "${VM_SOUND}" ] || myconfig+=(${VM_SOUND})
# other devices
[ -z "${VM_DEV}" ] || myconfig+=(${VM_DEV})
# cdrom settings
[ -z "${VM_CDROM}" ] || myconfig+=(-cdrom ${VM_CDROM})
if [ -n "${VM_BOOT_DEV}" ]; then
case ${VM_BOOT_DEV} in
floppy|a) myconfig+=(-boot a) ;;
harddisk|c) myconfig+=(-boot c) ;;
cdrom|d) myconfig+=(-boot d) ;;
network|n) myconfig+=(-boot n) ;;
esac
fi
# add qvm parameters (not in configfiles)
# used to override default values whten starting vms
[ -z "${VM_PARAMETER}" ] || myconfig+=(${VM_PARAMETER})
}
# check if a given vm is running and exit if not
_check_running_vm() {
# if the executing user is not root we only see vm's of the user since the PID
# files for system vm's are at different place and usually not readable for
# users
if ! [ -f "${VMPID}" ]; then
echo "\"${VMNAME}\" not running!" | (loginfo)
exit 1
fi
}
# check if qemu guest agent where enabled for vm
_check_qga(){
if $(${QGA_AVAILABLE}); then
if [ -e "${VMQGASOCK}" ]; then
return 0
else
echo "QEMU guest agent not active for ${VMNAME}" | (logwarn)
return 1
fi
else
echo "QEMU guest agent not available. Install app-emulation/qemu[python]" | (logwarn)
return 1
fi
}
vm_list() {
if [ -n "${1}" ]; then
_check_running_vm
fi
if [ -z "${VMNAME}" ]; then
echo "System VMs:"
[ -f /etc/conf.d/kvm ] && source /etc/conf.d/kvm
[ -z "${CONF_PATH}" ] && CONF_PATH="/etc/qvm/"
if [ -d ${CONF_PATH} ]; then
for i in $(find ${CONF_PATH} -type f); do
if [ -e /etc/init.d/kvm.${i##*/} ]; then
if [ -r "/run/kvm/${i##*/}.pid" ]; then
echo "${i##*/} (PID: $(cat /run/kvm/${i##*/}.pid))" | (loginfo)
else
if [ -e "/run/kvm/${i##*/}.pid" ]; then
echo "${i##*/} (PID: -)" | (loginfo)
else
echo "${i##*/}" | (logerr)
fi
fi
fi
done
fi
if [ -n "${CFG_DIR}" ]; then
echo "User VMs:"
for i in $(find ${CFG_DIR} -type f); do
if [ -e ${PID_DIR}/${i##*/}.qvm.pid ]; then
echo "${i##*/} (PID: $(cat ${PID_DIR}/${i##*/}.qvm.pid))" | (loginfo)
else
echo "${i##*/}" | (logerr)
fi
done
fi
else
if [ -e "${VMPID}" ]; then
local _vm_pid="$(cat /${VMPID})"
local _vm_starttime="$(ps -o "lstart=" -p ${_vm_pid})"
local _vm_uptime="$(ps -o "etime=" -p ${_vm_pid}|tr -d '[:space:]')"
local _vm_spice_settings="$(echo "info spice" \
| ${NC} -U -q1 ${VMSOCK} 2>&1 \
| grep --only-matching --perl-regex "(?<=address\:).*[0-9]" \
| head -n1|tr -d '[:space:]')"
# don't include the 4 in the regex so the result won't include: (ipv4)
local _vm_vnc_settings="$(echo "info vnc" \
| ${NC} -U -q1 ${VMSOCK} 2>&1 \
| grep --only-matching --perl-regex "(?<=Server\:).*[1,2,3,5,6,7,8,9,0]" \
| head -n1|tr -d '[:space:]')"
echo "Details of ${VMNAME}:"
echo -e " VM started at:\t\t${_vm_starttime}"
echo -e " Uptime:\t\t${_vm_uptime}"
echo -e " PID:\t\t\t${_vm_pid}"
echo -e " Spice:\t\t\t${_vm_spice_settings}"
echo -e " VNC:\t\t\t${_vm_vnc_settings}"
# TODO: info about networks -> echo "info network" | nc -U -q1 *.sock
fi
fi
}
vm_update() {
local _setting="${1}"
local _value="${2}"
if [ -z "${VMNAME}" ] || [ -z "${_setting}" ] || [ -z "${_value}" ]; then
echo "missing var: please make sure you passed [vmname] [setting] [value]. see: qvm help" | (logerr)
exit 1
fi
_check_running_vm
case ${_setting} in
mem|memory)
echo "Changing Memory to ${_value}MB for ${VMNAME}" | (loginfo)
_vm_edit balloon ${_value}
;;
key|sendkey)
echo "Sending key combination: ${_value}" | (loginfo)
_vm_edit sendkey ${_value}
;;
vnc)
echo "Changing VNC password to ${_value}" | (loginfo)
_vm_edit "set_password vnc" ${_value}
;;
spice)
echo "Changing SPICE password to ${_value}" | (loginfo)
_vm_edit "set_password spice" ${_value}
;;
esac
}
vm_snapshot() {
local _setting="${1}" # create | delete | list | load
local _value="${2}" # optional
if [ -z "${VMNAME}" ] || [ -z "${_setting}" ]; then
echo "missing var: please make sure you passed at least [vmname] and [setting]. see: qvm help" | (logerr)
exit 1
fi
_check_running_vm
case ${_setting} in
create)
if [ -n "${_value}" ]; then
echo "Creating snapshot ${_value} for ${VMNAME}" | (loginfo)
else
echo "Creating snapshot with default tag for ${VMNAME}" | (loginfo)
fi
_vm_edit savevm ${_value}
;;
delete)
if [ -z "${_value}" ]; then
echo "missing snapshot tag. see: qvm help" | (logerr)
exit 1
else
echo "Deleting snapshot: ${_value}" | (loginfo)
_vm_edit delvm ${_value}
fi
;;
list)
echo "Listing available snapshots of ${VMNAME}" | (loginfo)
_vm_edit_snapshot_output "info snapshots"
;;
load)
if [ -z "${_value}" ]; then
echo "missing snapshot tag. see: qvm help" | (logerr)
exit 1
else
echo "loading snapshot: ${_value}" | (loginfo)
_vm_edit loadvm ${_value}
fi
;;
esac
}
vm_hw(){
local _setting="${1}" # add | remove | list
local _type="${2}" # type network|harddisk
local _value1="${3}" # value 1
if [ -z "${VMNAME}" ] || [ -z "${_setting}" ]; then
echo "missing var: please make sure you passed [vmname], [setting]. see: qvm help" | (logerr)
exit 1
fi
_check_running_vm
if ! [ -e "${VMPCIM}" ]; then
echo "You need to enable PCIE root bus. Check config" | (logerr)
exit 1
else
local _pcie_ports_count="$(tr -d -c '|\n' < "${VMPCIM}" | awk '{ print length; }')"
fi
case ${_setting} in
list)
local _id="${_pcie_ports_count}"
while [ ${_id} -gt 0 ]; do
if [ -n "$(cut -d'|' -f${_id} "${VMPCIM}")" ]; then
echo "PCIE ID ${_id}: $(cut -d'|' -f${_id} "${VMPCIM}")" | (loginfo)
fi
_id=$(expr ${_id} - 1)
done
;;
add)
local _id="${_pcie_ports_count}"
while [ ${_id} -gt 0 ]; do
if [ -z "$(cut -d'|' -f${_id} "${VMPCIM}")" ]; then
local _free_port="${_id}"
break
fi
_id=$(expr ${_id} - 1)
done
if [ "${_id}" = "0" ]; then
echo "Error: No free PCI ports available. Add more or remove others" | (logerr)
exit 1
fi
case ${_type} in
network)
echo "adding additional pci network device"
if [ -z "${_value1}" ]; then
echo "Error: Need bridge interface" | (logerr)
exit 1
fi
#value1=bridge interface
_vm_edit netdev_add "bridge,br=${_value1},id=net${_id}"
_vm_edit device_add "virtio-net-pci,netdev=net${_id},bus=pcie-${_id},id=net-${_id}"
awk -v "id=${_id}" -v "var=virtio-net-pci,${_value1}" -i inplace 'BEGIN{FS=OFS="|"} {if (NR==1) {$id = var }; print}' ${VMPCIM}
;;
harddisk)
echo "Adding additonal PCI harddisk (${_value1})" | (loginfo)
local _format="${_value1##*.}"
if [ -z ${_value1} ]; then
echo "Error: You need to provide a harddisk file."
exit 1
fi
if ! [ -f ${_value1} ]; then
if ! [ -f ${IMG_DIR}/${_value1} ]; then
echo "Error: ${_value1} is not a regular file" | (logerr)
exit 1
else
_value1="${IMG_DIR}/${_value1}"
fi
fi
#value1=file
_vm_edit "drive_add 0" "if=none,file=${_value1},format=${_format},id=disk${_id}"
_vm_edit device_add "virtio-blk-pci,drive=disk${_id},bus=pcie-${_id},id=disk-${_id}"
awk -v "id=${_id}" -v "var=virtio-blk-pci,${_value1}" -i inplace 'BEGIN{FS=OFS="|"} {if (NR==1) {$id = var }; print}' ${VMPCIM}
;;
esac
;;
remove)
local _dev="$(cut -d'|' -f${_type:0:1} ${VMPCIM} | cut -d ',' -f1)"
if [ -z "${_dev}" ]; then
echo "${_type}: doesn't exist: Nothing was removed" | (logwarn)
else