Skip to content

Instantly share code, notes, and snippets.

@paul-chambers
Last active February 23, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-chambers/26a111e8f5206aab38c14be35bc3964e to your computer and use it in GitHub Desktop.
Save paul-chambers/26a111e8f5206aab38c14be35bc3964e to your computer and use it in GitHub Desktop.
nvidia GPU passthru to an LXC container (for Debian)
#!/bin/bash
# In both host and container, add the nVidia repros (see https://nvidia.github.io/nvidia-container-runtime/)
set -x
which curl > /dev/null || apt install curl
# add the repo key:
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | sudo apt-key add -
# add the 'nvidia container runtime' repo to apt's sources
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | \
sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
# add the non-free repos to be able to install the nvidia driver
sed --in-place -e 's/non-free//' -e 's/contrib//' -e 's/main/main contrib non-free/' /etc/apt/sources.list
# update the apt database
apt update
# install the nivida driver and related packages
apt install nvidia-driver nvidia-kernel-dkms nvidia-smi nvidia-cuda-toolkit
apt install nvidia-persistenced && systemctl enable nvidia-persistenced && systemctl start nvidia-persistenced
# append the following to the lxc config file:
# lxc.cgroup.devices.allow: c 195:* rwm
# lxc.cgroup.devices.allow: c 235:* rwm
# lxc.cgroup.devices.allow: c 511:* rwm
# lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
# lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
# lxc.mount.entry: /dev/nvidia-modeset dev/nvidia-modeset none bind,optional,create=file
# lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file
# lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,optional,create=file
#which=$(which lxc-ls > /dev/null && echo host || echo container)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment