Bladeren bron

Automate NVIDIA driver installation on startup

ehfd 4 jaren geleden
bovenliggende
commit
0af7b8966e
4 gewijzigde bestanden met toevoegingen van 42 en 53 verwijderingen
  1. 10 46
      Dockerfile
  2. 3 4
      README.md
  3. 27 3
      bootstrap.sh
  4. 2 0
      xgl.yaml

+ 10 - 46
Dockerfile

@@ -1,6 +1,5 @@
 FROM ubuntu:20.04
 
-# Based on https://github.com/ryought/glx-docker-headless-gpu/blob/master/Dockerfile
 LABEL maintainer "https://github.com/ehfd"
 
 # Make all NVIDIA GPUS visible, but we want to manually install drivers
@@ -13,6 +12,7 @@ ENV NVIDIA_DRIVER_CAPABILITIES all
 ENV VNCPASS vncpasswd
 ENV SIZEW 1920
 ENV SIZEH 1080
+ENV CDEPTH 24
 
 # Install locales to prevent errors
 RUN apt-get clean && \
@@ -24,9 +24,9 @@ ENV LANG en_US.UTF-8
 ENV LANGUAGE en_US:en
 ENV LC_ALL en_US.UTF-8
 
-# Almost same as nvidia/driver https://gitlab.com/nvidia/container-images/driver/-/blob/master/ubuntu20.04/Dockerfile
+# https://gitlab.com/nvidia/container-images/driver/-/blob/master/ubuntu20.04/Dockerfile
 RUN dpkg --add-architecture i386 && \
-    apt-get update && apt-get install -y --no-install-recommends \
+    apt-get update && apt-get install -o APT::Immediate-Configure=false -y --no-install-recommends \
         apt-utils \
         build-essential \
         ca-certificates \
