|
| 1 | +#! /bin/bash |
| 2 | +# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved. |
| 3 | + |
| 4 | +set -euxo pipefail |
| 5 | +shopt -s lastpipe |
| 6 | + |
| 7 | +source "common.sh" |
| 8 | +source "docker.sh" |
| 9 | + |
| 10 | +install_nvidia_container_runtime_toolkit() { |
| 11 | + log INFO "Installing the NVIDIA Container Runtime Toolkit" |
| 12 | + |
| 13 | + local -r destination="${1:-/run/nvidia}" |
| 14 | + local -a packages=("/usr/bin/nvidia-container-runtime" \ |
| 15 | + "/usr/bin/nvidia-container-toolkit" \ |
| 16 | + "/usr/bin/nvidia-container-cli" \ |
| 17 | + "/etc/nvidia-container-runtime/config.toml" \ |
| 18 | + "/usr/lib/x86_64-linux-gnu/libnvidia-container.so.1") |
| 19 | + |
| 20 | + # TODO workaround until we fix the runtime requiring this |
| 21 | + # directory and file to exist at that location |
| 22 | + cp ./config.toml /etc/nvidia-container-runtime |
| 23 | + |
| 24 | + # Bash variables starts at 0 |
| 25 | + # ZSH variables starts at 1 |
| 26 | + for ((i=0; i < ${#packages[@]}; i++)); do |
| 27 | + packages[$i]=$(readlink -f ${packages[$i]}) |
| 28 | + done |
| 29 | + |
| 30 | + if [[ ! -d "${destination}" ]]; then |
| 31 | + log ERROR "Destination directory doesn't exist in container" |
| 32 | + log ERROR "Ensure that you have correctly mounted the destination directoy" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + |
| 36 | + cp "${packages[@]}" "${destination}" |
| 37 | + |
| 38 | + # Setup links to the real binaries to ensure that variables and configs |
| 39 | + # are pointing to the right path |
| 40 | + mv "${destination}/nvidia-container-toolkit" \ |
| 41 | + "${destination}/nvidia-container-toolkit.real" |
| 42 | + mv "${destination}/nvidia-container-runtime" \ |
| 43 | + "${destination}/nvidia-container-runtime.real" |
| 44 | + |
| 45 | + |
| 46 | + # Setup aliases so as to ensure that the path is correctly set |
| 47 | + cat <<- EOF > ${destination}/nvidia-container-toolkit |
| 48 | + #! /bin/sh |
| 49 | + LD_LIBRARY_PATH="${destination}" \ |
| 50 | + PATH="\$PATH:${destination}" \ |
| 51 | + ${destination}/nvidia-container-toolkit.real \ |
| 52 | + -config "${destination}/config.toml" \ |
| 53 | + \$@ |
| 54 | + EOF |
| 55 | + |
| 56 | + cat <<- EOF > ${destination}/nvidia-container-runtime |
| 57 | + #! /bin/sh |
| 58 | + LD_LIBRARY_PATH="${destination}" \ |
| 59 | + PATH="\$PATH:${destination}" \ |
| 60 | + ${destination}/nvidia-container-runtime.real \ |
| 61 | + \$@ |
| 62 | + EOF |
| 63 | + |
| 64 | + # Make sure that the alias files are executable |
| 65 | + chmod +x "${destination}/nvidia-container-toolkit" |
| 66 | + chmod +x "${destination}/nvidia-container-runtime" |
| 67 | +} |
| 68 | + |
| 69 | +main() { |
| 70 | + local -r destination="${1:-/run/nvidia}" |
| 71 | + local -r docker_socket="${2:-/var/run/docker.socket}" |
| 72 | + local -r nvidia_runtime="$(docker::info ${docker_socket})" |
| 73 | + |
| 74 | + if [[ "${nvidia_runtime}" = "${destination}/nvidia-container-runtime" ]]; then |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | + |
| 78 | + install_nvidia_container_runtime_toolkit "${destination}" |
| 79 | + docker::setup "${destination}" |
| 80 | + echo "docker info: $(docker::info ${docker_socket})" |
| 81 | +} |
| 82 | + |
| 83 | +main "$@" |
0 commit comments