Virtualization with GPU Passthrough

  • Reasons why
  • Technologies
  • UEFI BIos settings
  • Hardware
  • Grub VFIO settings
  • Creating VM
  • Kernel module fix for RX5700XT
  • Fixing issues and mitigation
  • Automatic start of power control VM from sleep, cold boot.
  • Enabling hotkeys for VM start, Display toggle.
  • Configs files

Reasons:

As a Linux enthusiast, I want to run Linux, Period! But I also want to enjoy me VR gaming on my Oculus quest, which is sadly not supported on Linux yet. Or rather, Facebook will properly never support Linux in terms on the Oculus Quest.

I do not want to dualboot, rather I want to be able to start up a VM concurrently, and have a pleasent time with my VR Gaming, while still having access to my tools on my Linux box.

Though now that might change with Valve supporting everything including cheat protection soon. If so, the Valve Index might be another solution to run under Linux, but until then, I will run a setup where I doubleboot. One as bare metal, other as a virtual machine.

UEFI Bios settings

  • Version:F61A
  • I have enabled IOMMU, and disabled SMT(Multithreads)
  • As I am running a mix of older and newer ECC RAM, which is error correcting and should protect against bit flips.
  • The RAM is not super fast, but I can get stable clockspeeds on the RAM running 2933MHz on all memory modules.
  • Disabled RGB Fusion
  • Disabled Fastboot
  • Disabled Secure Boot (not strcty neded)
  • CSM support Enabled
  • The first PCIe slot is running x16 lanes where the second PCIe slot is has x8 lanes, the 3 slot also have 8x lanes, but is sharing with the NVME slotAs I want to use my Rx550 as primary for Ubuntu, and this is connected secodary PCI-Express Slot for the VM, set Initial Display Output to PCIe 2 slot.

Technologies:

  • GNU/Linux, Ubuntu
  • KVM, Libvirt, Virsh, Virtual Machine Manager
  • IOMMU
  • Bash scripts
  • xrand
  • SystemD,
  • Grub Boot loader.
  • Linux Kernel Modules, dkms
  • Git
  • Nano, text editor
  • Bluetooth

Hardware

