]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
test: add shutdown test
[thirdparty/systemd.git] / src / kernel-install / kernel-install
1 #!/usr/bin/env bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 # SPDX-License-Identifier: LGPL-2.1-or-later
5 #
6 # This file is part of systemd.
7 #
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
21 SKIP_REMAINING=77
22
23 usage()
24 {
25 echo "Usage:"
26 echo " $0 [OPTIONS...] add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
27 echo " $0 [OPTIONS...] remove KERNEL-VERSION"
28 echo "Options:"
29 echo " -h,--help Print this help"
30 echo " -v,--verbose Increase verbosity"
31 }
32
33 dropindirs_sort()
34 {
35 local suffix=$1; shift
36 local -a files
37 local f d i
38
39 readarray -t files <<<"$(
40 for d in "$@"; do
41 for i in "$d/"*"$suffix"; do
42 if [[ -e "$i" ]]; then
43 echo "${i##*/}"
44 fi
45 done
46 done | sort -Vu
47 )"
48
49 for f in "${files[@]}"; do
50 for d in "$@"; do
51 if [[ -e "$d/$f" ]]; then
52 echo "$d/$f"
53 continue 2
54 fi
55 done
56 done
57 }
58
59 export LC_COLLATE=C
60
61 for i in "$@"; do
62 if [ "$i" == "--help" -o "$i" == "-h" ]; then
63 usage
64 exit 0
65 fi
66 done
67
68 KERNEL_INSTALL_VERBOSE=0
69 if [ "$1" == "--verbose" -o "$1" == "-v" ]; then
70 shift
71 KERNEL_INSTALL_VERBOSE=1
72 fi
73 export KERNEL_INSTALL_VERBOSE
74
75 if [[ "${0##*/}" == 'installkernel' ]]; then
76 COMMAND='add'
77 # make install doesn't pass any parameter wrt initrd handling
78 INITRD_OPTIONS=()
79 else
80 COMMAND="$1"
81 shift
82 INITRD_OPTIONS=( "${@:3}" )
83 fi
84
85 KERNEL_VERSION="$1"
86 KERNEL_IMAGE="$2"
87
88 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
89 echo "Not enough arguments" >&2
90 exit 1
91 fi
92
93 if [ -r "/etc/kernel/install.conf" ]; then
94 . /etc/kernel/install.conf
95 elif [ -r "/usr/lib/kernel/install.conf" ]; then
96 . /usr/lib/kernel/install.conf
97 fi
98
99 # Prefer to use an existing machine ID from /etc/machine-info or /etc/machine-id. If we're using the machine
100 # ID /etc/machine-id, try to persist it in /etc/machine-info. If no machine ID is found, try to generate
101 # a new machine ID in /etc/machine-info. If that fails, use "Default".
102
103 [ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ] && source /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
104 [ -z "$MACHINE_ID" ] && [ -f /etc/machine-id ] && read -r MACHINE_ID </etc/machine-id
105 [ -n "$MACHINE_ID" ] && [ -z "$KERNEL_INSTALL_MACHINE_ID" ] && echo "KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID" >>/etc/machine-info
106 [ -z "$MACHINE_ID" ] && NEW_MACHINE_ID="$(systemd-id128 new)" && echo "KERNEL_INSTALL_MACHINE_ID=$NEW_MACHINE_ID" >>/etc/machine-info
107 [ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ] && source /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
108 [ -z "$MACHINE_ID" ] && MACHINE_ID="Default"
109
110 [ -z "$BOOT_ROOT" ] && for suff in "$MACHINE_ID" "loader/entries"; do
111 for pref in "/efi" "/boot" "/boot/efi" ; do
112 if [ -d "$pref/$suff" ]; then
113 BOOT_ROOT="$pref"
114 break 2
115 fi
116 done
117 done
118
119 [ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot/efi"; do
120 if mountpoint -q "$pref"; then
121 BOOT_ROOT="$pref"
122 break
123 fi
124 done
125 [ -z "$BOOT_ROOT" ] && BOOT_ROOT="/boot"
126
127
128 ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
129
130 export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
131 export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
132
133 if [ -z "$layout" ]; then
134 # Administrative decision: if not present, some scripts generate into /boot.
135 if [ -d "$BOOT_ROOT/$MACHINE_ID" ]; then
136 layout="bls"
137 else
138 layout="other"
139 fi
140 fi
141
142
143 ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
144
145 export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
146 export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
147 export KERNEL_INSTALL_LAYOUT="$layout"
148
149 [ "$layout" = "bls" ]
150 MAKE_ENTRY_DIR_ABS=$?
151
152
153 ret=0
154
155 readarray -t PLUGINS <<<"$(
156 dropindirs_sort ".install" \
157 "/etc/kernel/install.d" \
158 "/usr/lib/kernel/install.d"
159 )"
160
161 case $COMMAND in
162 add)
163 if [[ ! "$KERNEL_IMAGE" ]]; then
164 echo "Command 'add' requires an argument" >&2
165 exit 1
166 fi
167
168 if [[ ! -f "$KERNEL_IMAGE" ]]; then
169 echo "Kernel image argument ${KERNEL_IMAGE} not a file" >&2
170 exit 1
171 fi
172
173 if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
174 # Compatibility with earlier versions that used the presence of $BOOT_ROOT/$MACHINE_ID
175 # to signal to 00-entry-directory to create $ENTRY_DIR_ABS
176 # to serve as the indication to use or to not use the BLS
177 if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
178 echo "+mkdir -v -p $ENTRY_DIR_ABS"
179 mkdir -v -p "$ENTRY_DIR_ABS"
180 else
181 mkdir -p "$ENTRY_DIR_ABS"
182 fi
183 fi
184
185 for f in "${PLUGINS[@]}"; do
186 if [[ -x $f ]]; then
187 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
188 echo "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS $KERNEL_IMAGE ${INITRD_OPTIONS[@]}"
189 "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
190 x=$?
191 if [[ $x == $SKIP_REMAINING ]]; then
192 break
193 fi
194 ((ret+=$x))
195 fi
196 done
197 ;;
198
199 remove)
200 for f in "${PLUGINS[@]}"; do
201 if [[ -x $f ]]; then
202 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
203 echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
204 "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
205 x=$?
206 if [[ $x == $SKIP_REMAINING ]]; then
207 break
208 fi
209 ((ret+=$x))
210 fi
211 done
212
213 if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
214 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Removing $ENTRY_DIR_ABS/"
215 rm -rf "$ENTRY_DIR_ABS"
216 fi
217 ;;
218
219 *)
220 echo "Unknown command '$COMMAND'" >&2
221 exit 1
222 ;;
223 esac
224
225 exit $ret