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

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