I have choosen my hardware to be effective, stable and silent.

  • AMD Ryzen 7 2700x 8core
  • Gigabyte Aorus x470 Wifi7 Motherboard
  • 2x8GB Kingston ECC 2400MHz Value Ram
  • 2x16GB Kingston ECC 2666MHz Value RAM
  • Gigabyte RX 550 GPU
  • Gigabyte RX 5700 XT Aurus
  • Multiple SSD from Samsung and OCZ
  • Onboard HD Audio (Family 17
  • Logitech Keyboard K613 Lightspeed/Bluetooth
  • Logitech Mouse K603 Bluetooth
  • Presonus Audiobox USB
  • Dell 23″ 2013 Display port
  • Dell 23″ 2012 Display port + DVI connection
  • Fractal Design Define C, Tempered Glass
  • Fractal Design Celsius S24 Water Cooling
  • Bose Companion2, Series 2. (2x input)

Enable VFIO and Identifying hardware:

Edit /etc/default/grub and find the line below. Append “iommu=1 intel_iommu=on”

GRUB_CMDLINE_LINUX_DEFAULT=”iommu=1 amd_iommu=on”

For Intel systems it should look something like this:

GRUB_CMDLINE_LINUX_DEFAULT=”iommu=1 intel_iommu=on”

Update Grub and reboot, for the edit to take affect.

sudo grub-update

sudo reboot

run the script ls-iommu as show below.

Look for the Entries with VGA, take note of the IOMMU group, and the hardware address [1003:8A23]

Under each GPU entry, you should also have the soundcard from GPU in the same IOMMU group.

sudo nano /etc/default/grub

# If you change this file, run ‘update-grub’ afterwards to update

# /boot/grub/grub.cfg.

# For full documentation of the options in this file, see:

# info -f grub -n ‘Simple configuration’

GRUB_DEFAULT=0

GRUB_TIMEOUT_STYLE=hidden

GRUB_TIMEOUT=10

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

GRUB_CMDLINE_LINUX_DEFAULT=”amd_iommu=on iommu=1″

GRUB_CMDLINE_LINUX=””

GRUB_RECORDFAIL_TIMEOUT=5

Once file has grub has been edited, you will need to run update-grub

sudo update-grub

sudo reboot

Output from ls-iommu:

  • IOMMU Group 0 00:01.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]
  • IOMMU Group 10 00:08.1 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Internal PCIe GPP Bridge 0 to Bus B [1022:1454]
  • IOMMU Group 11 00:14.0 SMBus [0c05]: Advanced Micro Devices, Inc. [AMD] FCH SMBus Controller [1022:790b] (rev 59)
  • IOMMU Group 11 00:14.3 ISA bridge [0601]: Advanced Micro Devices, Inc. [AMD] FCH LPC Bridge [1022:790e] (rev 51)
  • IOMMU Group 12 00:18.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 0 [1022:1460]
  • IOMMU Group 12 00:18.1 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 1 [1022:1461]
  • IOMMU Group 12 00:18.2 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 2 [1022:1462]
  • IOMMU Group 12 00:18.3 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 3 [1022:1463]
  • IOMMU Group 12 00:18.4 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 4 [1022:1464]
  • IOMMU Group 12 00:18.5 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 5 [1022:1465]
  • IOMMU Group 12 00:18.6 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 6 [1022:1466]
  • IOMMU Group 12 00:18.7 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 7 [1022:1467]
  • IOMMU Group 13 01:00.0 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD] Device [1022:43d0] (rev 01)
  • IOMMU Group 13 01:00.1 SATA controller [0106]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset SATA Controller [1022:43c8] (rev 01)
  • IOMMU Group 13 01:00.2 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Bridge [1022:43c6] (rev 01)
  • IOMMU Group 13 02:00.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 02:01.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 02:02.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 02:03.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 02:04.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 02:09.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] 400 Series Chipset PCIe Port [1022:43c7] (rev 01)
  • IOMMU Group 13 05:00.0 Ethernet controller [0200]: Intel Corporation I211 Gigabit Network Connection [8086:1539] (rev 03)
  • IOMMU Group 13 06:00.0 Network controller [0280]: Intel Corporation Wireless-AC 9260 [8086:2526] (rev 29)
  • IOMMU Group 13 07:00.0 Non-Volatile memory controller [0108]: Samsung Electronics Co Ltd NVMe SSD Controller SM981/PM981/PM983 [144d:a808]
  • IOMMU Group 13 08:00.0 USB controller [0c03]: ASMedia Technology Inc. ASM1143 USB 3.1 Host Controller [1b21:1343]
  • IOMMU Group 14 09:00.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD/ATI] Navi 10 XL Upstream Port of PCI Express Switch [1002:1478] (rev c1)
  • IOMMU Group 15 0a:00.0 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD/ATI] Navi 10 XL Downstream Port of PCI Express Switch [1002:1479]
  • IOMMU Group 16 0b:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Navi 10 [Radeon RX 5600 OEM/5600 XT / 5700/5700 XT] [1002:731f] (rev ff)
  • IOMMU Group 17 0b:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Navi 10 HDMI Audio [1002:ab38] (rev ff)
  • IOMMU Group 18 0c:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Lexa PRO [Radeon 540/540X/550/550X / RX 540X/550/550X] [1002:699f] (rev c7)
  • IOMMU Group 18 0c:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Baffin HDMI/DP Audio [Radeon RX 550 640SP / RX 560/560X] [1002:aae0]
  • IOMMU Group 19 0d:00.0 Non-Essential Instrumentation [1300]: Advanced Micro Devices, Inc. [AMD] Zeppelin/Raven/Raven2 PCIe Dummy Function [1022:145a]
  • IOMMU Group 1 00:01.3 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) PCIe GPP Bridge [1022:1453]
  • IOMMU Group 20 0d:00.2 Encryption controller [1080]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Platform Security Processor [1022:1456]
  • – IOMMU Group 21 0d:00.3 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD] Zeppelin USB 3.0 Host controller [1022:145f]
  • IOMMU Group 22 0e:00.0 Non-Essential Instrumentation [1300]: Advanced Micro Devices, Inc. [AMD] Zeppelin/Renoir PCIe Dummy Function [1022:1455]
  • IOMMU Group 23 0e:00.2 SATA controller [0106]: Advanced Micro Devices, Inc. [AMD] FCH SATA Controller [AHCI mode] [1022:7901] (rev 51)
  • IOMMU Group 24 0e:00.3 Audio device [0403]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) HD Audio Controller [1022:1457]
  • IOMMU Group 2 00:02.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]
  • IOMMU Group 3 00:03.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]
  • IOMMU Group 4 00:03.1 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) PCIe GPP Bridge [1022:1453]
  • IOMMU Group 5 00:03.2 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) PCIe GPP Bridge [1022:1453]
  • IOMMU Group 6 00:04.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]
  • IOMMU Group 7 00:07.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]
  • IOMMU Group 8 00:07.1 PCI bridge [0604]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) Internal PCIe GPP Bridge 0 to Bus B [1022:1454]
  • IOMMU Group 9 00:08.0 Host bridge [0600]: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge [1022:1452]

