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
# Build the VM configuration
local DISKS=""
local NET_DEVICE=""
local SMARTCARD=""
local USB_HOSTPASS=""
local USB_SPICEPASS=""
# Boot the VM
local args=()
if [ "${guest_os}" == "macos" ]; then
DISKS="-drive if=pflash,format=raw,readonly=on,file=${EFI_CODE}
-drive if=pflash,format=raw,file=${EFI_VARS}
-drive id=OpenCore,if=none,snapshot=on,format=qcow2,file=${VMDIR}/OpenCore.qcow2
-device virtio-blk-pci,drive=OpenCore,scsi=off"
if [ -n "${img}" ]; then
DISKS="${DISKS}
-drive id=InstallMedia,if=none,format=raw,readonly=on,file=${img}
-device virtio-blk-pci,drive=InstallMedia,scsi=off"
fi
DISKS="${DISKS}
-drive id=SystemDisk,if=none,format=qcow2,file=${disk_img} ${STATUS_QUO}
-device virtio-blk-pci,drive=SystemDisk,scsi=off"
NET_DEVICE="vmxnet3"
# UNTESTED! USB2 passthrough since USB3 isn't(?) supported in macOS VMs
# USB_SPICEPASS="
# -device usb-ehci,id=spicepass
# -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
if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then
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}"
# shellcheck disable=SC2054,SC2206
args+=(-name ${VMNAME},process=${VMNAME}
-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
# Boot the VM
${QEMU} \
-name ${VMNAME},process=${VMNAME} \
-enable-kvm -machine q35,vmport=off ${GUEST_TWEAKS} \
${CPU} ${SMP} \
-m ${RAM_VM} -device virtio-balloon \
${DISKS} \
${VIDEO} -display ${OUTPUT} \
-device usb-ehci,id=input \
-device usb-kbd,bus=input.0 \
-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" &
if [ "${guest_os}" == "macos" ]; then
# shellcheck disable=SC2054
args+=(-device ahci,id=ahci
-drive id=OpenCore,if=none,format=qcow2,file="${VMDIR}/OpenCore.qcow2"
-device ide-hd,bus=ahci.0,drive=OpenCore)
if [ -n "${img}" ]; then
# shellcheck disable=SC2054
args+=(-drive id=InstallMedia,if=none,format=raw,file="${img}"
-device ide-hd,bus=ahci.1,drive=InstallMedia)
fi
# TODO: High Sierra and older doesn't support VirtIO disks.
# Add an option to use VirtIO
if true; then
# shellcheck disable=SC2054,SC2206
args+=(-drive id=SystemDisk,if=none,format=qcow2,file="${disk_img}"
-device ide-hd,bus=ahci.2,drive=SystemDisk ${STATUS_QUO})
else
# shellcheck disable=SC2054,SC2206
args+=(-drive id=SystemDisk,if=none,format=qcow2,file="${disk_img}"
-device virtio-blk-pci,drive=SystemDisk,scsi=off ${STATUS_QUO})
fi
# shellcheck disable=SC2054
args+=(-drive media=cdrom,index=0,file="${iso}")
else
# shellcheck disable=SC2054,SC2206
args+=(-drive media=cdrom,index=0,file="${iso}"
-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: ${!}"
# If output is 'none' then SPICE was requested.
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
}