@@ -39,7 +39,7 @@ RUN dpkg --add-architecture i386 && \
         pkg-config && \
     rm -rf /var/lib/apt/lists/*
 
-# Install X server and desktop before driver
+# Install Xorg and desktop packages
 RUN apt-get update && apt-get install -y \
         software-properties-common \
         wget \
@@ -75,39 +75,7 @@ RUN apt-get update && apt-get install -y \
         supervisor \
         net-tools \
         ubuntu-mate-core \
-        ubuntu-mate-desktop && \
-    rm -rf /var/lib/apt/lists/*
-
-# Install NVIDIA drivers, including X graphic drivers by omitting --x-{prefix,module-path,library-path,sysconfig-path}
-# Driver version must be equal to the host
-#ARG BASE_URL=https://us.download.nvidia.com/tesla
-ARG BASE_URL=http://us.download.nvidia.com/XFree86/Linux-x86_64
-ENV DRIVER_VERSION 460.39
-RUN cd /tmp && \
-    curl -fSsl -O $BASE_URL/$DRIVER_VERSION/NVIDIA-Linux-x86_64-$DRIVER_VERSION.run && \
-    sh NVIDIA-Linux-x86_64-$DRIVER_VERSION.run -x && \
-    cd NVIDIA-Linux-x86_64-$DRIVER_VERSION* && \
-    ./nvidia-installer --silent \
-                       --no-kernel-module \
-                       --install-compat32-libs \
-                       --no-nouveau-check \
-                       --no-nvidia-modprobe \
-                       --no-rpms \
-                       --no-backup \
-                       --no-check-for-alternate-installs \
-                       --no-libglx-indirect \
-                       --no-install-libglvnd && \
-    mkdir -p /usr/src/nvidia-$DRIVER_VERSION && \
-    mv LICENSE mkprecompiled kernel /usr/src/nvidia-$DRIVER_VERSION && \
-    sed '9,${/^\(kernel\|LICENSE\)/!d}' .manifest > /usr/src/nvidia-$DRIVER_VERSION/.manifest && \
-    rm -rf /tmp/*
-
-# Install packages related to X server
-# pkg-config: nvidia-xconfig requires this package
-# mesa-utils: This package includes glxgears and glxinfo, which is useful for testing GLX drivers
-# x11vnc: Make connection between X11 server and VNC client.
-# x11-apps: xeyes can be used to make sure that X11 server is running.
-RUN apt-get update && apt-get install -y --no-install-recommends \
+        ubuntu-mate-desktop \
         mesa-utils \
         x11vnc \
         x11-apps && \
@@ -125,7 +93,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
     cd vkdevicechooser && \
     meson builddir --prefix=/usr && \
     meson install -C builddir && \
-    cd / && rm -rf /tmp/*
+    rm -rf /tmp/*
 
 # Sound driver including PulseAudio and GTK library
 # If you want to use sounds on docker, try 'pulseaudio --start'
@@ -136,22 +104,18 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
     rm -rf /var/lib/apt/lists/*
 
 # noVNC and Websockify
-ENV NOVNC_VERSION 1.1.0
-RUN wget https://github.com/novnc/noVNC/archive/v$NOVNC_VERSION.zip && \
-    unzip -q v$NOVNC_VERSION.zip && \
-    rm -rf v$NOVNC_VERSION.zip && \
-    mv noVNC-$NOVNC_VERSION /opt/noVNC && \
+ENV NOVNC_VERSION 1.2.0
+RUN curl -fsSL https://github.com/novnc/noVNC/archive/v${NOVNC_VERSION}.tar.gz | tar -xzf - -C /opt && \
+    mv /opt/noVNC-${NOVNC_VERSION} /opt/noVNC && \
     ln -s /opt/noVNC/vnc.html /opt/noVNC/index.html && \
     git clone https://github.com/novnc/websockify /opt/noVNC/utils/websockify
 
-# X server segfault error mitigation
+# Xorg segfault error mitigation
 RUN apt-get update && apt-get install -y --no-install-recommends \
         dbus-x11 \
         libdbus-c++-1-0v5 && \
     rm -rf /var/lib/apt/lists/*
 
-RUN sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config
-
 # Create user with password ${VNCPASS}
 RUN apt-get update && apt-get install -y --no-install-recommends \
         sudo && \

+ 3 - 4
README.md

@@ -10,10 +10,9 @@ Use
 for a MATE Desktop container that directly accesses NVIDIA GPUs without using an
 X Server (without Vulkan support).
 
-**Change the NVIDIA GPU driver version inside the container to be equal to the
-host and build your own Dockerfile.** Change **bootstrap.sh** also if you are
-using a headless GPU like Tesla. Corresponding container toolkit on the host for
-allocating GPUs should also be set up.
+Corresponding container toolkit on the host for allocating GPUs should be set
+up. Container startup should take some time as it automatically installs NVIDIA
+drivers.
 
 Connect to the spawned noVNC WebSocket instance with a browser in port 5901, no
 VNC client required (password for the default user is 'vncpasswd').

+ 27 - 3
bootstrap.sh

@@ -5,13 +5,37 @@ trap "echo TRAPed signal" HUP INT QUIT KILL TERM
 
 echo "user:$VNCPASS" | sudo chpasswd
 
-# NVIDIA driver version inside the container from Dockerfile must be equal to the host
+# Install NVIDIA drivers, including X graphic drivers by omitting --x-{prefix,module-path,library-path,sysconfig-path}
+export DRIVER_VERSION=$(head -n1 </proc/driver/nvidia/version | awk '{ print $8 }')
+BASE_URL=https://us.download.nvidia.com/XFree86/Linux-x86_64
+cd /tmp
+curl -fSsl -O $BASE_URL/$DRIVER_VERSION/NVIDIA-Linux-x86_64-$DRIVER_VERSION.run
+sudo sh NVIDIA-Linux-x86_64-$DRIVER_VERSION.run -x
+cd NVIDIA-Linux-x86_64-$DRIVER_VERSION
+sudo ./nvidia-installer --silent \
+                   --no-kernel-module \
+                   --install-compat32-libs \
+                   --no-nouveau-check \
+                   --no-nvidia-modprobe \
+                   --no-rpms \
+                   --no-backup \
+                   --no-check-for-alternate-installs \
+                   --no-libglx-indirect \
+                   --no-install-libglvnd
+sudo rm -rf /tmp/NVIDIA*
+cd ~
+
+sudo sed -i "s/allowed_users=console/allowed_users=anybody/;$ a needs_root_rights=yes" /etc/X11/Xwrapper.config
+
+if ! sudo nvidia-smi --id="$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1)" -q | grep -q "Tesla"; then
+  DISPLAYSTRING="--use-display-device=None"
+fi
+
 HEX_ID=$(sudo nvidia-smi --query-gpu=pci.bus_id --id="$(echo "$NVIDIA_VISIBLE_DEVICES" | cut -d ',' -f1)" --format=csv | sed -n 2p)
 IFS=":." ARR_ID=($HEX_ID)
 unset IFS
 BUS_ID=PCI:$((16#${ARR_ID[1]})):$((16#${ARR_ID[2]})):$((16#${ARR_ID[3]}))
-# Leave out --use-display-device=None if GPU is headless such as Tesla, and download links of such GPU drivers in Dockerfile should also be changed
-sudo nvidia-xconfig --virtual="${SIZEW}x${SIZEH}" --allow-empty-initial-configuration --enable-all-gpus --no-use-edid-dpi --busid="$BUS_ID" --use-display-device=None
+sudo nvidia-xconfig --virtual="${SIZEW}x${SIZEH}" --depth="$CDEPTH" --allow-empty-initial-configuration --enable-all-gpus --no-use-edid-dpi --busid="$BUS_ID" --only-one-x-screen "$DISPLAYSTRING"
 
 if [ "x$SHARED" == "xTRUE" ]; then
   export SHARESTRING="-shared"

+ 2 - 0
xgl.yaml

@@ -23,6 +23,8 @@ spec:
           value: "1920"
         - name: SIZEH
           value: "1080"
+        - name: CDEPTH
+          value: "24"
         - name: SHARED
           value: "TRUE"
         - name: VNCPASS