]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
Merge pull request #21805 from наб
[thirdparty/systemd.git] / src / kernel-install / kernel-install
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 <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 suffix="$1"
36 shift
37
38 for d; do
39 for i in "$d/"*"$suffix"; do
40 [ -e "$i" ] && echo "${i##*/}"
41 done
42 done | sort -Vu | while read -r f; do
43 for d; do
44 if [ -e "$d/$f" ]; then
45 [ -x "$d/$f" ] && echo "$d/$f"
46 continue 2
47 fi
48 done
49 done
50 }
51
52 export LC_COLLATE=C
53
54 for i; do
55 if [ "$i" = "--help" ] || [ "$i" = "-h" ]; then
56 usage
57 exit 0
58 fi
59 done
60
61 export KERNEL_INSTALL_VERBOSE=0
62 if [ "$1" = "--verbose" ] || [ "$1" = "-v" ]; then
63 shift
64 KERNEL_INSTALL_VERBOSE=1
65 fi
66
67 if [ "${0##*/}" = "installkernel" ]; then
68 COMMAND=add
69 # make install doesn't pass any initrds
70 else
71 COMMAND="$1"
72 [ $# -ge 1 ] && shift
73 fi
74
75 if [ $# -lt 1 ]; then
76 echo "Not enough arguments" >&2
77 exit 1
78 fi
79
80 KERNEL_VERSION="$1"
81 shift
82
83 if [ -r "/etc/kernel/install.conf" ]; then
84 . /etc/kernel/install.conf
85 elif [ -r "/usr/lib/kernel/install.conf" ]; then
86 . /usr/lib/kernel/install.conf
87 fi
88
89 # Prefer to use an existing machine ID from /etc/machine-info or /etc/machine-id. If we're using the machine
90 # ID /etc/machine-id, try to persist it in /etc/machine-info. If no machine ID is found, try to generate
91 # a new machine ID in /etc/machine-info. If that fails, use "Default".
92 [ -z "$MACHINE_ID" ] && [ -r /etc/machine-info ] && . /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
93 [ -z "$MACHINE_ID" ] && [ -r /etc/machine-id ] && read -r MACHINE_ID </etc/machine-id
94 [ -n "$MACHINE_ID" ] && [ -z "$KERNEL_INSTALL_MACHINE_ID" ] && echo "KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID" >>/etc/machine-info
95 [ -z "$MACHINE_ID" ] && NEW_MACHINE_ID="$(systemd-id128 new)" && echo "KERNEL_INSTALL_MACHINE_ID=$NEW_MACHINE_ID" >>/etc/machine-info
96 [ -z "$MACHINE_ID" ] && [ -r /etc/machine-info ] && . /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
97 [ -z "$MACHINE_ID" ] && MACHINE_ID="Default"
98
99 [ -z "$BOOT_ROOT" ] && for suff in "$MACHINE_ID" "loader/entries"; do
100 for pref in "/efi" "/boot" "/boot/efi" ; do
101 if [ -d "$pref/$suff" ]; then
102 BOOT_ROOT="$pref"
103 break 2
104 fi
105 done
106 done
107
108 [ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot/efi"; do
109 if mountpoint -q "$pref"; then
110 BOOT_ROOT="$pref"
111 break
112 fi
113 done
114 [ -z "$BOOT_ROOT" ] && BOOT_ROOT="/boot"
115
116
117 if [ -z "$layout" ]; then
118 # Administrative decision: if not present, some scripts generate into /boot.
119 if [ -d "$BOOT_ROOT/$MACHINE_ID" ]; then
120 layout="bls"
121 else
122 layout="other"
123 fi
124 fi
125
126
127 ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
128
129 export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
130 export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
131 export KERNEL_INSTALL_LAYOUT="$layout"
132
133 [ "$layout" = "bls" ]
134 MAKE_ENTRY_DIR_ABS=$?
135
136
137 ret=0
138
139 PLUGINS="$(
140 dropindirs_sort ".install" \
141 "/etc/kernel/install.d" \
142 "/usr/lib/kernel/install.d"
143 )"
144 IFS="
145 "
146
147 case "$COMMAND" in
148 add)
149 if [ $# -lt 1 ]; then
150 echo "Command 'add' requires a kernel image" >&2
151 exit 1
152 fi
153
154 if ! [ -f "$1" ]; then
155 echo "Kernel image argument $1 not a file" >&2
156 exit 1
157 fi
158
159 if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
160 # Compatibility with earlier versions that used the presence of $BOOT_ROOT/$MACHINE_ID
161 # to signal to 00-entry-directory to create $ENTRY_DIR_ABS
162 # to serve as the indication to use or to not use the BLS
163 if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
164 echo "+mkdir -v -p $ENTRY_DIR_ABS"
165 mkdir -v -p "$ENTRY_DIR_ABS"
166 else
167 mkdir -p "$ENTRY_DIR_ABS"
168 fi
169 fi
170
171 for f in $PLUGINS; do
172 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS $*"
173 "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$@"
174 err=$?
175 [ $err -eq $skip_remaining ] && break
176 ret=$(( ret + err ))
177 done
178 ;;
179
180 remove)
181 for f in $PLUGINS; do
182 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
183 "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
184 err=$?
185 [ $err -eq $skip_remaining ] && break
186 ret=$(( ret + err ))
187 done
188
189 if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
190 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Removing $ENTRY_DIR_ABS/"
191 rm -rf "$ENTRY_DIR_ABS"
192 fi
193 ;;
194
195 *)
196 echo "Unknown command '$COMMAND'" >&2
197 exit 1
198 ;;
199 esac
200
201 exit "$ret"