]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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"
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 else
69 COMMAND="$1"
70 shift
71 fi
72
73 KERNEL_VERSION="$1"
74 KERNEL_IMAGE="$2"
75
76 if [[ -f /etc/machine-id ]]; then
77 read MACHINE_ID < /etc/machine-id
78 fi
79
80 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
81 echo "Not enough arguments" >&2
82 exit 1
83 fi
84
85 if ! [[ $MACHINE_ID ]]; then
86 BOOT_DIR_ABS=$(mktemp -d /tmp/kernel-install.XXXXX) || exit 1
87 trap "rm -rf '$BOOT_DIR_ABS'" EXIT INT QUIT PIPE
88 elif [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
89 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
90 elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
91 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
92 elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
93 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
94 elif mountpoint -q /efi; then
95 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
96 elif mountpoint -q /boot/efi; then
97 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
98 else
99 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
100 fi
101
102 export KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID
103
104 ret=0
105
106 readarray -t PLUGINS <<<"$(
107 dropindirs_sort ".install" \
108 "/etc/kernel/install.d" \
109 "/usr/lib/kernel/install.d"
110 )"
111
112 case $COMMAND in
113 add)
114 if [[ ! "$KERNEL_IMAGE" ]]; then
115 echo "Command 'add' requires an argument" >&2
116 exit 1
117 fi
118
119 mkdir -p "$BOOT_DIR_ABS" || {
120 echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
121 exit 1
122 }
123
124 for f in "${PLUGINS[@]}"; do
125 if [[ -x $f ]]; then
126 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE"
127 x=$?
128 if [[ $x == $SKIP_REMAINING ]]; then
129 ret=0
130 break
131 fi
132 ((ret+=$x))
133 fi
134 done
135
136 if ! [[ $MACHINE_ID ]] && ! rmdir "$BOOT_DIR_ABS"; then
137 echo "Warning: In kernel-install plugins, requiring BOOT_DIR_ABS to be preset is deprecated." >&2
138 echo " All plugins should not put anything in BOOT_DIR_ABS if the environment" >&2
139 echo " variable KERNEL_INSTALL_MACHINE_ID is empty." >&2
140 rm -rf "$BOOT_DIR_ABS"
141 ((ret+=$?))
142 fi
143 ;;
144
145 remove)
146 for f in "${PLUGINS[@]}"; do
147 if [[ -x $f ]]; then
148 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
149 x=$?
150 if [[ $x == $SKIP_REMAINING ]]; then
151 ret=0
152 break
153 fi
154 ((ret+=$x))
155 fi
156 done
157
158 rm -rf "$BOOT_DIR_ABS"
159 ((ret+=$?))
160 ;;
161
162 *)
163 echo "Unknown command '$COMMAND'" >&2
164 exit 1
165 ;;
166 esac
167
168 exit $ret