Skip to content

Instantly share code, notes, and snippets.

@prehensilecode
Created December 2, 2024 15:23
Show Gist options
  • Save prehensilecode/8df6a78927e6c9050513e262c208d45a to your computer and use it in GitHub Desktop.
Save prehensilecode/8df6a78927e6c9050513e262c208d45a to your computer and use it in GitHub Desktop.
Remove Nvidia & Nouveau kernel modules from initramfs and make persistent
#!/bin/bash
# Removes both nvidia and nouveau drivers from initramfs and rebuilds them.
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
if [[ $( id -u ) -ne 0 ]]
then
echo "This must be run as root"
exit 1
else
kernelversion=$(uname -r)
timestamp=$(date -u +%Y%m%d-%H%m%S)
# backup current initramfs
cp /boot/initramfs-${kernelversion}.img /boot/initramfs-${kernelversion}.img.${timestamp}.bak
# rebuild the initramfs image
dracut --omit-drivers "nvidia nouveau" -f
# exclude nvidia from future kernel upgrades and initramfs rebuilds
if [[ ! -f /etc/dracut.conf.d/omit-nvidia.conf ]]
then
echo "omit_dracutmodules+=\" nvidia \"" >> /etc/dracut.conf.d/omit-nvidia.conf
fi
if [[ ! -f /etc/dracut.conf.d/omit-nouveau.conf ]]
then
echo "omit_dracutmodules+=\" nouveau \"" >> /etc/dracut.conf.d/omit-nouveau.conf
fi
# update grub2
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
# backup current kdump initramfs
cp /boot/initramfs-${kernelversion}kdump.img /boot/initramfs-${kernelversion}kdump.img.${timestamp}.bak
# add blacklist to omit nvidia from kdump initramfs
grep -q "rd\.driver\.blacklist=nvidia,nouveau" /etc/sysconfig/kdump
if [[ $? -ne 0 ]]
then
sed -i '/^KDUMP_COMMANDLINE_APPEND=/s/"$/ rd.driver.blacklist=nvidia,nouveau"/' /etc/sysconfig/kdump
fi
# restart kdump service to pick up changes to kdump's initrd
kdumpctl restart
# rebuild kdump initramfs
mkdumprd -f /boot/initramfs-${kernelversion}kdump.img
# need reboot to take effect
echo "$(uname -n): DONE initramfs changes - REBOOT needed to take effect"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment