Skip to content

Commit 7c178f6

Browse files
Add fast-track to skip uninstall/install if NVIDIA driver modules present
Signed-off-by: Karthik Vetrivel <[email protected]>
1 parent 9cae45a commit 7c178f6

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

ubuntu24.04/nvidia-driver

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,33 @@ _get_module_params() {
244244
fi
245245
}
246246

247+
# Read the currently loaded NVIDIA driver version from sysfs.
248+
_read_loaded_version() {
249+
cat /sys/module/nvidia/version 2>/dev/null || return 1
250+
}
251+
252+
_is_rootfs_mounted() {
253+
findmnt -rno TARGET "${RUN_DIR}/driver" >/dev/null 2>&1
254+
}
255+
256+
# Ensure the driver rootfs is mounted exactly once.
257+
_ensure_rootfs_mounted_idempotent() {
258+
_is_rootfs_mounted || _mount_rootfs
259+
}
260+
261+
_ensure_persistence_running() {
262+
local pid_file=/var/run/nvidia-persistenced/nvidia-persistenced.pid pid
263+
if pid=$(<"${pid_file}" 2>/dev/null) && [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
264+
return 0
265+
fi
266+
267+
if command -v nvidia-persistenced >/dev/null 2>&1; then
268+
nvidia-persistenced --persistence-mode || true
269+
else
270+
echo "nvidia-persistenced not found; continuing without persistence"
271+
fi
272+
}
273+
247274
# Load the kernel modules and start persistenced.
248275
_load_driver() {
249276
echo "Parsing kernel module parameters..."
@@ -584,7 +611,27 @@ init() {
584611
trap "echo 'Caught signal'; exit 1" HUP INT QUIT PIPE TERM
585612
trap "_shutdown" EXIT
586613

587-
_unload_driver || exit 1
614+
# Fast path: if the NVIDIA kernel modules are already loaded and match the desired
615+
# version, avoid any heavy reinstall/build. Ensure rootfs is mounted and
616+
# persistenced is running, then hold the container.
617+
if [ -f /sys/module/nvidia/refcnt ]; then
618+
loaded_version=$(_read_loaded_version || true)
619+
if [ -n "${loaded_version}" ] && [ "${loaded_version}" = "${DRIVER_VERSION}" ]; then
620+
echo "Detected matching loaded driver (${loaded_version}); skipping reinstall"
621+
_ensure_rootfs_mounted_idempotent
622+
_ensure_persistence_running
623+
_write_kernel_update_hook
624+
echo "Done, now waiting for signal"
625+
sleep infinity &
626+
trap "echo 'Caught signal'; _shutdown && { kill $!; exit 0; }" HUP INT QUIT PIPE TERM
627+
trap - EXIT
628+
while true; do wait $! || continue; done
629+
exit 0
630+
fi
631+
fi
632+
633+
634+
_unload_driver || exit 1
588635
_unmount_rootfs
589636

590637
_update_ca_certificates

0 commit comments

Comments
 (0)