| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: LGPL-2.1+ |
| 4 |
|
| 5 |
set -eu |
| 6 |
|
| 7 |
# NVIDIA_VISIBLE_DEVICES="" *or* NVIDIA_VISIBLE_DEVICES="void" |
| 8 |
# GPU support was explicitly disabled, exit early. |
| 9 |
if [ -z "${NVIDIA_VISIBLE_DEVICES-x}" ] || [ "${NVIDIA_VISIBLE_DEVICES:-}" = "void" ]; then |
| 10 |
exit 0 |
| 11 |
fi |
| 12 |
|
| 13 |
# https://github.com/nvidia/nvidia-container-runtime#cuda_version |
| 14 |
if [ -n "${CUDA_VERSION:-}" ] && [ -z "${NVIDIA_REQUIRE_CUDA:-}" ]; then |
| 15 |
# Legacy CUDA image: default to all devices and all driver capabilities. |
| 16 |
if [ -z "${NVIDIA_VISIBLE_DEVICES+x}" ]; then |
| 17 |
NVIDIA_VISIBLE_DEVICES="all" |
| 18 |
fi |
| 19 |
if [ -z "${NVIDIA_DRIVER_CAPABILITIES:-}" ]; then |
| 20 |
NVIDIA_DRIVER_CAPABILITIES="all" |
| 21 |
fi |
| 22 |
if [[ "${CUDA_VERSION}" =~ ^[0-9]+\.[0-9]+ ]]; then |
| 23 |
NVIDIA_REQUIRE_CUDA="cuda>=${BASH_REMATCH[0]}" |
| 24 |
fi |
| 25 |
else |
| 26 |
# NVIDIA_VISIBLE_DEVICES unset and it's not a legacy CUDA image. |
| 27 |
# This is not a GPU image, exit early. |
| 28 |
if [ -z "${NVIDIA_VISIBLE_DEVICES+x}" ]; then |
| 29 |
exit 0 |
| 30 |
fi |
| 31 |
fi |
| 32 |
|
| 33 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 34 |
if ! command -v nvidia-container-cli >/dev/null; then |
| 35 |
echo "ERROR: Missing tool nvidia-container-cli, see https://github.com/NVIDIA/libnvidia-container" >&2 |
| 36 |
exit 1 |
| 37 |
fi |
| 38 |
|
| 39 |
in_userns() { |
| 40 |
[ -e /proc/self/uid_map ] || { echo no; return; } |
| 41 |
while read line; do |
| 42 |
fields=$(echo $line | awk '{ print $1 " " $2 " " $3 }') |
| 43 |
[ "$fields" = "0 0 4294967295" ] && { echo no; return; } || true |
| 44 |
echo $fields | grep -q " 0 1$" && { echo userns-root; return; } || true |
| 45 |
done < /proc/self/uid_map |
| 46 |
|
| 47 |
if [ -e /proc/1/uid_map ]; then |
| 48 |
if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then |
| 49 |
echo userns-root |
| 50 |
return |
| 51 |
fi |
| 52 |
fi |
| 53 |
echo yes |
| 54 |
} |
| 55 |
|
| 56 |
get_ldconfig() { |
| 57 |
command -v "ldconfig.real" || command -v "ldconfig" |
| 58 |
return $? |
| 59 |
} |
| 60 |
|
| 61 |
capability_to_cli() { |
| 62 |
case "$1" in |
| 63 |
compute) echo "--compute";; |
| 64 |
compat32) echo "--compat32";; |
| 65 |
display) echo "--display";; |
| 66 |
graphics) echo "--graphics";; |
| 67 |
utility) echo "--utility";; |
| 68 |
video) echo "--video";; |
| 69 |
*) exit 1;; |
| 70 |
esac |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
# Same behavior as strconv.ParseBool in golang |
| 75 |
parse_bool() { |
| 76 |
case "$1" in |
| 77 |
1|t|T|TRUE|true|True) echo "true";; |
| 78 |
0|f|F|FALSE|false|False) echo "false";; |
| 79 |
*) exit 1;; |
| 80 |
esac |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
usage() { |
| 85 |
cat <<EOF |
| 86 |
nvidia-container-cli hook for LXC |
| 87 |
|
| 88 |
Special arguments: |
| 89 |
[ -h | --help ]: Print this help message and exit. |
| 90 |
|
| 91 |
Optional arguments: |
| 92 |
[ --no-load-kmods ]: Do not try to load the NVIDIA kernel modules. |
| 93 |
[ --disable-require ]: Disable all the constraints of the form NVIDIA_REQUIRE_*. |
| 94 |
[ --debug <path> ]: The path to the log file. |
| 95 |
[ --ldcache <path> ]: The path to the host system's DSO cache. |
| 96 |
[ --root <path> ]: The path to the driver root directory. |
| 97 |
[ --ldconfig <path> ]: The path to the ldconfig binary, use a '@' prefix for a host path. |
| 98 |
EOF |
| 99 |
return 0 |
| 100 |
} |
| 101 |
|
| 102 |
options=$(getopt -o h -l help,no-load-kmods,disable-require,debug:,ldcache:,root:,ldconfig: -- "$@") |
| 103 |
if [ $? -ne 0 ]; then |
| 104 |
usage |
| 105 |
exit 1 |
| 106 |
fi |
| 107 |
eval set -- "$options" |
| 108 |
|
| 109 |
CLI_LOAD_KMODS="true" |
| 110 |
CLI_DISABLE_REQUIRE="false" |
| 111 |
CLI_DEBUG= |
| 112 |
CLI_LDCACHE= |
| 113 |
CLI_ROOT= |
| 114 |
CLI_LDCONFIG= |
| 115 |
|
| 116 |
while :; do |
| 117 |
case "$1" in |
| 118 |
--help) usage && exit 1;; |
| 119 |
--no-load-kmods) CLI_LOAD_KMODS="false"; shift 1;; |
| 120 |
--disable-require) CLI_DISABLE_REQUIRE="true"; shift 1;; |
| 121 |
--debug) CLI_DEBUG=$2; shift 2;; |
| 122 |
--ldcache) CLI_LDCACHE=$2; shift 2;; |
| 123 |
--root) CLI_ROOT=$2; shift 2;; |
| 124 |
--ldconfig) CLI_LDCONFIG=$2; shift 2;; |
| 125 |
--) shift 1; break;; |
| 126 |
*) break;; |
| 127 |
esac |
| 128 |
done |
| 129 |
|
| 130 |
HOOK_SECTION= |
| 131 |
HOOK_TYPE= |
| 132 |
case "${LXC_HOOK_VERSION:-0}" in |
| 133 |
0) HOOK_SECTION="${2:-}"; HOOK_TYPE="${3:-}";; |
| 134 |
1) HOOK_SECTION="${LXC_HOOK_SECTION:-}"; HOOK_TYPE="${LXC_HOOK_TYPE:-}";; |
| 135 |
*) echo "ERROR: Unsupported hook version: ${LXC_HOOK_VERSION}." >&2; exit 1;; |
| 136 |
esac |
| 137 |
|
| 138 |
if [ "${HOOK_SECTION}" != "lxc" ]; then |
| 139 |
echo "ERROR: Not running through LXC." >&2 |
| 140 |
exit 1 |
| 141 |
fi |
| 142 |
|
| 143 |
if [ "${HOOK_TYPE}" != "mount" ]; then |
| 144 |
echo "ERROR: This hook must be used as a \"mount\" hook." >&2 |
| 145 |
exit 1 |
| 146 |
fi |
| 147 |
|
| 148 |
USERNS=$(in_userns) |
| 149 |
if [ "${USERNS}" != "yes" ]; then |
| 150 |
# This is a limitation of libnvidia-container. |
| 151 |
echo "FIXME: This hook currently only works in unprivileged mode." >&2 |
| 152 |
exit 1 |
| 153 |
fi |
| 154 |
|
| 155 |
if [ "${USERNS}" = "yes" ]; then |
| 156 |
CLI_LOAD_KMODS="false" |
| 157 |
if ! grep -q nvidia_uvm /proc/modules; then |
| 158 |
echo "WARN: Kernel module nvidia_uvm is not loaded, nvidia-container-cli might fail. Make sure the NVIDIA device driver is installed and loaded." >&2 |
| 159 |
fi |
| 160 |
fi |
| 161 |
|
| 162 |
# https://github.com/nvidia/nvidia-container-runtime#nvidia_disable_require |
| 163 |
if [ -n "${NVIDIA_DISABLE_REQUIRE:-}" ]; then |
| 164 |
if [ "$(parse_bool "${NVIDIA_DISABLE_REQUIRE}")" = "true" ]; then |
| 165 |
CLI_DISABLE_REQUIRE="true" |
| 166 |
fi |
| 167 |
fi |
| 168 |
|
| 169 |
if [ -z "${CLI_DEBUG}" ]; then |
| 170 |
if [ "${LXC_LOG_LEVEL}" = "DEBUG" ] || [ "${LXC_LOG_LEVEL}" = "TRACE" ]; then |
| 171 |
rootfs_path="${LXC_ROOTFS_PATH#*:}" |
| 172 |
hookdir="${rootfs_path/%rootfs/hook}" |
| 173 |
if mkdir -p "${hookdir}"; then |
| 174 |
CLI_DEBUG="${hookdir}/nvidia.log" |
| 175 |
fi |
| 176 |
fi |
| 177 |
fi |
| 178 |
|
| 179 |
# A '@' prefix means a host path. |
| 180 |
if [ -z "${CLI_LDCONFIG}" ]; then |
| 181 |
if host_ldconfig=$(get_ldconfig); then |
| 182 |
CLI_LDCONFIG="@${host_ldconfig}" |
| 183 |
fi |
| 184 |
fi |
| 185 |
|
| 186 |
# https://github.com/nvidia/nvidia-container-runtime#nvidia_visible_devices |
| 187 |
CLI_DEVICES="${NVIDIA_VISIBLE_DEVICES}" |
| 188 |
|
| 189 |
# https://github.com/nvidia/nvidia-container-runtime#nvidia_driver_capabilities |
| 190 |
CLI_CAPABILITIES= |
| 191 |
if [ -n "${NVIDIA_DRIVER_CAPABILITIES:-}" ]; then |
| 192 |
CLI_CAPABILITIES="${NVIDIA_DRIVER_CAPABILITIES//,/ }" |
| 193 |
fi |
| 194 |
|
| 195 |
if [ "${CLI_CAPABILITIES}" = "all" ]; then |
| 196 |
CLI_CAPABILITIES="compute compat32 display graphics utility video" |
| 197 |
fi |
| 198 |
|
| 199 |
if [ -z "${CLI_CAPABILITIES}" ]; then |
| 200 |
CLI_CAPABILITIES="utility" |
| 201 |
fi |
| 202 |
|
| 203 |
global_args=() |
| 204 |
configure_args=() |
| 205 |
|
| 206 |
if [ -n "${CLI_DEBUG}" ]; then |
| 207 |
echo "INFO: Writing nvidia-container-cli log at ${CLI_DEBUG}." >&2 |
| 208 |
global_args+=("--debug=${CLI_DEBUG}") |
| 209 |
fi |
| 210 |
|
| 211 |
if [ "${CLI_LOAD_KMODS}" = "true" ]; then |
| 212 |
global_args+=(--load-kmods) |
| 213 |
fi |
| 214 |
|
| 215 |
if [ "${USERNS}" = "yes" ]; then |
| 216 |
global_args+=(--user) |
| 217 |
configure_args+=(--no-cgroups) |
| 218 |
fi |
| 219 |
|
| 220 |
if [ -n "${CLI_LDCACHE}" ]; then |
| 221 |
global_args+=(--ldcache="${CLI_LDCACHE}") |
| 222 |
fi |
| 223 |
|
| 224 |
if [ -n "${CLI_ROOT}" ]; then |
| 225 |
global_args+=(--root="${CLI_ROOT}") |
| 226 |
fi |
| 227 |
|
| 228 |
if [ -n "${CLI_LDCONFIG}" ]; then |
| 229 |
configure_args+=(--ldconfig="${CLI_LDCONFIG}") |
| 230 |
fi |
| 231 |
|
| 232 |
if [ -n "${CLI_DEVICES}" ] && [ "${CLI_DEVICES}" != "none" ]; then |
| 233 |
configure_args+=(--device="${CLI_DEVICES}") |
| 234 |
fi |
| 235 |
|
| 236 |
for cap in ${CLI_CAPABILITIES}; do |
| 237 |
if arg=$(capability_to_cli "${cap}"); then |
| 238 |
configure_args+=("${arg}") |
| 239 |
else |
| 240 |
echo "ERROR: Unknown driver capability \"${cap}\"." >&2 |
| 241 |
exit 1 |
| 242 |
fi |
| 243 |
done |
| 244 |
|
| 245 |
# https://github.com/nvidia/nvidia-container-runtime#nvidia_require_ |
| 246 |
if [ "${CLI_DISABLE_REQUIRE}" = "false" ]; then |
| 247 |
for req in $(compgen -e "NVIDIA_REQUIRE_"); do |
| 248 |
configure_args+=("--require=${!req}") |
| 249 |
done |
| 250 |
fi |
| 251 |
|
| 252 |
if [ -d "/sys/kernel/security/apparmor" ]; then |
| 253 |
# Try to transition to the unconfined AppArmor profile. |
| 254 |
echo "changeprofile unconfined" > /proc/self/attr/current || true |
| 255 |
fi |
| 256 |
|
| 257 |
set -x |
| 258 |
exec nvidia-container-cli ${global_args[@]} configure "${configure_args[@]}" "${LXC_ROOTFS_MOUNT}" |