Adding the highlighted hardware to the “GRUB_CMDLINE LINUX DEFAULT”

Notice I did not put the USB controller in, as this will remove my entire controller, this is only done for each time VM has been started. Note the fix section if USB does not return after sleep.

sudo nano /etc/default/grub

# If you change this file, run ‘update-grub’ afterwards to update

# /boot/grub/grub.cfg.

# For full documentation of the options in this file, see:

# info -f grub -n ‘Simple configuration’

GRUB_DEFAULT=0 Version:F61A

GRUB_TIMEOUT_STYLE=hidden

GRUB_TIMEOUT=10

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash amd_iommu=on iommu=pt kvm_amd.npt=1 kvm_amd.avic=1 vfio_pci.ids=1002:1478,1002:1478,1002:731f,1002:ab38,1022:1457″

GRUB_CMDLINE_LINUX=””

GRUB_RECORDFAIL_TIMEOUT=5

Once file has grup has been edited, you will need to run update-grub

sudo update-grub

sudo reboot

Install KVM QEMU OVMF & Virtmanager:

sudo apt install virt-manager qemu-kvm ovmf

sudo systemctl enable systemctl

sudo systemctl start systemctl

Adding Mouse/keyboard as bluetooth

As my mouse + keyboard is capable of running both USB/Bluetooth, i have chosen to connect both devices in the USB controller that will be shared with the VM.

If I need to change to the Ubuntu part of the machine, i will connect via Bluetooth and let the VM have the fastest connection via USB. To set this up, go to “Settings” and select the tab: “Bluetooth”. Now you can add the mouse/keyboard by holding in bluetooth connect button on the devices. After 5-10 second, they should start to rapidly blink blue. You should now be able to select this in the control panel, either by moving the mouse or keyboard up/down + enter. If this give issues, I can recommend temporary adding a secondary mouse. From now on, I can now connect/switch machine on the mouse/keyboard directly.

Creating VM: Win10-Gaming

Your PC must be running in UEFI mode, Virtualization activated in UEFI, you else this will not work. I assume you have already downloaded a Windos 10 ISO, if not download from here https://www.microsoft.com/en-us/software-download/windows10ISO
The same goes for the Virtio driverstack ISO. This can be downloaded from here: https://github.com/virtio-win/virtio-win-pkg-scripts/blob/master/README.md
For best experience I would recommend having a license for a Windows 10 Pro.
Start Virtual Machine Manager.
Create new Virtual machine.
Select the ISO Set memory for 20GB memory and 4 CPUs cores.
Select a location for image file.
I would recommend minimum 50GB-100GB for system disk, as system files easily takes 40GB and then comes Windows swap and hibernation files. Set the Name of the VM to “win10-gaming”

*Important* Tick the option: “Customize configuration before install
Under the tab “Overview”, select Chipset “Q35” and Firmware “UEFI x86_64 /usr/share/OVMF_CODE.ms.fd”
Under the tab “CPUs”, select Topology, tic “Manually set CPU topology”.
In my configiration, I have set 1 Physical CPU, 4 Cores, 1 Thread pr Core.
Select “Add Hardware” in the bottom, select “PCI Host device” Add the Graphics card, Zeppelin USB Controller & onboard soundcard as identified earlier.
Add a new Storage device, under “Device type” select CDROM device, and select the virtio ISO.

Remove the following devices: Display Spice, Sound ich9, Channel Spice, Video QXL.
Under the tab “SATA Disk 1” Tic the “Advanced options” and under Disk Bus, change from “SATA” to “Virtio“.
Under the tab “Boot Options” tick the “Enable boot menu”, and tick both “Virtio Disk 1” and “SATA CDROM 1”.
Under the tab “NIC”, Set “Device Model” to “VirtIO”.

