Last active
August 21, 2022 14:18
-
-
Save dbfin/6735531 to your computer and use it in GitHub Desktop.
Sets default kernel and modifies update policy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -- | |
# requires bash 4.0+ | |
### FUNCTIONS | |
# usage: error_exit message | |
function error_exit() { | |
echo -e "\e[31m$1\e[0m" | |
exit 1 | |
} | |
# usage: get_command_option command option | |
function get_command_option() { | |
[ -z "$1" -o -z "$2" ] && error_exit 'get_command_option: missing arguments...' | |
value="$( $1 | grep "^$2=" )" | |
echo "${value#$2=}" | |
} | |
# usage: get_file_option file option | |
function get_file_option() { | |
[ -f "$1" ] || error_exit "get_file_option: file \"$1\" not found" | |
get_command_option "cat $1" "$2" | |
} | |
function get_grub_default() { | |
get_file_option '/etc/default/grub' 'GRUB_DEFAULT' | |
} | |
function update_grub_default() { | |
grub_default="$( get_grub_default )" | |
if [ ! "$grub_default" == 'saved' ]; then | |
sudo sed -i 's|^\(GRUB_DEFAULT=\).*$|\1saved|' /etc/default/grub \ | |
&& echo -e "\nUpdated grub to use \e[32m$( get_grub_default )\e[0m kernel version." \ | |
|| error_exit '\nupdate_grub_default: cannot update /etc/default/grub to use saved kernel version...' | |
[ $( which update-grub 2>/dev/null ) ] && sudo update-grub \ | |
|| [ $( which grub2-mkconfig 2>/dev/null ) ] && sudo grub2-mkconfig -o /boot/grub2/grub.cfg \ | |
|| error_exit 'update_grub_default: cannot reconfigure grub...' | |
else | |
echo -e "\nGrub is set to use \e[33m$( get_grub_default )\e[0m kernel version." | |
fi | |
} | |
function get_saved_kernel() { | |
get_command_option 'grub2-editenv list' 'saved_entry' | |
} | |
function set_saved_kernel() { | |
[ -z "$1" ] && error_exit 'set_saved_kernel: missing argument...' | |
sudo grub2-set-default "$1" | |
} | |
function get_kernel_update_default() { | |
get_file_option '/etc/sysconfig/kernel' 'UPDATEDEFAULT' | |
} | |
function toggle_kernel_update_default() { | |
kernel_update_default=$( get_kernel_update_default ) | |
if [ "${kernel_update_default,,}" == 'yes' ]; then | |
sudo sed -i 's|^\(UPDATEDEFAULT=\).*$|\1no|' /etc/sysconfig/kernel | |
else | |
sudo sed -i 's|^\(UPDATEDEFAULT=\).*$|\1yes|' /etc/sysconfig/kernel | |
fi | |
} | |
function get_kernels_limit() { | |
get_file_option '/etc/yum.conf' 'installonly_limit' | |
} | |
function update_kernel_removing() { | |
if [ -f /etc/yum.conf ]; then | |
kernels_limit="$( get_kernels_limit )" | |
if [[ "$kernels_limit" =~ ^[0-9]+$ ]]; then | |
kernel_update_default=$( get_kernel_update_default ) | |
ok=0 | |
if [ "${kernel_update_default,,}" == 'yes' -a $kernels_limit -eq 0 ]; then | |
sudo sed -i 's|^\(installonly_limit=\).*$|\13|' /etc/yum.conf && ok=1 || ok=-1 | |
elif [ "${kernel_update_default,,}" == "no" -a $kernels_limit -ne 0 ]; then | |
sudo sed -i 's|^\(installonly_limit=\).*$|\10|' /etc/yum.conf && ok=1 || ok=-1 | |
fi | |
case $ok in | |
1) echo -e "\nUpdated kernel removing policy.\nThe number of kernels to keep set to:" \ | |
"\e[32m$( get_kernels_limit | sed 's|^0$|infinity|' )\e[0m" ;; | |
-1) error_exit '\nCannot update kernel removing policy...' ;; | |
0) echo -e '\nNo updated kernel removing policy needed.' ;; | |
esac | |
fi | |
fi | |
} | |
### MAIN | |
# check/update grub default kernel policy to make saved kernel default | |
update_grub_default | |
# show current saved kernel | |
echo -e "\nCurrent default kernel: \e[33m$( get_saved_kernel )\e[0m" | |
# obtain the list of available kernels | |
kernels=() | |
count=0 | |
echo -e '\nAvailable kernels:' | |
while read s; do | |
let count=$count+1 | |
kernels[$count]="$s" | |
echo "$count: ${kernels[$count]}" | |
done < <( sudo cat /boot/grub2/grub.cfg \ | |
| grep '^menuentry.*[Ll]inux' \ | |
| grep -v 'rescue' \ | |
| sed "s|^\s*menuentry\s*['\"]\([^'\"]\+\)['\"].*$|\1|" \ | |
) | |
[ $count -eq 0 ] && error_exit 'No kernels found in /boot/grub2/grub.cfg...' | |
# ask for the number of the new default kernel | |
read -p "Enter kernel number to set as default (1-$count): [skip] " n | |
if [ -z "$n" ]; then | |
: | |
elif ! [[ "$n" =~ ^[0-9]*$ ]] || [ $n -lt 1 -o $n -gt $count ]; then | |
error_exit 'Wrong number.' | |
else | |
# set the new default kernel | |
kernel="${kernels[$n]}" | |
echo -e "\nDefault kernel will be set as $kernel" | |
read -p 'Confirm (y/n) ? [n] ' yn | |
if [[ "$yn" =~ ^[Yy]$ ]]; then | |
set_saved_kernel "$kernel" | |
echo -e "\nCurrent default kernel: \e[32m$( get_saved_kernel )\e[0m" | |
fi | |
fi | |
# set default kernel update behavior | |
echo -en "\nWhen the kernel gets updated it can automatically set the new kernel version as default." \ | |
"Current status of automatic default kernel updates: \e[33m$( get_kernel_update_default )\e[0m" \ | |
"\nDo you want to switch this behaviour (y/n) ? [n] " | |
read yn | |
if [[ "$yn" =~ ^[Yy]$ ]]; then | |
toggle_kernel_update_default | |
echo -e "\nCurrent status of automatic default kernel updates: \e[32m$( get_kernel_update_default )\e[0m" | |
# prevent kernel removing if needed: yum only | |
update_kernel_removing | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment