-
Notifications
You must be signed in to change notification settings - Fork 7
/
installkernel
executable file
·261 lines (234 loc) · 7.28 KB
/
installkernel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh
#
# Copyright (C) 1995 - 1998, Ian A. Murdock <imurdock@debian.org>
# Copyright (C) 1998, 1999, Guy Maor
# Copyright (C) 2002, Matthew Wilcox
# Copyright (C) 2002, 2004, 2005, 2007, 2009 Clint Adams
# Copyright (C) 2009 Manoj Srivasta
# Copyright 2020-2024 Gentoo Authors
#
# Install the kernel on a Linux system.
#
# This script is called by the kernel's "make install" if it is installed as
# /sbin/installkernel. It is also called by kernel-install.eclass.
SYSTEMD_KERNEL_INSTALL="${SYSTEMD_KERNEL_INSTALL:=0}"
if [ "${SYSTEMD_KERNEL_INSTALL}" = "1" ] && command -v kernel-install >/dev/null; then
# If the ${0} of kernel-install is installkernel it takes its arguments
# in the same way we do here. We could use "exec -a ${0} .. ${@}" for this,
# but the -a argument is not POSIX, only bash.
# TODO: maybe revisit this if we ever bashify the script.
exec kernel-install add --verbose "${1}" "${2}"
fi
set -e
# Parse the command line options. Of course, powerpc has to be all
# different, and passes in a fifth argument, just because it is
# "special". We ignore the fifth argument, and do not flag is as an
# error, which it would be for any arch apart from powerpc
if [ ${#} -eq 3 ] || [ ${#} -eq 4 ] || [ ${#} -eq 5 ]; then
ver="${1}"
img="${2}"
map="${3}"
if [ ${#} -ge 4 ] && [ -n "${4}" ]; then
dir="${4}"
else
dir="/boot"
fi
else
echo "Usage: installkernel <version> <image> <System.map> <directory>"
exit 1
fi
dropindirs_sort() {
for d; do
for i in "${d}/"*; do
[ -e "${i}" ] && echo "${i##*/}"
done
done | sort -Vu | while read -r f; do
for d; do
if [ -e "${d}/${f}" ]; then
[ -x "${d}/${f}" ] && echo "${d}/${f}"
continue 2
fi
done
done
}
# Create backups of older versions before installing
updatever() {
if [ -f "${dir}/${1}-${ver}${3}" ]; then
# If they are hardlinked together, mv will fail.
if [ "${3%.efi}" != "${3}" ]; then
# To EFI stub boot, file must have .efi suffix
rm -f "${dir}/${1}-${ver}-old${3}"
mv "${dir}/${1}-${ver}${3}" "${dir}/${1}-${ver}-old${3}"
else
rm -f "${dir}/${1}-${ver}${3}.old"
mv "${dir}/${1}-${ver}${3}" "${dir}/${1}-${ver}${3}.old"
fi
fi
cat "${2}" >"${dir}/${1}-${ver}${3}"
# This section is for backwards compatibility only
if test -f "${dir}/${1}${3}" || test -L "${dir}/${1}${3}"; then
# The presence of "${dir}/${1}${3}" is unusual in modern installations, and
# the results are mostly unused. So only recreate them if they
# already existed.
if test -L "${dir}/${1}${3}"; then
# If we were using links, continue to use links, updating if
# we need to.
if [ "$(readlink -f "${dir}/${1}${3}")" = "${dir}/${1}-${ver}${3}" ]; then
# Yup, we need to change
ln -sf "${1}-${ver}${3}.old" "${dir}/${1}${3}.old"
else
# If they are hardlinked together, mv will fail.
rm -f "${dir}/${1}${3}.old"
mv "${dir}/${1}${3}" "${dir}/${1}${3}.old"
fi
ln -sf "${1}-${ver}${3}" "${dir}/${1}${3}"
else # No links
# If they are hardlinked together, mv will fail.
rm -f "${dir}/${1}${3}.old"
mv "${dir}/${1}${3}" "${dir}/${1}${3}.old"
cat "${2}" >"${dir}/${1}${3}"
fi
fi
}
if [ -n "${INSTALLKERNEL_CONF_ROOT}" ]; then
install_conf="${INSTALLKERNEL_CONF_ROOT}/install.conf"
elif [ -f "/etc/kernel/install.conf" ]; then
install_conf="/etc/kernel/install.conf"
elif [ -f "/usr/lib/kernel/install.conf" ]; then
install_conf="/usr/lib/kernel/install.conf"
else
install_conf=
fi
if [ -f "${install_conf}" ]; then
echo "Reading ${install_conf}..."
# shellcheck source=/dev/null
. "${install_conf}"
if [ -n "${layout}" ]; then
echo "${install_conf} configures layout=${layout}"
if [ -z "${INSTALLKERNEL_LAYOUT}" ]; then
INSTALLKERNEL_LAYOUT="${layout}"
fi
fi
if [ -n "${initrd_generator}" ]; then
echo "${install_conf} configures initrd_generator=${initrd_generator}"
if [ -z "${INSTALLKERNEL_INITRD_GENERATOR}" ]; then
INSTALLKERNEL_INITRD_GENERATOR="${initrd_generator}"
fi
fi
if [ -n "${uki_generator}" ]; then
echo "${install_conf} configures uki_generator=${uki_generator}"
if [ -z "${INSTALLKERNEL_UKI_GENERATOR}" ]; then
INSTALLKERNEL_UKI_GENERATOR="${uki_generator}"
fi
fi
fi
export INSTALLKERNEL_LAYOUT
export INSTALLKERNEL_INITRD_GENERATOR
export INSTALLKERNEL_UKI_GENERATOR
if [ -f /etc/os-release ]; then
# shellcheck source=/dev/null
. /etc/os-release
elif [ -f /usr/lib/os-release ]; then
# shellcheck source=/dev/null
. /usr/lib/os-release
fi
# Set sane default if no or broken os-release
export NAME="${NAME:=Linux}"
export ID="${ID:=linux}"
export PRETTY_NAME="${PRETTY_NAME:=Linux}"
suffix=
if [ "${INSTALLKERNEL_LAYOUT}" = efistub ]; then
if [ ${#} -le 3 ] || [ "${4}" = "/boot" ]; then
# Relocate to ESP
for candidate in /boot/EFI /boot/efi /boot /efi; do
if [ -d "${candidate}/EFI/${NAME}" ]; then
dir=${candidate}/EFI/${NAME}
suffix=.efi
else
continue
fi
done
fi
fi
# If installing in the usual directory, run the same scripts that hook
# into kernel package installation. Also make sure the PATH includes
# /usr/sbin and /sbin, just as dpkg would.
if [ ${#} -le 3 ] || [ "${4}" = "/boot" ]; then
(
#shellcheck disable=SC2030
export LC_ALL=C PATH="${PATH}:/usr/sbin:/sbin"
if [ -z "${INSTALLKERNEL_PREINST_PLUGINS}" ]; then
INSTALLKERNEL_PREINST_PLUGINS="$(
dropindirs_sort \
"/etc/kernel/preinst.d" \
"/usr/lib/kernel/preinst.d"
)"
fi
for plugin in ${INSTALLKERNEL_PREINST_PLUGINS}; do
echo ""
echo "Running ${plugin} ${ver} ${img}"
echo ""
"${plugin}" "${ver}" "${img}"
done
)
fi
# use the same input path as /usr/lib/kernel/install.d/50-dracut.install
# and the same output path as dracut and genkernel-4 for best interop
base_dir=$(dirname "${img}")
initrd=${base_dir}/initrd
uki=${base_dir}/uki.efi
if [ "${img%vmlinux*}" != "${img}" ]; then
img_dest=vmlinux
else
img_dest=vmlinuz
fi
# If we found a uki.efi, install it instead of kernel+initrd
if [ -f "${uki}" ] && [ "${INSTALLKERNEL_LAYOUT}" = uki ]; then
suffix=.efi
if [ ${#} -le 3 ] || [ "${4}" = "/boot" ]; then
# Relocate to ESP
for candidate in /boot/EFI /boot/efi /boot /efi; do
if [ -d "${candidate}/EFI/Linux" ]; then
dir=${candidate}/EFI/Linux
img_dest=${ID}
else
continue
fi
done
fi
updatever "${img_dest}" "${uki}" "${suffix}"
else
updatever "${img_dest}" "${img}" "${suffix}"
if [ -f "${initrd}" ]; then
updatever initramfs "${initrd}" .img
fi
fi
updatever System.map "${map}"
config=$(dirname "${map}")
config="${config}/.config"
if [ -f "${config}" ]; then
updatever config "${config}"
fi
# If installing in the usual directory, run the same scripts that hook
# into kernel package installation. Also make sure the PATH includes
# /usr/sbin and /sbin, just as dpkg would.
if [ ${#} -le 3 ] || [ "${4}" = "/boot" ]; then
(
#shellcheck disable=SC2031
export LC_ALL=C PATH="${PATH}:/usr/sbin:/sbin"
if [ -z "${INSTALLKERNEL_POSTINST_PLUGINS}" ]; then
INSTALLKERNEL_POSTINST_PLUGINS="$(
dropindirs_sort \
"/etc/kernel/postinst.d" \
"/usr/lib/kernel/postinst.d"
)"
fi
for plugin in ${INSTALLKERNEL_POSTINST_PLUGINS}; do
echo ""
echo "Running ${plugin} ${ver} ${dir}/${img_dest}-${ver}${suffix}"
echo ""
"${plugin}" "${ver}" "${dir}/${img_dest}-${ver}${suffix}"
done
)
fi
exit 0