Start the VM and run an installation. When selecting installation disk, there will be no disk available, select have/disk and point to the virtio disk. You should now have access to install to the VirtIO disk, this will be faster than if set to SATA.
When it comes to user creation, I prefer to have a local account with no password, this will make sure machine will automaticly logon to that user. No secrets here, just games.

Once user creation is complete, and Windows is ready on desktop, go to the VirtIO CD-rom drive and run “e:\virtio-win-guest-tools.exe” This should install all needed drivers, including network drivers. Let Windows update, this will also take care of Graphics driver for the AMD Radeon card.

I have previosly experienced a blue screen of death, if I installed the complete AMD Catalyst driverpack from AMD. However this was fixed som time ago, by why not just use Microsoft approved drivers.

Creating VM: Win10-power

This VM is for controlling the power output. The easiest is to copy, edit and import the profile from win10-gaming to a new win10-power VM.

sudo cp /etc/libvirt/qemu/win10-gaming.xml ~/win10-gaming.xml

sudo cp win10-gaming.xml win10-power.xml

nano win10-power.xml

Change the following:

<name>win10-gaming</name> to <name>win10-power</name>

<uuid>5b875c23-5f2e-40e4-bb1a-d5f8cfa814b5</uuid> to <uuid>5b875c23-5f2e-40e4-bb1a-d5f8cfa814a14</uuid>

The <uuid> part can be changed to whatever, this is just a system identifier, and needs to be different then the one in the VM: win10-gamingChange it according to a hex value -> 0-9 + A+F Save file and exit (CTRL+O & F12) Import the VM be typing:

virsh define win10-power.xml

You should now be able to see this as Valid virtual machine, in fact apart from the name and uuid, it is the same. However, we will change that In Virtual Machine Manager: Right click on the machine, and remove the hardware Zeppelin USB controller, and disable the network. I usually also limit the memory size to 4GB instead of the 20GB, and set the CPU core count to 2 instead of 4.

Once done, you can again save copy the profile for win10-power to your homedir, and save is together with win10-gaming for future use.

sudo cp /etc/libvirt/qemu/win10-power .

Fixing errors

Fix Installing Vendor reset kernel module for RX5700XT

As AMD was not following the standard in terms of how the power BAR was constructed on the RX5700 line of GPU, this causes a big problems in terms of how my GPU reacts when handed over to VFIO. You can only switch on/off GPU once, and then you will get a error message, and card will not power back on/off.The error message will be something in the line of:

Error stating domain: internal error: Unknown PCI header type ‘127’ for device ‘0000:0b:00:0’

To mitigate this issue, we can load a kernel module, that works around this issue, for the most part.

sudo apt install git dkms build-essential

mkdir ~/build

cd ~/build

git clone https://github.com/gnif/vendor-reset.git

cd vendor-reset.git

sudo dkms install .

sudo modprobe vendor-reset

sudo echo “vendor-reset” >> /etc/modules

sudo update-grub

sudo reboot

Fix: GDM3 shows login screen on wrong monitor

Login to Gnome with a Wayland session with you user, set the display and configuration as you want is to be.Start a terminal and run this:

sudo cp ~/.config/monitors.xml ~gdm/.config/monitors.xml

sudo chown gdm:gdm ~gdm/.config/monitors.xml

sudo reboot

Now the Login screen should now be on the primary screen you have set. For Fedora check this link: https://itso.dk/?p=422

Fix: Disable Monitor2 (DVI) from RX550

As my monitors has multiple input, this has been set for automatic, and when I start the Win10-Gaming VM, it should automaticly disable Monitor2 (DVI) from RX550 and switch over to Displayport on the RX5700TX. This can be done when in X11 and the command:

xrandr –output $MainDisplay –auto –output $SecondDisplay –off

I have created a script that has been set as my hotkey: CTRL+SHIFT+M Look at script below: /usr/bin/togglemonitor.sh Remember to make it executable

chmod 755 /usr/bin/togglemonitor.sh

You can check your display setting by typing:

xrand

Edit variable section in /usr/bin/togglemonitor.sh to match your setup.

MainDisplay=DisplayPort-0

SecondDisplay=DVI-D-0

FIX: Stop heavy power usage

When PC powers on either from sleep or from cold boot, my RX5700XT uses 80-90W when just idle. This can be fixed by adding letting PC start VM with GPU, and then shutdown again. Making sure that Win10-Power VM is started on wake up from sleep:

sudo nano /lib/systemd/system-sleep/start-win10-power

