From 54911cb97bee800358fda4010d83347ccac4bce6 Mon Sep 17 00:00:00 2001 From: "Josh.5" Date: Thu, 6 Oct 2022 22:26:50 +1300 Subject: [PATCH] Fix bug preventing input after the first startup of the container Xorg was starting before udev could finish the monitor command. This prevented the X server from having access to the input devices. For some reason, any subsequent starts would be fine. Adding a small delay to the X server start and triggering a request for device events from the kernel after a delay seems to fix this issue. --- overlay/etc/cont-init.d/30-configure_udev.sh | 2 + .../cont-init.d/60-configure_gpu_driver.sh | 4 +- overlay/etc/cont-init.d/70-configure_xorg.sh | 4 +- overlay/etc/supervisor.d/vnc.ini | 4 +- overlay/etc/supervisor.d/xorg.ini | 2 +- overlay/usr/bin/common-functions.sh | 25 +++++++++++++ overlay/usr/bin/start-desktop.sh | 9 +++-- overlay/usr/bin/start-pulseaudio.sh | 2 +- overlay/usr/bin/start-sunshine.sh | 19 +++------- overlay/usr/bin/start-udev.sh | 20 +++++----- overlay/usr/bin/start-x11vnc.sh | 30 +++++++++++++++ overlay/usr/bin/start-xorg.sh | 37 +++++++++++++++++++ 12 files changed, 122 insertions(+), 36 deletions(-) create mode 100755 overlay/usr/bin/common-functions.sh create mode 100755 overlay/usr/bin/start-x11vnc.sh create mode 100755 overlay/usr/bin/start-xorg.sh diff --git a/overlay/etc/cont-init.d/30-configure_udev.sh b/overlay/etc/cont-init.d/30-configure_udev.sh index 9a80cc8..14ab3e8 100644 --- a/overlay/etc/cont-init.d/30-configure_udev.sh +++ b/overlay/etc/cont-init.d/30-configure_udev.sh @@ -42,3 +42,5 @@ if [[ -e /dev/uinput ]]; then echo "**** Ensure the default user has permission to r/w on input devices ****"; chmod 0666 /dev/uinput fi + +echo "DONE" diff --git a/overlay/etc/cont-init.d/60-configure_gpu_driver.sh b/overlay/etc/cont-init.d/60-configure_gpu_driver.sh index b119044..fd6c76d 100644 --- a/overlay/etc/cont-init.d/60-configure_gpu_driver.sh +++ b/overlay/etc/cont-init.d/60-configure_gpu_driver.sh @@ -1,8 +1,8 @@ # Fech NVIDIA GPU device (if one exists) -if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ]; then +if [ "${NVIDIA_VISIBLE_DEVICES:-}" == "all" ]; then export gpu_select=$(nvidia-smi --format=csv --query-gpu=uuid 2> /dev/null | sed -n 2p) -elif [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then +elif [ -z "${NVIDIA_VISIBLE_DEVICES:-}" ]; then export gpu_select=$(nvidia-smi --format=csv --query-gpu=uuid 2> /dev/null | sed -n 2p) else export gpu_select=$(nvidia-smi --format=csv --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid | sed -n 2p) diff --git a/overlay/etc/cont-init.d/70-configure_xorg.sh b/overlay/etc/cont-init.d/70-configure_xorg.sh index 9b1eb84..cde3568 100644 --- a/overlay/etc/cont-init.d/70-configure_xorg.sh +++ b/overlay/etc/cont-init.d/70-configure_xorg.sh @@ -1,8 +1,8 @@ # Fech NVIDIA GPU device (if one exists) -if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ]; then +if [ "${NVIDIA_VISIBLE_DEVICES:-}" == "all" ]; then export gpu_select=$(nvidia-smi --format=csv --query-gpu=uuid 2> /dev/null | sed -n 2p) -elif [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then +elif [ -z "${NVIDIA_VISIBLE_DEVICES:-}" ]; then export gpu_select=$(nvidia-smi --format=csv --query-gpu=uuid 2> /dev/null | sed -n 2p) else export gpu_select=$(nvidia-smi --format=csv --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid | sed -n 2p) diff --git a/overlay/etc/supervisor.d/vnc.ini b/overlay/etc/supervisor.d/vnc.ini index f518cf1..4e81587 100644 --- a/overlay/etc/supervisor.d/vnc.ini +++ b/overlay/etc/supervisor.d/vnc.ini @@ -4,7 +4,8 @@ priority=30 autostart=false autorestart=true user=%(ENV_USER)s -command=/usr/bin/x11vnc -display %(ENV_DISPLAY)s -rfbport %(ENV_PORT_VNC)s -shared -forever +command=/usr/bin/start-x11vnc.sh +environment=HOME="/home/%(ENV_USER)s",USER="%(ENV_USER)s",DISPLAY="%(ENV_DISPLAY)s",PORT_VNC="%(ENV_PORT_VNC)s" stopsignal=INT stdout_logfile=/home/%(ENV_USER)s/.cache/log/x11vnc.log stdout_logfile_maxbytes=10MB @@ -18,7 +19,6 @@ priority=30 autostart=false autorestart=true user=%(ENV_USER)s -# /opt/noVNC/utils/launch.sh --vnc localhost:${PORT_VNC} --listen ${PORT_NOVNC_SERVICE} command=/opt/noVNC/utils/launch.sh --vnc localhost:%(ENV_PORT_VNC)s --listen %(ENV_PORT_NOVNC_SERVICE)s environment=HOME="/home/%(ENV_USER)s",USER="%(ENV_USER)s" stopsignal=INT diff --git a/overlay/etc/supervisor.d/xorg.ini b/overlay/etc/supervisor.d/xorg.ini index 4869f69..78c3ecc 100644 --- a/overlay/etc/supervisor.d/xorg.ini +++ b/overlay/etc/supervisor.d/xorg.ini @@ -4,7 +4,7 @@ priority=20 autostart=false autorestart=true user=root -command=/usr/bin/Xorg -ac -noreset -novtswitch -sharevts -dpi "%(ENV_DISPLAY_DPI)s" +extension "GLX" +extension "RANDR" +extension "RENDER" vt7 "%(ENV_DISPLAY)s" +command=/usr/bin/start-xorg.sh environment=DISPLAY="%(ENV_DISPLAY)s",DISPLAY_DPI="%(ENV_DISPLAY_DPI)s",XDG_RUNTIME_DIR="%(ENV_XDG_RUNTIME_DIR)s" stopsignal=INT stdout_logfile=/home/%(ENV_USER)s/.cache/log/xorg.log diff --git a/overlay/usr/bin/common-functions.sh b/overlay/usr/bin/common-functions.sh new file mode 100755 index 0000000..1643da1 --- /dev/null +++ b/overlay/usr/bin/common-functions.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +### +# File: common-functions.sh +# Project: bin +# File Created: Tuesday, 6th October 2022 9:30:00 pm +# Author: Josh.5 (jsunnex@gmail.com) +# ----- +# Last Modified: Tuesday, 6th October 2022 9:30:00 pm +# Modified By: Josh.5 (jsunnex@gmail.com) +### + +wait_for_x() { + # Wait for X server to start + # (Credit: https://gist.github.com/tullmann/476cc71169295d5c3fe6) + MAX=60 # About 30 seconds + CT=0 + while ! xdpyinfo >/dev/null 2>&1; do + sleep 0.50s + CT=$(( CT + 1 )) + if [ "$CT" -ge "$MAX" ]; then + LOG "FATAL: $0: Gave up waiting for X server $DISPLAY" + exit 11 + fi + done +} diff --git a/overlay/usr/bin/start-desktop.sh b/overlay/usr/bin/start-desktop.sh index 5a6c0ce..72f31c0 100755 --- a/overlay/usr/bin/start-desktop.sh +++ b/overlay/usr/bin/start-desktop.sh @@ -8,15 +8,14 @@ # Last Modified: Wednesday, 26th January 2022 5:38:23 pm # Modified By: Console and webGui login account (jsunnex@gmail.com) ### -# -# I made this wrapper script so that I could easily try a range of desktop environments -# + +source /usr/bin/common-functions.sh # CATCH TERM SIGNAL: _term() { kill -TERM "$desktop_pid" 2>/dev/null } -trap _term SIGTERM +trap _term SIGTERM SIGINT # CONFIGURE: @@ -31,6 +30,8 @@ ln -sf /usr/share/backgrounds/steam.jpg /etc/alternatives/desktop-background # EXECUTE PROCESS: +# Wait for the X server to start +wait_for_x # Run the desktop environment in order of priority if [[ -e /usr/bin/cinnamon-session ]]; then /usr/bin/cinnamon-session --display=${DISPLAY} & diff --git a/overlay/usr/bin/start-pulseaudio.sh b/overlay/usr/bin/start-pulseaudio.sh index 2497c8c..28cfe74 100755 --- a/overlay/usr/bin/start-pulseaudio.sh +++ b/overlay/usr/bin/start-pulseaudio.sh @@ -13,7 +13,7 @@ _term() { kill -TERM "$pulseaudio_pid" 2>/dev/null } -trap _term SIGTERM +trap _term SIGTERM SIGINT # EXECUTE PROCESS: diff --git a/overlay/usr/bin/start-sunshine.sh b/overlay/usr/bin/start-sunshine.sh index a8c8ec9..25c6b3f 100755 --- a/overlay/usr/bin/start-sunshine.sh +++ b/overlay/usr/bin/start-sunshine.sh @@ -9,11 +9,13 @@ # Modified By: Josh.5 (jsunnex@gmail.com) ### +source /usr/bin/common-functions.sh + # CATCH TERM SIGNAL: _term() { kill -TERM "$sunshine_pid" 2>/dev/null } -trap _term SIGTERM +trap _term SIGTERM SIGINT # CONFIGURE: @@ -25,18 +27,9 @@ fi # EXECUTE PROCESS: -# Wait for X server to start -# (Credit: https://gist.github.com/tullmann/476cc71169295d5c3fe6) -MAX=60 # About 30 seconds -CT=0 -while ! xdpyinfo >/dev/null 2>&1; do - sleep 0.50s - CT=$(( CT + 1 )) - if [ "$CT" -ge "$MAX" ]; then - LOG "FATAL: $0: Gave up waiting for X server $DISPLAY" - exit 11 - fi -done +# Wait for the X server to start +wait_for_x +# Start the sunshine server sunshine min_log_level=info /home/${USER}/sunshine/sunshine.conf & sunshine_pid=$! diff --git a/overlay/usr/bin/start-udev.sh b/overlay/usr/bin/start-udev.sh index 77c931a..0e70759 100755 --- a/overlay/usr/bin/start-udev.sh +++ b/overlay/usr/bin/start-udev.sh @@ -12,30 +12,28 @@ # CATCH TERM SIGNAL: _term() { kill -TERM "$monitor_pid" 2>/dev/null - kill -TERM "$trigger_pid" 2>/dev/null - kill -TERM "$udevd_pid" 2>/dev/null } -trap _term SIGTERM +trap _term SIGTERM SIGINT # EXECUTE PROCESS: +# Remove lockfile +rm -f /tmp/.udev-started # Start udev -# Source: https://github.com/balena-io-playground/balena-base-images/ if command -v udevd &>/dev/null; then unshare --net udevd --daemon &>/dev/null - udevd_pid=$! else unshare --net /lib/systemd/systemd-udevd --daemon &>/dev/null - udevd_pid=$! fi -udevadm trigger &>/dev/null -trigger_pid=$! # Monitor kernel uevents udevadm monitor & monitor_pid=$! - +# Touch lockfile +sleep 1 +touch /tmp/.udev-started +# Wait for 10 seconds, then request device events from the kernel +sleep 10 +udevadm trigger # WAIT FOR CHILD PROCESS: wait "$monitor_pid" -wait "$trigger_pid" -wait "$udevd_pid" diff --git a/overlay/usr/bin/start-x11vnc.sh b/overlay/usr/bin/start-x11vnc.sh new file mode 100755 index 0000000..41e3e35 --- /dev/null +++ b/overlay/usr/bin/start-x11vnc.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +### +# File: start-x11vnc.sh +# Project: bin +# File Created: Tuesday, 6th October 2022 9:30:00 pm +# Author: Josh.5 (jsunnex@gmail.com) +# ----- +# Last Modified: Tuesday, 6th October 2022 9:30:00 pm +# Modified By: Josh.5 (jsunnex@gmail.com) +### + +source /usr/bin/common-functions.sh + +# CATCH TERM SIGNAL: +_term() { + kill -TERM "$x11vnc_pid" 2>/dev/null +} +trap _term SIGTERM SIGINT + + +# EXECUTE PROCESS: +# Wait for the X server to start +wait_for_x +# Start the x11vnc server +/usr/bin/x11vnc -display ${DISPLAY} -rfbport ${PORT_VNC} -shared -forever& +x11vnc_pid=$! + + +# WAIT FOR CHILD PROCESS: +wait "$x11vnc_pid" diff --git a/overlay/usr/bin/start-xorg.sh b/overlay/usr/bin/start-xorg.sh new file mode 100755 index 0000000..add5e59 --- /dev/null +++ b/overlay/usr/bin/start-xorg.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +### +# File: start-xorg.sh +# Project: bin +# File Created: Tuesday, 11th January 2022 8:28:52 pm +# Author: Josh.5 (jsunnex@gmail.com) +# ----- +# Last Modified: Friday, 6th October 2022 9:21:00 pm +# Modified By: Josh.5 (jsunnex@gmail.com) +### + +# CATCH TERM SIGNAL: +_term() { + kill -TERM "$xorg_pid" 2>/dev/null +} +trap _term SIGTERM SIGINT + + +# EXECUTE PROCESS: +# Wait for udev +MAX=10 +CT=0 +while [ ! -f /tmp/.udev-started ]; do + sleep 1 + CT=$(( CT + 1 )) + if [ "$CT" -ge "$MAX" ]; then + LOG "FATAL: $0: Gave up waiting for udev server to start" + exit 11 + fi +done +# Run X server +/usr/bin/Xorg -ac -noreset -novtswitch -sharevts -dpi "${DISPLAY_DPI}" +extension "GLX" +extension "RANDR" +extension "RENDER" vt7 "${DISPLAY}" & +xorg_pid=$! + + +# WAIT FOR CHILD PROCESS: +wait "$xorg_pid"