Ubuntu – Anybody managed to run Linux and KVM Windows with a single passthrough graphics card

kvmpci

I have a Ubuntu system that runs FreeNAS in a KVM with one SATA controller card as passthrough. Because of that I only have one PCIe slot on my motherboard that fits a graphic card. I would now like to also run a Windows KVM capable of running 3D CAD software that require a GPU.

I have read serveral entries that describe windows running in KVM using passthrough of a second graphic card. However I would like to run a different setup with only one graphic card:

  • run main Linux headless without a graphics card (only when installing or maintaining attach to it)
  • passthrough the single graphic card to the Windows KVM, also passthrough mouse/keyboard to Windows
  • operate linux via ssh of alternatively install a No-Machine server and connect via that from Windows.

Is there some resource that would describe howto implement such a setup?
Has anybody tried it before?
I guess the sequence would be something like:

  • Install vnc server in linux, operate though the vnc server interface
  • Add a GRUB boot setting that would leave the graphic card handled by vfio
  • Reboot into headless and setup an autostarted Windows KVM with graphics card attached

Sounds doable (?) but I guess there are numerouse pitfalls and I dont want to risk my current setup…

Best Answer

Starting with this tutorial I got AMD 5450 passthrough of primary graphics card (host runs headless) working with the below script.

  • Doing the standard vfio preparation
  • add video=efifb:off to cmdline otherwise efifb would grab the PCI bar before vfio.
  • Guest UEFI (OVMF image from apt-get install ovmf)
  • -vga qlx used and later qlx Monitor is disabled inside Windows (Display Settings)
  • no romfiles for AMD 5450.

I also tried with a NVidia 710b, however whas permanently stuck with "Code 43". Probably because romfiles are needed there when trying to passthrough the primary card.

Script:

#!/bin/bash

b=$(pwd)

passthrough=0
qxl=1
uefi=1
ovmf=0
net=0
while getopts "bpQUon" opt; do
  case $opt in
      p) passthrough=1 ;;
      Q) qxl=0 ;;
      U) uefi=0 ;;
      n) net=1 ;;
      o) ovmf=1 ;;
  esac
done

cp ${b}/uefi/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd .
OPTS=""
# Basic CPU settings.
OPTS="$OPTS -cpu host,kvm=off"
OPTS="$OPTS -smp 4,sockets=1,cores=4,threads=1"
# Enable KVM full virtualization support.
OPTS="$OPTS -enable-kvm"
# Assign memory to the vm.
OPTS="$OPTS -m 4000"

# VFIO GPU and GPU sound passthrough.
if [ "$passthrough" == "1" ]; then
    #,multifunction=on,romfile=/mnt/nvidia_efi.rom" ,romfile=${b}/bioses/XFX.HD5450.1024.110612.rom
    #OPTS="$OPTS -device vfio-pci,host=05:00.0,multifunction=on,romfile=${b}/bioses/XFX.HD5450.1024.110612.rom"
    #,romfile=${b}/bioses/XFX.HD5450.1024.110612_1.rom
    OPTS="$OPTS -device vfio-pci,host=01:00.0,multifunction=on" 
    #,romfile=/mnt/data-n0/vms-win/romfile_radeon.bin
    #,romfile=/mnt/nvidia_efi.rom"
    OPTS="$OPTS -device vfio-pci,host=01:00.1"
fi

# Supply OVMF (general UEFI bios, needed for EFI boot support with GPT disks).
if [ "$uefi" == "1" ]; then
    if [ "$ovmf" == "0" ]; then
    OPTS="$OPTS -drive if=pflash,format=raw,readonly,file=${b}/uefi/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd"
    OPTS="$OPTS -drive if=pflash,format=raw,file=${b}/OVMF_VARS-pure-efi.fd"
    else
    mkdir -p ${b}/hda-contents
    #OPTS="$OPTS -pflash ${b}/ovmf_pkg/OVMF.fd"
    OPTS="$OPTS -drive if=pflash,format=raw,readonly,file=${b}/ovmf_pkg/OVMF.fd"
    OPTS="$OPTS -drive if=pflash,format=raw,file=${b}/OVMF_VARS-pure-efi.fd"
    fi
fi

# Load our created VM image as a harddrive.
OPTS="$OPTS -hda ${b}/win10_uefi_cpu_host_qemu_vm.qcow2"
# Load our OS setup image e.g. ISO file.

#OPTS="$OPTS -cdrom ${b}/windows_10.iso"
OPTS="$OPTS -cdrom ${b}/virtio-win-0.1.141.iso"

if [ "$qxl" == "1" ]; then
    # Use the following emulated video device (use none for disabled).
    OPTS="$OPTS -vga qxl"
else
    # Use an emulated video device (use none for disabled).
    #  -vga none -device qxl
    OPTS="$OPTS -vga none -device qxl "
fi

OPTS="$OPTS -spice port=5900,addr=127.0.0.1,disable-ticketing "

# Redirect QEMU's console input and output.
OPTS="$OPTS -monitor stdio"

if [ "${net}" == "1" ]; then
    # Improve the network performance by utilizing virtio-net.
    OPTS="$OPTS -device virtio-net,netdev=net0,mac=de:ad:be:ef:33:4a"
    OPTS="$OPTS -netdev tap,id=net0,ifname=vmtap0,script=./qemu-ifup,downscript=./qemu-ifdown"
else
    OPTS="$OPTS -net none "
fi

#Bus 003 Device 004: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory
#Bus 003 Device 002: ID 1a40:0201 Terminus Technology Inc. FE 2.1 7-port Hub

# USB mouse
if [ "$passthrough" == "1" ]; then
    OPTS="$OPTS -usb"
    OPTS="$OPTS -device usb-ehci,id=ehci"
    OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x17ef,productid=0x6019 "
    OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x1c4f,productid=0x0002 "
fi
#if [ "$passthrough" == "1" ]; then
#    OPTS="$OPTS -usb"
#    OPTS="$OPTS -device usb-ehci,id=ehci"
#    OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x17ef,productid=0x6019 "
#    OPTS="$OPTS -device usb-host,bus=usb-bus.0,vendorid=0x1c4f,productid=0x0002 "
#fi

#OPTS="$OPTS -device usb-host,hostbus=3,hostaddr=4"
#OPTS="$OPTS -device usb-host,hostbus=3,hostaddr=2"

#OPTS="$OPTS -device usb-host,bus=xhci.0,vendorid=0x1c4f,productid=0x0002 "
#OPTS="$OPTS -device usb-host,bus=xhci.0,vendorid=0x1a40,productid=0x0201 "

#OPTS="$OPTS -usbdevice host:1c4f:0002"
# USB keyboard
#OPTS="$OPTS -usbdevice host:1a40:0201"

sudo qemu-system-x86_64 $OPTS
Related Question