Debian Codegraph @

src/kernel-install/90-uki-copy.install

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 # shellcheck disable=SC2034
26 ENTRY_DIR_ABS="$3"
27 KERNEL_IMAGE="$4"
28
29 ENTRY_TOKEN="$KERNEL_INSTALL_ENTRY_TOKEN"
30 BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
31
32 UKI_DIR="$BOOT_ROOT/EFI/Linux"
33
34 case "$COMMAND" in
35 remove)
36 if [ "$KERNEL_INSTALL_BOOT_ENTRY_TYPE" = "type1" ]; then
37 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
38 echo "BOOT_ENTRY_TYPE=type1, not removing UKI related."
39 exit 0
40 fi
41 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
42 echo "Removing $UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION*.efi and extras"
43 exec rm -rf \
44 "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi" \
45 "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi.extra.d/" \
46 "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+"*".efi"
47 ;;
48 add)
49 ;;
50 *)
51 exit 0
52 ;;
53 esac
54
55 [ "$KERNEL_INSTALL_LAYOUT" = "uki" ] || exit 0
56
57 if ! [ -d "$UKI_DIR" ]; then
58 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "creating $UKI_DIR"
59 mkdir -p "$UKI_DIR"
60 fi
61
62 TRIES_FILE="${KERNEL_INSTALL_CONF_ROOT:-/etc/kernel}/tries"
63
64 if [ -f "$TRIES_FILE" ]; then
65 read -r TRIES <"$TRIES_FILE"
66 if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
67 echo "$TRIES_FILE does not contain an integer." >&2
68 exit 1
69 fi
70 if [ -f "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi" ]; then
71 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
72 echo "Removing previous UKI '$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi' without boot counting." >&2
73 rm -f "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi"
74 fi
75 for uki in "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+"*.efi; do
76 [ "$uki" = "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+*.efi" ] && break
77 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
78 echo "Removing previous UKI '$uki' that has not yet booted successfully." >&2
79 rm -f "$uki"
80 done
81
82 UKI_FILE="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+$TRIES.efi"
83 else
84 UKI_FILE="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi"
85 fi
86
87 EXTRA_DIR="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi.extra.d"
88
89 # If there is a UKI named uki.efi on the staging area use that, if not use what
90 # was passed in as $KERNEL_IMAGE but insist it has a .efi extension
91 if [ -f "$KERNEL_INSTALL_STAGING_AREA/uki.efi" ]; then
92 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $KERNEL_INSTALL_STAGING_AREA/uki.efi as $UKI_FILE"
93 install -m 0644 "$KERNEL_INSTALL_STAGING_AREA/uki.efi" "$UKI_FILE" || {
94 echo "Error: could not copy '$KERNEL_INSTALL_STAGING_AREA/uki.efi' to '$UKI_FILE'." >&2
95 exit 1
96 }
97 for f in "$KERNEL_INSTALL_STAGING_AREA/uki.efi.extra.d"/*; do
98 [ -f "$f" ] || continue
99 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $f as $EXTRA_DIR/$(basename "$f")"
100 install -D -m 0644 "$f" "$EXTRA_DIR/$(basename "$f")" || {
101 echo "Error: could not copy '$f' to '$EXTRA_DIR/$(basename "$f")'." >&2
102 exit 1
103 }
104 done
105 elif [ -n "$KERNEL_IMAGE" ]; then
106 [ -f "$KERNEL_IMAGE" ] || {
107 echo "Error: UKI '$KERNEL_IMAGE' not a file." >&2
108 exit 1
109 }
110 [ "$KERNEL_IMAGE" != "${KERNEL_IMAGE%*.efi}.efi" ] && {
111 echo "Error: $KERNEL_IMAGE is missing .efi suffix." >&2
112 exit 1
113 }
114 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $KERNEL_IMAGE as $UKI_FILE"
115 install -m 0644 "$KERNEL_IMAGE" "$UKI_FILE" || {
116 echo "Error: could not copy '$KERNEL_IMAGE' to '$UKI_FILE'." >&2
117 exit 1
118 }
119 for f in "$KERNEL_IMAGE.extra.d"/* "$KERNEL_INSTALL_STAGING_AREA/uki.efi.extra.d"/*; do
120 [ -f "$f" ] || continue
121 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Installing $f as $EXTRA_DIR/$(basename "$f")"
122 install -D -m 0644 "$f" "$EXTRA_DIR/$(basename "$f")" || {
123 echo "Error: could not copy '$f' to '$EXTRA_DIR/$(basename "$f")'." >&2
124 exit 1
125 }
126 done
127 else
128 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "No UKI available. Nothing to do."
129 exit 0
130 fi
131 chown root:root "$UKI_FILE" || :
132
133 exit 0