Advertisement
Guest User

VFIO Helper Script

a guest
Jan 24th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.25 KB | None | 0 0
  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.
  14. export KEYBOARD="04d9:4545"
  15. export MOUSE="1532:0043"
  16.  
  17. vfiobind() {
  18.     DEV="$1"
  19.    
  20.     echo -n Binding VFIO to ${DEV}...
  21.  
  22.     echo ${DEV} > /sys/bus/pci/devices/${DEV}/driver/unbind
  23.     sleep 0.2
  24.  
  25.     echo vfio-pci > /sys/bus/pci/devices/${DEV}/driver_override
  26.     echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/bind
  27.     echo > /sys/bus/pci/devices/${DEV}/driver_override
  28.     sleep 0.5
  29.  
  30.     echo OK!
  31. }
  32.  
  33. vfiounbind() {
  34.     DEV="$1"
  35.  
  36.     echo -n Unbinding VFIO from ${DEV}...
  37.  
  38.     echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/unbind
  39.     sleep 0.2
  40.  
  41.     echo ${DEV} > /sys/bus/pci/drivers_probe
  42.     sleep 0.5
  43.  
  44.     echo OK!
  45. }
  46.  
  47. # Xorg shouldn't run.
  48. if [ -n "$( ps -C xinit | grep xinit )" ];
  49. then
  50.     echo Don\'t run this inside Xorg!
  51.     exit 1
  52. fi
  53.  
  54. # Unbind specified graphics card and audio device.
  55. echo Pulling the plug on the specified passthrough devices...
  56.  
  57. vfiobind $VGA_DEVICE
  58. vfiobind $AUDIO_DEVICE
  59.  
  60. # Meh.
  61. export QEMU_AUDIO_DRV=pa
  62.  
  63. # Get the bus addresses for keyboard and mouse.
  64. 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' )
  65. 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' )
  66.  
  67. echo Starting virtual machine...
  68.  
  69. # Run pulseaudio as root but not system daemon. Hack.
  70. pulseaudio --start
  71.  
  72. # QEMU stuff
  73. qemu-system-x86_64 \
  74.     -machine type=q35,accel=kvm,kernel_irqchip=on \
  75.     -rtc base=localtime \
  76.     -enable-kvm \
  77.     -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time,hv-vendor-id=servo,hv-vpindex,hv-synic,hv-reset,hv-stimer,hv-runtime,hv-crash \
  78.     -smp 8,sockets=1,cores=4,threads=2 \
  79.     -m 12288 \
  80.     -device ioh3420,bus=pcie.0,addr=1c.0,multifunction=on,port=1,chassis=1,id=root.1 \
  81.     -object iothread,id=io1 \
  82.     -device virtio-blk-pci,id=blk0,drive=drive0,iothread=io1,bus="pcie.0" \
  83.     -drive if=none,id=drive0,file=/dev/bcache/by-uuid/84ea4a08-eed7-4651-93b6-24d5ed6d570a,format=raw,aio=threads,cache=none,cache.direct=on \
  84.     -vga none \
  85.     -drive if=pflash,format=raw,readonly,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd \
  86.     -drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd \
  87.     -display none \
  88.     -device vfio-pci,host=01:00.0,bus="root.1" \
  89.     -usb \
  90.     -device nec-usb-xhci,id=xhci,bus="pcie.0" \
  91.     $QEMU_KEYB \
  92.     $QEMU_MOUS \
  93.     -device virtio-net,netdev=net0,mac=de:ad:be:ef:33:4a,bus="pcie.0" \
  94.     -netdev tap,id=net0,ifname=vmtap0,script=./qemu-ifup,downscript=./qemu-ifdown \
  95.     -device ich9-intel-hda,bus="pcie.0",id=sound0 \
  96.     -device hda-duplex,cad=0,bus=sound0.0 \
  97.     -parallel none -serial none
  98.  
  99. # Kill pulseaudio instance started earlier.
  100. pulseaudio --kill
  101.  
  102. # Rebind the devices for the host.
  103. echo Adios vfio, reloading the host drivers for the passedthrough devices...
  104.  
  105. vfiounbind $AUDIO_DEVICE
  106. vfiounbind $VGA_DEVICE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement