]> git.ipfire.org Git - people/arne_f/kernel-updater.git/blame - src/kernel-updater.in
Check if kernel actually exists
[people/arne_f/kernel-updater.git] / src / kernel-updater.in
CommitLineData
0365c80b
MT
1#!/bin/bash
2###############################################################################
3# #
4# firmware-update - IPFire Firmware Update Tool #
5# Copyright (C) 2019 IPFire Development Team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22PACKAGE_NAME="@PACKAGE_NAME@"
23PACKAGE_VERSION="@PACKAGE_VERSION@"
24
7ee4984c
MT
25GRUB_CFG="/boot/grub/grub.cfg"
26MODULES_PATH="/lib/modules"
27
458c8b54
MT
28RUNNING_VERSION="$(uname -r)"
29
7ee4984c
MT
30update_bootloader() {
31 # GRUB
32 if [ -e "${GRUB_CFG}" ]; then
33 grub-mkconfig -o "${GRUB_CFG}" || return $?
34 fi
35
36 return 0
37}
38
39# This function installs a new kernel
40do_install() {
41 local version="${1}"
42
43 # Check if we actually have a kernel with this version
44 if ! check_version "${version}"; then
45 echo "Kernel ${version} does not exist" >&2
46 return 2
47 fi
48
49 # Update module dependencies
50 echo "Updating module dependencies..."
51 if ! depmod -A "${version}"; then
52 echo "Could not update module dependencies. Exiting." >&2
53 return 1
54 fi
55
56 # Generate initramfs
57 echo "Generating initramfs..."
58 if ! dracut --quiet --force --early-microcode --xz \
59 "/boot/initramfs-${version}.img" "${version}"; then
60 echo "Could not generate the initramfs" >&2
61 return 1
62 fi
63
64 # Update bootloader
65 echo "Updating bootloader configuration..."
66 if ! update_bootloader; then
67 echo "Could not update the bootloader configuration" >&2
68 return 1
69 fi
70
71 # All done
72 return 0
73}
74
458c8b54
MT
75do_uninstall() {
76 local version="${1}"
77
78 # Check if we are removing the running kernel
79 if [ "${version}" = "${RUNNING_VERSION}" ]; then
80 echo "You cannot remove the currently running kernel ${RUNNING_VERSION}" >&2
81 return 1
82 fi
83
84 # Remove initramfs
85 rm -f "/boot/initramfs-${version}.img"
86
87 # Update the bootloader configuration
88 update_bootloader || return $?
89
90 return 0
91}
92
7ee4984c
MT
93check_version() {
94 local version="${1}"
95
a284bf4f 96 if [ -d "${MODULES_PATH}/${version}/kernel" ]; then
7ee4984c
MT
97 return 0
98 else
99 return 1
100 fi
101}
102
0365c80b
MT
103main() {
104 local action="${1}"
7ee4984c
MT
105 local version="${2}"
106 shift 2
107
108 if [ -z "${version}" ]; then
458c8b54 109 version="${RUNNING_VERSION}"
7ee4984c 110 fi
0365c80b
MT
111
112 case "${action}" in
7ee4984c
MT
113 install)
114 do_install "${version}" || return $?
115 ;;
116
458c8b54
MT
117 uninstall)
118 do_uninstall "${version}" || return $?
119 ;;
120
eeb83428
MT
121 version)
122 echo "${PACKAGE_NAME}: Version ${PACKAGE_VERSION}"
123 return 0
124 ;;
125
0365c80b
MT
126 "")
127 echo "No command given" >&2
128 return 2
129 ;;
130
131 *)
132 echo "Invalid command: ${action}" >&2
133 return 2
134 ;;
135 esac
136}
137
138# Call main
139main "$@" || exit ${?}