Making sure that Win10-Power VM is started on cold boot

virsh autostart win10-power

FIX: Connection to USB controller is lost.

I have had the issue of USB controller is not started after machine wakes of from sleep. This can be mitigates by running the following command:

virsh nodedev-reattach pci_0000_0d_00_3

Just like the autostart of Win10-power VM after boot, this can be done by creating a script that automaticly starts after PC has been woken from sleep.

sudo nano /lib/systemd/system-sleep/wakeusb

Add the content as below, listen at the end of the article

Thoughts on power usage:

I have run a few tests on the hardware with a power monitor. When I write Power fix, I mean boot up either one of the VM: win10-power or win10-gaming. This is also applied after the Windows VM has been shut down. At first it might be a waste of CPU cycles to start a Windows VM, but since this mitigate the RX5700 powerbar issue, it is well worth it. 70-90 watts can be saved in pure idle

Configuration files and scripts:

/usr/bin/ls-iommu

#!/bin/bash

for d in /sys/kernel/iommu_groups/*/devices/*; do

n=${d#*/iommu_groups/*}; n=${n%%/*}

printf ‘IOMMU Group %s ‘ “$n”

lspci -nns “${d##*/}”

done

remember to make it executable

sudo chmod 755 /usr/bin/ls-iommu

/dev/etc/default/grub – Settings for grub loading at boot

# If you change this file, run ‘update-grub’ afterwards to update

GRUB_DEFAULT=0

GRUB_TIMEOUT_STYLE=hidden

GRUB_TIMEOUT=10

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

#GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”

GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash amd_iommu=on iommu=pt kvm_amd.npt=1 kvm_amd.avic=1 vfio_pci.ids=1002:1478,1002:1478,1002:731f,1002:ab38,1022:1457″

GRUB_CMDLINE_LINUX=””

GRUB_RECORDFAIL_TIMEOUT=5

/lib/systemd/system-sleep/wake-usb # Wake USB after sleep

#!/bin/sh

case $1/$2 in

