]> git.proxmox.com Git - systemd.git/blame - src/kernel-install/kernel-install
New upstream version 250.2
[systemd.git] / src / kernel-install / kernel-install
CommitLineData
46cdbd49 1#!/usr/bin/env bash
663996b3
MS
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
a032b68d 4# SPDX-License-Identifier: LGPL-2.1-or-later
663996b3
MS
5#
6# This file is part of systemd.
7#
663996b3
MS
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
8a584da2
MP
21SKIP_REMAINING=77
22
663996b3
MS
23usage()
24{
60f067b4 25 echo "Usage:"
478ed938
MB
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"
663996b3
MS
31}
32
33dropindirs_sort()
34{
35 local suffix=$1; shift
36 local -a files
37 local f d i
38
2897b343 39 readarray -t files <<<"$(
663996b3
MS
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
2897b343 47 )"
663996b3
MS
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
59export LC_COLLATE=C
60
60f067b4
JS
61for i in "$@"; do
62 if [ "$i" == "--help" -o "$i" == "-h" ]; then
63 usage
64 exit 0
65 fi
66done
67
bb4f798a
MB
68KERNEL_INSTALL_VERBOSE=0
69if [ "$1" == "--verbose" -o "$1" == "-v" ]; then
70 shift
71 KERNEL_INSTALL_VERBOSE=1
72fi
73export KERNEL_INSTALL_VERBOSE
74
14228c0d
MB
75if [[ "${0##*/}" == 'installkernel' ]]; then
76 COMMAND='add'
7c20daf6
FS
77 # make install doesn't pass any parameter wrt initrd handling
78 INITRD_OPTIONS=()
14228c0d
MB
79else
80 COMMAND="$1"
81 shift
7c20daf6 82 INITRD_OPTIONS=( "${@:3}" )
14228c0d
MB
83fi
84
85KERNEL_VERSION="$1"
86KERNEL_IMAGE="$2"
663996b3 87
663996b3 88if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
60f067b4 89 echo "Not enough arguments" >&2
663996b3
MS
90 exit 1
91fi
92
ea0999c9
MB
93if [ -r "/etc/kernel/install.conf" ]; then
94 . /etc/kernel/install.conf
95elif [ -r "/usr/lib/kernel/install.conf" ]; then
96 . /usr/lib/kernel/install.conf
97fi
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
36ceca03 111 for pref in "/efi" "/boot" "/boot/efi" ; do
ea0999c9
MB
112 if [ -d "$pref/$suff" ]; then
113 BOOT_ROOT="$pref"
114 break 2
115 fi
116 done
117done
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
124done
125[ -z "$BOOT_ROOT" ] && BOOT_ROOT="/boot"
126
127
128ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
129
130export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
131export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
132
133if [ -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
5a920b42
MP
140fi
141
ea0999c9
MB
142
143ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
144
145export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
146export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
147export KERNEL_INSTALL_LAYOUT="$layout"
148
149[ "$layout" = "bls" ]
150MAKE_ENTRY_DIR_ABS=$?
151
81c58355 152
663996b3
MS
153ret=0
154
2897b343 155readarray -t PLUGINS <<<"$(
663996b3
MS
156 dropindirs_sort ".install" \
157 "/etc/kernel/install.d" \
158 "/usr/lib/kernel/install.d"
2897b343 159)"
663996b3
MS
160
161case $COMMAND in
162 add)
60f067b4
JS
163 if [[ ! "$KERNEL_IMAGE" ]]; then
164 echo "Command 'add' requires an argument" >&2
663996b3
MS
165 exit 1
166 fi
167
bb4f798a
MB
168 if [[ ! -f "$KERNEL_IMAGE" ]]; then
169 echo "Kernel image argument ${KERNEL_IMAGE} not a file" >&2
663996b3 170 exit 1
bb4f798a 171 fi
663996b3 172
ea0999c9
MB
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
663996b3
MS
185 for f in "${PLUGINS[@]}"; do
186 if [[ -x $f ]]; then
bb4f798a
MB
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[@]}"
8a584da2
MP
190 x=$?
191 if [[ $x == $SKIP_REMAINING ]]; then
81c58355 192 break
8a584da2
MP
193 fi
194 ((ret+=$x))
663996b3
MS
195 fi
196 done
197 ;;
198
199 remove)
200 for f in "${PLUGINS[@]}"; do
201 if [[ -x $f ]]; then
bb4f798a
MB
202 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
203 echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
204 "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
8a584da2
MP
205 x=$?
206 if [[ $x == $SKIP_REMAINING ]]; then
81c58355 207 break
8a584da2
MP
208 fi
209 ((ret+=$x))
663996b3
MS
210 fi
211 done
212
ea0999c9
MB
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
663996b3
MS
217 ;;
218
219 *)
60f067b4 220 echo "Unknown command '$COMMAND'" >&2
663996b3
MS
221 exit 1
222 ;;
223esac
224
225exit $ret