sunshine-entrypoint.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. set -euo pipefail
  3. echo "=============================================================="
  4. echo " Sunshine Gaming Desktop"
  5. echo "=============================================================="
  6. echo
  7. echo "Starting existing desktop stack..."
  8. # On conserve temporairement tout le mécanisme Selkies existant :
  9. # Xorg NVIDIA, XFCE, PipeWire, WirePlumber, D-Bus, etc.
  10. /usr/bin/supervisord -c /etc/supervisord.conf &
  11. SUPERVISOR_PID=$!
  12. cleanup() {
  13. echo "Stopping supervisor..."
  14. kill "${SUPERVISOR_PID}" 2>/dev/null || true
  15. wait "${SUPERVISOR_PID}" 2>/dev/null || true
  16. }
  17. trap cleanup EXIT TERM INT
  18. echo
  19. echo "Waiting for Xorg on ${DISPLAY}..."
  20. for i in $(seq 1 60); do
  21. if xdpyinfo -display "${DISPLAY}" >/dev/null 2>&1; then
  22. echo "Xorg is ready."
  23. break
  24. fi
  25. if ! kill -0 "${SUPERVISOR_PID}" 2>/dev/null; then
  26. echo "ERROR: supervisord exited before Xorg became available."
  27. exit 1
  28. fi
  29. if [ "${i}" -eq 60 ]; then
  30. echo "ERROR: Xorg did not become ready."
  31. exit 1
  32. fi
  33. sleep 1
  34. done
  35. # Restore NVIDIA 32-bit userspace libraries after NVIDIA Container Toolkit
  36. # has performed its runtime driver injection.
  37. if [ ! -e /usr/lib/i386-linux-gnu/libGLX_nvidia.so.0 ] \
  38. && [ -d /opt/nvidia-i386/usr/lib/i386-linux-gnu ]; then
  39. echo "[sunshine] Restoring NVIDIA i386 userspace libraries..."
  40. sudo mkdir -p /usr/lib/i386-linux-gnu
  41. sudo cp -a \
  42. /opt/nvidia-i386/usr/lib/i386-linux-gnu/. \
  43. /usr/lib/i386-linux-gnu/
  44. sudo /sbin/ldconfig
  45. fi
  46. echo
  47. echo "GPU:"
  48. nvidia-smi --query-gpu=name,driver_version --format=csv,noheader || true
  49. echo
  50. echo "Display:"
  51. xdpyinfo -display "${DISPLAY}" \
  52. | grep -E 'dimensions:|resolution:' \
  53. || true
  54. mkdir -p "${HOME}/.config/sunshine"
  55. cat > "${HOME}/.config/sunshine/sunshine.conf" <<'EOF'
  56. system_tray = disabled
  57. capture = nvfbc
  58. encoder = nvenc
  59. EOF
  60. echo "Sunshine configuration:"
  61. cat "${HOME}/.config/sunshine/sunshine.conf"
  62. echo
  63. echo "Starting Sunshine..."
  64. /usr/bin/sunshine &
  65. SUNSHINE_PID=$!
  66. cleanup() {
  67. echo "Stopping Sunshine..."
  68. kill "${SUNSHINE_PID}" 2>/dev/null || true
  69. wait "${SUNSHINE_PID}" 2>/dev/null || true
  70. echo "Stopping supervisor..."
  71. kill "${SUPERVISOR_PID}" 2>/dev/null || true
  72. wait "${SUPERVISOR_PID}" 2>/dev/null || true
  73. }
  74. trap cleanup EXIT TERM INT
  75. echo
  76. echo "Waiting for Sunshine virtual input devices..."
  77. for i in $(seq 1 30); do
  78. if grep -q 'Name="Mouse passthrough"' /proc/bus/input/devices \
  79. && grep -q 'Name="Keyboard passthrough"' /proc/bus/input/devices; then
  80. echo "Sunshine virtual mouse and keyboard detected."
  81. break
  82. fi
  83. if ! kill -0 "${SUNSHINE_PID}" 2>/dev/null; then
  84. echo "ERROR: Sunshine exited before creating input devices."
  85. wait "${SUNSHINE_PID}"
  86. exit $?
  87. fi
  88. sleep 1
  89. done
  90. echo
  91. echo "Restarting Xorg so it picks up Sunshine virtual input devices..."
  92. supervisorctl stop entrypoint || true
  93. pkill -TERM -x Xorg || true
  94. for i in $(seq 1 20); do
  95. if ! pgrep -x Xorg >/dev/null; then
  96. break
  97. fi
  98. sleep 0.5
  99. done
  100. if pgrep -x Xorg >/dev/null; then
  101. echo "Xorg did not stop cleanly, sending SIGKILL..."
  102. pkill -KILL -x Xorg || true
  103. sleep 1
  104. fi
  105. echo "Starting desktop/Xorg again..."
  106. supervisorctl start entrypoint
  107. echo "Waiting for restarted Xorg..."
  108. for i in $(seq 1 60); do
  109. if xdpyinfo -display "${DISPLAY}" >/dev/null 2>&1; then
  110. echo "Restarted Xorg is ready."
  111. break
  112. fi
  113. sleep 1
  114. done
  115. echo
  116. echo "Checking Sunshine input devices in Xorg log..."
  117. grep -Ei \
  118. 'Mouse passthrough|Keyboard passthrough|libinput' \
  119. /home/ubuntu/.local/share/xorg/Xorg.20.log \
  120. | tail -30 \
  121. || true
  122. echo
  123. echo "Sunshine Gaming Desktop ready."
  124. wait "${SUNSHINE_PID}"