🐛 fix(scripts): fix entrypoint.sh crashes

This commit is contained in:
Wanjohi
2024-04-16 03:50:03 +03:00
parent 2fc7d24fee
commit 993d8238a3

View File

@@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash -e
trap "echo TRAPed signal" HUP INT QUIT TERM trap "echo TRAPed signal" HUP INT QUIT TERM
# Create and modify permissions of XDG_RUNTIME_DIR # Create and modify permissions of XDG_RUNTIME_DIR
@@ -7,11 +7,12 @@ sudo chown ubuntu:ubuntu /tmp/runtime-ubuntu
sudo -u ubuntu chmod 700 /tmp/runtime-ubuntu sudo -u ubuntu chmod 700 /tmp/runtime-ubuntu
# Make user directory owned by the user in case it is not # Make user directory owned by the user in case it is not
sudo chown ubuntu:ubuntu /home/ubuntu || sudo chown ubuntu:ubuntu /home/ubuntu/* || { echo "Failed to change user directory permissions. There may be permission issues."; } sudo chown ubuntu:ubuntu /home/ubuntu || sudo chown ubuntu:ubuntu /home/ubuntu/* || { echo "Failed to change user directory permissions. There may be permission issues."; }
# Remove directories to make sure the desktop environment starts # Remove directories to make sure the desktop environment starts
sudo rm -rf /tmp/.X* ~/.cache sudo rm -rf /tmp/.X* ~/.cache
# Change time zone from environment variable # Change time zone from environment variable
sudo ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" | sudo tee /etc/timezone >/dev/null sudo ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" | sudo tee /etc/timezone > /dev/null
# Add gamescope directory # Add gamescope directories to path
export PATH="${PATH:+${PATH}:}/usr/local/games:/usr/games" export PATH="${PATH:+${PATH}:}/usr/local/games:/usr/games"
# This symbolic link enables running Xorg inside a container with `-sharevts` # This symbolic link enables running Xorg inside a container with `-sharevts`
@@ -20,74 +21,71 @@ sudo ln -snf /dev/ptmx /dev/tty7
sudo /etc/init.d/dbus start sudo /etc/init.d/dbus start
# Install NVIDIA userspace driver components including X graphic libraries # Install NVIDIA userspace driver components including X graphic libraries
if ! command -v nvidia-xconfig &>/dev/null; then if ! command -v nvidia-xconfig &> /dev/null; then
# Driver version is provided by the kernel through the container toolkit # Driver version is provided by the kernel through the container toolkit
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/')" 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/')"
export DRIVER_VERSION="$(head -n1 </proc/driver/nvidia/version | awk '{print $8}')" export DRIVER_VERSION="$(head -n1 </proc/driver/nvidia/version | awk '{print $8}')"
cd /tmp cd /tmp
# If version is different, new installer will overwrite the existing components # If version is different, new installer will overwrite the existing components
if [ ! -f "/tmp/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" ]; then if [ ! -f "/tmp/NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" ]; then
# Check multiple sources in order to probe both consumer and datacenter driver versions # Check multiple sources in order to probe both consumer and datacenter driver versions
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" || { 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; }
echo "Failed NVIDIA GPU driver download. Exiting." fi
exit 1 # Extract installer before installing
} sudo sh "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" -x
fi cd "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}"
# Extract installer before installing # Run installation without the kernel modules and host components
sudo sh "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}.run" -x sudo ./nvidia-installer --silent \
cd "NVIDIA-Linux-${DRIVER_ARCH}-${DRIVER_VERSION}" --no-kernel-module \
# Run installation without the kernel modules and host components --install-compat32-libs \
sudo ./nvidia-installer --silent \ --no-nouveau-check \
--no-kernel-module \ --no-nvidia-modprobe \
--install-compat32-libs \ --no-rpms \
--no-nouveau-check \ --no-backup \
--no-nvidia-modprobe \ --no-check-for-alternate-installs
--no-rpms \ sudo rm -rf /tmp/NVIDIA* && cd ~
--no-backup \
--no-check-for-alternate-installs
sudo rm -rf /tmp/NVIDIA* && cd ~
fi fi
# Allow starting Xorg from a pseudoterminal instead of strictly on a tty console # Allow starting Xorg from a pseudoterminal instead of strictly on a tty console
if [ ! -f /etc/X11/Xwrapper.config ]; then if [ ! -f /etc/X11/Xwrapper.config ]; then
echo -e "allowed_users=anybody\nneeds_root_rights=yes" | sudo tee /etc/X11/Xwrapper.config >/dev/null echo -e "allowed_users=anybody\nneeds_root_rights=yes" | sudo tee /etc/X11/Xwrapper.config > /dev/null
fi fi
if grep -Fxq "allowed_users=console" /etc/X11/Xwrapper.config; then if grep -Fxq "allowed_users=console" /etc/X11/Xwrapper.config; then
sudo sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config sudo sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config
fi fi
# Remove existing Xorg configuration # Remove existing Xorg configuration
if [ -f "/etc/X11/xorg.conf" ]; then if [ -f "/etc/X11/xorg.conf" ]; then
sudo rm -f "/etc/X11/xorg.conf" sudo rm -f "/etc/X11/xorg.conf"
fi fi
# Get first GPU device if all devices are available or $(NVIDIA_VISIBLE_DEVICES) is not set # Get first GPU device if all devices are available or `NVIDIA_VISIBLE_DEVICES` is not set
if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ] || [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then if [ "$NVIDIA_VISIBLE_DEVICES" == "all" ] || [ -z "$NVIDIA_VISIBLE_DEVICES" ]; then
export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)" export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
# Get first GPU device out of the visible devices in other situations # Get first GPU device out of the visible devices in other situations
else else
export GPU_SELECT="$(sudo nvidia-smi --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid --format=csv | sed -n 2p)" export GPU_SELECT="$(sudo nvidia-smi --id=$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1) --query-gpu=uuid --format=csv | sed -n 2p)"
if [ -z "$GPU_SELECT" ]; then if [ -z "$GPU_SELECT" ]; then
export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)" export GPU_SELECT="$(sudo nvidia-smi --query-gpu=uuid --format=csv | sed -n 2p)"
fi fi
fi fi
if [ -z "$GPU_SELECT" ]; then if [ -z "$GPU_SELECT" ]; then
echo "No NVIDIA GPUs detected or nvidia-container-toolkit not configured. Exiting." echo "No NVIDIA GPUs detected or nvidia-container-toolkit not configured. Exiting."
exit 1 exit 1
fi fi
# Setting `VIDEO_PORT` to none disables RANDR/XRANDR, do not set this if using datacenter GPUs # Setting `VIDEO_PORT` to none disables RANDR/XRANDR, do not set this if using datacenter GPUs
if [ "${VIDEO_PORT,,}" = "none" ]; then if [ "${VIDEO_PORT,,}" = "none" ]; then
export CONNECTED_MONITOR="--use-display-device=None" export CONNECTED_MONITOR="--use-display-device=None"
# 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 # 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
else else
export CONNECTED_MONITOR="--connected-monitor=${VIDEO_PORT}" export CONNECTED_MONITOR="--connected-monitor=${VIDEO_PORT}"
fi fi
# 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 # 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
HEX_ID="$(sudo nvidia-smi --query-gpu=pci.bus_id --id="$GPU_SELECT" --format=csv | sed -n 2p)" HEX_ID="$(sudo nvidia-smi --query-gpu=pci.bus_id --id="$GPU_SELECT" --format=csv | sed -n 2p)"
IFS=":." ARR_ID=("$HEX_ID") IFS=":." ARR_ID=($HEX_ID)
unset IFS unset IFS
BUS_ID="PCI:$((16#${ARR_ID[1]}))@$((16#${ARR_ID[0]})):$((16#${ARR_ID[2]})):$((16#${ARR_ID[3]}))" BUS_ID="PCI:$((16#${ARR_ID[1]}))@$((16#${ARR_ID[0]})):$((16#${ARR_ID[2]})):$((16#${ARR_ID[3]}))"
# A custom modeline should be generated because there is no monitor to fetch this information normally # A custom modeline should be generated because there is no monitor to fetch this information normally
@@ -99,7 +97,7 @@ sudo sed -i '/Driver\s\+"nvidia"/a\ Option "ModeValidation" "NoMaxPCl
# Add custom generated modeline to the configuration # Add custom generated modeline to the configuration
sudo sed -i '/Section\s\+"Monitor"/a\ '"$MODELINE" /etc/X11/xorg.conf sudo sed -i '/Section\s\+"Monitor"/a\ '"$MODELINE" /etc/X11/xorg.conf
# Prevent interference between GPUs, add this to the host or other containers running Xorg as well # Prevent interference between GPUs, add this to the host or other containers running Xorg as well
echo -e "Section \"ServerFlags\"\n Option \"AutoAddGPU\" \"false\"\nEndSection" | sudo tee -a /etc/X11/xorg.conf >/dev/null echo -e "Section \"ServerFlags\"\n Option \"AutoAddGPU\" \"false\"\nEndSection" | sudo tee -a /etc/X11/xorg.conf > /dev/null
# Default display is :0 across the container # Default display is :0 across the container
export DISPLAY=":0" export DISPLAY=":0"
@@ -108,7 +106,7 @@ export DISPLAY=":0"
# Wait for X11 to start # Wait for X11 to start
echo "Waiting for X socket" echo "Waiting for X socket"
until [ -S "/tmp/.X11-unix/X${DISPLAY/:/}" ]; do sleep 1; done until [ -S "/tmp/.X11-unix/X${DISPLAY/:/}" ]; do sleep 1; done
echo "X socket is ready" echo "X socket is ready"
echo "Session Running. Press [Return] to exit." echo "Session Running. Press [Return] to exit."