Debian Codegraph @

src/kernel-install/90-loaderentry.install.in

raw
1 #!/bin/sh
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 <https://www.gnu.org/licenses/>.
20
21 set -e
22
23 COMMAND="${1:?}"
24 KERNEL_VERSION="${2:?}"
25 ENTRY_DIR_ABS="${3:?}"
26 KERNEL_IMAGE="$4"
27 INITRD_OPTIONS_SHIFT=4
28
29 if [ "$KERNEL_INSTALL_LAYOUT" != "bls" ]; then
30 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
31 echo "KERNEL_INSTALL_LAYOUT=$KERNEL_INSTALL_LAYOUT, quitting."
32 exit 0
33 fi
34
35 MACHINE_ID="${KERNEL_INSTALL_MACHINE_ID:?}"
36 ENTRY_TOKEN="${KERNEL_INSTALL_ENTRY_TOKEN:?}"
37 BOOT_ROOT="${KERNEL_INSTALL_BOOT_ROOT:?}"
38
39 [ -n "$BOOT_MNT" ] || BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
40 if [ "$BOOT_MNT" = '/' ]; then
41 ENTRY_DIR="$ENTRY_DIR_ABS"
42 else
43 ENTRY_DIR="${ENTRY_DIR_ABS#"$BOOT_MNT"}"
44 fi
45
46 KERNEL_DEST="$ENTRY_DIR_ABS/linux"
47 KERNEL_ENTRY="$ENTRY_DIR/linux"
48 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION.conf"
49
50 case "$COMMAND" in
51 remove)
52 if [ -f "/lib/modules/$KERNEL_VERSION/vmlinuz" ] && [ "$KERNEL_INSTALL_BOOT_ENTRY_TYPE" = "type2" ]; then
53 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
54 echo "BOOT_ENTRY_TYPE=type2, not removing loader entries."
55 exit 0
56 fi
57 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
58 echo "Removing ${LOADER_ENTRY%.conf}*.conf"
59 exec rm -f \
60 "$LOADER_ENTRY" \
61 "${LOADER_ENTRY%.conf}"*".conf"
62 ;;
63 add)
64 ;;
65 *)
66 exit 0
67 ;;
68 esac
69
70 if [ -f /etc/os-release ]; then
71 # shellcheck source=/dev/null
72 . /etc/os-release
73 elif [ -f /usr/lib/os-release ]; then
74 # shellcheck source=/dev/null
75 . /usr/lib/os-release
76 fi
77
78 [ -n "$PRETTY_NAME" ] || PRETTY_NAME="Linux $KERNEL_VERSION"
79
80 SORT_KEY="$IMAGE_ID"
81 [ -z "$SORT_KEY" ] && SORT_KEY="$ID"
82
83 if [ -n "$KERNEL_INSTALL_CONF_ROOT" ]; then
84 if [ -f "$KERNEL_INSTALL_CONF_ROOT/cmdline" ]; then
85 BOOT_OPTIONS="$(grep -Gv '^\s*#' "$KERNEL_INSTALL_CONF_ROOT/cmdline" | tr -s "$IFS" ' ')"
86 fi
87 elif [ -f /etc/kernel/cmdline ]; then
88 BOOT_OPTIONS="$(grep -Gv '^\s*#' /etc/kernel/cmdline | tr -s "$IFS" ' ')"
89 elif [ -f /usr/lib/kernel/cmdline ]; then
90 BOOT_OPTIONS="$(grep -Gv '^\s*#' /usr/lib/kernel/cmdline | tr -s "$IFS" ' ')"
91 elif ! systemd-detect-virt --container --quiet; then
92 BOOT_OPTIONS="$(tr -s "$IFS" '\n' </proc/cmdline | grep -ve '^BOOT_IMAGE=' -e '^initrd=' | tr '\n' ' ')"
93 else
94 BOOT_OPTIONS=""
95 fi
96
97 BOOT_OPTIONS="${BOOT_OPTIONS% }"
98
99 # If the boot entries are named after the machine ID, then suffix the kernel
100 # command line with the machine ID we use, so that the machine ID remains
101 # stable, even during factory reset, in the initrd (where the system's machine
102 # ID is not directly accessible yet), and if the root file system is volatile.
103 if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ] && ! echo "$BOOT_OPTIONS" | grep -q "systemd.machine_id="; then
104 BOOT_OPTIONS="$BOOT_OPTIONS systemd.machine_id=$MACHINE_ID"
105 fi
106
107 TRIES_FILE="${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries"
108
109 if [ -f "$TRIES_FILE" ]; then
110 read -r TRIES <"$TRIES_FILE"
111 if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
112 echo "$TRIES_FILE does not contain an integer." >&2
113 exit 1
114 fi
115 if [ -f "$LOADER_ENTRY" ]; then
116 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
117 echo "Removing previous loader entry '$LOADER_ENTRY' without boot counting." >&2
118 rm -f "$LOADER_ENTRY"
119 fi
120 for loaderentry in "${LOADER_ENTRY%.conf}+"*.conf; do
121 [ "$loaderentry" = "${LOADER_ENTRY%.conf}+*.conf" ] && break
122 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
123 echo "Removing previous loader entry '$loaderentry' that has not yet booted successfully." >&2
124 rm -f "$loaderentry"
125 done
126 LOADER_ENTRY="${LOADER_ENTRY%.conf}+$TRIES.conf"
127 fi
128
129 if ! [ -d "$ENTRY_DIR_ABS" ]; then
130 echo "Error: entry directory '$ENTRY_DIR_ABS' does not exist" >&2
131 exit 1
132 fi
133
134 install -m 0644 "$KERNEL_IMAGE" "$KERNEL_DEST" || {
135 echo "Error: could not copy '$KERNEL_IMAGE' to '$KERNEL_DEST'." >&2
136 exit 1
137 }
138 chown root:root "$KERNEL_DEST" || :
139
140 if [ -n "$KERNEL_INSTALL_CONF_ROOT" ]; then
141 if [ -f "$KERNEL_INSTALL_CONF_ROOT/devicetree" ]; then
142 read -r DEVICETREE <"$KERNEL_INSTALL_CONF_ROOT/devicetree"
143 fi
144 elif [ -f /etc/kernel/devicetree ]; then
145 read -r DEVICETREE </etc/kernel/devicetree
146 elif [ -f /usr/lib/kernel/devicetree ]; then
147 read -r DEVICETREE </usr/lib/kernel/devicetree
148 fi
149 if [ -n "$DEVICETREE" ]; then
150 for prefix in \
151 "$KERNEL_INSTALL_CONF_ROOT" \
152 "/usr/lib/firmware/$KERNEL_VERSION/device-tree" \
153 "/usr/lib/linux-image-$KERNEL_VERSION" \
154 "/usr/lib/modules/$KERNEL_VERSION/dtb"
155 do
156 [ -n "$prefix" ] || continue
157 [ -f "$prefix/$DEVICETREE" ] || continue
158 DEVICETREE_SRC="$prefix/$DEVICETREE"
159 break
160 done
161
162 [ -n "$DEVICETREE_SRC" ] || {
163 echo "Error: could not find device tree blob '$DEVICETREE'." >&2
164 exit 1
165 }
166
167 DEVICETREE_DEST="$ENTRY_DIR_ABS/${DEVICETREE##*/}"
168 DEVICETREE_ENTRY="$ENTRY_DIR/${DEVICETREE##*/}"
169
170 install -m 0644 "$DEVICETREE_SRC" "$DEVICETREE_DEST" || {
171 echo "Error: could not copy '$DEVICETREE_SRC' to '$DEVICETREE_DEST'." >&2
172 exit 1
173 }
174 chown root:root "$DEVICETREE_DEST" || :
175 fi
176
177 shift "$INITRD_OPTIONS_SHIFT"
178 # All files listed as arguments, and staged files starting with "initrd" are installed as initrds.
179 for initrd in "${KERNEL_INSTALL_STAGING_AREA}"/microcode* "${@}" "${KERNEL_INSTALL_STAGING_AREA}"/initrd*; do
180 [ -f "$initrd" ] || {
181 case "$initrd" in
182 "${KERNEL_INSTALL_STAGING_AREA}/initrd*" | "${KERNEL_INSTALL_STAGING_AREA}/microcode*")
183 continue ;;
184 esac
185 echo "Error: '$initrd' is not a file." >&2
186 exit 1
187 }
188
189 initrd_basename="${initrd##*/}"
190 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $ENTRY_DIR_ABS/$initrd_basename"
191 install -m 0644 "$initrd" "$ENTRY_DIR_ABS/$initrd_basename" || {
192 echo "Error: could not copy '$initrd' to '$ENTRY_DIR_ABS/$initrd_basename'." >&2
193 exit 1
194 }
195 chown root:root "$ENTRY_DIR_ABS/$initrd_basename" || :
196 done
197
198 mkdir -p "${LOADER_ENTRY%/*}" || {
199 echo "Error: could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
200 exit 1
201 }
202
203 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Creating $LOADER_ENTRY"
204 {
205 echo "# Boot Loader Specification type#1 entry"
206 echo "# File created by $0 (systemd {{VERSION_TAG}})"
207 echo "title $PRETTY_NAME"
208 echo "version $KERNEL_VERSION"
209 if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ]; then
210 # See similar logic above for the systemd.machine_id= kernel command line option
211 echo "machine-id $MACHINE_ID"
212 fi
213 [ -n "$SORT_KEY" ] && echo "sort-key $SORT_KEY"
214 [ -n "$BOOT_OPTIONS" ] && echo "options $BOOT_OPTIONS"
215 echo "linux $KERNEL_ENTRY"
216 [ -n "$DEVICETREE_ENTRY" ] && echo "devicetree $DEVICETREE_ENTRY"
217
218 have_initrd=
219 for initrd in "${KERNEL_INSTALL_STAGING_AREA}"/microcode* "${@}" "${KERNEL_INSTALL_STAGING_AREA}"/initrd*; do
220 [ -f "$initrd" ] || continue
221 echo "initrd $ENTRY_DIR/${initrd##*/}"
222 have_initrd=yes
223 done
224
225 # Try a few variations that are generated by various initrd generators in their kernel-install hooks if
226 # no initrds were supplied.
227
228 if [ -z "$have_initrd" ] && [ -f "$ENTRY_DIR_ABS/initrd" ]; then
229 echo "initrd $ENTRY_DIR/initrd"
230 have_initrd=yes
231 fi
232
233 if [ -z "$have_initrd" ] && [ -f "$BOOT_ROOT/initramfs-$KERNEL_VERSION.img" ]; then
234 echo "initrd /initramfs-$KERNEL_VERSION.img"
235 have_initrd=yes
236 fi
237 :
238 } >"$LOADER_ENTRY" || {
239 echo "Error: could not create loader entry '$LOADER_ENTRY'." >&2
240 exit 1
241 }
242 exit 0