44set -eu
55
66KERNEL_VERSION=$( uname -r)
7- OPEN_KERNEL_MODULES_ENABLED=" ${OPEN_KERNEL_MODULES_ENABLED:- false} "
87RUN_DIR=/run/nvidia
98PID_FILE=${RUN_DIR} /${0##*/ } .pid
109DRIVER_BRANCH=${DRIVER_BRANCH:? " Missing driver version" }
@@ -14,6 +13,8 @@ NVIDIA_MODULE_PARAMS=()
1413NVIDIA_UVM_MODULE_PARAMS=()
1514NVIDIA_MODESET_MODULE_PARAMS=()
1615NVIDIA_PEERMEM_MODULE_PARAMS=()
16+ TARGETARCH=${TARGETARCH:? " Missing TARGETARCH env" }
17+ KERNEL_MODULE_TYPE=${KERNEL_MODULE_TYPE:- auto}
1718
1819_update_package_cache () {
1920 if [ " ${PACKAGE_TAG:- } " != " builtin" ]; then
@@ -232,6 +233,36 @@ _unload_driver() {
232233 return 0
233234}
234235
236+ _resolve_kernel_type_from_driver_branch () {
237+ [[ " ${DRIVER_BRANCH} " -lt 560 ]] && KERNEL_TYPE=kernel || KERNEL_TYPE=kernel-open
238+ }
239+
240+ # _resolve_kernel_type determines which kernel module type, open or proprietary, to install.
241+ # This function assumes that the nvidia-installer binary is in the PATH, so this function
242+ # should only be invoked after the userspace driver components have been installed.
243+ #
244+ # KERNEL_MODULE_TYPE is the frontend interface that users can use to configure which module
245+ # to install. Valid values for KERNEL_MODULE_TYPE are 'auto' (default), 'open', and 'proprietary'.
246+ # When 'auto' is configured, we use the nvidia-installer to recommend the module type to install.
247+ _resolve_kernel_type () {
248+ if [ " ${KERNEL_MODULE_TYPE} " == " proprietary" ]; then
249+ KERNEL_TYPE=kernel
250+ elif [ " ${KERNEL_MODULE_TYPE} " == " open" ]; then
251+ KERNEL_TYPE=kernel-open
252+ elif [ " ${KERNEL_MODULE_TYPE} " == " auto" ]; then
253+ kernel_module_type=$( nvidia-installer --print-recommended-kernel-module-type 2> /dev/null)
254+ if [ $? -ne 0 ]; then
255+ echo " failed to retrieve the recommended kernel module type from nvidia-installer, falling back to using the driver branch"
256+ _resolve_kernel_type_from_driver_branch
257+ return 0
258+ fi
259+ [[ " ${kernel_module_type} " == " open" ]] && KERNEL_TYPE=kernel-open || KERNEL_TYPE=kernel
260+ else
261+ echo " invalid value for the KERNEL_MODULE_TYPE variable: ${KERNEL_MODULE_TYPE} "
262+ return 1
263+ fi
264+ }
265+
235266# Link and install the kernel modules from a precompiled packages
236267_install_driver () {
237268 # Install necessary driver userspace packages
@@ -244,7 +275,7 @@ _install_driver() {
244275 libnvidia-fbc1-${DRIVER_BRANCH} -server
245276
246277 # Now install the precompiled kernel module packages signed by Canonical
247- if [ " $OPEN_KERNEL_MODULES_ENABLED " = true ]; then
278+ if [ " $KERNEL_TYPE " = " kernel-open " ]; then
248279 echo " Installing Open NVIDIA driver kernel modules..."
249280 apt-get install --no-install-recommends -y \
250281 linux-signatures-nvidia-${KERNEL_VERSION} \
@@ -276,6 +307,9 @@ _unmount_rootfs() {
276307}
277308
278309init () {
310+ # Determine the kernel module type
311+ _resolve_kernel_type || exit 1
312+
279313 echo -e " \n========== NVIDIA Software Installer ==========\n"
280314 echo -e " Starting installation of NVIDIA driver branch ${DRIVER_BRANCH} for Linux kernel version ${KERNEL_VERSION} \n"
281315
0 commit comments