epm-kernel_update 4.79 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2013, 2016, 2017  Etersoft
# Copyright (C) 2013, 2016, 2017  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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/>.
#

20
load_helper epm-check_updated_repo
21
load_helper epm-sh-warmup
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
EFI=$(bootctl -p 2>/dev/null)
sdboot_loader_id=$(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

44 45
epm_kernel_update()
{
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

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 ;;
64 65 66 67 68

    '--used-kflavour' )
        used_kflavour
        info 'You used $USED_KFLAVOUR kernel kflavour'
        return ;;
69 70 71 72 73

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

76
    warmup_bases
77

78 79
    update_repo_if_needed

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

82 83 84 85 86 87 88 89
    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
90
        sudocmd update-kernel $dryrun $(subst_option non_interactive -y) $force $interactive $reinstall $verbose "$@" || return
91 92 93
        #docmd epm remove-old-kernels "$@" || fatal
        return ;;
    esac
94

95
    case $PMTYPE in
96
    dnf-rpm|dnf5-rpm)
97 98
        docmd epm install kernel
        ;;
99
    apt-*)
100
        message "Skipping: kernel package will update during dist-upgrade"
101
        ;;
102
    *)
103
        fatal 'Have no suitable command for $PMTYPE'
104 105
        ;;
    esac
106
}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

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
126
                sed -i "s|\(^$options[\"']\)\(.*\)\([\"']\)|\1\2 $search_string\3|" "$entry_file"
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            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
}
143 144

used_kflavour () {
145
    if [ $(uname -r | grep "def") ] ; then
146
        USED_KFLAVOUR=$(uname -r | awk -F'-' '{print $2 "-" $3}')
147 148
    else
        USED_KFLAVOUR=$(uname -r | awk -F'-' '{print $2}')
149 150
    fi
}
151 152 153

check_run_kernel() {
    used_kflavour
154
    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
155 156 157 158 159 160
        echo "The newest installed ${USED_KFLAVOUR} kernel is running."
        return 0
    else
        echo "The system has a newer ${USED_KFLAVOUR} kernel."
        return 1
    fi
161
}