-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickemu-mod
executable file
·3649 lines (2534 loc) · 106 KB
/
quickemu-mod
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
## Copyright (c) Alex Genovese https://github.com/TuxVinyards
# licence: GPL3 https://www.gnu.org/licenses
## A modified version of the 'quickemu' frontend for 'qemu'
# that provides a text menu interface & more modifiable qemu settings
# https://github.com/quickemu-project/quickemu https://gitlab.com/qemu-project/qemu
# Users should install 'quickemu' and may set up Virtual Machines as normal.
# Q-Mod may be installed alongside quickemu in /usr/bin & run by typing 'q-mod' or 'quickemu-mod'
# Or renamed as quickemu & used as a replacement for the standard one.
# The script may be placed elsewhere, including your desktop or the main VM folder where the vm.conf files are located.
# Click on this script to run it, or open an in-folder terminal and use './quickemu-mod' (make sure it has execute permissions)
## Shellcheck: To allow easier editing the following lines suppress multiple warnings inherited from original quickemu,
# particularly from the 'args' array structure. These may be commented out thus "# shellcheck disable= # SC2140,SC2054,SC2206,SC2207"
# shellcheck disable=SC2140,SC2054,SC2206,SC2207
# https://www.shellcheck.net/wiki/SC2242 unfortunately clashes with use of exit traps
# used to keep mouse click scripts open, so needs to be added
# shellcheck disable=SC2242
VERSION="4.4.mod"
## Some sections of 'quickemu' will have been modified for readability, others may be as-is ....
# For as-is code taken directly from the quickemu repository, the MIT licence may apply.
# See accompanying LICENCE text file.
# If adding code, please refer to http://mywiki.wooledge.org/BashGuide/Practices#Readability
# API
# Use --mod.menu "path/folder" "file.conf" for direct calls that bypass of the array selector.
# Or --mod.conf "file.conf" for .conf files in the default VM folder
## SETTINGS
# For details, see notes at start of settings file
# Use full quoted path & name if in different folder to this script or different to a default
CurrentFolder="$(pwd)"
if [[ -e "$CurrentFolder/qmod_settings" ]] && [[ "$CurrentFolder" != '/usr'* ]]; then
Q_Mod_SettingsFile="$CurrentFolder/qmod_settings"
elif [[ -e "$CurrentFolder/.qmod_settings" ]] && [[ "$CurrentFolder" != '/usr'* ]]; then
Q_Mod_SettingsFile="$CurrentFolder/.qmod_settings"
elif [[ -e "/$HOME/.qmod_settings" ]] ; then
Q_Mod_SettingsFile="/$HOME/.qmod_settings"
elif [[ -e "/$HOME/qmod_settings" ]] ; then
Q_Mod_SettingsFile="/$HOME/qmod_settings"
else
Q_Mod_SettingsFile=
fi
# Declare global associative VM ARRAY
declare -gA VM_Array=()
# Load Settings & Array Presets (flag QMOD_1 Version 1 has the Windows Recipes)
if [[ -e "$Q_Mod_SettingsFile" ]]; then
# shellcheck disable=SC1090
. "$Q_Mod_SettingsFile" QMOD
else
printf "\n\n ERROR 'qmod_settings' file not found \n\n Re-check Installation &/or Installation instructions \n\n"
fi
# Make sure that 'Default_VM_Folder' has a value
[[ ! $Default_VM_Folder ]] && Default_VM_Folder="$CurrentFolder"
## Make sure shell is set during session to decimal separator of dot
# LC_ALL=C changes too much, just set the numeric.
# See locale setting discussion: https://unix.stackexchange.com/a/149129
# Also https://unix.stackexchange.com/questions/62316/why-is-there-no-euro-english-locale?rq=1
# & http://www.unicode.org/L2/L2001/01102-POSIX15897.htm
export "LC_NUMERIC=C"
export "LC_COLLATE=C"
## MOD Standard Quickemu checks for '< 4' which at 2022/3 now needs a bump.
# Some ver 5 script is present in the standard release too ...
# More Version 5 style scripting should be used:
# See http://mywiki.wooledge.org/BashGuide/Practices#Choose_Your_Shell
if ((BASH_VERSINFO[0] < 5)); then
echo "Sorry, you need Bash 5.0 or newer to run this script."
exit 1
fi
LAUNCHER="$(basename "$0")"
ModName="$LAUNCHER"
DISK_MIN_SIZE=$((197632 * 8))
QEMU=$(command -v qemu-system-x86_64)
QEMU_IMG=$(command -v qemu-img)
if [ ! -e "${QEMU}" ] || [ ! -e "${QEMU_IMG}" ]; then
echo "ERROR! QEMU not found. Please make install qemu-system-x86_64 and qemu-img"
exit 1
fi
show_qe_api_usage () {
## quickemu-mod itself doesn't have a direct API, as does std quickemu
# q-mod routes the standard API into quickemu as a built in function
# BUT the modding has been tweaked so that the standard methods can still be used (by quickgui for example ??)
# Use this table for reference. Some items have been modded out.
# The parameter '--help' has been added for original quickemu use as command without parameters now routes to the menu system
printColor "\n\n Command line API Usage: "
printf "%s --vm vm-name.conf [--path \"/path/to/vm/folder\"] " "$LAUNCHER"
printf "\n\n Replace '--vm' with '--vm-menu' to use menu mode. Optional parameter '--path' may be used."
printf "\n\n The starting terminal should be located in the VM folder or a path should be supplied. "
printf "\n\n\n QuickEmu-Mod may be started in-situ in a folder terminal: ./quickmod-mod or by mouse click"
printf "\n\n It can be placed in 'usr/bin' alongside original 'quickemu' or renamed there to 'quickemu' to work as a "
printf "\n\n drop in replacement. QMOD HAS SOME NEW ADDED COMMANDS & WILL ACCEPT MOST OF THE ORIGINAL ONES."
printf "\n\n\n Non-Menu Mode additional optional parameters: \n\n"
read -rp " [enter] to continue > "
# Erase text and adjust cursor: "\e[1A\r" "\e[2K\r" etc See https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
printf "\e[1A\r"
# echo " --shortcut : Create a desktop shortcut" # Not needed with q-mod
# echo " --ignore-msrs-always : Always ignore KVM unhandled machine-specific registers" # Too permanent & Code needed improvements.
# : MOD Re-Written as a reversible function 'toggle_msr_defaults'
echo " --version : Show versioning"
echo " --help | -h : Show help"
echo " --path : If the starting terminal is not located in the VM folder, a path should be supplied"
echo " --toggle_msr_defaults : Configure default KVM behaviour for unhandled machine-specific registers"
echo " --braille : Enable braille support. Requires SDL."
echo " --display : Select display backend. 'sdl' (default), 'gtk', 'none', 'spice' or 'spice-app'"
echo " --fullscreen : Starts VM in full screen mode (Ctl+Alt+f to exit)"
echo " --screen <screen> : Use specified screen to determine the window size."
echo " --snapshot apply <tag> : Apply/restore a snapshot."
echo " --snapshot create <tag> : Create a snapshot."
echo " --snapshot delete <tag> : Delete a snapshot."
echo " --snapshot info : Show disk/snapshot info."
echo " --status-quo : Do not commit any changes to disk/snapshot."
echo " --viewer <viewer> : Choose an alternative viewer. @Options: 'spicy' (default), 'remote-viewer', 'none'"
echo " --ssh-port <port> : Set ssh-port manually"
echo " --spice-port <port> : Set spice-port manually"
echo " --public-dir <path> : expose share directory. @Options: '' (default: xdg-user-dir PUBLICSHARE), '<directory>', 'none'"
echo " --monitor <type> : Set monitor connection type. @Options: 'socket' (default), 'telnet', 'none'"
echo " --monitor-telnet-host <ip/host> : Set telnet host for monitor. (default: 'localhost')"
echo " --monitor-telnet-port <port> : Set telnet port for monitor. (default: '4440')"
echo " --monitor-cmd <cmd> : Send command to monitor if available. (Example: system_powerdown)"
echo " --serial <type> : Set serial connection type. @Options: 'socket' (default), 'telnet', 'none'"
echo " --serial-telnet-host <ip/host> : Set telnet host for serial. (default: 'localhost')"
echo " --serial-telnet-port <port> : Set telnet port for serial. (default: '6660')"
echo " --keyboard <type> : Set keyboard. @Options: 'usb' (default), 'ps2', 'virtio'"
echo " --keyboard_layout <layout> : Set keyboard layout."
echo " --mouse <type> : Set mouse. @Options: 'tablet' (default), 'ps2', 'usb', 'virtio'"
echo " --usb-controller <type> : Set usb-controller. @Options: 'ehci' (default), 'xhci', 'none'"
echo " --show_args : Show all the compiled qemu & swmtp arguments as a human readable list, before starting VM"
echo
echo " extra_args : Extra args may be placed in the settings file (MOD) "
# Enabled for use by 'quickgui' : This is not really necessary for Q Mod as its interface is more file manager based. REVIEW
echo " --delete-disk : Delete the disk image and EFI variables"
echo " --delete-vm : Delete the entire VM and it's configuration"
# printf "\n\n\n QuickEmu-Mod may be started in-situ in a folder terminal: ./quickmod-mod or by mouse click"
# printf "\n\n It can be placed in 'usr/bin' alongside original 'quickemu' or renamed there to 'quickemu' to work as a "
# printf "\n\n drop in replacement. QMOD will accept most of the original quickemu commands, as detailed."
[[ $CLI ]] && exit 0
printf "\n\n\n"
}
## Load these two delete functions for quickgui (if substituting quickemu-mod for the standard release... )
function delete_disk() {
if [ -e "${disk_img}" ]; then
rm "${disk_img}"
# Remove any EFI vars, but not for macOS
rm "${VMDIR}"/OVMF_VARS*.fd >/dev/null 2>&1
rm "${VMPATH}/${VMDIR}"/OVMF_VARS*.fd >/dev/null 2>&1
rm "${VMDIR}/${VMNAME}-vars.fd" >/dev/null 2>&1
rm "${VMPATH}/${VMDIR}/${VMNAME}-vars.fd" > /dev/null 2>&1
echo "SUCCESS! Deleted ${disk_img}"
delete_shortcut
else
echo "NOTE! ${disk_img} not found. Doing nothing."
fi
}
function delete_vm() {
if [ -d "${VMDIR}" ]; then
rm -rf "${VMDIR}"
rm "${VM}"
echo "SUCCESS! Deleted ${VM} and ${VMDIR}"
delete_shortcut
else
echo "NOTE! ${VMDIR} not found. Doing nothing."
fi
}
## LOAD other quickemu functions into the quickemu-mod envelope
# Also loads/converts the two main code sections of quickemu as functions: 'quickemu' & 'quickemu-actions'
function snapshot_apply() {
local TAG="${1}"
if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided."
exit
fi
if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -a "${TAG}" "${disk_img}"; then
echo "SUCCESS! Applied snapshot ${TAG} to ${disk_img}"
else
echo "ERROR! Failed to apply snapshot ${TAG} to ${disk_img}"
fi
else
echo "NOTE! ${disk_img} not found. Doing nothing."
fi
}
function snapshot_create() {
local TAG="${1}"
if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided."
exit
fi
if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -c "${TAG}" "${disk_img}"; then
echo "SUCCESS! Created snapshot ${TAG} of ${disk_img}"
else
echo "ERROR! Failed to create snapshot ${TAG} of ${disk_img}"
fi
else
echo "NOTE! ${disk_img} not found. Doing nothing."
fi
}
function snapshot_delete() {
local TAG="${1}"
if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided."
exit
fi
if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -d "${TAG}" "${disk_img}"; then
echo "SUCCESS! Deleted snapshot ${TAG} of ${disk_img}"
else
echo "ERROR! Failed to delete snapshot ${TAG} of ${disk_img}"
fi
else
echo "NOTE! ${disk_img} not found. Doing nothing."
fi
}
function snapshot_info() {
if [ -e "${disk_img}" ]; then
${QEMU_IMG} info "${disk_img}"
fi
}
function get_port() {
local PORT_START=$1
local PORT_RANGE=$((PORT_START+$2))
local PORT
for ((PORT = PORT_START; PORT <= PORT_RANGE; PORT++)); do
# Make sure port scans do not block too long.
timeout 0.1s bash -c "echo >/dev/tcp/127.0.0.1/${PORT}" >/dev/null 2>&1
if [ ${?} -eq 1 ]; then
echo "${PORT}"
break
fi
done
}
function enable_usb_passthrough() {
local DEVICE=""
local USB_BUS=""
local USB_DEV=""
local USB_NAME=""
local VENDOR_ID=""
local PRODUCT_ID=""
local USB_NOT_READY=0
# Have any USB devices been requested for pass-through?
if (( ${#usb_devices[@]} )); then
echo " - USB: Host pass-through requested:"
for DEVICE in "${usb_devices[@]}"; do
VENDOR_ID=$(echo "${DEVICE}" | cut -d':' -f1)
PRODUCT_ID=$(echo "${DEVICE}" | cut -d':' -f2)
USB_BUS=$(lsusb -d "${VENDOR_ID}:${PRODUCT_ID}" | cut -d' ' -f2)
USB_DEV=$(lsusb -d "${VENDOR_ID}:${PRODUCT_ID}" | cut -d' ' -f4 | cut -d':' -f1)
USB_NAME=$(lsusb -d "${VENDOR_ID}:${PRODUCT_ID}" | cut -d' ' -f7-)
if [ -z "${USB_NAME}" ]; then
echo " ! USB device ${VENDOR_ID}:${PRODUCT_ID} not found. Check your configuration"
continue
elif [ -w "/dev/bus/usb/${USB_BUS}/${USB_DEV}" ]; then
echo " o ${USB_NAME} on bus ${USB_BUS} device ${USB_DEV} is accessible."
else
echo " x ${USB_NAME} on bus ${USB_BUS} device ${USB_DEV} needs permission changes:"
echo " sudo chown -v root:${USER} /dev/bus/usb/${USB_BUS}/${USB_DEV}"
USB_NOT_READY=1
fi
USB_PASSTHROUGH="${USB_PASSTHROUGH} -device usb-host,bus=hostpass.0,vendorid=0x${VENDOR_ID},productid=0x${PRODUCT_ID}"
done
if [ "${USB_NOT_READY}" -eq 1 ]; then
echo " ERROR! USB permission changes are required 👆"
exit 1
fi
fi
}
function check_cpu_flag() {
local HOST_CPU_FLAG="${1}"
if lscpu | grep -o "^Flags\b.*: .*\b${HOST_CPU_FLAG}\b" > /dev/null; then
return 0
else
return 1
fi
}
function efi_vars() {
local VARS_IN=""
local VARS_OUT=""
VARS_IN="${1}"
VARS_OUT="${2}"
if [ ! -e "${VARS_OUT}" ]; then
if [ -e "${VARS_IN}" ]; then
cp "${VARS_IN}" "${VARS_OUT}"
else
echo "ERROR! ${VARS_IN} was not found. Please install edk2."
exit 1
fi
fi
}
function vm_boot() {
local AUDIO_DEV=""
local BALLOON="-device virtio-balloon"
local BOOT_STATUS=""
local CPU=""
local DISK_USED=""
local DISPLAY_DEVICE=""
local DISPLAY_RENDER=""
local EFI_CODE="${EFI_CODE}"
local EFI_VARS=""
local GUEST_CPU_CORES=""
local GUEST_CPU_LOGICAL_CORES=""
local GUEST_CPU_THREADS=""
local HOST_CPU_CORES=""
local HOST_CPU_SMT=""
local HOST_CPU_SOCKETS=""
local HOST_CPU_VENDOR=""
local GUEST_TWEAKS=""
local KERNEL_NAME="Unknown"
local KERNEL_NODE=""
local KERNEL_VER="?"
local LSB_DESCRIPTION="Unknown OS"
local MACHINE_TYPE="${MACHINE_TYPE:-q35}"
local MAC_BOOTLOADER=""
local MAC_MISSING=""
local MAC_DISK_DEV="${MAC_DISK_DEV:-ide-hd,bus=ahci.2}"
local NET_DEVICE="${NET_DEVICE:-virtio-net}"
local OSK=""
local SMM="${SMM:-off}"
local USB_HOST_PASSTHROUGH_CONTROLLER="qemu-xhci"
local VGA=""
local VIDEO=""
KERNEL_NAME=$(uname --kernel-name)
KERNEL_NODE="($(uname --nodename))"
KERNEL_VER=$(uname --kernel-release | cut -d'.' -f1-2)
if command -v lsb_release &>/dev/null; then
LSB_DESCRIPTION=$(lsb_release --description --short)
elif [ -e /etc/os-release ]; then
LSB_DESCRIPTION=$(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)
fi
echo
echo " Quickemu ${VERSION} using ${QEMU} v${QEMU_VER_LONG}"
echo
echo " Compiling the following instructions for qemu:"
echo
echo " - Host: ${LSB_DESCRIPTION} running ${KERNEL_NAME} ${KERNEL_VER} ${KERNEL_NODE}"
HOST_CPU_CORES=$(nproc --all)
HOST_CPU_MODEL=$(lscpu | grep '^Model name:' | cut -d':' -f2 | sed 's/ //g')
HOST_CPU_SOCKETS=$(lscpu | grep -E 'Socket' | cut -d':' -f2 | sed 's/ //g')
HOST_CPU_VENDOR=$(lscpu | grep -E 'Vendor' | cut -d':' -f2 | sed 's/ //g')
# A CPU with Intel VT-x / AMD SVM support is required
if [ "${HOST_CPU_VENDOR}" == "GenuineIntel" ]; then
if ! check_cpu_flag vmx; then
echo "ERROR! Intel VT-x support is required."
exit 1
fi
elif [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
if ! check_cpu_flag svm; then
echo "ERROR! AMD SVM support is required."
exit 1
fi
fi
if [ -z "${cpu_cores}" ]; then
if [ "${HOST_CPU_CORES}" -ge 32 ]; then
GUEST_CPU_CORES="16"
elif [ "${HOST_CPU_CORES}" -ge 16 ]; then
GUEST_CPU_CORES="8"
elif [ "${HOST_CPU_CORES}" -ge 8 ]; then
GUEST_CPU_CORES="4"
elif [ "${HOST_CPU_CORES}" -ge 4 ]; then
GUEST_CPU_CORES="2"
else
GUEST_CPU_CORES="1"
fi
else
GUEST_CPU_CORES="${cpu_cores}"
fi
# Account for Hyperthreading/SMT.
if [ -e /sys/devices/system/cpu/smt/control ] && [ "${GUEST_CPU_CORES}" -ge 2 ]; then
HOST_CPU_SMT=$(cat /sys/devices/system/cpu/smt/control)
case ${HOST_CPU_SMT} in
on)
GUEST_CPU_THREADS=2
GUEST_CPU_LOGICAL_CORES=$(( GUEST_CPU_CORES / GUEST_CPU_THREADS ))
;;
*)
GUEST_CPU_THREADS=1
GUEST_CPU_LOGICAL_CORES=${GUEST_CPU_CORES}
;;
esac
else
GUEST_CPU_THREADS=1
GUEST_CPU_LOGICAL_CORES=${GUEST_CPU_CORES}
fi
local SMP="-smp cores=${GUEST_CPU_LOGICAL_CORES},threads=${GUEST_CPU_THREADS},sockets=${HOST_CPU_SOCKETS}"
echo " - CPU: ${HOST_CPU_MODEL}"
echo -n " - CPU VM: ${HOST_CPU_SOCKETS} Socket(s), ${GUEST_CPU_LOGICAL_CORES} Core(s), ${GUEST_CPU_THREADS} Thread(s)"
local RAM_VM="2G"
if [ -z "${ram}" ]; then
local RAM_HOST=""
RAM_HOST=$(free --mega -h | grep Mem | cut -d':' -f2 | cut -d'G' -f1 | sed 's/ //g')
#Round up - https://github.com/wimpysworld/quickemu/issues/11
RAM_HOST=$(printf '%.*f\n' 0 "${RAM_HOST}")
if [ "${RAM_HOST}" -ge 128 ]; then
RAM_VM="32G"
elif [ "${RAM_HOST}" -ge 64 ]; then
RAM_VM="16G"
elif [ "${RAM_HOST}" -ge 16 ]; then
RAM_VM="8G"
elif [ "${RAM_HOST}" -ge 8 ]; then
RAM_VM="4G"
fi
else
RAM_VM="${ram}"
fi
echo ", ${RAM_VM} RAM"
if [ "${guest_os}" == "macos" ] || [ "${guest_os}" == "windows" ]; then
if [ "${RAM_VM//G/}" -lt 4 ]; then
echo "ERROR! You have insufficient RAM to run ${guest_os} in a VM"
exit 1
fi
fi
# Force to lowercase.
boot=${boot,,}
guest_os=${guest_os,,}
## EFI:
if [ "${guest_os}" == "macos" ]; then
# Always Boot macOS using EFI
boot="efi"
if [ -e "${VMDIR}/OVMF_CODE.fd" ] && [ -e "${VMDIR}/OVMF_VARS-1024x768.fd" ]; then
EFI_CODE="${VMDIR}/OVMF_CODE.fd"
EFI_VARS="${VMDIR}/OVMF_VARS-1024x768.fd"
else
MAC_MISSING="Firmware"
fi
if [ -e "${VMDIR}/OpenCore.qcow2" ]; then
MAC_BOOTLOADER="${VMDIR}/OpenCore.qcow2"
elif [ -e "${VMDIR}/ESP.qcow2" ]; then
# Backwards compatibility for Clover
MAC_BOOTLOADER="${VMDIR}/ESP.qcow2"
else
MAC_MISSING="Bootloader"
fi
if [ -n "${MAC_MISSING}" ]; then
echo "ERROR! macOS ${MAC_MISSING} was not found."
echo " Use 'quickget' to download the required files."
exit 1
fi
BOOT_STATUS="EFI (macOS), OVMF ($(basename "${EFI_CODE}")), SecureBoot (${secureboot})."
elif [[ "${boot}" == *"efi"* ]]; then
EFI_VARS="${VMDIR}/OVMF_VARS.fd"
# Preserve backward compatibility
if [ -e "${VMDIR}/${VMNAME}-vars.fd" ]; then
mv "${VMDIR}/${VMNAME}-vars.fd" "${EFI_VARS}"
elif [ -e "${VMDIR}/OVMF_VARS_4M.fd" ]; then
mv "${VMDIR}/OVMF_VARS_4M.fd" "${EFI_VARS}"
fi
# OVMF_CODE_4M.fd is for booting guests in non-Secure Boot mode.
# While this image technically supports Secure Boot, it does so
# without requiring SMM support from QEMU
# OVMF_CODE.secboot.fd is like OVMF_CODE_4M.fd, but will abort if QEMU
# does not support SMM.
# https://bugzilla.redhat.com/show_bug.cgi?id=1929357#c5
# shellcheck disable=SC2054,SC2206
if [ -n "${EFI_CODE}" ] || [ ! -e "${EFI_CODE}" ]; then
case ${secureboot} in
on)
ovmfs=("/usr/share/OVMF/OVMF_CODE_4M.secboot.fd","/usr/share/OVMF/OVMF_VARS_4M.fd" \
"/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd","/usr/share/edk2/ovmf/OVMF_VARS.fd" \
"/usr/share/OVMF/x64/OVMF_CODE.secboot.fd","/usr/share/OVMF/x64/OVMF_VARS.fd" \
"/usr/share/edk2-ovmf/OVMF_CODE.secboot.fd","/usr/share/edk2-ovmf/OVMF_VARS.fd" \
"/usr/share/qemu/ovmf-x86_64-smm-ms-code.bin","/usr/share/qemu/ovmf-x86_64-smm-ms-vars.bin" \
"/usr/share/qemu/edk2-x86_64-secure-code.fd","/usr/share/qemu/edk2-x86_64-code.fd" \
"/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd","/usr/share/edk2-ovmf/x64/OVMF_VARS.fd"
)
;;
*)
ovmfs=("/usr/share/OVMF/OVMF_CODE_4M.fd","/usr/share/OVMF/OVMF_VARS_4M.fd" \
"/usr/share/edk2/ovmf/OVMF_CODE.fd","/usr/share/edk2/ovmf/OVMF_VARS.fd" \
"/usr/share/OVMF/OVMF_CODE.fd","/usr/share/OVMF/OVMF_VARS.fd" \
"/usr/share/OVMF/x64/OVMF_CODE.fd","/usr/share/OVMF/x64/OVMF_VARS.fd" \
"/usr/share/edk2-ovmf/OVMF_CODE.fd","/usr/share/edk2-ovmf/OVMF_VARS.fd" \
"/usr/share/qemu/ovmf-x86_64-4m-code.bin","/usr/share/qemu/ovmf-x86_64-4m-vars.bin" \
"/usr/share/qemu/edk2-x86_64-code.fd","/usr/share/qemu/edk2-x86_64-code.fd" \
"/usr/share/edk2-ovmf/x64/OVMF_CODE.fd","/usr/share/edk2-ovmf/x64/OVMF_VARS.fd"
)
;;
esac
# declare -p ovmfs
# Attempt each EFI_CODE file one by one, selecting the corresponding code and vars
# when an existing file is found.
_IFS=$IFS
IFS=","
for f in "${ovmfs[@]}"; do
#printf "\n\n f is %s" "$f" # MOD TODO redo ALL this EFI section & associatively
# shellcheck disable=SC2086
set -- $f;
if [ -e "${1}" ]; then
EFI_CODE="${1}"
EFI_EXTRA_VARS="${2}"
#printf "\n\n Set: EFI_CODE is %s EFI_EXTRA_VARS are %s \n" "$EFI_CODE" "$EFI_EXTRA_VARS"
fi
done
IFS=$_IFS
fi
#EFI_CODE="/usr/share/OVMF/OVMF_CODE_4M.ms.fd"
#EFI_EXTRA_VARS="/usr/share/OVMF/OVMF_VARS_4M.ms.fd"
#EFI_CODE="/usr/share/OVMF/OVMF_CODE_4M.secboot.fd"
#EFI_EXTRA_VARS="/usr/share/OVMF/OVMF_VARS_4M.fd"
if [ -z "${EFI_CODE}" ] || [ ! -e "${EFI_CODE}" ]; then
if [ "$secureboot" == "on" ]; then
echo "ERROR! SecureBoot was requested but no SecureBoot capable firmware was found."
else
echo "ERROR! EFI boot requested but no EFI firmware found."
fi
echo " Please install OVMF firmware."
exit 1
fi
if [ -n "${EFI_EXTRA_VARS}" ]; then
if [ ! -e "${EFI_EXTRA_VARS}" ]; then
echo " - EFI: ERROR! EFI_EXTRA_VARS file ${EFI_EXTRA_VARS} does not exist."
exit 1
fi
efi_vars "${EFI_EXTRA_VARS}" "${EFI_VARS}"
fi
# Make sure EFI_VARS references an actual, writeable, file
if [ ! -f "${EFI_VARS}" ] || [ ! -w "${EFI_VARS}" ]; then
echo " - EFI: ERROR! ${EFI_VARS} is not a regular file or not writeable."
echo " Deleting ${EFI_VARS}. Please re-run quickemu."
rm -f "${EFI_VARS}"
exit 1
fi
# If EFI_CODE references a symlink, resolve it to the real file.
if [ -L "${EFI_CODE}" ]; then
echo " - EFI: WARNING! ${EFI_CODE} is a symlink."
echo -n " Resolving to... "
EFI_CODE=$(realpath "${EFI_CODE}")
echo "${EFI_CODE}"
fi
BOOT_STATUS="EFI (${guest_os^}), OVMF (${EFI_CODE}), SecureBoot (${secureboot})."
else
BOOT_STATUS="Legacy BIOS (${guest_os^})"
boot="legacy"
secureboot="off"
fi
echo " - BOOT: ${BOOT_STATUS}"
# Make any OS specific adjustments
case ${guest_os} in
batocera|*bsd|freedos|haiku|linux)
CPU="-cpu host,kvm=on"
if [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
CPU="${CPU},topoext"
fi
if [ "${guest_os}" == "freebsd" ] || [ "${guest_os}" == "ghostbsd" ]; then
MOUSE="usb"
elif [ "${guest_os}" == "batocera" ] || [ "${guest_os}" == "freedos" ] || [ "${guest_os}" == "haiku" ]; then
MACHINE_TYPE="pc"
NET_DEVICE="rtl8139"
fi
if [ "${guest_os}" == "freedos" ] ; then
# fix for #382
SMM="on"
fi
if [ -z "${disk_size}" ]; then
disk_size="16G"
fi
;;
kolibrios|reactos)
CPU="-cpu qemu32,kvm=on"
if [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
CPU="${CPU},topoext"
fi
MACHINE_TYPE="pc"
case ${guest_os} in
kolibrios) NET_DEVICE="rtl8139";;
reactos)
NET_DEVICE="e1000"
KEYBOARD="ps2"
;;
esac
;;
macos)
#https://www.nicksherlock.com/2020/06/installing-macos-big-sur-on-proxmox/
# A CPU with SSE4.1 support is required for >= macOS Sierra
# A CPU with AVX2 support is required for >= macOS Ventura
case ${macos_release} in
ventura)
if check_cpu_flag sse4_1 && check_cpu_flag avx2; then
CPU="-cpu Haswell,kvm=on,vendor=GenuineIntel,+hypervisor,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt"
else
echo "ERROR! macOS ${macos_release} requires a CPU with SSE 4.1 and AVX2 support."
exit 1
fi
;;
*)
if check_cpu_flag sse4_1; then
# Used in past versions: +movbe,+smep,+xgetbv1,+xsavec,+avx2
# Warn on AMD: +fma4,+pcid
CPU="-cpu Penryn,kvm=on,vendor=GenuineIntel,+aes,+avx,+bmi1,+bmi2,+fma,+hypervisor,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt,+popcnt,+ssse3,+sse4.2,vmware-cpuid-freq=on,+xsave,+xsaveopt,check"
else
echo "ERROR! macOS ${macos_release} requires a CPU with SSE 4.1 support."
exit 1
fi
;;
esac
OSK=$(echo "bheuneqjbexolgurfrjbeqfthneqrqcyrnfrqbagfgrny(p)NccyrPbzchgreVap" | tr 'A-Za-z' 'N-ZA-Mn-za-m')
# Disable S3 support in the VM to prevent macOS suspending during install
GUEST_TWEAKS="-no-hpet -global kvm-pit.lost_tick_policy=discard -global ICH9-LPC.disable_s3=1 -device isa-applesmc,osk=${OSK}"
# Tune Qemu optimisations based on the macOS release, or fallback to lowest
# common supported options if none is specified.
# * VirtIO Block Media doesn't work in High Sierra (at all) or the Mojave (Recovery Image)
# * VirtIO Network is supported since Big Sur
# * VirtIO Memory Balloning is supported since Big Sur (https://pmhahn.github.io/virtio-balloon/)
# * VirtIO RNG is supported since Big Sur, but exposed to all guests by default.
case ${macos_release} in
catalina)
BALLOON=""
MAC_DISK_DEV="ide-hd,bus=ahci.2"
NET_DEVICE="vmxnet3"
USB_HOST_PASSTHROUGH_CONTROLLER="usb-ehci"
;;
big-sur|monterey|ventura)
BALLOON="-device virtio-balloon"
MAC_DISK_DEV="ide-hd,bus=ahci.2"
NET_DEVICE="virtio-net"
USB_HOST_PASSTHROUGH_CONTROLLER="nec-usb-xhci"
GUEST_TWEAKS="${GUEST_TWEAKS} -global nec-usb-xhci.msi=off"
;;
*)
# Backwards compatibility if no macos_release is specified.
# Also safe catch all for High Sierra and Mojave
BALLOON=""
MAC_DISK_DEV="ide-hd,bus=ahci.2"
NET_DEVICE="vmxnet3"
USB_HOST_PASSTHROUGH_CONTROLLER="usb-ehci"
;;
esac
if [ -z "${disk_size}" ]; then
disk_size="96G"
fi
;;
## WINDOWS & HYPERVISOR (needs its own special highlight ...)
windows)
# qe 4.4/4.5 - CPU="-cpu host,kvm=on,+hypervisor,+invtsc,l3-cache=on,migratable=no,hv_passthrough"
# dec 14 2022 introduces here : if [ "${QEMU_VER_SHORT}" -gt 60 ]; then
# https://github.com/quickemu-project/quickemu/commit/23982c3f9da2c99d3fae333a0c6e89677bded499
# Tests for -gt 60 but standard quickemu also exits the script if -lt 60 ...
# Appears something very specific about qemu ver 6.0.0 ... (??) REVIEW
# MOD: add recipe settings. If recipe use that, otherwise use standard 'quickemu 4.5 defaults'.
if [[ $WinHyperV_RC ]]; then CPU="$WinHyperV_RC"
elif [[ $QEMU_VER_SHORT == "60" ]]; then
CPU="-cpu host,kvm=on,+hypervisor,+invtsc,l3-cache=on,migratable=no,hv_frequencies,kvm_pv_unhalt,hv_reenlightenment,\
hv_relaxed,hv_spinlocks=8191,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vendor_id=1234567890ab,hv_vpindex"
else CPU="-cpu host,kvm=on,+hypervisor,+invtsc,l3-cache=on,migratable=no,hv_passthrough"
fi
# See https://github.com/quickemu-project/quickemu/issues/572 & https://www.qemu.org/docs/master/system/i386/hyperv.html
# From qemu docs hyperv:
# Note: hv-passthrough flag only enables enlightenments which are known to QEMU (have corresponding ‘hv-‘ flag)
# and copies hv-spinlocks and hv-vendor-id values from KVM to QEMU. hv-passthrough overrides all other ‘hv-‘ settings on the command line.
# Also, enabling this flag effectively prevents migration as the list of enabled enlightenments may differ between target and destination hosts.
# This above may account for VM's built with quickemu 4.2 not working with 4.3 ... ? YMMV
# Also of interest: https://bbs.archlinux.org/viewtopic.php?id=233713 https://kevinlocke.name/bits/2021/12/10/windows-11-guest-virtio-libvirt/
if [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
CPU="${CPU},topoext"
fi
# Disable S3 support in the VM to ensure Windows can boot with SecureBoot enabled
# - https://wiki.archlinux.org/title/QEMU#VM_does_not_boot_when_using_a_Secure_Boot_enabled_OVMF
GUEST_TWEAKS="-no-hpet -global kvm-pit.lost_tick_policy=discard -global ICH9-LPC.disable_s3=1"
if [ -z "${disk_size}" ]; then
disk_size="64G"
fi
SMM="on"
;;
## EO WINDOWS
*)
CPU="-cpu host,kvm=on"
NET_DEVICE="rtl8139"
if [ -z "${disk_size}" ]; then
disk_size="32G"
fi
echo "WARNING! Unrecognised guest OS: ${guest_os}"
;;
esac
echo " - Disk: ${disk_img} (${disk_size})"
if [ ! -f "${disk_img}" ]; then
# If there is no disk image, create a new image.
mkdir -p "${VMDIR}" 2>/dev/null
case ${preallocation} in
off|metadata|falloc|full) true;;
*)
echo "ERROR! ${preallocation} is an unsupported disk preallocation option."
exit 1;;
esac
# https://blog.programster.org/qcow2-performance
if ! ${QEMU_IMG} create -q -f qcow2 -o lazy_refcounts=on,preallocation="${preallocation}" "${disk_img}" "${disk_size}"; then
echo "ERROR! Failed to create ${disk_img}"
exit 1
fi
if [ -z "${iso}" ] && [ -z "${img}" ]; then
echo "ERROR! You haven't specified a .iso or .img image to boot from."
exit 1
fi
echo " Just created, booting from ${iso}${img}"
DISK_USED="no"
elif [ -e "${disk_img}" ]; then
# Check there isn't already a process attached to the disk image.
if ! ${QEMU_IMG} info "${disk_img}" >/dev/null; then
echo " Failed to get \"write\" lock. Is another process using the disk?"
exit 1
else
# Only check disk image size if preallocation is off
if [ "${preallocation}" == "off" ]; then
DISK_CURR_SIZE=$(stat -c%s "${disk_img}")
if [ "${DISK_CURR_SIZE}" -le "${DISK_MIN_SIZE}" ]; then
echo " Looks unused, booting from ${iso}${img}"
if [ -z "${iso}" ] && [ -z "${img}" ]; then
echo "ERROR! You haven't specified a .iso or .img image to boot from."
exit 1
fi
else
DISK_USED="yes"
fi
else
DISK_USED="yes"
fi
fi
fi
if [ "${DISK_USED}" == "yes" ] && [ "${guest_os}" != "kolibrios" ]; then
# If there is a disk image that appears to be used do not boot from installation media.
iso=""
img=""
fi
# Has the status quo been requested?
if [ "${STATUS_QUO}" == "-snapshot" ]; then
if [ -z "${img}" ] && [ -z "${iso}" ]; then
echo " Existing disk state will be preserved, no writes will be committed."
fi
fi
if [ -n "${iso}" ] && [ -e "${iso}" ]; then
echo " - Boot ISO: ${iso}"
elif [ -n "${img}" ] && [ -e "${img}" ]; then
echo " - Recovery: ${img}"
fi
if [ -n "${fixed_iso}" ] && [ -e "${fixed_iso}" ]; then
echo " - CD-ROM: ${fixed_iso}"
fi
# Setup the appropriate audio device based on the display output
# https://www.kraxel.org/blog/2020/01/qemu-sound-audiodev/
case ${OUTPUT} in
none|spice|spice-app) AUDIO_DEV="spice,id=audio0";;
*) AUDIO_DEV="pa,id=audio0";;
esac
# Determine a sane resolution for Linux guests.
if [ "${guest_os}" == "linux" ]; then
local X_RES=1152
local Y_RES=648
if [ "${XDG_SESSION_TYPE}" == "x11" ]; then
local LOWEST_WIDTH=""
if [ -z "${SCREEN}" ]; then
LOWEST_WIDTH=$(xrandr --listmonitors | grep -v Monitors | cut -d' ' -f4 | cut -d'/' -f1 | sort | head -n1)
else
LOWEST_WIDTH=$(xrandr --listmonitors | grep -v Monitors | grep "^ ${SCREEN}:" | cut -d' ' -f4 | cut -d'/' -f1 | head -n1)
fi
if [ "${FULLSCREEN}" ]; then
if [ -z "${SCREEN}" ]; then
X_RES=$(xrandr --listmonitors | grep -v Monitors | cut -d' ' -f4 | cut -d'/' -f1 | sort | head -n1)
Y_RES=$(xrandr --listmonitors | grep -v Monitors | cut -d' ' -f4 | cut -d'/' -f2 | cut -d'x' -f2 | sort | head -n1)
else
X_RES=$(xrandr --listmonitors | grep -v Monitors | grep "^ ${SCREEN}:" | cut -d' ' -f4 | cut -d'/' -f1 | head -n1)
Y_RES=$(xrandr --listmonitors | grep -v Monitors | grep "^ ${SCREEN}:" | cut -d' ' -f4 | cut -d'/' -f2 | cut -d'x' -f2 | head -n1)
fi
elif [ "${LOWEST_WIDTH}" -ge 3840 ]; then
X_RES=3200
Y_RES=1800
elif [ "${LOWEST_WIDTH}" -ge 2560 ]; then
X_RES=2048
Y_RES=1152
elif [ "${LOWEST_WIDTH}" -ge 1920 ]; then
X_RES=1664
Y_RES=936
elif [ "${LOWEST_WIDTH}" -ge 1280 ]; then