]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/kernel-install/kernel-install
Merge pull request #11578 from keszybz/gcc-9-fixes
[thirdparty/systemd.git] / src / kernel-install / kernel-install
CommitLineData
81516adc
HH
1#!/bin/bash
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
d9215cd8 4# SPDX-License-Identifier: LGPL-2.1+
81516adc
HH
5#
6# This file is part of systemd.
7#
81516adc
HH
8#
9# systemd is free software; you can redistribute it and/or modify it
10# under the terms of the GNU Lesser General Public License as published by
11# the Free Software Foundation; either version 2.1 of the License, or
12# (at your option) any later version.
13#
14# systemd is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public License
20# along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
eb933128
ZJS
22SKIP_REMAINING=77
23
6886b044
MM
24usage()
25{
a6c3d202 26 echo "Usage:"
0912c0b8 27 echo " $0 add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
d838db0d 28 echo " $0 remove KERNEL-VERSION"
6886b044
MM
29}
30
31dropindirs_sort()
32{
33 local suffix=$1; shift
34 local -a files
35 local f d i
36
db1e2bfc 37 readarray -t files <<<"$(
6886b044
MM
38 for d in "$@"; do
39 for i in "$d/"*"$suffix"; do
40 if [[ -e "$i" ]]; then
41 echo "${i##*/}"
42 fi
43 done
44 done | sort -Vu
db1e2bfc 45 )"
6886b044
MM
46
47 for f in "${files[@]}"; do
48 for d in "$@"; do
49 if [[ -e "$d/$f" ]]; then
50 echo "$d/$f"
51 continue 2
52 fi
53 done
54 done
55}
56
81516adc
HH
57export LC_COLLATE=C
58
a6c3d202
ZJS
59for i in "$@"; do
60 if [ "$i" == "--help" -o "$i" == "-h" ]; then
61 usage
62 exit 0
63 fi
64done
65
ea52e2ae
TG
66if [[ "${0##*/}" == 'installkernel' ]]; then
67 COMMAND='add'
d279b185
MAP
68 # make install doesn't pass any parameter wrt initrd handling
69 INITRD_OPTIONS=()
ea52e2ae
TG
70else
71 COMMAND="$1"
72 shift
d279b185 73 INITRD_OPTIONS=( "${@:3}" )
ea52e2ae
TG
74fi
75
76KERNEL_VERSION="$1"
77KERNEL_IMAGE="$2"
81516adc 78
6886b044
MM
79if [[ -f /etc/machine-id ]]; then
80 read MACHINE_ID < /etc/machine-id
81fi
82
6886b044 83if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
a6c3d202 84 echo "Not enough arguments" >&2
81516adc
HH
85 exit 1
86fi
87
9d8813b3
YW
88if ! [[ $MACHINE_ID ]]; then
89 BOOT_DIR_ABS=$(mktemp -d /tmp/kernel-install.XXXXX) || exit 1
90 trap "rm -rf '$BOOT_DIR_ABS'" EXIT INT QUIT PIPE
91elif [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
5b8411a2
LP
92 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
93elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
340defcd 94 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
5b8411a2
LP
95elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
96 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
97elif mountpoint -q /efi; then
98 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
99elif mountpoint -q /boot/efi; then
340defcd
HH
100 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
101else
102 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
103fi
104
9d8813b3
YW
105export KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID
106
81516adc
HH
107ret=0
108
db1e2bfc 109readarray -t PLUGINS <<<"$(
81516adc
HH
110 dropindirs_sort ".install" \
111 "/etc/kernel/install.d" \
112 "/usr/lib/kernel/install.d"
db1e2bfc 113)"
81516adc 114
6886b044 115case $COMMAND in
81516adc 116 add)
a6c3d202
ZJS
117 if [[ ! "$KERNEL_IMAGE" ]]; then
118 echo "Command 'add' requires an argument" >&2
d82d87da
MAP
119 exit 1
120 fi
6886b044
MM
121
122 mkdir -p "$BOOT_DIR_ABS" || {
123 echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
124 exit 1
125 }
81516adc
HH
126
127 for f in "${PLUGINS[@]}"; do
8f51399e 128 if [[ -x $f ]]; then
d279b185 129 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
eb933128
ZJS
130 x=$?
131 if [[ $x == $SKIP_REMAINING ]]; then
9d8813b3
YW
132 ret=0
133 break
eb933128
ZJS
134 fi
135 ((ret+=$x))
8f51399e 136 fi
81516adc 137 done
9d8813b3
YW
138
139 if ! [[ $MACHINE_ID ]] && ! rmdir "$BOOT_DIR_ABS"; then
140 echo "Warning: In kernel-install plugins, requiring BOOT_DIR_ABS to be preset is deprecated." >&2
141 echo " All plugins should not put anything in BOOT_DIR_ABS if the environment" >&2
142 echo " variable KERNEL_INSTALL_MACHINE_ID is empty." >&2
143 rm -rf "$BOOT_DIR_ABS"
144 ((ret+=$?))
145 fi
81516adc
HH
146 ;;
147
148 remove)
149 for f in "${PLUGINS[@]}"; do
8f51399e
HH
150 if [[ -x $f ]]; then
151 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
eb933128
ZJS
152 x=$?
153 if [[ $x == $SKIP_REMAINING ]]; then
9d8813b3
YW
154 ret=0
155 break
eb933128
ZJS
156 fi
157 ((ret+=$x))
8f51399e 158 fi
81516adc
HH
159 done
160
8f51399e
HH
161 rm -rf "$BOOT_DIR_ABS"
162 ((ret+=$?))
81516adc
HH
163 ;;
164
165 *)
a6c3d202 166 echo "Unknown command '$COMMAND'" >&2
6886b044
MM
167 exit 1
168 ;;
81516adc
HH
169esac
170
81516adc 171exit $ret