prepare-suspend 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. # Source Qubes library.
  3. # shellcheck source=init/functions
  4. . /usr/lib/qubes/init/functions
  5. action=$1
  6. [ -z "$action" ] && action=suspend
  7. MODULES_BLACKLIST=""
  8. if [ -r /etc/qubes-suspend-module-blacklist ]; then
  9. MODULES_BLACKLIST="$MODULES_BLACKLIST $(grep -v '^#' /etc/qubes-suspend-module-blacklist)"
  10. fi
  11. if [ -r /rw/config/suspend-module-blacklist ]; then
  12. MODULES_BLACKLIST="$MODULES_BLACKLIST $(grep -v '^#' /rw/config/suspend-module-blacklist)"
  13. fi
  14. if [ x"$action" = x"suspend" ]; then
  15. if qsvc network-manager ; then
  16. dbus-send --system --print-reply \
  17. --dest=org.freedesktop.NetworkManager \
  18. /org/freedesktop/NetworkManager \
  19. org.freedesktop.NetworkManager.Sleep boolean:true || \
  20. service NetworkManager stop
  21. fi
  22. # Force interfaces down, just in case when NM didn't done it
  23. for intf in /sys/class/net/*; do
  24. intf=$(basename "$intf")
  25. if [ "$intf" = "lo" ] || [[ "$intf" = "vif"* ]]; then
  26. continue
  27. fi
  28. if [ "$(cat "/sys/class/net/$intf/device/devtype" 2>/dev/null$)" = "vif" ]; then
  29. continue
  30. fi
  31. ip l s "$intf" down
  32. done
  33. # detach all drivers from PCI devices (the real ones, not emulated by qemu)
  34. echo -n > /var/run/qubes-suspend-pci-devs-detached
  35. for dev_path in /sys/bus/pci/devices/*; do
  36. subsystem_vendor=$(cat "$dev_path/subsystem_vendor")
  37. vendor=$(cat "$dev_path/vendor")
  38. class=$(cat "$dev_path/class")
  39. # skip qemu emulated devs
  40. if [ "$subsystem_vendor" = "0x1af4" ] || [ "$subsystem_vendor" = "0x5853" ]; then
  41. continue
  42. fi
  43. # skip Intel Graphics Device
  44. if [ "$vendor" = "0x8086" ] && [ "$class" = "0x030000" ]; then
  45. continue
  46. fi
  47. if ! [ -e "$dev_path/driver" ]; then
  48. continue
  49. fi
  50. bdf=$(basename "$dev_path")
  51. driver_path=$(readlink -f "$dev_path/driver")
  52. echo "$bdf" > "$driver_path/unbind"
  53. echo "$bdf $driver_path" >> /var/run/qubes-suspend-pci-devs-detached
  54. done
  55. LOADED_MODULES=""
  56. for mod in $MODULES_BLACKLIST; do
  57. if lsmod |grep -q "$mod"; then
  58. LOADED_MODULES="$LOADED_MODULES $mod"
  59. modprobe -r "$mod"
  60. fi
  61. done
  62. echo "$LOADED_MODULES" > /var/run/qubes-suspend-modules-loaded
  63. else
  64. # shellcheck disable=SC2013
  65. for mod in $(cat /var/run/qubes-suspend-modules-loaded); do
  66. modprobe "$mod"
  67. done
  68. rm -f /var/run/qubes-suspend-modules-loaded
  69. while read -r dev driver_path; do
  70. echo "$dev" > "$driver_path/bind"
  71. done < /var/run/qubes-suspend-pci-devs-detached
  72. rm -f /var/run/qubes-suspend-pci-devs-detached
  73. if qsvc network-manager ; then
  74. dbus-send --system --print-reply \
  75. --dest=org.freedesktop.NetworkManager \
  76. /org/freedesktop/NetworkManager \
  77. org.freedesktop.NetworkManager.Sleep boolean:false || \
  78. { under_systemd && systemctl start NetworkManager.service; } || service qubes-core-netvm start
  79. fi
  80. fi