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.
- export KEYBOARD="04d9:4545"
- export MOUSE="1532:0043"
- vfiobind() {
- DEV="$1"
- echo -n Binding VFIO to ${DEV}...
- echo ${DEV} > /sys/bus/pci/devices/${DEV}/driver/unbind
- sleep 0.2
- 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 ${DEV} > /sys/bus/pci/drivers/vfio-pci/unbind
- sleep 0.2
- echo ${DEV} > /sys/bus/pci/drivers_probe
- sleep 0.5
- 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' )
- 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_vapic,hv_time,hv-vendor-id=servo,hv-vpindex,hv-synic,hv-reset,hv-stimer,hv-runtime,hv-crash \
- -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-blk-pci,id=blk0,drive=drive0,iothread=io1,bus="pcie.0" \
- -drive if=none,id=drive0,file=/dev/bcache/by-uuid/84ea4a08-eed7-4651-93b6-24d5ed6d570a,format=raw,aio=threads,cache=none,cache.direct=on \
- -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 \
- -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...
- vfiounbind $AUDIO_DEVICE
- vfiounbind $VGA_DEVICE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement