forked from quickemu-project/quickemu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickemu
executable file
·2203 lines (2014 loc) · 82.5 KB
/
quickemu
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
#!/usr/bin/env bash
export LC_ALL=C
if ((BASH_VERSINFO[0] < 4)); then
echo "Sorry, you need bash 4.0 or newer to run this script."
exit 1
fi
function ignore_msrs_always() {
# Make sure the host has /etc/modprobe.d
if [ -d /etc/modprobe.d ]; then
# Skip if ignore_msrs is already enabled, assumes initramfs has been rebuilt
if ! grep -lq 'ignore_msrs=Y' /etc/modprobe.d/kvm-quickemu.conf >/dev/null 2>&1; then
echo "options kvm ignore_msrs=Y" | sudo tee /etc/modprobe.d/kvm-quickemu.conf
sudo update-initramfs -k all -u
fi
else
echo "ERROR! /etc/modprobe.d was not found, I don't know how to configure this system."
exit 1
fi
}
function ignore_msrs_alert() {
local ignore_msrs=""
if [ "${OS_KERNEL}" == "Darwin" ]; then
return
elif [ -e /sys/module/kvm/parameters/ignore_msrs ]; then
ignore_msrs=$(cat /sys/module/kvm/parameters/ignore_msrs)
if [ "${ignore_msrs}" == "N" ]; then
echo " - MSR: WARNING! Ignoring unhandled Model-Specific Registers is disabled."
echo
echo " echo 1 | sudo tee /sys/module/kvm/parameters/ignore_msrs"
echo
echo " If you are unable to run macOS or Windows VMs then run the above 👆"
echo " This will enable ignoring of unhandled MSRs until you reboot the host."
echo " You can make this change permanent by running: 'quickemu --ignore-msrs-always'"
fi
fi
}
function delete_shortcut() {
local SHORTCUT_DIR="${HOME}/.local/share/applications"
if [ -e "${SHORTCUT_DIR}/${VMNAME}.desktop" ]; then
rm "${SHORTCUT_DIR}/${VMNAME}.desktop"
echo " - Deleted ${SHORTCUT_DIR}/${VMNAME}.desktop"
fi
}
function delete_disk() {
echo "Deleting ${VMNAME} virtual hard disk"
if [ -e "${disk_img}" ]; then
rm "${disk_img}" >/dev/null 2>&1
# Remove any EFI vars, but not for macOS
rm "${VMDIR}"/OVMF_VARS*.fd >/dev/null 2>&1
rm "${VMDIR}/${VMNAME}-vars.fd" >/dev/null 2>&1
echo " - Deleted ${disk_img}"
delete_shortcut
else
echo " - ${disk_img} not found. Doing nothing."
fi
}
function delete_vm() {
echo "Deleting ${VMNAME} completely"
if [ -d "${VMDIR}" ]; then
rm -rf "${VMDIR}"
rm "${VM}"
echo " - Deleted ${VM} and ${VMDIR}/"
delete_shortcut
else
echo " - ${VMDIR} not found. Doing nothing."
fi
}
function kill_vm() {
echo "Killing ${VMNAME}"
if [ -z "${VM_PID}" ]; then
echo " - ${VMNAME} is not running."
rm -f "${VMDIR}/${VMNAME}.pid"
elif [ -n "${VM_PID}" ]; then
if kill -9 "${VM_PID}" > /dev/null 2>&1; then
echo " - ${VMNAME} (${VM_PID}) killed."
rm -f "${VMDIR}/${VMNAME}.pid"
else
echo " - ${VMNAME} (${VM_PID}) was not killed."
fi
elif [ ! -r "${VMDIR}/${VMNAME}.pid" ]; then
echo " - ${VMNAME} has no ${VMDIR}/${VMNAME}.pid"
fi
}
function snapshot_apply() {
echo "Snapshot apply to ${disk_img}"
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 " - 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() {
echo "Snapshotting ${disk_img}"
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 " - Created snapshot '${TAG}' for ${disk_img}"
else
echo " - ERROR! Failed to create snapshot '${TAG}' for ${disk_img}"
fi
else
echo " - NOTE! ${disk_img} not found. Doing nothing."
fi
}
function snapshot_delete() {
echo "Snapshot removal ${disk_img}"
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 " - Deleted snapshot '${TAG}' from ${disk_img}"
else
echo " - ERROR! Failed to delete snapshot '${TAG}' from ${disk_img}"
fi
else
echo " - NOTE! ${disk_img} not found. Doing nothing."
fi
}
function snapshot_info() {
echo
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 configure_usb() {
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
}
# get the number of processing units
function get_nproc() {
if command -v nproc &>/dev/null; then
nproc
elif command -v sysctl &>/dev/null; then
sysctl -n hw.ncpu
else
echo "ERROR! Unable to determine the number of processing units."
exit 1
fi
}
# macOS and Linux compatible get_cpu_info function
function get_cpu_info() {
local INFO_NAME="${1}"
if [ "${OS_KERNEL}" == "Darwin" ]; then
if [ "^Model name:" == "${INFO_NAME}" ]; then
sysctl -n machdep.cpu.brand_string
elif [ "Socket" == "${INFO_NAME}" ]; then
sysctl -n hw.packages
elif [ "Vendor" == "${INFO_NAME}" ]; then
if [ "${ARCH_HOST}" == "arm64" ]; then
sysctl -n machdep.cpu.brand_string | cut -d' ' -f1
else
sysctl -n machdep.cpu.vendor | sed 's/ //g'
fi
else
echo "ERROR! Could not find macOS translation for ${INFO_NAME}"
exit 1
fi
else
if [ "^Model name:" == "${INFO_NAME}" ]; then
for MODEL_NAME in $(IFS=$'\n' lscpu | grep "${INFO_NAME}" | cut -d':' -f2 | sed -e 's/^[[:space:]]*//'); do
echo -n "${MODEL_NAME} "
done
else
lscpu | grep -E "${INFO_NAME}" | cut -d':' -f2 | sed 's/ //g' | sort -u
fi
fi
}
# returns an enabled or disable CPU flag for QEMU, based on the host CPU
# capabilities, or nothing if the flag is not supported
# converts the flags appropriately from macOS and Linux to QEMU
function configure_cpu_flag() {
local HOST_CPU_FLAG="${1}"
# Convert the flag to lowercase for QEMU
local QEMU_CPU_FLAG=${HOST_CPU_FLAG,,}
if check_cpu_flag "${HOST_CPU_FLAG}"; then
# Replace _ with - to make it compatible with QEMU
QEMU_CPU_FLAG="${HOST_CPU_FLAG//_/-}"
QEMU_CPU_FLAG="${QEMU_CPU_FLAG//4_/4\.}"
# macOS uses different flag names
if [ "${OS_KERNEL}" == "Darwin" ]; then
case "${HOST_CPU_FLAG}" in
avx) QEMU_CPU_FLAG="AVX1.0";;
esac
fi
echo ",+${QEMU_CPU_FLAG}"
else
# Fully disable any QEMU flags that are not supported by the host CPU
if [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
case ${HOST_CPU_FLAG} in
pcid) echo ",-${QEMU_CPU_FLAG}";;
esac
fi
fi
}
# checks if a CPU flag is supported by the host CPU on Linux and macOS
function check_cpu_flag() {
local HOST_CPU_FLAG=""
if [ "${OS_KERNEL}" == "Darwin" ]; then
# Make the macOS compatible: uppercase, replace _ with . and replace X2APIC with x2APIC
HOST_CPU_FLAG="${1^^}"
HOST_CPU_FLAG="${HOST_CPU_FLAG//_/.}"
HOST_CPU_FLAG="${HOST_CPU_FLAG//X2APIC/x2APIC}"
if [ "${HOST_CPU_FLAG}" == "AVX" ]; then
HOST_CPU_FLAG="AVX1.0"
fi
if sysctl -n machdep.cpu.features | grep -o "${HOST_CPU_FLAG}" > /dev/null; then
return 0
else
return 1
fi
else
HOST_CPU_FLAG="${1}"
if lscpu | grep -o "^Flags\b.*: .*\b${HOST_CPU_FLAG}\b" > /dev/null; then
return 0
else
return 1
fi
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 configure_cpu() {
HOST_CPU_CORES=$(get_nproc)
HOST_CPU_MODEL=$(get_cpu_info '^Model name:')
HOST_CPU_SOCKETS=$(get_cpu_info 'Socket')
HOST_CPU_VENDOR=$(get_cpu_info 'Vendor')
if [ "${HOST_CPU_SOCKETS}" = "-" ]; then
HOST_CPU_SOCKETS=1
fi
CPU_MODEL="host"
QEMU_ACCEL="tcg"
# Configure appropriately for the host platform
if [ "${OS_KERNEL}" == "Darwin" ]; then
MANUFACTURER=$(ioreg -l | grep -e Manufacturer | grep -v iMan | cut -d'"' -f4 | sort -u)
CPU_KVM_UNHALT=""
QEMU_ACCEL="hvf"
# QEMU for macOS from Homebrew does not support SMM
SMM="off"
else
if [ -r /sys/class/dmi/id/sys_vendor ]; then
MANUFACTURER=$(head -n 1 /sys/class/dmi/id/sys_vendor)
fi
CPU_KVM_UNHALT=",kvm_pv_unhalt"
GUEST_TWEAKS+=" -global kvm-pit.lost_tick_policy=discard"
QEMU_ACCEL="kvm"
fi
if [ "${ARCH_VM}" == "aarch64" ]; then
# Support to run aarch64 VMs (best guess; untested)
# https://qemu-project.gitlab.io/qemu/system/arm/virt.html
case ${ARCH_HOST} in
arm64|aarch64) CPU_MODEL="max"
MACHINE_TYPE="virt,highmem=off";;
esac
elif [ "${ARCH_VM}" != "${ARCH_HOST}" ]; then
# If the architecture of the VM is different from the host, disable acceleration
CPU_MODEL="qemu64"
CPU_KVM_UNHALT=""
QEMU_ACCEL="tcg"
fi
# TODO: More robust detection of running in a VM
# - macOS check for CPU flag: vmx
# - Linux AMD check for CPU flag: svm
# - Linux Intel check for CPU flag: vmx
case ${MANUFACTURER,,} in
qemu|virtualbox) CPU_MODEL="qemu64"
QEMU_ACCEL="tcg"
HYPERVISOR="${MANUFACTURER,,}";;
*) HYPERVISOR="";;
esac
if [ -z "${HYPERVISOR}" ]; then
# 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
fi
CPU="-cpu ${CPU_MODEL}"
# Make any OS specific adjustments
if [ "${guest_os}" == "freedos" ] || [ "${guest_os}" == "windows" ] || [ "${guest_os}" == "windows-server" ]; then
# SMM is not available on QEMU for macOS via Homebrew
if [ "${OS_KERNEL}" == "Linux" ]; then
SMM="on"
fi
fi
case ${guest_os} in
batocera|freedos|haiku|solaris) MACHINE_TYPE="pc";;
kolibrios|reactos)
CPU="-cpu qemu32"
MACHINE_TYPE="pc";;
macos)
# If the host has an Intel CPU, passes the host CPU model features, model, stepping, exactly to the guest.
# Disable huge pages (,-pdpe1gb) on macOS to prevent crashes
# - https://stackoverflow.com/questions/60231203/qemu-qcow2-mmu-gva-to-gpa-crash-in-mac-os-x
if [ "${HOST_CPU_VENDOR}" == "GenuineIntel" ] && [ -z "${HYPERVISOR}" ]; then
CPU_MODEL="host"
CPU="-cpu ${CPU_MODEL},-pdpe1gb,+hypervisor"
else
CPU_MODEL="Haswell-v2"
CPU="-cpu ${CPU_MODEL},vendor=GenuineIntel,-pdpe1gb,+avx,+sse,+sse2,+ssse3,vmware-cpuid-freq=on"
fi
# A CPU with fma is required for Metal support
# A CPU with invtsc is required for macOS to boot
case ${macos_release} in
ventura|sonoma)
# A CPU with AVX2 support is required for >= macOS Ventura
if check_cpu_flag sse4_2 && check_cpu_flag avx2; then
if [ "${HOST_CPU_VENDOR}" != "GenuineIntel" ] && [ -z "${HYPERVISOR}" ]; then
CPU+=",+avx2,+sse4.2"
fi
else
echo "ERROR! macOS ${macos_release} requires a CPU with SSE 4.2 and AVX2 support."
echo " Try macOS Monterey or Big Sur."
exit 1
fi;;
catalina|big-sur|monterey)
# A CPU with SSE4.2 support is required for >= macOS Catalina
if check_cpu_flag sse4_2; then
if [ "${HOST_CPU_VENDOR}" != "GenuineIntel" ] && [ -z "${HYPERVISOR}" ]; then
CPU+=",+sse4.2"
fi
else
echo "ERROR! macOS ${macos_release} requires a CPU with SSE 4.2 support."
exit 1
fi;;
*)
# A CPU with SSE4.1 support is required for >= macOS Sierra
if check_cpu_flag sse4_1; then
if [ "${HOST_CPU_VENDOR}" != "GenuineIntel" ] && [ -z "${HYPERVISOR}" ]; then
CPU+=",+sse4.1"
fi
else
echo "ERROR! macOS ${macos_release} requires a CPU with SSE 4.1 support."
exit 1
fi;;
esac
if [ "${HOST_CPU_VENDOR}" != "GenuineIntel" ] && [ -z "${HYPERVISOR}" ]; then
for FLAG in abm adx aes amd-ssbd apic arat bmi1 bmi2 clflush cmov cx8 cx16 de \
eist erms f16c fma fp87 fsgsbase fxsr invpcid invtsc lahf_lm lm \
mca mce mmx movbe mpx msr mtrr nx pae pat pcid pge pse popcnt pse36 \
rdrand rdtscp sep smep syscall tsc tsc_adjust vaes vbmi2 vmx vpclmulqdq \
x2apic xgetbv1 xsave xsaveopt; do
CPU+=$(configure_cpu_flag "${FLAG}")
done
fi
# Disable S3 support in the VM to prevent macOS suspending during install
GUEST_TWEAKS+=" -global ICH9-LPC.disable_s3=1 -device isa-applesmc,osk=$(echo "bheuneqjbexolgurfrjbeqfthneqrqcyrnfrqbagfgrny(p)NccyrPbzchgreVap" | tr 'A-Za-z' 'N-ZA-Mn-za-m')"
# Disable High Precision Timer
if [ "${QEMU_VER_SHORT}" -ge 70 ]; then
MACHINE_TYPE+=",hpet=off"
else
GUEST_TWEAKS+=" -no-hpet"
fi
;;
windows|windows-server)
if [ "${QEMU_VER_SHORT}" -gt 60 ]; then
CPU="-cpu ${CPU_MODEL},+hypervisor,+invtsc,l3-cache=on,migratable=no,hv_passthrough"
else
CPU="-cpu ${CPU_MODEL},+hypervisor,+invtsc,l3-cache=on,migratable=no,hv_frequencies${CPU_KVM_UNHALT},hv_reenlightenment,hv_relaxed,hv_spinlocks=8191,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vendor_id=1234567890ab,hv_vpindex"
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+=" -global ICH9-LPC.disable_s3=1"
# Disable High Precision Timer
if [ "${QEMU_VER_SHORT}" -ge 70 ]; then
MACHINE_TYPE+=",hpet=off"
else
GUEST_TWEAKS+=" -no-hpet"
fi
;;
esac
if [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ] && [ "${guest_os}" != "macos" ]; then
CPU+=",topoext"
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
# macOS guests cannot boot with most core counts not powers of 2.
# Find the nearest but lowest power of 2 using a predefined table
if [ "${guest_os}" == "macos" ]; then
local POWERS=(1 2 4 8 16 32 64 128 256 512 1024)
for (( i=${#POWERS[@]}-1; i>=0; i-- )); do
if [ "${POWERS[i]}" -le "${GUEST_CPU_CORES}" ]; then
GUEST_CPU_CORES="${POWERS[i]}"
break
fi
done
fi
if [ "${OS_KERNEL}" == "Darwin" ]; then
# Get the number of physical cores
physicalcpu=$(sysctl -n hw.physicalcpu)
# Get the number of logical processors
logicalcpu=$(sysctl -n hw.logicalcpu)
# Check if Hyper-Threading is enabled
if [ "${logicalcpu}" -gt "${physicalcpu}" ]; then
HOST_CPU_SMT="on"
else
HOST_CPU_SMT="off"
fi
elif [ -e /sys/devices/system/cpu/smt/control ]; then
HOST_CPU_SMT=$(cat /sys/devices/system/cpu/smt/control)
fi
# Account for Hyperthreading/SMT.
if [ "${GUEST_CPU_CORES}" -ge 2 ]; then
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
SMP="-smp cores=${GUEST_CPU_LOGICAL_CORES},threads=${GUEST_CPU_THREADS},sockets=${HOST_CPU_SOCKETS}"
echo " - CPU: ${HOST_CPU_MODEL}"
echo " - CPU VM: ${CPU_MODEL%%,*}, ${HOST_CPU_SOCKETS} Socket(s), ${GUEST_CPU_LOGICAL_CORES} Core(s), ${GUEST_CPU_THREADS} Thread(s)"
if [ "${guest_os}" == "macos" ] || [ "${guest_os}" == "windows" ] || [ "${guest_os}" == "windows-server" ]; then
# Display MSRs alert if the guest is macOS or windows
ignore_msrs_alert
fi
}
function configure_ram() {
local OS_PRETTY_NAME=""
RAM_VM="2G"
if [ -z "${ram}" ]; then
local RAM_HOST=""
if [ "${OS_KERNEL}" == "Darwin" ]; then
RAM_HOST=$(($(sysctl -n hw.memsize) / (1048576*1024)))
else
# Determine the number of gigabytes of RAM in the host by extracting the first numerical value from the output.
RAM_HOST=$(free --giga | tr ' ' '\n' | grep -m 1 "[0-9]" )
fi
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_VM} RAM"
case "${guest_os}" in
windows|windows-server)
OS_PRETTY_NAME="Windows"
min_ram="4"
;;
macos)
OS_PRETTY_NAME="macOS"
min_ram="8"
;;
esac
if [ -n "${min_ram}" ] && [ "${RAM_VM//G/}" -lt "${min_ram}" ]; then
if [ -z "${ram}" ]; then
echo " ERROR! The guest virtual machine has been allocated insufficient RAM to run ${OS_PRETTY_NAME}."
echo " You can override the guest RAM allocation by adding 'ram=${min_ram}G' to ${VM}"
exit 1
else
echo " WARNING! You have allocated less than the recommended amount of RAM to run ${OS_PRETTY_NAME}."
fi
fi
}
function configure_bios() {
# Always Boot macOS using EFI
if [ "${guest_os}" == "macos" ]; then
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"
elif [ -e "${VMDIR}/OVMF_CODE.fd" ] && [ -e "${VMDIR}/OVMF_VARS-1920x1080.fd" ]; then
EFI_CODE="${VMDIR}/OVMF_CODE.fd"
EFI_VARS="${VMDIR}/OVMF_VARS-1920x1080.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.
local SHARE_PATH="/usr/share"
if [ "${OS_KERNEL}" == "Darwin" ]; then
# Do not assume brew; quickemu could have been installed via Nix
if command -v brew &>/dev/null; then
SHARE_PATH="$(brew --prefix qemu)/share"
fi
fi
# https://bugzilla.redhat.com/show_bug.cgi?id=1929357#c5
# TODO: Check if macOS should use 'edk2-i386-vars.fd'
if [ -n "${EFI_CODE}" ] || [ ! -e "${EFI_CODE}" ]; then
case ${secureboot} in
on) # shellcheck disable=SC2054,SC2140
ovmfs=("${SHARE_PATH}/OVMF/OVMF_CODE_4M.secboot.fd","${SHARE_PATH}/OVMF/OVMF_VARS_4M.fd" \
"${SHARE_PATH}/edk2/ovmf/OVMF_CODE.secboot.fd","${SHARE_PATH}/edk2/ovmf/OVMF_VARS.fd" \
"${SHARE_PATH}/OVMF/x64/OVMF_CODE.secboot.fd","${SHARE_PATH}/OVMF/x64/OVMF_VARS.fd" \
"${SHARE_PATH}/edk2-ovmf/OVMF_CODE.secboot.fd","${SHARE_PATH}/edk2-ovmf/OVMF_VARS.fd" \
"${SHARE_PATH}/qemu/ovmf-x86_64-smm-ms-code.bin","${SHARE_PATH}/qemu/ovmf-x86_64-smm-ms-vars.bin" \
"${SHARE_PATH}/qemu/edk2-x86_64-secure-code.fd","${SHARE_PATH}/qemu/edk2-x86_64-code.fd" \
"${SHARE_PATH}/edk2-ovmf/x64/OVMF_CODE.secboot.fd","${SHARE_PATH}/edk2-ovmf/x64/OVMF_VARS.fd"
);;
*) # shellcheck disable=SC2054,SC2140
ovmfs=("${SHARE_PATH}/OVMF/OVMF_CODE_4M.fd","${SHARE_PATH}/OVMF/OVMF_VARS_4M.fd" \
"${SHARE_PATH}/edk2/ovmf/OVMF_CODE.fd","${SHARE_PATH}/edk2/ovmf/OVMF_VARS.fd" \
"${SHARE_PATH}/OVMF/OVMF_CODE.fd","${SHARE_PATH}/OVMF/OVMF_VARS.fd" \
"${SHARE_PATH}/OVMF/x64/OVMF_CODE.fd","${SHARE_PATH}/OVMF/x64/OVMF_VARS.fd" \
"${SHARE_PATH}/edk2-ovmf/OVMF_CODE.fd","${SHARE_PATH}/edk2-ovmf/OVMF_VARS.fd" \
"${SHARE_PATH}/qemu/ovmf-x86_64-4m-code.bin","${SHARE_PATH}/qemu/ovmf-x86_64-4m-vars.bin" \
"${SHARE_PATH}/qemu/edk2-x86_64-code.fd","${SHARE_PATH}/qemu/edk2-x86_64-code.fd" \
"${SHARE_PATH}/edk2-ovmf/x64/OVMF_CODE.fd","${SHARE_PATH}/edk2-ovmf/x64/OVMF_VARS.fd"
);;
esac
# 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
# shellcheck disable=SC2086
set -- ${f};
if [ -e "${1}" ]; then
EFI_CODE="${1}"
EFI_EXTRA_VARS="${2}"
fi
done
IFS=$_IFS
fi
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}"
}
function configure_os_quirks() {
if [ "${guest_os}" == "batocera" ] || [ "${guest_os}" == "freedos" ] || [ "${guest_os}" == "haiku" ] || [ "${guest_os}" == "kolibrios" ]; then
NET_DEVICE="rtl8139"
fi
if [ "${guest_os}" == "freebsd" ] || [ "${guest_os}" == "ghostbsd" ]; then
mouse="usb"
fi
case ${guest_os} in
windows-server) NET_DEVICE="e1000";;
*bsd|linux*|windows) NET_DEVICE="virtio-net";;
freedos) sound_card="sb16";;
*solaris) usb_controller="xhci"
sound_card="ac97";;
reactos) NET_DEVICE="e1000"
keyboard="ps2";;
macos)
# 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
big-sur|monterey|ventura|sonoma)
BALLOON="-device virtio-balloon"
MAC_DISK_DEV="virtio-blk-pci"
NET_DEVICE="virtio-net"
USB_HOST_PASSTHROUGH_CONTROLLER="nec-usb-xhci"
GUEST_TWEAKS+=" -global nec-usb-xhci.msi=off"
sound_card="${sound_card:-usb-audio}"
usb_controller="xhci";;
*)
# Backwards compatibility if no macos_release is specified.
# Also safe catch all for High Sierra and Mojave
BALLOON=""
if [ "${macos_release}" == "catalina" ]; then
MAC_DISK_DEV="virtio-blk-pci"
else
MAC_DISK_DEV="ide-hd,bus=ahci.2"
fi
NET_DEVICE="vmxnet3"
USB_HOST_PASSTHROUGH_CONTROLLER="usb-ehci";;
esac
;;
*) NET_DEVICE="rtl8139";;
esac
}
function configure_storage() {
local create_options=""
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
case ${disk_format} in
qcow2) create_options="lazy_refcounts=on,preallocation=${preallocation},nocow=on";;
raw) create_options="preallocation=${preallocation}";;
*) true;;
esac
# https://blog.programster.org/qcow2-performance
if ! ${QEMU_IMG} create -q -f "${disk_format}" -o "${create_options=}" "${disk_img}" "${disk_size}"; then
echo "ERROR! Failed to create ${disk_img} using ${disk_format} format."
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
# If the VM is not running, check for disk related issues.
if [ -z "${VM_PID}" ]; 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
fi
else
if ! ${QEMU_IMG} check -q "${disk_img}"; then
echo " Disk integrity check failed. Please run qemu-img check --help."
echo
"${QEMU_IMG}" check "${disk_img}"
exit 1
fi
fi
# 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
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
}
function configure_display() {
# Determine which audio driver use between Pulseaudio or ALSA
local AUDIO_DRIVER="pa"
if ! command -v pacmd >/dev/null 2>&1 ; then
AUDIO_DRIVER="alsa"
fi
# Setup the appropriate audio device based on the display output
# https://www.kraxel.org/blog/2020/01/qemu-sound-audiodev/
case ${display} in
cocoa) AUDIO_DEV="coreaudio,id=audio0";;
none|spice|spice-app) AUDIO_DEV="spice,id=audio0";;
*) AUDIO_DEV="${AUDIO_DRIVER},id=audio0";;
esac
# Determine a sane resolution for Linux guests.
local X_RES="1280"
local Y_RES="800"
if [ -n "${width}" ] && [ -n "${height}" ]; then
local X_RES="${width}"
local Y_RES="${height}"
fi
# https://www.kraxel.org/blog/2019/09/display-devices-in-qemu/
case ${guest_os} in
*bsd) DISPLAY_DEVICE="VGA";;
linux_old|solaris) DISPLAY_DEVICE="vmware-svga";;
linux)
case ${display} in
none|spice|spice-app) DISPLAY_DEVICE="virtio-gpu";;
*) DISPLAY_DEVICE="virtio-vga";;
esac;;
macos)
# qxl-vga and VGA supports seamless mouse and sane resolutions if only
# one scanout is used. '-vga none' is added to the QEMU command line
# to avoid having two scanouts.
DISPLAY_DEVICE="VGA";;
windows|windows-server)
# virtio-gpu "works" with gtk but is limited to 1024x1024 and exhibits other issues
# https://kevinlocke.name/bits/2021/12/10/windows-11-guest-virtio-libvirt/#video
case ${display} in
gtk|none|spice) DISPLAY_DEVICE="qxl-vga";;
cocoa|sdl|spice-app) DISPLAY_DEVICE="virtio-vga";;
esac;;
*) DISPLAY_DEVICE="qxl-vga";;
esac
# Map Quickemu $display to QEMU -display
case ${display} in
gtk) DISPLAY_RENDER="${display},grab-on-hover=on,zoom-to-fit=off,gl=${gl}";;
none|spice) DISPLAY_RENDER="none";;
sdl) DISPLAY_RENDER="${display},gl=${gl}";;
spice-app) DISPLAY_RENDER="${display},gl=${gl}";;
*) DISPLAY_RENDER="${display}";;
esac
# https://www.kraxel.org/blog/2021/05/virtio-gpu-qemu-graphics-update/
if [ "${gl}" == "on" ] && [ "${DISPLAY_DEVICE}" == "virtio-vga" ]; then
if [ "${QEMU_VER_SHORT}" -ge 61 ]; then
DISPLAY_DEVICE="${DISPLAY_DEVICE}-gl"
else
DISPLAY_DEVICE="${DISPLAY_DEVICE},virgl=on"
fi
echo -n " - Display: ${display^^}, ${DISPLAY_DEVICE}, GL (${gl}), VirGL (on)"
else
echo -n " - Display: ${display^^}, ${DISPLAY_DEVICE}, GL (${gl}), VirGL (off)"
fi
# Build the video configuration
VIDEO="-device ${DISPLAY_DEVICE}"
# Try and coerce the display resolution for Linux guests only.
if [ "${DISPLAY_DEVICE}" != "vmware-svga" ]; then
VIDEO="${VIDEO},xres=${X_RES},yres=${Y_RES}"
echo " @ (${X_RES} x ${Y_RES})"
else
echo " "
fi
# Allocate VRAM to VGA devices
case ${DISPLAY_DEVICE} in
bochs-display) VIDEO="${VIDEO},vgamem=67108864";;
qxl|qxl-vga) VIDEO="${VIDEO},ram_size=65536,vram_size=65536,vgamem_mb=64";;
ati-vga|cirrus-vga|VGA|vmware-svga) VIDEO="${VIDEO},vgamem_mb=256";;
esac
# Configure multiscreen if max_outputs was provided in the .conf file
if [ -n "${max_outputs}" ]; then
VIDEO="${VIDEO},max_outputs=${max_outputs}"
fi
# Run QEMU with '-vga none' to avoid having two scanouts, one for VGA and
# another for virtio-vga-gl. This works around a GTK assertion failure and
# allows seamless mouse in macOS when using the qxl-vga device.
# https://www.collabora.com/news-and-blog/blog/2021/11/26/venus-on-qemu-enabling-new-virtual-vulkan-driver/
# https://github.com/quickemu-project/quickemu/issues/222
VGA="-vga none"
# Add fullscreen options
VIDEO="${VGA} ${VIDEO} ${FULLSCREEN}"
}
function configure_audio() {
# Build the sound hardware configuration
case ${sound_card} in
ich9-intel-hda|intel-hda) SOUND="-device ${sound_card} -device ${sound_duplex},audiodev=audio0";;
usb-audio) SOUND="-device ${sound_card},audiodev=audio0";;
ac97|es1370|sb16) SOUND="-device ${sound_card},audiodev=audio0";;
none) SOUND="";;
esac
echo " - Sound: ${sound_card} (${sound_duplex})"
}
function configure_ports() {
echo -n "" > "${VMDIR}/${VMNAME}.ports"
if [ -z "${ssh_port}" ]; then
# Find a free port to expose ssh to the guest
ssh_port=$(get_port 22220 9)
fi