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