entrypoint.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. set -e
  6. trap "echo TRAPed signal" HUP INT QUIT TERM
  7. # Create and modify permissions of XDG_RUNTIME_DIR
  8. mkdir -pm700 /tmp/runtime-ubuntu
  9. chown ubuntu:ubuntu /tmp/runtime-ubuntu
  10. chmod 700 /tmp/runtime-ubuntu
  11. # Make user directory owned by the user in case it is not
  12. chown ubuntu:ubuntu /home/ubuntu || sudo-root chown ubuntu:ubuntu /home/ubuntu || chown user:user /home/ubuntu/* || sudo-root chown user:user /home/ubuntu/* || echo 'Failed to change user directory permissions, there may be permission issues'
  13. # Change operating system password to environment variable
  14. echo "ubuntu:$PASSWD" | chpasswd
  15. # Remove directories to make sure the desktop environment starts
  16. rm -rf /tmp/.X* ~/.cache
  17. # Change time zone from environment variable
  18. ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" | tee /etc/timezone > /dev/null
  19. # Add Lutris directories to path
  20. export PATH="${PATH:+${PATH}:}/usr/local/games:/usr/games"
  21. # Add LibreOffice to library path
  22. export LD_LIBRARY_PATH="/usr/lib/libreoffice/program${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
  23. # Configure joystick interposer
  24. export SELKIES_INTERPOSER='/usr/$LIB/selkies_joystick_interposer.so'
  25. export LD_PRELOAD="${SELKIES_INTERPOSER}${LD_PRELOAD:+:${LD_PRELOAD}}"
  26. export SDL_JOYSTICK_DEVICE=/dev/input/js0
  27. mkdir -pm777 /dev/input || sudo-root mkdir -pm777 /dev/input || echo 'Failed to create joystick interposer directory'
  28. touch /dev/input/js0 /dev/input/js1 /dev/input/js2 /dev/input/js3 || sudo-root touch /dev/input/js0 /dev/input/js1 /dev/input/js2 /dev/input/js3 || echo 'Failed to create joystick interposer devices'
  29. # Set default display
  30. export DISPLAY="${DISPLAY:-:0}"
  31. # PipeWire-Pulse server socket location
  32. export PIPEWIRE_LATENCY="32/48000"
  33. export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}"
  34. export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
  35. export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
  36. export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
  37. # Install NVIDIA userspace driver components including X graphic libraries
  38. if ! command -v nvidia-xconfig &> /dev/null; then
  39. # Driver version is provided by the kernel through the container toolkit
  40. export DRIVER_ARCH="$(dpkg --print-architecture | sed -e 's/arm64/aarch64/' -e 's/armhf/32bit-ARM/' -e 's/i.*86/x86/' -e 's/amd64/x86_64/' -e 's/unknown/x86_64/')"
  41. export DRIVER_VERSION="$(head -n1 </proc/driver/nvidia/version | awk '{for(i=1;i<=NF;i++) if ($i ~ /^[0-9]+\.[0-9\.]+/) {print $i; exit}}')"
  42. cd /tmp
  43. # If version is different, new installer will overwrite the existing components
  44. if [ ! -f "/tmp/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" ]; then
  45. # Check multiple sources in order to probe both consumer and datacenter driver versions
  46. curl -fsSL -O "https://international.download.nvidia.com/XFree86/Linux-${DRIVER_ARCH}/${DRIVER_VERSION}/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" || curl -fsSL -O "https://international.download.nvidia.com/tesla/${DRIVER_VERSION}/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" || { echo "Failed NVIDIA GPU driver download. Exiting."; exit 1; }
  47. fi
  48. # Extract installer before installing
  49. sh "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" -x
  50. cd "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}"
  51. # Run installation without the kernel modules and host components
  52. ./nvidia-installer --silent \
  53. --no-kernel-module \
  54. --install-compat32-libs \
  55. --no-nouveau-check \
  56. --no-nvidia-modprobe \
  57. --no-rpms \
  58. --no-backup \
  59. --no-check-for-alternate-installs
  60. rm -rf /tmp/NVIDIA* && cd ~
  61. fi
  62. # Allow starting Xorg from a pseudoterminal instead of strictly on a tty console
  63. if [ ! -f /etc/X11/Xwrapper.config ]; then
  64. echo -e "allowed_users=anybody\nneeds_root_rights=yes" | tee /etc/X11/Xwrapper.config > /dev/null
  65. fi
  66. if grep -Fxq "allowed_users=console" /etc/X11/Xwrapper.config; then
  67. sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config
  68. fi
  69. # Remove existing Xorg configuration
  70. if [ -f "/etc/X11/xorg.conf" ]; then
  71. rm -f "/etc/X11/xorg.conf"
  72. fi
  73. # Get first GPU device if all devices are available or `NVIDIA_VISIBLE_DEVICES` is not set
  74. if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ] || [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then
  75. export GPU_SELECT="$(nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
  76. # Get first GPU device out of the visible devices in other situations
  77. else
  78. export GPU_SELECT="$(nvidia-smi --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid --format=csv | sed -n 2p)"
  79. if [ -z "$GPU_SELECT" ]; then
  80. export GPU_SELECT="$(nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
  81. fi
  82. fi
  83. if [ -z "$GPU_SELECT" ]; then
  84. echo "No NVIDIA GPUs detected or nvidia-container-toolkit not configured. Exiting."
  85. exit 1
  86. fi
  87. # Setting `VIDEO_PORT` to none disables RANDR/XRANDR, do not set this if using datacenter GPUs
  88. if [ "${VIDEO_PORT,,}" = "none" ]; then
  89. export CONNECTED_MONITOR="--use-display-device=None"
  90. # The X server is otherwise deliberately set to a specific video port despite not being plugged to enable RANDR/XRANDR, monitor will display the screen if plugged to the specific port
  91. else
  92. export CONNECTED_MONITOR="--connected-monitor=${VIDEO_PORT}"
  93. fi
  94. # Bus ID from nvidia-smi is in hexadecimal format and should be converted to decimal format (including the domain) which Xorg understands, required because nvidia-xconfig doesn't work as intended in a container
  95. HEX_ID="$(nvidia-smi --query-gpu=pci.bus_id --id="$GPU_SELECT" --format=csv | sed -n 2p)"
  96. IFS=":." ARR_ID=($HEX_ID)
  97. unset IFS
  98. BUS_ID="PCI:$((16#${ARR_ID[1]}))@$((16#${ARR_ID[0]})):$((16#${ARR_ID[2]})):$((16#${ARR_ID[3]}))"
  99. # A custom modeline should be generated because there is no monitor to fetch this information normally
  100. export MODELINE="$(cvt -r "${DESKTOP_SIZEW}" "${DESKTOP_SIZEH}" "${DESKTOP_REFRESH}" | sed -n 2p)"
  101. # Generate /etc/X11/xorg.conf with nvidia-xconfig
  102. nvidia-xconfig --virtual="${DESKTOP_SIZEW}x${DESKTOP_SIZEH}" --depth="$DESKTOP_CDEPTH" --mode="$(echo "$MODELINE" | awk '{print $2}' | tr -d '\"')" --allow-empty-initial-configuration --no-probe-all-gpus --busid="$BUS_ID" --include-implicit-metamodes --mode-debug --no-sli --no-base-mosaic --only-one-x-screen ${CONNECTED_MONITOR}
  103. # Guarantee that the X server starts without a monitor by adding more options to the configuration
  104. sed -i '/Driver\s\+"nvidia"/a\ Option "ModeValidation" "NoMaxPClkCheck,NoEdidMaxPClkCheck,NoMaxSizeCheck,NoHorizSyncCheck,NoVertRefreshCheck,NoVirtualSizeCheck,NoExtendedGpuCapabilitiesCheck,NoTotalSizeCheck,NoDualLinkDVICheck,NoDisplayPortBandwidthCheck,AllowNon3DVisionModes,AllowNonHDMI3DModes,AllowNonEdidModes,NoEdidHDMI2Check,AllowDpInterlaced"' /etc/X11/xorg.conf
  105. # Add custom generated modeline to the configuration
  106. sed -i '/Section\s\+"Monitor"/a\ '"$MODELINE" /etc/X11/xorg.conf
  107. # Prevent interference between GPUs, add this to the host or other containers running Xorg as well
  108. echo -e "Section \"ServerFlags\"\n Option \"AutoAddGPU\" \"false\"\nEndSection" | tee -a /etc/X11/xorg.conf > /dev/null
  109. # This symbolic link enables running Xorg inside a container with `-sharevts`
  110. ln -snf /dev/ptmx /dev/tty7 || sudo-root ln -snf /dev/ptmx /dev/tty7 || echo 'Failed to create /dev/tty7 device'
  111. # Run Xorg server with required extensions
  112. /usr/bin/Xorg vt7 -noreset -novtswitch -sharevts -dpi "${DESKTOP_DPI}" +extension "COMPOSITE" +extension "DAMAGE" +extension "GLX" +extension "RANDR" +extension "RENDER" +extension "MIT-SHM" +extension "XFIXES" +extension "XTEST" "${DISPLAY}" &
  113. # Wait for X server to start
  114. echo 'Waiting for X socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  115. # Run the x11vnc + noVNC fallback web interface if enabled
  116. if [ "${NOVNC_ENABLE,,}" = "true" ]; then
  117. if [ -n "$NOVNC_VIEWPASS" ]; then export NOVNC_VIEWONLY="-viewpasswd ${NOVNC_VIEWPASS}"; else unset NOVNC_VIEWONLY; fi
  118. /usr/local/bin/x11vnc -display "${DISPLAY}" -passwd "${SELKIES_BASIC_AUTH_PASSWORD:-$PASSWD}" -shared -forever -repeat -xkb -snapfb -threads -xrandr "resize" -rfbport 5900 ${NOVNC_VIEWONLY} &
  119. /opt/noVNC/utils/novnc_proxy --vnc localhost:5900 --listen 8080 --heartbeat 10 &
  120. fi
  121. # Start KDE desktop environment
  122. /usr/bin/dbus-launch --exit-with-session /usr/bin/startplasma-x11 &
  123. # Start Fcitx input method framework
  124. /usr/bin/fcitx &
  125. # Add custom processes right below this line, or within `supervisord.conf` to perform service management similar to systemd
  126. echo "Session Running. Press [Return] to exit."
  127. read