#!/bin/bash
OPTS=""
# Basic CPU settings.  Also fix Nvidia fuckery
#OPTS="$OPTS -cpu host,kvm=off,hv_time,hv_relaxed,hv_vapic,hv_spinlocks=0x1fff"
OPTS="$OPTS -cpu host,kvm=off,hv_time,hv_relaxed,hv_vapic,hv_spinlocks=0x1fff,hv_vendor_id=Nvidia43FIX"
#OPTS="$OPTS -cpu host,kvm=off"
#OPTS="$OPTS -smp 4,sockets=1,cores=4,threads=1"
OPTS="$OPTS -smp 3,sockets=1,cores=3,threads=1"
# Enable KVM full virtualization support.
OPTS="$OPTS -enable-kvm"
# Assign memory to the vm.
OPTS="$OPTS -m 8192"
# VFIO GPU and GPU sound passthrough.
OPTS="$OPTS -device vfio-pci,host=01:00.0,multifunction=on"
OPTS="$OPTS -device vfio-pci,host=01:00.1"
# VFIO USB Card Passthrough
OPTS="$OPTS -device vfio-pci,host=06:00.0"
# Supply OVMF (general UEFI bios, needed for EFI boot support with GPT disks).
OPTS="$OPTS -drive if=pflash,format=raw,readonly,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd"
OPTS="$OPTS -drive if=pflash,format=raw,file=$(pwd)/OVMF_VARS-pure-efi.fd"
# Load our created VM image as a harddrive.
OPTS="$OPTS -hda $(pwd)/qemu_vm.qcow2"
# Load secondary drive for game storage
OPTS="$OPTS -hdb /media/storage/vmstorage/gamevmaddl.qcow2"
# Load our OS setup image e.g. ISO file.
OPTS="$OPTS -cdrom $(pwd)/win10.iso"
# Enable USB support for passthrough
OPTS="$OPTS -usb"
# Use the following emulated video device (use none for disabled).
#OPTS="$OPTS -vga qxl"
OPTS="$OPTS -vga none"
# Redirect QEMU's console input and output.
OPTS="$OPTS -monitor stdio"
# Add audio
OPTS="$OPTS -soundhw hda"
# Add networking
OPTS="$OPTS -device e1000,netdev=net0,mac=DE:AD:BE:EF:01:02 -netdev tap,id=net0"
# Remote desktop
#OPTS="$OPTS -vnc :1"

VM_SOUND=""
VM_SOUND="$VM_SOUND QEMU_AUDIO_DRV=pa"
VM_SOUND="$VM_SOUND QEMU_PA_SAMPLES=64"

sudo /usr/local/bin/vfio-bind 0000:06:00.0
sleep 5
sudo bash -c "echo 400000 > /sys/module/kvm/parameters/halt_poll_ns"
sudo $VM_SOUND qemu-system-x86_64 $OPTS

#usb_add host:054c:05c4
