Advertisement
samicrusader

libvirt hook for vgpu/dgpu vms

Sep 20th, 2022
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.39 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. if [ "$2" == "prepare" ]; then
  4.     if [ "$1" == "win10-vgpu" ]; then
  5.         virsh nodedev-start mdev_d5e23cdc_7dfd_4bf7_8446_7da1b011cc71_0000_06_00_0
  6.     elif [ "$1" == "win10-school-vgpu" ]; then
  7.         virsh nodedev-start mdev_54f0bb75_cdef_4718_b45f_8640f6504789_0000_06_00_0
  8.     elif [ "$1" == "win7-vgpu" ]; then
  9.         virsh nodedev-start mdev_8a5f743b_aa48_41e5_a2cb_627c880de856_0000_06_00_0
  10.     fi
  11. fi
  12.  
  13. if [[ "$1" == *-dgpu ]] && [ "$2" == "prepare" ]; then
  14.     echo "nvidia kill" >> /log.txt
  15.     virsh destroy win10-vgpu || true
  16.     virsh destroy win10-school-vgpu || true
  17.     sleep 2s
  18.     virsh nodedev-destroy mdev_8a5f743b_aa48_41e5_a2cb_627c880de856_0000_06_00_0
  19.     virsh nodedev-destroy mdev_54f0bb75_cdef_4718_b45f_8640f6504789_0000_06_00_0
  20.     virsh nodedev-destroy mdev_d5e23cdc_7dfd_4bf7_8446_7da1b011cc71_0000_06_00_0
  21.     sleep 3s
  22.     systemctl stop nvidia-vgpu{d,-mgr}
  23.     sleep 1s
  24.     rmmod nvidia_vgpu_vfio
  25.     systemctl stop lightdm
  26.     sleep 1s
  27.     rmmod nvidia_drm nvidia_modeset nvidia_uvm
  28.     sleep 1s
  29.     rmmod nvidia
  30.     sleep 1s
  31. fi
  32.  
  33. if [[ $(basename -s "-vgpu" $(basename -s "-dgpu" "$1")) =~ ^(win10|win10-school|win7)$ ]] && [ "$2" == "prepare" ]; then
  34.     echo "memory drop" >> /log.txt
  35.     swapoff --all
  36.     echo 1 > /proc/sys/vm/drop_caches
  37.     echo 2 > /proc/sys/vm/drop_caches
  38.     echo 3 > /proc/sys/vm/drop_caches
  39.     echo 1 > /proc/sys/vm/compact_memory
  40.     sleep 4s
  41.     swapon --all
  42. fi
  43.  
  44. if [ "$2" == "prepare" ]; then
  45.     echo "allocate hugepages" >> /log.txt
  46.     sysctl -e vm.nr_hugepages=4096
  47.     if [[ "$1" =~ ^(win10-vgpu,win7-vgpu)$ ]]; then
  48.         echo "cpu pin vgpu" >> /log.txt
  49.         systemctl set-property --runtime -- user.slice AllowedCPUs=0-4,9-16
  50.         systemctl set-property --runtime -- system.slice AllowedCPUs=0-4,9-16
  51.         systemctl set-property --runtime -- init.scope AllowedCPUs=0-4,9-16
  52.     elif [[ "$1" =~ ^(win10-dgpu,win7-dgpu)$ ]]; then
  53.         echo "cpu pin dgpu" >> /log.txt
  54.         systemctl set-property --runtime -- user.slice AllowedCPUs=0-4
  55.         systemctl set-property --runtime -- system.slice AllowedCPUs=0-4
  56.         systemctl set-property --runtime -- init.scope AllowedCPUs=0-4
  57.     fi
  58. fi
  59.  
  60. if [[ "$1" == *-vgpu ]]; then
  61.     if [ "$2" == "prepare" ]; then
  62.         echo "create lg ivshmem" >> /log.txt
  63.         touch "/dev/shm/looking-glass-$1" || true
  64.         chown samicrusader:kvm "/dev/shm/looking-glass-$1"
  65.         chmod 660 "/dev/shm/looking-glass-$1"
  66.     elif [ "$2" == "started" ]; then
  67.         if [ ${1%"-vgpu"} == "win10" ]; then
  68.              export title="Kurome (Windows 10)"
  69.         elif [ ${1%"-vgpu"} == "win10-school" ]; then
  70.              export title="School (Windows 10)"
  71.         elif [ ${1%"-vgpu"} == "win7" ]; then
  72.              export title="Uzume (Windows 7)"
  73.         fi
  74.         echo "start lg $title" >> /log.txt
  75.         export $(xpub); su "$XUSER" -c "screen -dmL -Logfile /home/samicrusader/lg.log looking-glass-client app:shmFile=\"/dev/shm/looking-glass-$1\" win:title=\"$title\" win:autoResize=yes win:keepAspect=yes win:dontUpscale=yes win:shrinkOnUpscale=no win:autoScreensaver=yes win:quickSplash=yes win:uiFont=\"Koruri\" win:uiSize=12 input:escapeKey=KEY_INSERT input:rawMouse=yes spice:host=\"/tmp/$1.sock\" spice:port=0 spice:alwaysShowCursor=yes spice:showCursorDot=no" || true
  76.     fi
  77. fi
  78.  
  79. if [ "$2" == "release" ]; then
  80.     echo "unallocate hugepages" >> /log.txt
  81.     sysctl -e vm.nr_hugepages=0
  82.  
  83.     echo "release cpu" >> /log.txt
  84.     systemctl set-property --runtime -- user.slice AllowedCPUs=0-23
  85.     systemctl set-property --runtime -- system.slice AllowedCPUs=0-23
  86.     systemctl set-property --runtime -- init.scope AllowedCPUs=0-23
  87.  
  88.     if [ "$1" == "win10-vgpu" ]; then
  89.         virsh nodedev-destroy mdev_d5e23cdc_7dfd_4bf7_8446_7da1b011cc71_0000_06_00_0
  90.     elif [ "$1" == "win10-school-vgpu" ]; then
  91.         virsh nodedev-destroy mdev_54f0bb75_cdef_4718_b45f_8640f6504789_0000_06_00_0
  92.     elif [ "$1" == "win7-vgpu" ]; then
  93.         virsh nodedev-destroy mdev_8a5f743b_aa48_41e5_a2cb_627c880de856_0000_06_00_0
  94.     fi
  95.  
  96.     if [[ "$1" == *-dgpu ]]; then
  97.         echo "release nvidia" >> /log.txt
  98.         modprobe nvidia
  99.         sleep 1s
  100.         modprobe nvidia_drm nvidia_modeset nvidia_uvm
  101.         sleep 3s
  102.         modprobe nvidia_vgpu_vfio
  103.         sleep 2s
  104.         systemctl start nvidia-vgpu{d,-mgr}
  105.         systemctl start lightdm
  106.     fi
  107. fi
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement