entrypoint.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash -e
  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. trap "echo TRAPed signal" HUP INT QUIT TERM
  6. # Create and modify permissions of XDG_RUNTIME_DIR
  7. sudo -u user mkdir -pm700 /tmp/runtime-user
  8. sudo chown user:user /tmp/runtime-user
  9. sudo -u user chmod 700 /tmp/runtime-user
  10. # Make user directory owned by the user in case it is not
  11. sudo chown user:user /home/user || sudo chown user:user /home/user/* || { echo "Failed to change user directory permissions. There may be permission issues."; }
  12. # Change operating system password to environment variable
  13. echo "user:$PASSWD" | sudo chpasswd
  14. # Remove directories to make sure the desktop environment starts
  15. sudo rm -rf /tmp/.X* ~/.cache
  16. # Change time zone from environment variable
  17. sudo ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" | sudo tee /etc/timezone > /dev/null
  18. # Add Lutris directories to path
  19. export PATH="${PATH:+${PATH}:}/usr/local/games:/usr/games"
  20. # Add LibreOffice to library path
  21. export LD_LIBRARY_PATH="/usr/lib/libreoffice/program${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
  22. # This symbolic link enables running Xorg inside a container with `-sharevts`
  23. sudo ln -snf /dev/ptmx /dev/tty7
  24. # Start DBus without systemd
  25. sudo /etc/init.d/dbus start
  26. # Install NVIDIA userspace driver components including X graphic libraries
  27. if ! command -v nvidia-xconfig &> /dev/null; then
  28. # Driver version is provided by the kernel through the container toolkit
  29. 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/')"
  30. 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}}')"
  31. cd /tmp
  32. # If version is different, new installer will overwrite the existing components
  33. if [ ! -f "/tmp/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" ]; then
  34. # Check multiple sources in order to probe both consumer and datacenter driver versions
  35. 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; }
  36. fi
  37. # Extract installer before installing
  38. sudo sh "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" -x
  39. cd "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}"
  40. # Run installation without the kernel modules and host components
  41. sudo ./nvidia-installer --silent \
  42. --no-kernel-module \
  43. --install-compat32-libs \
  44. --no-nouveau-check \
  45. --no-nvidia-modprobe \
  46. --no-rpms \
  47. --no-backup \
  48. --no-check-for-alternate-installs
  49. sudo rm -rf /tmp/NVIDIA* && cd ~
  50. fi
  51. # Allow starting Xorg from a pseudoterminal instead of strictly on a tty console
  52. if [ ! -f /etc/X11/Xwrapper.config ]; then
  53. echo -e "allowed_users=anybody\nneeds_root_rights=yes" | sudo tee /etc/X11/Xwrapper.config > /dev/null
  54. fi
  55. if grep -Fxq "allowed_users=console" /etc/X11/Xwrapper.config; then
  56. sudo sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config
  57. fi
  58. # Remove existing Xorg configuration
  59. if [ -f "/etc/X11/xorg.conf" ]; then
  60. sudo rm -f "/etc/X11/xorg.conf"
  61. fi
  62. # Get first GPU device if all devices are available or `NVIDIA_VISIBLE_DEVICES` is not set
  63. if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ] || [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then
  64. export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
  65. # Get first GPU device out of the visible devices in other situations
  66. else
  67. export GPU_SELECT="$(sudo nvidia-smi --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid --format=csv | sed -n 2p)"
  68. if [ -z "$GPU_SELECT" ]; then
  69. export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
  70. fi
  71. fi
  72. if [ -z "$GPU_SELECT" ]; then
  73. echo "No NVIDIA GPUs detected or nvidia-container-toolkit not configured. Exiting."
  74. exit 1
  75. fi
  76. # Setting `VIDEO_PORT` to none disables RANDR/XRANDR, do not set this if using datacenter GPUs
  77. if [ "${VIDEO_PORT,,}" = "none" ]; then
  78. export CONNECTED_MONITOR="--use-display-device=None"
  79. # 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
  80. else
  81. export CONNECTED_MONITOR="--connected-monitor=${VIDEO_PORT}"
  82. fi
  83. # 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
  84. HEX_ID="$(sudo nvidia-smi --query-gpu=pci.bus_id --id="$GPU_SELECT" --format=csv | sed -n 2p)"
  85. IFS=":." ARR_ID=($HEX_ID)
  86. unset IFS
  87. BUS_ID="PCI:$((16#${ARR_ID[1]}))@$((16#${ARR_ID[0]})):$((16#${ARR_ID[2]})):$((16#${ARR_ID[3]}))"
  88. # A custom modeline should be generated because there is no monitor to fetch this information normally
  89. export MODELINE="$(cvt -r "${SIZEW}" "${SIZEH}" "${REFRESH}" | sed -n 2p)"
  90. # Generate /etc/X11/xorg.conf with nvidia-xconfig
  91. sudo nvidia-xconfig --virtual="${SIZEW}x${SIZEH}" --depth="$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}
  92. # Guarantee that the X server starts without a monitor by adding more options to the configuration
  93. sudo 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
  94. # Add custom generated modeline to the configuration
  95. sudo sed -i '/Section\s\+"Monitor"/a\ '"$MODELINE" /etc/X11/xorg.conf
  96. # Prevent interference between GPUs, add this to the host or other containers running Xorg as well
  97. echo -e "Section \"ServerFlags\"\n Option \"AutoAddGPU\" \"false\"\nEndSection" | sudo tee -a /etc/X11/xorg.conf > /dev/null
  98. # Default display is :0 across the container
  99. export DISPLAY=":0"
  100. # Run Xorg server with required extensions
  101. /usr/bin/Xorg vt7 -noreset -novtswitch -sharevts -dpi "${DPI}" +extension "COMPOSITE" +extension "DAMAGE" +extension "GLX" +extension "RANDR" +extension "RENDER" +extension "MIT-SHM" +extension "XFIXES" +extension "XTEST" "${DISPLAY}" &
  102. # Wait for X11 to start
  103. echo "Waiting for X socket"
  104. until [ -S "/tmp/.X11-unix/X${DISPLAY/:/}" ]; do sleep 1; done
  105. echo "X socket is ready"
  106. # Run the x11vnc + noVNC fallback web interface if enabled
  107. if [ "${NOVNC_ENABLE,,}" = "true" ]; then
  108. if [ -n "$NOVNC_VIEWPASS" ]; then export NOVNC_VIEWONLY="-viewpasswd ${NOVNC_VIEWPASS}"; else unset NOVNC_VIEWONLY; fi
  109. /usr/local/bin/x11vnc -display "${DISPLAY}" -passwd "${BASIC_AUTH_PASSWORD:-$PASSWD}" -shared -forever -repeat -xkb -snapfb -threads -xrandr "resize" -rfbport 5900 ${NOVNC_VIEWONLY} &
  110. /opt/noVNC/utils/novnc_proxy --vnc localhost:5900 --listen 8080 --heartbeat 10 &
  111. fi
  112. # Start KDE desktop environment
  113. /usr/bin/dbus-launch /usr/bin/startplasma-x11 &
  114. # Start Fcitx input method framework
  115. /usr/bin/fcitx &
  116. # Add custom processes right below this line, or within `supervisord.conf` to perform service management similar to systemd
  117. echo "Session Running. Press [Return] to exit."
  118. read