Refactor VM configuration builder using an array or arguments

Also create a (non-executable) shell script in the VM directory which includes the last used VM configuration.
This commit is contained in:
Martin Wimpress 2021-09-27 22:50:06 +01:00
parent 0dcbe75fb5
commit 89e61c4e9e
No known key found for this signature in database
GPG key ID: 61DF940515E06DA3

171
quickemu
View file

@ -436,99 +436,98 @@ function vm_boot() {
enable_usb_passthrough enable_usb_passthrough
# Build the VM configuration # Boot the VM
local DISKS="" local args=()
local NET_DEVICE=""
local SMARTCARD=""
local USB_HOSTPASS=""
local USB_SPICEPASS=""
if [ "${guest_os}" == "macos" ]; then # shellcheck disable=SC2054,SC2206
DISKS="-drive if=pflash,format=raw,readonly=on,file=${EFI_CODE} args+=(-name ${VMNAME},process=${VMNAME}
-drive if=pflash,format=raw,file=${EFI_VARS} -enable-kvm -machine q35,vmport=off ${GUEST_TWEAKS}
-drive id=OpenCore,if=none,snapshot=on,format=qcow2,file=${VMDIR}/OpenCore.qcow2 ${CPU} ${SMP}
-device virtio-blk-pci,drive=OpenCore,scsi=off" -m ${RAM_VM} -device virtio-balloon
if [ -n "${img}" ]; then -smbios type=2
DISKS="${DISKS} ${VIDEO} -display ${OUTPUT}
-drive id=InstallMedia,if=none,format=raw,readonly=on,file=${img} -device usb-ehci,id=input
-device virtio-blk-pci,drive=InstallMedia,scsi=off" -device usb-kbd,bus=input.0
fi -device usb-tablet,bus=input.0
DISKS="${DISKS} -device ${NET_DEVICE},netdev=nic -netdev ${NET},id=nic
-drive id=SystemDisk,if=none,format=qcow2,file=${disk_img} ${STATUS_QUO} -audiodev pa,id=pa,out.stream-name=${LAUNCHER}-${VMNAME},in.stream-name=${LAUNCHER}-${VMNAME}
-device virtio-blk-pci,drive=SystemDisk,scsi=off" -device intel-hda -device hda-duplex,audiodev=pa,mixer=off
NET_DEVICE="vmxnet3" -rtc base=localtime,clock=host
# UNTESTED! USB2 passthrough since USB3 isn't(?) supported in macOS VMs -object rng-random,id=rng0,filename=/dev/urandom
# USB_SPICEPASS=" -device virtio-rng-pci,rng=rng0
# -device usb-ehci,id=spicepass -spice ${SPICE}
# -device ich9-usb-uhci1,masterbus=spicepass.0,firstport=0,multifunction=on -device virtio-serial-pci
# -device ich9-usb-uhci2,masterbus=spicepass.0,firstport=2 -chardev spicevmc,id=vdagent0,name=vdagent
# -device ich9-usb-uhci3,masterbus=spicepass.0,firstport=4 -device virtserialport,chardev=vdagent0,name=com.redhat.spice.0
# -chardev spicevmc,name=usbredir,id=usbredirchardev1 -device virtio-serial-pci
# -device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 -chardev spiceport,id=webdav0,name=org.spice-space.webdav.0
# -chardev spicevmc,name=usbredir,id=usbredirchardev2 -device virtserialport,chardev=webdav0,name=org.spice-space.webdav.0
# -device usb-redir,chardev=usbredirchardev2,id=usbredirdev2 -serial mon:stdio)
# -chardev spicevmc,name=usbredir,id=usbredirchardev3
# -device usb-redir,chardev=usbredirchardev3,id=usbredirdev3" # Add the disks
USB_HOSTPASS="-device usb-ehci,id=hostpass if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then
${USB_PASSTHROUGH}" # shellcheck disable=SC2054
else args+=(-drive if=pflash,format=raw,readonly=on,file="${EFI_CODE}"
if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then -drive if=pflash,format=raw,file="${EFI_VARS}")
DISKS="-drive if=pflash,format=raw,readonly=on,file=${EFI_CODE}
-drive if=pflash,format=raw,file=${EFI_VARS}"
fi
DISKS="${DISKS}
-drive media=cdrom,index=0,file=${iso}
-drive media=cdrom,index=1,file=${driver_iso}
-drive if=none,id=drive0,cache=directsync,aio=native,format=qcow2,file=${disk_img}
-device virtio-blk-pci,drive=drive0,scsi=off ${STATUS_QUO}"
NET_DEVICE="virtio-net"
SMARTCARD="-device usb-ccid
-chardev spicevmc,id=ccid,name=smartcard
-device ccid-card-passthru,chardev=ccid"
USB_SPICEPASS="-device qemu-xhci,id=spicepass
-chardev spicevmc,id=usbredirchardev1,name=usbredir
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1
-chardev spicevmc,id=usbredirchardev2,name=usbredir
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2
-chardev spicevmc,id=usbredirchardev3,name=usbredir
-device usb-redir,chardev=usbredirchardev3,id=usbredirdev3"
USB_HOSTPASS="-device qemu-xhci,id=hostpass
${USB_PASSTHROUGH}"
fi fi
# Boot the VM if [ "${guest_os}" == "macos" ]; then
${QEMU} \ # shellcheck disable=SC2054
-name ${VMNAME},process=${VMNAME} \ args+=(-device ahci,id=ahci
-enable-kvm -machine q35,vmport=off ${GUEST_TWEAKS} \ -drive id=OpenCore,if=none,format=qcow2,file="${VMDIR}/OpenCore.qcow2"
${CPU} ${SMP} \ -device ide-hd,bus=ahci.0,drive=OpenCore)
-m ${RAM_VM} -device virtio-balloon \ if [ -n "${img}" ]; then
${DISKS} \ # shellcheck disable=SC2054
${VIDEO} -display ${OUTPUT} \ args+=(-drive id=InstallMedia,if=none,format=raw,file="${img}"
-device usb-ehci,id=input \ -device ide-hd,bus=ahci.1,drive=InstallMedia)
-device usb-kbd,bus=input.0 \ fi
-device usb-tablet,bus=input.0 \
${USB_SPICEPASS} \ # TODO: High Sierra and older doesn't support VirtIO disks.
${USB_HOSTPASS} \ # Add an option to use VirtIO
${SMARTCARD} \ if true; then
-device ${NET_DEVICE},netdev=nic -netdev ${NET},id=nic \ # shellcheck disable=SC2054,SC2206
-audiodev pa,id=pa,out.stream-name=${LAUNCHER}-${VMNAME},in.stream-name=${LAUNCHER}-${VMNAME} \ args+=(-drive id=SystemDisk,if=none,format=qcow2,file="${disk_img}"
-device intel-hda -device hda-duplex,audiodev=pa,mixer=off \ -device ide-hd,bus=ahci.2,drive=SystemDisk ${STATUS_QUO})
-rtc base=localtime,clock=host \ else
-object rng-random,id=rng0,filename=/dev/urandom \ # shellcheck disable=SC2054,SC2206
-device virtio-rng-pci,rng=rng0 \ args+=(-drive id=SystemDisk,if=none,format=qcow2,file="${disk_img}"
-spice ${SPICE} \ -device virtio-blk-pci,drive=SystemDisk,scsi=off ${STATUS_QUO})
-device virtio-serial-pci \ fi
-chardev spicevmc,id=vdagent0,name=vdagent \ # shellcheck disable=SC2054
-device virtserialport,chardev=vdagent0,name=com.redhat.spice.0 \ args+=(-drive media=cdrom,index=0,file="${iso}")
-device virtio-serial-pci \ else
-chardev spiceport,id=webdav0,name=org.spice-space.webdav.0 \ # shellcheck disable=SC2054,SC2206
-device virtserialport,chardev=webdav0,name=org.spice-space.webdav.0 \ args+=(-drive media=cdrom,index=0,file="${iso}"
-serial mon:stdio > "${VMDIR}/${VMNAME}.log" & -drive media=cdrom,index=1,file="${driver_iso}"
-drive id=drive0,if=none,cache=directsync,aio=native,format=qcow2,file="${disk_img}"
-device virtio-blk-pci,drive=drive0,scsi=off ${STATUS_QUO}
-device qemu-xhci,id=spicepass
-chardev spicevmc,id=usbredirchardev1,name=usbredir
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1
-chardev spicevmc,id=usbredirchardev2,name=usbredir
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2
-chardev spicevmc,id=usbredirchardev3,name=usbredir
-device usb-redir,chardev=usbredirchardev3,id=usbredirdev3
-device usb-ccid
-chardev spicevmc,id=ccid,name=smartcard
-device ccid-card-passthru,chardev=ccid)
fi
if [ -n "${USB_PASSTHROUGH}" ]; then
# shellcheck disable=SC2054,SC2206
args+=(-device ${USB_HOST_PASSTHROUGH_CONTROLLER},id=hostpass
${USB_PASSTHROUGH})
fi
echo "#!/usr/bin/env bash" > "${VMDIR}/${VMNAME}.sh"
echo "${QEMU}" "${args[@]}" >> "${VMDIR}/${VMNAME}.sh"
${QEMU} "${args[@]}" >> "${VMDIR}/${VMNAME}.log" &
echo " - PID: ${!}" echo " - PID: ${!}"
# If output is 'none' then SPICE was requested. # If output is 'none' then SPICE was requested.
if [ ${OUTPUT} == "none" ]; then if [ ${OUTPUT} == "none" ]; then
spicy --title "${VMNAME}" --port ${SPICE_PORT} ${FULLSPICY} --spice-shared-dir "${HOME}" >/dev/null 2>&1 spicy --title "${VMNAME}" --port "${SPICE_PORT}" "${FULLSPICY}" --spice-shared-dir "${HOME}" >/dev/null 2>&1
fi fi
} }