]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
Merge pull request #11357 from GiacintoCifelli/dbus_labels
[thirdparty/systemd.git] / src / kernel-install / kernel-install
1 #!/bin/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+
5 #
6 # This file is part of systemd.
7 #
8 #
9 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
22 SKIP_REMAINING=77
23
24 usage()
25 {
26 echo "Usage:"
27 echo " $0 add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
28 echo " $0 remove KERNEL-VERSION"
29 }
30
31 dropindirs_sort()
32 {
33 local suffix=$1; shift
34 local -a files
35 local f d i
36
37 readarray -t files <<<"$(
38 for d in "$@"; do
39 for i in "$d/"*"$suffix"; do
40 if [[ -e "$i" ]]; then
41 echo "${i##*/}"
42 fi
43 done
44 done | sort -Vu
45 )"
46
47 for f in "${files[@]}"; do
48 for d in "$@"; do
49 if [[ -e "$d/$f" ]]; then
50 echo "$d/$f"
51 continue 2
52 fi
53 done
54 done
55 }
56
57 export LC_COLLATE=C
58
59 for i in "$@"; do
60 if [ "$i" == "--help" -o "$i" == "-h" ]; then
61 usage
62 exit 0
63 fi
64 done
65
66 if [[ "${0##*/}" == 'installkernel' ]]; then
67 COMMAND='add'
68 # make install doesn't pass any parameter wrt initrd handling
69 INITRD_OPTIONS=()
70 else
71 COMMAND="$1"
72 shift
73 INITRD_OPTIONS=( "${@:3}" )
74 fi
75
76 KERNEL_VERSION="$1"
77 KERNEL_IMAGE="$2"
78
79 if [[ -f /etc/machine-id ]]; then
80 read MACHINE_ID < /etc/machine-id
81 fi
82
83 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
84 echo "Not enough arguments" >&2
85 exit 1
86 fi
87
88 if ! [[ $MACHINE_ID ]]; then
89 BOOT_DIR_ABS=$(mktemp -d /tmp/kernel-install.XXXXX) || exit 1
90 trap "rm -rf '$BOOT_DIR_ABS'" EXIT INT QUIT PIPE
91 elif [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
92 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
93 elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
94 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
95 elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
96 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
97 elif mountpoint -q /efi; then
98 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
99 elif mountpoint -q /boot/efi; then
100 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
101 else
102 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
103 fi
104
105 export KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID
106
107 ret=0
108
109 readarray -t PLUGINS <<<"$(
110 dropindirs_sort ".install" \
111 "/etc/kernel/install.d" \
112 "/usr/lib/kernel/install.d"
113 )"
114
115 case $COMMAND in
116 add)
117 if [[ ! "$KERNEL_IMAGE" ]]; then
118 echo "Command 'add' requires an argument" >&2
119 exit 1
120 fi
121
122 mkdir -p "$BOOT_DIR_ABS" || {
123 echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
124 exit 1
125 }
126
127 for f in "${PLUGINS[@]}"; do
128 if [[ -x $f ]]; then
129 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
130 x=$?
131 if [[ $x == $SKIP_REMAINING ]]; then
132 ret=0
133 break
134 fi
135 ((ret+=$x))
136 fi
137 done
138
139 if ! [[ $MACHINE_ID ]] && ! rmdir "$BOOT_DIR_ABS"; then
140 echo "Warning: In kernel-install plugins, requiring BOOT_DIR_ABS to be preset is deprecated." >&2
141 echo " All plugins should not put anything in BOOT_DIR_ABS if the environment" >&2
142 echo " variable KERNEL_INSTALL_MACHINE_ID is empty." >&2
143 rm -rf "$BOOT_DIR_ABS"
144 ((ret+=$?))
145 fi
146 ;;
147
148 remove)
149 for f in "${PLUGINS[@]}"; do
150 if [[ -x $f ]]; then
151 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
152 x=$?
153 if [[ $x == $SKIP_REMAINING ]]; then
154 ret=0
155 break
156 fi
157 ((ret+=$x))
158 fi
159 done
160
161 rm -rf "$BOOT_DIR_ABS"
162 ((ret+=$?))
163 ;;
164
165 *)
166 echo "Unknown command '$COMMAND'" >&2
167 exit 1
168 ;;
169 esac
170
171 exit $ret