-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstallkernel
executable file
·534 lines (495 loc) · 15.6 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/bin/sh
#
# Copyright (C) 1995 - 1998, Ian A. Murdock <[email protected]>
# 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:=0}"
: "${INSTALLKERNEL_VERBOSE:=0}"
_ik_install_all=0
_ik_remaining_optargs=
_ik_arg_index=0
# Use same defaults as systemd's kernel-install
_ik_arg_1="$(uname -r)"
_ik_arg_2=/lib/modules/${_ik_arg_1}/vmlinuz
_ik_arg_3=/lib/modules/${_ik_arg_1}/System.map
_ik_arg_4=/boot
# 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
_ik_arg_5=
while [ ${#} -gt 0 ]; do
case "${1}" in
--help|-h)
echo "Gentoo's helper utility to install new kernel versions"
echo
echo "Usage: installkernel <version> <image> <System.map> <directory>"
echo
echo "Optional arguments:"
echo " -h, --help: display this help text"
echo " -v, --verbose: run in verbose mode and export \$INSTALLKERNEL_VERBOSE"
echo " -a, --all: iteratively install all versions available in /lib/modules/"
echo " --systemd, --no-systemd: offload installation to systemd's kernel-install"
echo
echo "Other optional arguments are passed on to systemd's kernel-install if this is enabled"
echo "via the --systemd argument. The default value for --systemd|--no-systemd is controlled"
echo "by the value of the \"systemd\" USE flag on the sys-kernel/installkernel package."
if command -v kernel-install >/dev/null; then
echo
kernel-install --help
fi
echo
echo "See the installkernel(8) man page for details."
exit 0
;;
--verbose|-v)
INSTALLKERNEL_VERBOSE=1
_ik_remaining_optargs="${_ik_remaining_optargs} ${1}"
;;
--all|-a)
_ik_install_all=1
;;
--systemd)
SYSTEMD_KERNEL_INSTALL=1
;;
--no-systemd)
SYSTEMD_KERNEL_INSTALL=0
;;
-*)
_ik_remaining_optargs="${_ik_remaining_optargs} ${1}"
;;
*)
_ik_arg_index=$((_ik_arg_index+1))
if [ ${_ik_arg_index} -eq 1 ]; then
_ik_arg_1=${1}
elif [ ${_ik_arg_index} -eq 2 ]; then
_ik_arg_2=${1}
elif [ ${_ik_arg_index} -eq 3 ]; then
_ik_arg_3=${1}
elif [ ${_ik_arg_index} -eq 4 ]; then
_ik_arg_4=${1}
elif [ ${_ik_arg_index} -eq 5 ]; then
_ik_arg_5=${1}
else
echo "Too many arguments."
echo "Usage: installkernel <version> <image> <System.map> <directory>"
echo " [--verbose] [--all] [--systemd|--no-systemd] [--help]"
exit 1
fi
;;
esac
shift
done
if [ "${SYSTEMD_KERNEL_INSTALL}" -eq 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.
if [ ${_ik_arg_index} -le 3 ] || [ "${_ik_arg_4}" = "/boot" ]; then
# kernel-install does not support relocation (ignores $4, see manual)
if [ ${_ik_install_all} -eq 1 ]; then
# shellcheck disable=SC2086
exec kernel-install add-all ${_ik_remaining_optargs}
else
# shellcheck disable=SC2086
exec kernel-install add ${_ik_remaining_optargs} "${_ik_arg_1}" "${_ik_arg_2}"
fi
else
echo "WARNING: A custom installation directory is specified as fourth argument."
echo "WARNING: Systemd kernel-install does not support this. Falling back"
echo "WARNING: to legacy installkernel. SYSTEMD_KERNEL_INSTALL is ignored."
fi
fi
set -e
# Used to find pre and post install hooks
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() {
oldsuffix="${3}.old"
if [ "${3%.efi}" != "${3}" ]; then
# Some UEFIs enforce .efi suffix, so if using efi files retain suffix
oldsuffix="-old${3}"
fi
if [ -f "${dir}/${1}-${ver}${3}" ]; then
# If they are hardlinked together, mv will fail.
rm -f "${dir}/${1}-${ver}${oldsuffix}"
mv "${dir}/${1}-${ver}${3}" "${dir}/${1}-${ver}${oldsuffix}"
fi
cat "${2}" >"${dir}/${1}-${ver}${3}"
# Update static version-less symlink/copy
if [ -f "${dir}/${1}${3}" ] || [ -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 [ -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}${oldsuffix}" "${dir}/${1}${oldsuffix}"
else
# If they are hardlinked together, mv will fail.
rm -f "${dir}/${1}${oldsuffix}"
mv "${dir}/${1}${3}" "${dir}/${1}${oldsuffix}"
fi
ln -sf "${1}-${ver}${3}" "${dir}/${1}${3}"
else # No links
# If they are hardlinked together, mv will fail.
rm -f "${dir}/${1}${oldsuffix}"
mv "${dir}/${1}${3}" "${dir}/${1}${oldsuffix}"
cat "${2}" >"${dir}/${1}${3}"
fi
fi
}
cleanup() {
# shellcheck disable=SC2317
if [ -n "${INSTALLKERNEL_STAGING_AREA}" ]; then
rm -rf "${INSTALLKERNEL_STAGING_AREA}"
fi
}
trap cleanup EXIT
# Main installation script that we will call later
main() {
ver=${1:?}
img=${2:?}
map=${3:?}
dir=${4:-/boot}
if [ ${#} -gt 4 ]; then
echo "Too many arguments."
return 1
fi
INSTALLKERNEL_STAGING_AREA="$(mktemp -d -t installkernel.staging.XXXXXXX)"
export INSTALLKERNEL_STAGING_AREA
suffix=
if [ "${INSTALLKERNEL_LAYOUT}" = "efistub" ]; then
if [ ${#} -le 3 ] || [ "${4%/boot}" != "${4}" ]
then
# Relocate to ESP
for candidate in "${dir}/EFI" "${dir}/efi" "${dir}" "${dir%/boot}/efi"
do
if [ -d "${candidate}/EFI/${NAME}" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Found vendor directory on ESP ${candidate}"
fi
dir=${candidate}/EFI/${NAME}
suffix=.efi
# backwards compatibility
if [ -f "/boot/intel-uc.img" ]; then
cp "/boot/intel-uc.img" "${dir}/intel-uc.img"
fi
if [ -f "/boot/amd-uc.img" ]; then
cp "/boot/amd-uc.img" "${dir}/amd-uc.img"
fi
else
continue
fi
done
if [ "${dir}" = "/boot" ] || [ "${dir}" = "${4}" ]
then
# No vendor dir found, try to create one
for candidate in "${dir}/EFI" "${dir}/efi" "${dir}" "${dir%/boot}/efi"
do
if [ -d "${candidate}/EFI" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Creating vendor directory on ESP ${candidate}"
fi
mkdir -p "${candidate}/EFI/${NAME}"
dir=${candidate}/EFI/${NAME}
suffix=.efi
# backwards compatibility
if [ -f "/boot/intel-uc.img" ]; then
cp "/boot/intel-uc.img" "${dir}/intel-uc.img"
fi
if [ -f "/boot/amd-uc.img" ]; then
cp "/boot/amd-uc.img" "${dir}/amd-uc.img"
fi
else
continue
fi
done
fi
if [ "${dir}" = "/boot" ] || [ "${dir}" = "${4}" ]
then
# Still no vendor dir, warn and fallback
echo "WARNING: layout=efistub set, but did not find vendor directory on ESP."
echo "WARNING: Please create the EFI/${NAME} directory on the EFI System partition manually."
fi
fi
fi
base_dir=$(dirname "${img}")
prebuilt_initrd=${base_dir}/initrd
prebuilt_uki=${base_dir}/uki.efi
initrd=${INSTALLKERNEL_STAGING_AREA}/initrd
uki=${INSTALLKERNEL_STAGING_AREA}/uki.efi
# Copy prebuilt initrd, uki.efi at kernel location
if [ -f "${prebuilt_initrd}" ]; then
cp "${prebuilt_initrd}" "${initrd}"
fi
if [ -f "${prebuilt_uki}" ]; then
cp "${prebuilt_uki}" "${uki}"
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
err=0
(
#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
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo
echo "Running ${plugin} ${ver} ${img}..."
echo
fi
"${plugin}" "${ver}" "${img}" || exit ${?}
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo
echo "Hook ${plugin} finished successfully"
echo
fi
done
) || err=${?}
[ ${err} -eq 77 ] && return 0
[ ${err} -ne 0 ] && return ${err}
else
echo "WARNING: A custom installation directory is specified as fourth argument."
echo "WARNING: In this configuration running installation hooks is not supported."
echo "WARNING: All pre-installation hooks are ignored."
fi
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}" != "${4}" ]
then
# Relocate to ESP
for candidate in "${dir}/EFI" "${dir}/efi" "${dir}" "${dir%/boot}/efi"
do
if [ -d "${candidate}/EFI/Linux" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Found UKI directory on ESP ${candidate}"
fi
dir=${candidate}/EFI/Linux
img_dest=${ID}
else
continue
fi
done
if [ "${dir}" = "/boot" ] || [ "${dir}" = "${4}" ]
then
# No UKI dir found, try to create one
for candidate in "${dir}/EFI" "${dir}/efi" "${dir}" "${dir%/boot}/efi"
do
if [ -d "${candidate}/EFI" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Creating UKI directory on ESP ${candidate}"
fi
mkdir -p "${candidate}/EFI/Linux"
dir=${candidate}/EFI/Linux
img_dest=${ID}
else
continue
fi
done
fi
if [ "${dir}" = "/boot" ] || [ "${dir}" = "${4}" ]
then
# Still no UKI dir, warn and fallback
echo "WARNING: layout=uki set, but did not find the UKI directory on ESP"
echo "WARNING: Please create the EFI/Linux directory on the EFI System partition manually"
fi
fi
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing Unified Kernel Image for ${ver}..."
fi
updatever "${img_dest}" "${uki}" "${suffix}"
else
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing kernel image for ${ver}..."
fi
updatever "${img_dest}" "${img}" "${suffix}"
if [ -f "${initrd}" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing initramfs image for ${ver}..."
fi
updatever initramfs "${initrd}" .img
fi
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing System.map for ${ver}..."
fi
updatever System.map "${map}"
basedir=$(dirname "${map}")
if [ -f "${basedir}/.config" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing config for ${ver}..."
fi
updatever config "${basedir}/.config"
elif [ -f "${basedir}/config" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Installing config for ${ver}..."
fi
updatever config "${basedir}/config"
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
err=0
(
#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
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo
echo "Running ${plugin} ${ver} ${dir}/${img_dest}-${ver}${suffix}..."
echo
fi
"${plugin}" "${ver}" "${dir}/${img_dest}-${ver}${suffix}" || exit ${?}
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo
echo "Hook ${plugin} finished successfully"
echo
fi
done
) || err=${?}
[ ${err} -eq 77 ] && return 0
[ ${err} -ne 0 ] && return ${err}
else
echo "WARNING: A custom installation directory is specified as fourth argument."
echo "WARNING: In this configuration running installation hooks is not supported."
echo "WARNING: All post-installation hooks are ignored."
fi
return 0
}
# Find and read the install.conf
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 /run/kernel/install.conf ]; then
install_conf=/run/kernel/install.conf
elif [ -f /usr/local/lib/kernel/install.conf ]; then
install_conf=/usr/local/lib/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
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "Reading ${install_conf}..."
fi
# shellcheck source=/dev/null
. "${install_conf}"
if [ -n "${layout}" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "${install_conf} configures layout=${layout}"
fi
if [ -z "${INSTALLKERNEL_LAYOUT}" ]; then
INSTALLKERNEL_LAYOUT="${layout}"
fi
fi
if [ -n "${initrd_generator}" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "${install_conf} configures initrd_generator=${initrd_generator}"
fi
if [ -z "${INSTALLKERNEL_INITRD_GENERATOR}" ]; then
INSTALLKERNEL_INITRD_GENERATOR="${initrd_generator}"
fi
fi
if [ -n "${uki_generator}" ]; then
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "${install_conf} configures uki_generator=${uki_generator}"
fi
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
export INSTALLKERNEL_VERBOSE
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}"
# Now we actually run the install
main_err=0
if [ ${_ik_install_all} -eq 1 ]; then
for ver in /lib/modules/*; do
ver=$(basename "${ver}")
img=/lib/modules/${ver}/vmlinuz
map=/lib/modules/${ver}/System.map
if [ -f "${img}" ] && [ -f "${map}" ]; then
main "${ver}" "${img}" "${map}" "${_ik_arg_4}" ||
{ main_err=${?}; echo "WARNING: Installing ${ver} failed"; }
cleanup
else
if [ "${INSTALLKERNEL_VERBOSE}" -gt 0 ]; then
echo "WARNING: Skipping ${ver}, no kernel image or system map available."
fi
fi
done
else
if [ -f "${_ik_arg_2}" ] && [ -f "${_ik_arg_3}" ]; then
main "${_ik_arg_1}" "${_ik_arg_2}" "${_ik_arg_3}" "${_ik_arg_4}" ||
{ main_err=${?}; echo "ERROR: Installing ${_ik_arg_1} failed"; }
cleanup
else
echo "ERROR: No such kernel image or system map."
exit 1
fi
fi
exit ${main_err}