Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Which device and which related HDMI audio device. They're usually in pairs.
- export VGA_DEVICE=0000:01:00.0
- export AUDIO_DEVICE=0000:01:00.1
- # Placeholders. For radeon and shit.
- # export VGA_DRIVER=nvidia
- # export AUDIO_DRIVER=snd_hda_intel
- # Passing through USB devices. Querying bus address and feeding that to QEMU
- # instead of the device ID, so you can yank and replug the keyboard to regain
- # control on the host.
- export KEYBOARD="04d9:4545"
- export MOUSE="1532:0043"
- export SPACEMOUSE="046d:c626"
- vfiobind() {
- DEV="$1"
- # Check if VFIO is already bound, if so, return.
- VFIODRV="$( ls -l /sys/bus/pci/devices/${DEV}/driver | grep vfio )"
- if [ -n "$VFIODRV" ];
- then
- echo VFIO was already bound to this device!
- return 0
- fi
- echo -n Binding VFIO to ${DEV}...
- echo ${DEV} > /sys/bus/pci/devices/${DEV}/driver/unbind
- sleep 0.5
- echo vfio-pci > /sys/bus/pci/devices/${DEV}/driver_override
- echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/bind
- # echo > /sys/bus/pci/devices/${DEV}/driver_override
- sleep 0.5
- echo OK!
- }
- vfiounbind() {
- DEV="$1"
- echo -n Unbinding VFIO from ${DEV}...
- echo > /sys/bus/pci/devices/${DEV}/driver_override
- # echo ${DEV} > /sys/bus/pci/drivers/vfio-pci/unbind
- echo 1 > /sys/bus/pci/devices/${DEV}/remove
- sleep 0.2
- echo OK!
- }
- pcirescan() {
- echo -n Rescanning PCI bus...
- echo 1 > /sys/bus/pci/rescan
- sleep 0.2
- echo OK!
- }
- # Xorg shouldn't run.
- if [ -n "$( ps -C xinit | grep xinit )" ];
- then
- echo Don\'t run this inside Xorg!
- exit 1
- fi
- # Unbind specified graphics card and audio device.
- echo Pulling the plug on the specified passthrough devices...
- vfiobind $VGA_DEVICE
- vfiobind $AUDIO_DEVICE
- # Meh.
- export QEMU_AUDIO_DRV=pa
- # Get the bus addresses for keyboard and mouse.
- 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' )
- 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' )
- 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' )
- echo Starting virtual machine...
- # Run pulseaudio as root but not system daemon. Hack.
- pulseaudio --start
- # QEMU stuff
- qemu-system-x86_64 \
- -machine type=q35,accel=kvm,kernel_irqchip=on \
- -rtc base=localtime \
- -enable-kvm \
- -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 \
- -smp 8,sockets=1,cores=4,threads=2 \
- -m 12288 \
- -device ioh3420,bus=pcie.0,addr=1c.0,multifunction=on,port=1,chassis=1,id=root.1 \
- -object iothread,id=io1 \
- -device virtio-scsi-pci,id=scsi0,ioeventfd=on,iothread=io1,num_queues=4,bus="pcie.0" \
- -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 \
- -device scsi-hd,drive=drive0,id=scsihd0 \
- -vga none \
- -drive if=pflash,format=raw,readonly,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd \
- -drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd \
- -display none \
- -device vfio-pci,host=01:00.0,bus="root.1" \
- -usb \
- -device nec-usb-xhci,id=xhci,bus="pcie.0" \
- $QEMU_KEYB \
- $QEMU_MOUS \
- $QEMU_SPAC \
- -device virtio-net,netdev=net0,mac=de:ad:be:ef:33:4a,bus="pcie.0" \
- -netdev tap,id=net0,ifname=vmtap0,script=./qemu-ifup,downscript=./qemu-ifdown \
- -device ich9-intel-hda,bus="pcie.0",id=sound0 \
- -device hda-duplex,cad=0,bus=sound0.0 \
- -parallel none -serial none
- # Kill pulseaudio instance started earlier.
- pulseaudio --kill
- # Rebind the devices for the host.
- echo Adios vfio, reloading the host drivers for the passedthrough devices...
- sleep 0.5
- # Don't unbind audio, because it fucks up for whatever reason.
- # Leave vfio-pci on it.
- # vfiounbind $AUDIO_DEVICE
- vfiounbind $VGA_DEVICE
- pcirescan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement