epm-kernel_update 4.8 KB
#!/bin/sh
#
# Copyright (C) 2013, 2016, 2017  Etersoft
# Copyright (C) 2013, 2016, 2017  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

load_helper epm-check_updated_repo
load_helper epm-sh-warmup

EFI=$(a="" bootctl -p 2>/dev/null)
sdboot_loader_id=$(a="" bootctl status 2>/dev/null | grep -oP '(?<=id: ).*')

if [ -f "$EFI/loader/entries/$sdboot_loader_id" ]; then
    entry_file="$EFI/loader/entries/$sdboot_loader_id"
    options="options"
    bootloader="systemd"
elif [ -f "/etc/sysconfig/grub2" ]; then
    entry_file="/etc/sysconfig/grub2"
    options="GRUB_CMDLINE_LINUX_DEFAULT="
    bootloader="grub"
elif [ -f "/etc/default/grub" ]; then
    entry_file="/etc/default/grub"
    options="GRUB_CMDLINE_LINUX_DEFAULT="
    bootloader="grub"
elif [ -f "$EFI/refind_linux.conf" ]; then
    entry_file="$EFI/refind_linux.conf"
    options="Boot with standard options"
    bootloader="refind"
fi

epm_kernel_update()
{

case $1 in
    '--list-kernel-options' )
        assure_root
        kernel_options_list
        return ;;

    '--add-kernel-options')
        assure_root
        shift
        kernel_options_add "$@"
        return ;;

    '--remove-kernel-options')
        assure_root
        shift
        kernel_options_remove "$@"
        return ;;

    '--used-kflavour' )
        used_kflavour
        info 'You used $USED_KFLAVOUR kernel kflavour'
        return ;;

    '--check-run-kernel' )
        assure_root
        check_run_kernel
        return ;;
esac

    warmup_bases

    update_repo_if_needed

    info "Updating system kernel to the latest version..."

    case $BASEDISTRNAME in
    "alt")
        load_helper epm-query_package
        if ! __epm_query_package kernel-image >/dev/null ; then
            info "No installed kernel packages, skipping update"
            return
        fi
        assure_exists update-kernel update-kernel 0.9.9
        sudocmd update-kernel $dryrun $(subst_option non_interactive -y) $force $interactive $reinstall $verbose "$@" || return
        #docmd epm remove-old-kernels "$@" || fatal
        return ;;
    esac

    case $PMTYPE in
    dnf-rpm|dnf5-rpm)
        docmd epm install kernel
        ;;
    apt-*)
        message "Skipping: kernel package will update during dist-upgrade"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE'
        ;;
    esac
}

kernel_options_list () {
    if [ "$bootloader" = "refind" ] ; then
        grep "^\"$options\"" "$entry_file" | sed 's/^\"'"$options"'" //' | sed 's/\s*$//' | tr ' ' '\n'
    else
        grep "^$options" "$entry_file" | sed 's/^"'$options'" //' | sed 's/\s*$//' | tr ' ' '\n'
    fi
}

kernel_options_add () {
    for search_string in "$@"; do
        if grep -qF "$search_string" "$entry_file"; then
            echo "The string '$search_string' is already present in $entry_file"
        else
            echo "The string '$search_string' is not present in $entry_file"
            echo "Updating $entry_file"
            if [ $bootloader = "systemd" ]; then
                sed -i "/^$options/ s~\$~ $search_string~" "$entry_file"
            else
                sed -i "s|\(^$options[\"']\)\(.*\)\([\"']\)|\1\2 $search_string\3|" "$entry_file"
            fi
            echo "Added '$search_string' to the kernel boot parameters in $entry_file"
        fi
    done
}

kernel_options_remove() {
    for remove_string in "$@"; do
        if grep -qF "$remove_string" "$entry_file"; then
            sed -i "s~ $remove_string~~" "$entry_file"
            echo "Removed '$remove_string' from the kernel parameters in $entry_file"
        else
            echo "The string '$remove_string' is not present in $entry_file"
        fi
    done
}

used_kflavour () {
    if [ $(uname -r | grep "def") ] ; then
        USED_KFLAVOUR=$(uname -r | awk -F'-' '{print $2 "-" $3}')
    else
        USED_KFLAVOUR=$(uname -r | awk -F'-' '{print $2}')
    fi
}

check_run_kernel() {
    used_kflavour
    if ls /boot | grep -E "^vmlinuz-[0-9]+\.[0-9]+(\.[0-9]+)?-${USED_KFLAVOUR}-alt[0-9]*" | sort -Vr | head -n 1 | grep -q "$(uname -r)"; then
        echo "The newest installed ${USED_KFLAVOUR} kernel is running."
        return 0
    else
        echo "The system has a newer ${USED_KFLAVOUR} kernel."
        return 1
    fi
}