]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
core: reduce scope of variants
[thirdparty/systemd.git] / src / kernel-install / kernel-install
1 #!/usr/bin/env 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 # 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 local suffix=$1; shift
36 local -a files
37 local f d i
38
39 readarray -t files <<<"$(
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
47 )"
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
59 export LC_COLLATE=C
60
61 for i in "$@"; do
62 if [ "$i" == "--help" -o "$i" == "-h" ]; then
63 usage
64 exit 0
65 fi
66 done
67
68 KERNEL_INSTALL_VERBOSE=0
69 if [ "$1" == "--verbose" -o "$1" == "-v" ]; then
70 shift
71 KERNEL_INSTALL_VERBOSE=1
72 fi
73 export KERNEL_INSTALL_VERBOSE
74
75 if [[ "${0##*/}" == 'installkernel' ]]; then
76 COMMAND='add'
77 # make install doesn't pass any parameter wrt initrd handling
78 INITRD_OPTIONS=()
79 else
80 COMMAND="$1"
81 shift
82 INITRD_OPTIONS=( "${@:3}" )
83 fi
84
85 KERNEL_VERSION="$1"
86 KERNEL_IMAGE="$2"
87
88 # Reuse directory created without a machine ID present if it exists.
89 if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
90 MACHINE_ID="Default"
91 elif [[ -f /etc/machine-id ]]; then
92 read MACHINE_ID < /etc/machine-id
93 else
94 MACHINE_ID="Default"
95 fi
96
97 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
98 echo "Not enough arguments" >&2
99 exit 1
100 fi
101
102 if [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
103 ENTRY_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
104 elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
105 ENTRY_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
106 elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
107 ENTRY_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
108 elif mountpoint -q /efi; then
109 ENTRY_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
110 elif mountpoint -q /boot/efi; then
111 ENTRY_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
112 else
113 ENTRY_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
114 fi
115
116 export KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID
117
118 ret=0
119
120 readarray -t PLUGINS <<<"$(
121 dropindirs_sort ".install" \
122 "/etc/kernel/install.d" \
123 "/usr/lib/kernel/install.d"
124 )"
125
126 case $COMMAND in
127 add)
128 if [[ ! "$KERNEL_IMAGE" ]]; then
129 echo "Command 'add' requires an argument" >&2
130 exit 1
131 fi
132
133 if [[ ! -f "$KERNEL_IMAGE" ]]; then
134 echo "Kernel image argument ${KERNEL_IMAGE} not a file" >&2
135 exit 1
136 fi
137
138 for f in "${PLUGINS[@]}"; do
139 if [[ -x $f ]]; then
140 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
141 echo "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS $KERNEL_IMAGE ${INITRD_OPTIONS[@]}"
142 "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
143 x=$?
144 if [[ $x == $SKIP_REMAINING ]]; then
145 ret=0
146 break
147 fi
148 ((ret+=$x))
149 fi
150 done
151 ;;
152
153 remove)
154 for f in "${PLUGINS[@]}"; do
155 if [[ -x $f ]]; then
156 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
157 echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
158 "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
159 x=$?
160 if [[ $x == $SKIP_REMAINING ]]; then
161 ret=0
162 break
163 fi
164 ((ret+=$x))
165 fi
166 done
167
168 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
169 echo "Removing $ENTRY_DIR_ABS"
170
171 rm -rf "$ENTRY_DIR_ABS"
172 ((ret+=$?))
173 ;;
174
175 *)
176 echo "Unknown command '$COMMAND'" >&2
177 exit 1
178 ;;
179 esac
180
181 exit $ret