Guest User

VFIO Script

a guest
Feb 8th, 2016
1,042
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Which device and which related HDMI audio device. They're usually in pairs.
  4. export VGA_DEVICE=0000:01:00.0
  5. export AUDIO_DEVICE=0000:01:00.1
  6.  
  7. # Placeholders. For radeon and shit.
  8. # export VGA_DRIVER=nvidia
  9. # export AUDIO_DRIVER=snd_hda_intel
  10.  
  11. # Passing through USB devices. Querying bus address and feeding that to QEMU
  12. # instead of the device ID, so you can yank and replug the keyboard to regain
  13. # control on the host.
  14. export KEYBOARD="04d9:4545"
  15. export MOUSE="1532:0043"
  16. export SPACEMOUSE="046d:c626"
  17.  
  18. vfiobind() {
  19.     DEV="$1"
  20.  
  21.     # Check if VFIO is already bound, if so, return.
  22.     VFIODRV="$( ls -l /sys/bus/pci/devices/${DEV}/driver | grep vfio )"
  23.     if [ -n "$VFIODRV" ];
  24.     then
  25.         echo VFIO was already bound to this device!
  26.         return 0
  27.     fi
  28.  
  29.     echo -n Binding VFIO to ${DEV}...
  30.  
  31.     echo ${DEV} > /sys/bus/pci/devices/${DEV}/driver/unbind
  32.     sleep 0.5
  33.  
  34.     echo vfio-pci > /sys/bus/pci/devices/${DEV}/driver_override
  35.     echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/bind
  36.     # echo > /sys/bus/pci/devices/${DEV}/driver_override
  37.  
  38.     sleep 0.5
  39.  
  40.     echo OK!
  41. }
  42.  
  43. vfiounbind() {
  44.     DEV="$1"
  45.  
  46.     echo -n Unbinding VFIO from ${DEV}...
  47.  
  48.     echo > /sys/bus/pci/devices/${DEV}/driver_override
  49.     # echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/unbind
  50.     echo 1 > /sys/bus/pci/devices/${DEV}/remove
  51.     sleep 0.2
  52.  
  53.     echo OK!
  54. }
  55.  
  56. pcirescan() {
  57.  
  58.     echo -n Rescanning PCI bus...
  59.  
  60.     echo 1 > /sys/bus/pci/rescan
  61.     sleep 0.2
  62.  
  63.     echo OK!
  64.  
  65. }
  66.  
  67.  
  68. # Xorg shouldn't run.
  69. if [ -n "$( ps -C xinit | grep xinit )" ];
  70. then
  71.     echo Don\'t run this inside Xorg!
  72.     exit 1
  73. fi
  74.  
  75. # Unbind specified graphics card and audio device.
  76. echo Pulling the plug on the specified passthrough devices...
  77.  
  78. vfiobind $VGA_DEVICE
  79. vfiobind $AUDIO_DEVICE
  80.  
  81. # Meh.
  82. export QEMU_AUDIO_DRV=pa
  83.  
  84. # Get the bus addresses for keyboard and mouse.
  85. export QEMU_KEYB=$( lsusb | sed -n 's/Bus \([0-9]*\) Device \([0-9]*\): ID '$KEYBOARD'.*/-device usb-host,bus=xhci.0,hostbus=\1,hostaddr=\2/p' )
  86. export QEMU_MOUS=$( lsusb | sed -n 's/Bus \([0-9]*\) Device \([0-9]*\): ID '$MOUSE'.*/-device usb-host,bus=xhci.0,hostbus=\1,hostaddr=\2/p' )
  87. export QEMU_SPAC=$( lsusb | sed -n 's/Bus \([0-9]*\) Device \([0-9]*\): ID '$SPACEMOUSE'.*/-device usb-host,bus=xhci.0,hostbus=\1,hostaddr=\2/p' )
  88.  
  89. echo Starting virtual machine...
  90.  
  91. # Run pulseaudio as root but not system daemon. Hack.
  92. pulseaudio --start
  93.  
  94. # QEMU stuff
  95. qemu-system-x86_64 \
  96.     -machine type=q35,accel=kvm,kernel_irqchip=on \
  97.     -rtc base=localtime \
  98.     -enable-kvm \
  99.     -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv-vendor-id=servo,hv-vpindex,hv-reset,hv-runtime,hv-crash,hv-stimer \
  100.     -smp 8,sockets=1,cores=4,threads=2 \
  101.     -m 12288 \
  102.     -device ioh3420,bus=pcie.0,addr=1c.0,multifunction=on,port=1,chassis=1,id=root.1 \
  103.     -object iothread,id=io1 \
  104.     -device virtio-scsi-pci,id=scsi0,ioeventfd=on,iothread=io1,num_queues=4,bus="pcie.0" \
  105.     -drive if=none,id=drive0,file=/dev/bcache/by-uuid/84ea4a08-eed7-4651-93b6-24d5ed6d570a,format=raw,aio=native,cache=none,cache.direct=on,discard=unmap \
  106.     -device scsi-hd,drive=drive0,id=scsihd0 \
  107.     -vga none \
  108.     -drive if=pflash,format=raw,readonly,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd \
  109.     -drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd \
  110.     -display none \
  111.     -device vfio-pci,host=01:00.0,bus="root.1" \
  112.     -usb \
  113.     -device nec-usb-xhci,id=xhci,bus="pcie.0" \
  114.     $QEMU_KEYB \
  115.     $QEMU_MOUS \
  116.     $QEMU_SPAC \
  117.     -device virtio-net,netdev=net0,mac=de:ad:be:ef:33:4a,bus="pcie.0" \
  118.     -netdev tap,id=net0,ifname=vmtap0,script=./qemu-ifup,downscript=./qemu-ifdown \
  119.     -device ich9-intel-hda,bus="pcie.0",id=sound0 \
  120.     -device hda-duplex,cad=0,bus=sound0.0 \
  121.     -parallel none -serial none
  122.  
  123. # Kill pulseaudio instance started earlier.
  124. pulseaudio --kill
  125.  
  126. # Rebind the devices for the host.
  127. echo Adios vfio, reloading the host drivers for the passedthrough devices...
  128.  
  129. sleep 0.5
  130.  
  131. # Don't unbind audio, because it fucks up for whatever reason.
  132. # Leave vfio-pci on it.
  133. # vfiounbind $AUDIO_DEVICE
  134. vfiounbind $VGA_DEVICE
  135.  
  136. pcirescan
RAW Paste Data