prepare-suspend 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # skip qemu emulated devs
  37. subsystem_vendor=$(cat "$dev_path/subsystem_vendor")
  38. if [ "$subsystem_vendor" = "0x1af4" ] || [ "$subsystem_vendor" = "0x5853" ]; then
  39. continue
  40. fi
  41. if ! [ -e "$dev_path/driver" ]; then
  42. continue
  43. fi
  44. bdf=$(basename "$dev_path")
  45. driver_path=$(readlink -f "$dev_path/driver")
  46. echo "$bdf" > "$driver_path/unbind"
  47. echo "$bdf $driver_path" >> /var/run/qubes-suspend-pci-devs-detached
  48. done
  49. LOADED_MODULES=""
  50. for mod in $MODULES_BLACKLIST; do
  51. if lsmod |grep -q "$mod"; then
  52. LOADED_MODULES="$LOADED_MODULES $mod"
  53. modprobe -r "$mod"
  54. fi
  55. done
  56. echo "$LOADED_MODULES" > /var/run/qubes-suspend-modules-loaded
  57. else
  58. # shellcheck disable=SC2013
  59. for mod in $(cat /var/run/qubes-suspend-modules-loaded); do
  60. modprobe "$mod"
  61. done
  62. rm -f /var/run/qubes-suspend-modules-loaded
  63. while read -r dev driver_path; do
  64. echo "$dev" > "$driver_path/bind"
  65. done < /var/run/qubes-suspend-pci-devs-detached
  66. rm -f /var/run/qubes-suspend-pci-devs-detached
  67. if qsvc network-manager ; then
  68. dbus-send --system --print-reply \
  69. --dest=org.freedesktop.NetworkManager \
  70. /org/freedesktop/NetworkManager \
  71. org.freedesktop.NetworkManager.Sleep boolean:false || \
  72. { under_systemd && systemctl start NetworkManager.service; } || service qubes-core-netvm start
  73. fi
  74. fi