Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Check if the script is executed as root
- if [ "$EUID" -ne 0 ]
- then echo "Please run as root"
- exit 1
- fi
- # END Check if you are sudo
- source config
- # Memory lock limit
- if [ $(ulimit -a | grep "max locked memory" | awk '{print $6}') != $(( $(echo $RAM | tr -d 'G')*1048576+10 )) ]; then
- ulimit -l $(( $(echo $RAM | tr -d 'G')*1048576+10 ))
- fi
- ## Kill X and related
- systemctl stop lightdm > /dev/null 2>&1
- killall cinnamon > /dev/null 2>&1
- sleep 2
- # Kill the console to free the GPU
- echo 0 > /sys/class/vtconsole/vtcon0/bind
- echo 0 > /sys/class/vtconsole/vtcon1/bind
- echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
- # Unload the Kernel Modules that use the GPU
- #modprobe -r nvidia_drm
- #modprobe -r nvidia_modeset
- #modprobe -r nvidia
- modprobe -r snd_hda_intel
- modprobe -r amdgpu
- # Load the kernel module
- modprobe vfio
- modprobe vfio_iommu_type1
- modprobe vfio-pci
- # Detach the GPU from drivers and attach to vfio. Also the usb.
- #https://stackoverflow.com/questions/25317520/writing-in-sys-bus-pci-fails
- #gpu
- sh -c "echo $videoid > /sys/bus/pci/drivers/vfio-pci/new_id"
- #no such file or dir
- sh -c "echo $videobusid > /sys/bus/pci/devices/$videobusid/driver/unbind"
- sh -c "echo $videobusid > /sys/bus/pci/drivers/vfio-pci/bind"
- sh -c "echo $videoid > /sys/bus/pci/drivers/vfio-pci/remove_id"
- #gpu audio
- sh -c "echo $audioid > /sys/bus/pci/drivers/vfio-pci/new_id"
- #no such file or dir
- sh -c "echo $audiobusid > /sys/bus/pci/devices/$audiobusid/driver/unbind"
- sh -c "echo $audiobusid > /sys/bus/pci/drivers/vfio-pci/bind"
- sh -c "echo $audioid > /sys/bus/pci/drivers/vfio-pci/remove_id"
- #usb
- #sh -c "echo $usbid > /sys/bus/pci/drivers/vfio-pci/new_id"
- #no such file or dir
- #sh -c "echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind"
- #sh -c "echo $usbbusid > /sys/bus/pci/drivers/vfio-pci/bind"
- #sh -c "echo $usbid > /sys/bus/pci/drivers/vfio-pci/remove_id"
- # QEMU (VM) command
- qemu-system-x86_64 -runas $USER -enable-kvm \
- -nographic -vga none -parallel none -serial none \
- -enable-kvm \
- -m $RAM \
- -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv_vapic,hv_vendor_id=0xDEADBEEFFF \
- -rtc clock=host,base=localtime \
- -smp $CORES,sockets=1,cores=$CORES,threads=0 \
- -device vfio-pci,host=$IOMMU_GPU,multifunction=on,x-vga=on,romfile=$VBIOS \
- -device vfio-pci,host=$IOMMU_GPU_AUDIO \
- -device virtio-net-pci,netdev=n1 \
- -netdev user,id=n1 \
- -drive if=pflash,format=raw,readonly,file=$OVMF \
- -drive media=cdrom,file=$ISO,id=virtiocd1,if=none \
- -device ide-cd,bus=ide.1,drive=virtiocd1 \
- -drive media=cdrom,file=$VIRTIO,id=virtiocd2,if=none \
- -device ide-cd,bus=ide.1,drive=virtiocd2 \
- -device ide-drive,bus=ide.1,drive=rootfs \
- -drive id=rootfs,if=none,file=$IMG,format=raw > qemu_start.log &
- #-device vfio-pci,host=$IOMMU_USB \
- # END QEMU (VM) command
- # Wait for QEMU to finish before continue
- wait
- sleep 1
- # Unload the vfio module. I am lazy, this leaves the GPU without drivers
- modprobe -r vfio-pci
- modprobe -r vfio_iommu_type1
- modprobe -r vfio
- # Reload the kernel modules. This loads the drivers for the GPU
- modprobe snd_hda_intel
- #modprobe nvidia_drm
- #modprobe nvidia_modeset
- #modprobe nvidia
- modprobe amdgpu
- # Bind the usb
- #sh -c "echo $usbid > /sys/bus/pci/drivers/xhci_hcd/new_id"
- #sh -c "echo $usbbusid > /sys/bus/pci/devices/$usbbusid/driver/unbind"
- #sh -c "echo $usbbusid > /sys/bus/pci/drivers/xhci_hcd/bind"
- #sh -c "echo $usbid > /sys/bus/pci/drivers/xhci_hcd/remove_id"
- #ls -la /sys/bus/pci/devices/$usbbusid/
- # Re-Bind EFI-Framebuffer and Re-bind to virtual consoles
- # [Source] [https://github.com/joeknock90/Single-GPU-Passthrough/blob/master/README.md#vm-stop-script]
- echo 1 > /sys/class/vtconsole/vtcon0/bind
- sleep 1
- echo 1 > tee /sys/class/vtconsole/vtcon1/bind
- sleep 1
- # Reload the Display Manager to access X
- systemctl start lightdm
- sleep 2
- # Restore the Frame Buffer
- echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/bind
- sleep 1
- # Restore ulimit
- ulimit -l $ULIMIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement