-
Notifications
You must be signed in to change notification settings - Fork 14
/
entrypoint.sh
executable file
·286 lines (252 loc) · 9.19 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o pipefail
set -u
set -x
KERNEL_SRC_URL="https://cdn.kernel.org/pub/linux/kernel/v4.x"
KERNEL_SRC_ARCHIVE="linux-$(uname -r | cut -d- -f1).tar.xz"
KERNEL_SRC_DIR="${KERNEL_SRC_DIR:-/build/usr/src/linux}"
ROOT_OS_RELEASE="${ROOT_OS_RELEASE:-/root/etc/os-release}"
NVIDIA_DRIVER_VERSION="${NVIDIA_DRIVER_VERSION:-410.78}"
NVIDIA_DRIVER_DOWNLOAD_URL_DEFAULT="https://us.download.nvidia.com/XFree86/Linux-x86_64/${NVIDIA_DRIVER_VERSION}/NVIDIA-Linux-x86_64-${NVIDIA_DRIVER_VERSION}.run"
NVIDIA_DRIVER_DOWNLOAD_URL="${NVIDIA_DRIVER_DOWNLOAD_URL:-$NVIDIA_DRIVER_DOWNLOAD_URL_DEFAULT}"
NVIDIA_INSTALL_DIR_HOST="${NVIDIA_INSTALL_DIR_HOST:-/opt/nvidia}"
NVIDIA_INSTALL_DIR_CONTAINER="${NVIDIA_INSTALL_DIR_CONTAINER:-/usr/local/nvidia}"
NVIDIA_INSTALLER_RUNFILE="$(basename "${NVIDIA_DRIVER_DOWNLOAD_URL}")"
ROOT_MOUNT_DIR="${ROOT_MOUNT_DIR:-/root}"
CACHE_FILE="${NVIDIA_INSTALL_DIR_CONTAINER}/.cache"
set +x
RETCODE_SUCCESS=0
RETCODE_ERROR=1
RETRY_COUNT=5
_log() {
local -r prefix="$1"
shift
echo "[${prefix}$(date -u "+%Y-%m-%d %H:%M:%S %Z")] ""$*" >&2
}
info() {
_log "INFO " "$*"
}
warn() {
_log "WARNING " "$*"
}
error() {
_log "ERROR " "$*"
}
load_etc_os_release() {
if [[ ! -f "${ROOT_OS_RELEASE}" ]]; then
error "File ${ROOT_OS_RELEASE} not found, /etc/os-release from Container Linux host must be mounted."
exit ${RETCODE_ERROR}
fi
. "${ROOT_OS_RELEASE}"
info "Running on Container Linux version id ${VERSION_ID}"
}
unload_modules() {
info "Unloading existing kernel module drivers"
for module in nvidia_uvm nvidia_drm nvidia; do
if grep -q -w ${module} /proc/modules; then
rmmod ${module} || exit ${RETCODE_ERROR}
info "Successfully unloaded ${module}"
fi
done
}
check_cached_version() {
info "Checking cached version"
if [[ ! -f "${CACHE_FILE}" ]]; then
info "Cache file ${CACHE_FILE} not found."
return ${RETCODE_ERROR}
fi
# Source the cache file and check if the cached driver matches
# currently running image build and driver versions.
. "${CACHE_FILE}"
if [[ "${VERSION_ID}" == "${CACHE_VERSION_ID}" ]]; then
if [[ "${NVIDIA_DRIVER_VERSION}" == \
"${CACHE_NVIDIA_DRIVER_VERSION}" ]]; then
info "Found existing driver installation for image version ${BUILD_ID} \
and driver version ${NVIDIA_DRIVER_VERSION}."
return ${RETCODE_SUCCESS}
fi
fi
if [[ -d "${ROOT_MOUNT_DIR}${NVIDIA_INSTALL_DIR_HOST}" ]]; then
unload_modules
info "Removing existing driver installation from ${ROOT_MOUNT_DIR}${NVIDIA_INSTALL_DIR_HOST}"
rm -rf "${ROOT_MOUNT_DIR}${NVIDIA_INSTALL_DIR_HOST}"
fi
return ${RETCODE_ERROR}
}
update_cached_version() {
cat >"${CACHE_FILE}"<<__EOF__
CACHE_VERSION_ID=${VERSION_ID}
CACHE_NVIDIA_DRIVER_VERSION=${NVIDIA_DRIVER_VERSION}
__EOF__
info "Updated cached version as:"
cat "${CACHE_FILE}"
}
update_container_ld_cache() {
info "Updating container's ld cache"
echo "${NVIDIA_INSTALL_DIR_CONTAINER}/lib64" > /etc/ld.so.conf.d/nvidia.conf
ldconfig
}
download_kernel_src_archive() {
local -r download_url="$1"
info "Kernel source archive download URL: ${download_url}"
mkdir -p "${KERNEL_SRC_DIR}"
pushd "${KERNEL_SRC_DIR}"
local attempts=0
until time curl -sfS "${download_url}" -o "${KERNEL_SRC_ARCHIVE}"; do
attempts=$(( ${attempts} + 1 ))
if (( "${attempts}" >= "${RETRY_COUNT}" )); then
error "Could not download kernel sources from ${download_url}, giving up."
return ${RETCODE_ERROR}
fi
warn "Error fetching kernel source archive from ${download_url}, retrying"
sleep 1
done
popd
}
download_kernel_src_from_git_repo() {
# KERNEL_COMMIT_ID comes from /root/etc/os-release file.
local -r download_url="${KERNEL_SRC_URL}/${KERNEL_SRC_ARCHIVE}"
download_kernel_src_archive "${download_url}"
}
download_kernel_src() {
if [[ -z "$(ls -A "${KERNEL_SRC_DIR}")" ]]; then
info "Kernel sources not found locally, downloading"
mkdir -p "${KERNEL_SRC_DIR}"
if ! download_kernel_src_from_git_repo; then
return ${RETCODE_ERROR}
fi
fi
pushd "${KERNEL_SRC_DIR}"
tar xf "${KERNEL_SRC_ARCHIVE}" --strip-components=1
popd
}
configure_kernel_src() {
info "Configuring kernel sources"
pushd "${KERNEL_SRC_DIR}"
zcat /proc/config.gz > .config
make olddefconfig
make modules_prepare
# TODO: Figure out why the kernel magic version hack is required.
local kernel_version_uname="$(uname -r)"
local kernel_version_src="$(cat include/generated/utsrelease.h | awk '{ print $3 }' | tr -d '"')"
if [[ "${kernel_version_uname}" != "${kernel_version_src}" ]]; then
info "Modifying kernel version magic string in source files"
sed -i "s|${kernel_version_src}|${kernel_version_uname}|g" "include/generated/utsrelease.h"
fi
popd
}
configure_nvidia_installation_dirs() {
info "Configuring installation directories"
mkdir -p "${NVIDIA_INSTALL_DIR_CONTAINER}"
pushd "${NVIDIA_INSTALL_DIR_CONTAINER}"
# nvidia-installer does not provide an option to configure the
# installation path of `nvidia-modprobe` utility and always installs it
# under /usr/bin. The following workaround ensures that
# `nvidia-modprobe` is accessible outside the installer container
# filesystem.
mkdir -p bin bin-workdir
mount -t overlay -o lowerdir=/usr/bin,upperdir=bin,workdir=bin-workdir none /usr/bin
# nvidia-installer does not provide an option to configure the
# installation path of libraries such as libnvidia-ml.so. The following
# workaround ensures that the libs are accessible from outside the
# installer container filesystem.
mkdir -p lib64 lib64-workdir
mkdir -p /usr/lib/x86_64-linux-gnu
mount -t overlay -o lowerdir=/usr/lib/x86_64-linux-gnu,upperdir=lib64,workdir=lib64-workdir none /usr/lib/x86_64-linux-gnu
# nvidia-installer does not provide an option to configure the
# installation path of driver kernel modules such as nvidia.ko. The following
# workaround ensures that the modules are accessible from outside the
# installer container filesystem.
mkdir -p drivers drivers-workdir
mkdir -p /lib/modules/"$(uname -r)"/video
mount -t overlay -o lowerdir=/lib/modules/"$(uname -r)"/video,upperdir=drivers,workdir=drivers-workdir none /lib/modules/"$(uname -r)"/video
# Populate ld.so.conf to avoid warning messages in nvidia-installer logs.
update_container_ld_cache
# Install an exit handler to cleanup the overlayfs mount points.
trap "{ umount /lib/modules/\"$(uname -r)\"/video; umount /usr/lib/x86_64-linux-gnu ; umount /usr/bin; }" EXIT
popd
}
download_nvidia_installer() {
info "Downloading NVIDIA installer ... "
pushd "${NVIDIA_INSTALL_DIR_CONTAINER}"
curl -L -sS "${NVIDIA_DRIVER_DOWNLOAD_URL}" -o "${NVIDIA_INSTALLER_RUNFILE}"
popd
}
run_nvidia_installer() {
info "Running NVIDIA installer"
# Load deps
if ! grep -q -w ipmi_msghandler /proc/modules; then
insmod `find /root/lib/modules -iname ipmi_msghandler.ko`
fi
if ! grep -q -w ipmi_devintf /proc/modules; then
insmod `find /root/lib/modules -iname ipmi_devintf.ko`
fi
pushd "${NVIDIA_INSTALL_DIR_CONTAINER}"
IGNORE_MISSING_MODULE_SYMVERS=1 \
sh "${NVIDIA_INSTALLER_RUNFILE}" \
--kernel-source-path="${KERNEL_SRC_DIR}" \
--no-drm \
--no-opengl-files \
--silent
popd
}
configure_cached_installation() {
info "Configuring cached driver installation"
update_container_ld_cache
if ! grep -q -w nvidia /proc/modules; then
insmod "${NVIDIA_INSTALL_DIR_CONTAINER}/drivers/nvidia.ko"
fi
if ! grep -q -w nvidia_uvm /proc/modules; then
insmod "${NVIDIA_INSTALL_DIR_CONTAINER}/drivers/nvidia-uvm.ko"
fi
if ! grep -q -w nvidia_drm /proc/modules; then
insmod "${NVIDIA_INSTALL_DIR_CONTAINER}/drivers/nvidia-drm.ko"
fi
}
verify_nvidia_installation() {
info "Verifying NVIDIA installation"
export PATH="${NVIDIA_INSTALL_DIR_CONTAINER}/bin:${PATH}"
nvidia-smi
# Create unified memory device file.
nvidia-modprobe -c0 -u
# TODO: Add support for enabling persistence mode.
}
update_host_ld_cache() {
info "Updating host's ld cache"
mkdir -p ${ROOT_MOUNT_DIR}/etc/ld.so.conf.d
echo "${NVIDIA_INSTALL_DIR_HOST}/lib64" > "${ROOT_MOUNT_DIR}/etc/ld.so.conf.d/nvidia.conf"
ldconfig -r "${ROOT_MOUNT_DIR}"
}
main() {
load_etc_os_release
if check_cached_version; then
configure_cached_installation
verify_nvidia_installation
info "Found cached version, NOT building the drivers."
else
info "Did not find cached version, building the drivers..."
download_kernel_src
configure_nvidia_installation_dirs
download_nvidia_installer
configure_kernel_src
run_nvidia_installer
update_cached_version
verify_nvidia_installation
info "Finished installing the drivers."
fi
update_host_ld_cache
}
main "$@"