@@ -8,6 +8,8 @@ DRIVER_RESET_RETRIES=10
88DELAY_BEFORE_VF_CREATION=${DELAY_BEFORE_VF_CREATION:- 15}
99KERNEL_VERSION=$( uname -r)
1010RUN_DIR=/run/nvidia
11+ NVIDIA_MODULE_PARAMS=()
12+ MODPROBE_CONFIG_DIR=" /etc/modprobe.d"
1113
1214export DEBIAN_FRONTEND=noninteractive
1315
@@ -105,8 +107,24 @@ _unmount_rootfs() {
105107
106108_install_driver () {
107109 local tmp_dir=$( mktemp -d)
108-
109- sh NVIDIA-Linux-${DRIVER_ARCH} -${DRIVER_VERSION} -vgpu-kvm.run --ui=none --no-questions --tmpdir ${tmp_dir} --no-systemd
110+ local install_args=()
111+ # Specify the --skip-module-load flag for versions of the nvidia-installer that
112+ # support it. From the nvidia-installer help output:
113+ #
114+ # --skip-module-load
115+ # Skip the test load of the NVIDIA kernel modules after the modules are built,
116+ # and skip loading them after installation is complete.
117+ #
118+ # Without this flag, a subtle bug can occur if the nvidia-installer fails to unload
119+ # the NVIDIA kernel modules after the test load. The modules will remain loaded and
120+ # any custom NVIDIA module parameters configured as input to the driver container
121+ # will not be applied.
122+ #
123+ DRIVER_BRANCH=$( echo ${DRIVER_VERSION} | cut -d. -f1)
124+ if [ " ${DRIVER_BRANCH} " -ge " 550" ]; then
125+ install_args+=(" --skip-module-load" )
126+ fi
127+ sh NVIDIA-Linux-${DRIVER_ARCH} -${DRIVER_VERSION} -vgpu-kvm.run --ui=none --no-questions --tmpdir ${tmp_dir} --no-systemd ${install_args[@]+" ${install_args[@]} " }
110128}
111129
112130# Create /dev/char directory if it doesn't exist inside the container.
@@ -133,8 +151,53 @@ _set_fw_search_path() {
133151 echo -n " $nv_fw_search_path " > $fw_path_config_file
134152}
135153
136- # Currently _install_driver() takes care of loading nvidia modules. Just need to start necessary vgpu daemons
154+ # For each kernel module configuration file mounted into the container,
155+ # parse the file contents and extract the custom module parameters that
156+ # are to be passed as input to 'modprobe'.
157+ #
158+ # Assumptions:
159+ # - Configuration file is named nvidia.conf
160+ # - Configuration file is mounted inside the container at /drivers.
161+ # - Each line in the file contains at least one parameter, where parameters on the same line
162+ # are space delimited. It is up to the user to properly format the file to ensure
163+ # the correct set of parameters are passed to 'modprobe'.
164+ _get_module_params () {
165+ local base_path=" /drivers"
166+ # nvidia
167+ if [ -f " ${base_path} /nvidia.conf" ]; then
168+ while IFS=" " read -r param || [ -n " $param " ]; do
169+ NVIDIA_MODULE_PARAMS+=(" $param " )
170+ done < " ${base_path} /nvidia.conf"
171+ echo " Module parameters provided for nvidia: ${NVIDIA_MODULE_PARAMS[@]} "
172+ fi
173+ }
174+
175+ _create_module_params_conf () {
176+ echo " Parsing kernel module parameters..."
177+ _get_module_params
178+
179+ if [ ${# NVIDIA_MODULE_PARAMS[@]} -gt 0 ]; then
180+ echo " Configuring nvidia module parameters in ${MODPROBE_CONFIG_DIR} /nvidia.conf"
181+ echo " options nvidia ${NVIDIA_MODULE_PARAMS[@]} " > ${MODPROBE_CONFIG_DIR} /nvidia.conf
182+ fi
183+ }
184+
185+ # Load NVIDIA driver kernel modules with custom parameters and start vGPU daemons
137186_load_driver () {
187+ # Unload modules if they're already loaded so we can reload with custom parameters
188+ if [ -f /sys/module/nvidia_vgpu_vfio/refcnt ] || [ -f /sys/module/nvidia/refcnt ]; then
189+ echo " NVIDIA modules already loaded by installer, unloading to apply custom parameters..."
190+ rmmod nvidia_vgpu_vfio 2> /dev/null || true
191+ rmmod nvidia 2> /dev/null || true
192+ fi
193+
194+ echo " Loading NVIDIA driver kernel modules..."
195+ set -o xtrace +o nounset
196+ modprobe nvidia
197+ modprobe nvidia_vgpu_vfio
198+ set +o xtrace -o nounset
199+
200+ # Start vGPU daemons
138201 /usr/bin/nvidia-vgpud
139202 /usr/bin/nvidia-vgpu-mgr &
140203
@@ -260,6 +323,7 @@ init() {
260323 _install_prerequisites
261324 _create_dev_char_directory
262325 _set_fw_search_path
326+ _create_module_params_conf
263327 _install_driver
264328 _load_driver || exit 1
265329 _mount_rootfs
0 commit comments