Mainline Kernel on CentOS 7

From Leo's Notes
Last edited on 30 December 2021, at 02:23.

The following script will install the mainline kernel from ElRepo. The mainline kernel will be the most recent stable kernel rather than the more conservative kernel used in CentOS.

The following script was pulled from one of my setup scripts.

function install_new_kernel() {

	if ! yum repolist | grep elrepo > /dev/null ; then
		echo "This will now install the ELRepo repository from ELRepo.org."
		if ! prompt "Do you wish to install ELRepo on the system?" Y ; then
			return
		fi

		rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
		rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
		yum clean all
	fi

	# Need to enable the repo.
	sed -i '/\[elrepo-kernel\]/,/^\[/ s/enabled=0/enabled=1/' /etc/yum.repos.d/elrepo.repo

	# Should do a full update first.
	yum update

	# Install the mainline kernel
	yum install kernel-ml kernel-ml-devel kernel-ml-headers \
		|| die "Could not install kernel."

	Option=`awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg | /bin/grep -n 3.17 | head -n 1 | cut -f1 -d:`

	echo "Current kernels installed:"
	awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
	echo "-- End of List --"
	echo "Kernel made to boot will be: " $(awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg | sed -n "$Option{p;q}")
	
	if prompt "Is this okay?" Y ; then
		grub2-set-default $[$Option - 1]
		grub2-mkconfig -o /boot/grub2/grub.cfg
		echo "Default now set to: " $(grub2-editenv list)
		warning "You should now reboot the server to apply the new kernel."
	else
		echo "Please select the grub option and run:"
		echo " grub2-set-default X     - where X is the option"
		echo " grub2-mkconfig -o /boot/grub2/grub.cfg"
	fi
}

install_new_kernel