pre/*)

echo “Going to $2…”

# Place your pre suspend commands here, or `exit 0` if no pre suspend action required

;;

post/*)

echo “Waking up from $2…”

# Place your post suspend (resume) commands here, or `exit 0` if no post suspend action required

virsh nodedev-reattach pci_0000_0d_00_3

logger -it “say” “Tried to wake USB”

;;

esac

/lib/systemd/system-sleep/wake-usb # Wake Win10-power VM

#!/bin/sh

case $1/$2 in

pre/*)

echo “Going to $2…”

# Place your pre suspend commands here, or `exit 0` if no pre suspend action required

;;

post/*)

echo “Waking up from $2…”

# Place your post suspend (resume) commands here, or `exit 0` if no post suspend action required

virsh start win10-power && sleep 70 && virsh shutdown win10-power

;;

esac

#/usr/bin/tooglegaming.sh # Script for starting Gaming VM

#Variables

VM=”win10-gaming”

MainDisplay=DisplayPort-0

SecondDisplay=DVI-D-0

str=$(virsh list –inactive | grep $VM)

echo $str

if [[ $str == *”shut off”* ]]; then

notify-send “Starting VM: “$VM

xrandr –output $MainDisplay –auto –output $SecondDisplay –off

sleep 5

virsh start $VM

else

notify-send “Shutting down VM: “$VM

virsh shutdown $VM

sleep 30

xrandr –output $MainDisplay –auto –output $SecondDisplay –auto –right-of $MainDisplay

fi

Remember to change display variables to fit to your needs.Remember to make it executable.

sudo chmod 755 /usr/bin/togglegaming.sh

/usr/bin/tooglepowervm.sh # can be added for shortcut if everything else fails

#!/usr/bin/bash

virsh start win10-power

sleep 65

virsh shutdown win10-power

/usr/bin/toogledisplay.sh # Toogle seconday monitor off/on

#!/usr/bin/bash

#Variables

MainDisplay=DisplayPort-0

SecondDisplay=DVI-D-0

str=$(xrandr | grep $SecondDisplay)

echo $str

if [[ $str == *”1920×1080″* ]]; then

xrandr –output $MainDisplay –auto –output $SecondDisplay –off

monitor2=false

else

xrandr –output $MainDisplay –auto –output $SecondDisplay –auto –right-of $MainDisplay

monitor2=true

fi

# Notify user & system

if [[ $monitor2 == false ]]

then

notify-send “Right monitor Disabled” -i /usr/share/icons/Adwaita/32×32/legacy/insert-image.png

elif [[ $monitor2 == true ]]

then

notify-send “Right monitor Enabled” -i /usr/share/icons/Adwaita/32×32/legacy/insert-image.png

fi

Remember to make it executable

sudo chmod 755 /usr/bin/toggledisplay.sh

/etc/libvirt/qemu/win10-gaming.xml

<!–

WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE

OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:

or other application using the libvirt API.

virsh edit win10-gaming

–>

<domain type=’kvm’>

<name>win10-gaming</name>

<uuid>5b875c23-5f2e-40e4-bb1a-d5f8cfa814b5</uuid>

<metadata>

<libosinfo:libosinfo xmlns:libosinfo=”http://libosinfo.org/xmlns/libvirt/domain/1.0″>

<libosinfo:os id=”http://microsoft.com/win/10″/>

</libosinfo:libosinfo>

</metadata>

<memory unit=’KiB’>10338304</memory>

<currentMemory unit=’KiB’>10338304</currentMemory>

<vcpu placement=’static’ current=’1′>7</vcpu>

<os>

<type arch=’x86_64′ machine=’pc-q35-4.2′>hvm</type>

<loader readonly=’yes’ type=’pflash’>/usr/share/OVMF/OVMF_CODE.fd</loader>

<nvram>/var/lib/libvirt/qemu/nvram/win10-gaming_VARS.fd</nvram>

<boot dev=’hd’/>

</os>

<features>

<acpi/> <!– <acpi/>

<apic/>

<hyperv>

<relaxed state=’on’/>

<vapic state=’on’/>

<spinlocks state=’on’ retries=’8191’/>

</hyperv>

<vmport state=’off’/>

</features>

<cpu mode=’host-model’ check=’partial’>

<topology sockets=’1′ cores=’7′ threads=’1’/>

</cpu>

<clock offset=’localtime’>

<timer name=’rtc’ tickpolicy=’catchup’/>

<timer name=’pit’ tickpolicy=’delay’/>

<timer name=’hpet’ present=’no’/>

<timer name=’hypervclock’ present=’yes’/>

</clock>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>destroy</on_crash>

<pm>

<suspend-to-mem enabled=’no’/>

<suspend-to-disk enabled=’no’/>

</pm>

<devices>

<emulator>/usr/bin/qemu-system-x86_64</emulator>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2’/>

<source file=’/var/lib/libvirt/images/win10-Gaming2.qcow2’/>

<target dev=’vda’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x08′ slot=’0x00′ function=’0x0’/>

</disk>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2’/>

<source file=’/var/lib/libvirt/images/win10-Gaming-1.qcow2’/>

<target dev=’vdb’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x09′ slot=’0x00′ function=’0x0’/>

</disk>

<controller type=’usb’ index=’0′ model=’qemu-xhci’ ports=’15’>

<address type=’pci’ domain=’0x0000′ bus=’0x02′ slot=’0x00′ function=’0x0’/>

</controller>

<controller type=’sata’ index=’0′>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x1f’ function=’0x2’/>

</controller>

<controller type=’pci’ index=’0′ model=’pcie-root’/>

<controller type=’pci’ index=’1′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’1′ port=’0x8’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x0′ multifunction=’on’/>

</controller>

<controller type=’pci’ index=’2′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’2′ port=’0x9’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x1’/>

</controller>

<controller type=’pci’ index=’3′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’3′ port=’0xa’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x2’/>

</controller>

<controller type=’pci’ index=’4′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’4′ port=’0xb’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x3’/>

</controller>

<controller type=’pci’ index=’5′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’5′ port=’0xc’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x4’/>

</controller>

<controller type=’pci’ index=’6′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

</pm>

<devices>

<emulator>/usr/bin/qemu-system-x86_64</emulator>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2’/>

<source file=’/var/lib/libvirt/images/win10-Gaming2.qcow2’/>

<target dev=’vda’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x08′ slot=’0x00′ function=’0x0’/>

</disk>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2’/>

<source file=’/var/lib/libvirt/images/win10-Gaming-1.qcow2’/>

<target dev=’vdb’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x09′ slot=’0x00′ function=’0x0’/>

</disk>

<controller type=’usb’ index=’0′ model=’qemu-xhci’ ports=’15’>

<address type=’pci’ domain=’0x0000′ bus=’0x02′ slot=’0x00′ function=’0x0’/>

</controller>

<controller type=’sata’ index=’0′>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x1f’ function=’0x2’/>

</controller>

<controller type=’pci’ index=’0′ model=’pcie-root’/>

<controller type=’pci’ index=’1′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’1′ port=’0x8’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x0′ multifunction=’on’/>

</cobus=’0x0b’ slot=’0x00′ function=’0x0’/>

/etc/libvirt/qemu/win10-power.xml

# Same VM as Win10-Gaming, but with no USB passthrough, or network, lower CPU allocation

!–

WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE

OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:

virsh edit win10-power

or other application using the libvirt API.

–>

<domain type=’kvm’>

<name>win10-power</name>

<uuid>5b875c23-5f2e-40e4-bb1a-d5f8cfa814b4</uuid>

<metadata>

<libosinfo:libosinfo xmlns:libosinfo=”http://libosinfo.org/xmlns/libvirt/domain/1.0″>

<libosinfo:os id=”http://microsoft.com/win/10″/>

</libosinfo:libosinfo>

</metadata>

<memory unit=’KiB’>10338304</memory>

<currentMemory unit=’KiB’>10338304</currentMemory>

<vcpu placement=’static’>4</vcpu>

<os>

<type arch=’x86_64′ machine=’pc-q35-4.2′>hvm</type>

<loader readonly=’yes’ type=’pflash’>/usr/share/OVMF/OVMF_CODE.fd</loader>

<nvram>/var/lib/libvirt/qemu/nvram/win10-gaming_VARS.fd</nvram>

<boot dev=’hd’/>

</os>

<features>

<acpi/>

<apic/>

<hyperv>

<relaxed state=’on’/>

<vapic state=’on’/>

<spinlocks state=’on’ retries=’8191’/>

</hyperv>

<vmport state=’off’/>

</features>

<cpu mode=’host-model’ check=’partial’>

<topology sockets=’1′ cores=’4′ threads=’1’/>

</cpu>

<clock offset=’localtime’>

<timer name=’rtc’ tickpolicy=’catchup’/>

<timer name=’pit’ tickpolicy=’delay’/>

<timer name=’hpet’ present=’no’/>

<timer name=’hypervclock’ present=’yes’/>

</clock>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>destroy</on_crash>

<pm>

<suspend-to-mem enabled=’no’/>

<suspend-to-disk enabled=’no’/>

</pm>

<devices>

<emulator>/usr/bin/qemu-system-x86_64</emulator>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2’/>

<source file=’/var/lib/libvirt/images/win10-Gaming2.qcow2’/>

<target dev=’vda’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x08′ slot=’0x00′ function=’0x0’/>

</disk>

<controller type=’usb’ index=’0′ model=’qemu-xhci’ ports=’15’>

<address type=’pci’ domain=’0x0000′ bus=’0x02′ slot=’0x00′ function=’0x0’/>

</controller>

<controller type=’sata’ index=’0′>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x1f’ function=’0x2’/>

</controller>

<controller type=’pci’ index=’0′ model=’pcie-root’/>

<controller type=’pci’ index=’1′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’1′ port=’0x8’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x0′ multifunction=’on’/>

</controller>

<controller type=’pci’ index=’2′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’2′ port=’0x9’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x1’/>

</controller>

<controller type=’pci’ index=’3′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’3′ port=’0xa’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x2’/>

</controller>

<controller type=’pci’ index=’4′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’4′ port=’0xb’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x3’/>

</controller>

<controller type=’pci’ index=’5′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’5′ port=’0xc’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x4’/>

</controller>

<controller type=’pci’ index=’6′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’6′ port=’0xd’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x5’/>

</controller>

<controller type=’pci’ index=’7′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’7′ port=’0xe’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x6’/>

</controller>

<controller type=’pci’ index=’8′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’8′ port=’0xf’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′ function=’0x7’/>

</controller>

<controller type=’pci’ index=’9′ model=’pcie-root-port’>

<model name=’pcie-root-port’/>

<target chassis=’9′ port=’0x10’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x02′ function=’0x0’/>

</controller>

<interface type=’network’>

<mac address=’52:54:00:e9:f7:05’/>

<source network=’default’/>

Links:

https://forum.level1techs.com/t/play-games-in-windows-on-linux-pci-passthrough-quick-guide/108981

https://github.com/gnif/vendor-reset

https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF