entrypoint.sh 7.2 KB

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