Skip to content

Instantly share code, notes, and snippets.

@inclyc
Last active September 20, 2023 11:21
Show Gist options
  • Save inclyc/d9a36238c10101bc38e8cf0f94ec16b4 to your computer and use it in GitHub Desktop.
Save inclyc/d9a36238c10101bc38e8cf0f94ec16b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# shellcheck disable=SC2054
# Tweakable options, comment to unset.
# Passthrough the GPU (hardware) via PCI Passthrough (OVMF)
EnableGPUPassthrough=
# Whether or not add a sound device, connect to the system
# EnableSystemSound=
# Add vGPU (virtio)
# EnableEmulatedGPU=
# Evdev Keyboard & mouse
EnableEvdevInputs=
# [ The Machine ]
cmd=(
qemu-system-x86_64
# For PCIe.
-machine q35
-accel kvm
# Windows guest, Enabling Hyper-V enlightenments with KVM
# https://blog.wikichoon.com/2014/07/enabling-hyper-v-enlightenments-with-kvm.html
-cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time
# https://forum.level1techs.com/t/need-help-stop-qemu-from-crashing-when-looking-glass-ivshmem-plain-is-enabled/191836/3
# -cpu Skylake-Client-v4
-rtc base=localtime,clock=host
-smp 16
-m 32768
-serial none
# UEFI machine
-drive "if=pflash,format=raw,readonly=on,file=/home/lyc/VM/share/OVMF/OVMF_CODE.fd"
-drive "if=pflash,format=raw,file=./OVMF_VARS.fd"
)
# [ Sound Card ]
if [ ! -z ${EnableSystemSound+x} ]; then
cmd+=(
-audiodev pa,id=snd0
-device ich9-intel-hda
-device hda-output,audiodev=snd0
)
fi
# [ Display ]
if [ ! -z ${EnableEmulatedGPU+x} ]; then
cmd+=(
# -vnc :5
# -vga qxl
-spice port=5905,disable-ticketing=on
-device "virtio-vga-gl"
-display "sdl,gl=on"
)
else
cmd+=(
-nographic
-vga none
)
fi
if [ ! -z "${EnableGPUPassthrough+x}" ]; then
cmd+=(
# VGA compatible controller: NVIDIA Corporation AD102 [GeForce RTX 4090] (rev a1)
-device vfio-pci,host=01:00.0,x-vga=on
# Audio device: NVIDIA Corporation AD102 High Definition Audio Controller (rev a1)
-device vfio-pci,host=01:00.1
# VGA compatible controller [0300]: NVIDIA Corporation TU106 [GeForce RTX 2060 SUPER] [10de:1f06] (rev a1)
# -device vfio-pci,host=01:00.0,x-vga=on
# Audio device [0403]: NVIDIA Corporation TU106 High Definition Audio Controller [10de:10f9] (rev a1)
# -device vfio-pci,host=01:00.1
# USB controller [0c03]: NVIDIA Corporation TU106 USB 3.1 Host Controller [10de:1ada] (rev a1)
# -device vfio-pci,host=01:00.2
# Serial bus controller [0c80]: NVIDIA Corporation TU106 USB Type-C UCSI Controller [10de:1adb] (rev a1)
# -device vfio-pci,host=01:00.3
)
fi
# [ Input Devices ]
if [ ! -z ${EnableEvdevInputs+x} ]; then
cmd+=(
# A4Tech Co., Ltd. USB Mouse
# -device usb-host,bus=xhci.0,vendorid=0x09da,productid=0xc10a
-object "input-linux,id=mouse1,evdev=/dev/input/by-id/usb-A4Tech_USB_Mouse-event-mouse"
# STMicroelectronics 108EC-XRGB
# -device usb-host,bus=xhci.0,vendorid=0x0483,productid=0x5130
-object "input-linux,id=kbd1,evdev=/dev/input/by-id/usb-Milsky_108EC-XRGB_CA2018120001-event-kbd,grab_all=on,repeat=on"
)
fi
# [ USB ]
cmd+=(
-device qemu-xhci,id=xhci
# Logitech, Inc. Webcam C310
-device usb-host,bus=xhci.0,vendorid=0x046d,productid=0x081b
)
# [ Network ]
cmd+=(
# -device virtio-net,netdev=network0
# -netdev tap,id=network0,ifname=vmelaratap0,script=no,downscript=no
)
# [ Storage ]
cmd+=(
-drive file="$1"
# -snapshot
)
# [ Monitor]
cmd+=(
-monitor unix:./monitor.sock,server,nowait
)
exec "${cmd[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment