]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
kernel-install: add --verbose
[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 # 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 add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
27 echo " $0 remove KERNEL-VERSION"
28 }
29
30 dropindirs_sort()
31 {
32 local suffix=$1; shift
33 local -a files
34 local f d i
35
36 readarray -t files <<<"$(
37 for d in "$@"; do
38 for i in "$d/"*"$suffix"; do
39 if [[ -e "$i" ]]; then
40 echo "${i##*/}"
41 fi
42 done
43 done | sort -Vu
44 )"
45
46 for f in "${files[@]}"; do
47 for d in "$@"; do
48 if [[ -e "$d/$f" ]]; then
49 echo "$d/$f"
50 continue 2
51 fi
52 done
53 done
54 }
55
56 export LC_COLLATE=C
57
58 for i in "$@"; do
59 if [ "$i" == "--help" -o "$i" == "-h" ]; then
60 usage
61 exit 0
62 fi
63 done
64
65 KERNEL_INSTALL_VERBOSE=0
66 if [ "$1" == "--verbose" -o "$1" == "-v" ]; then
67 shift
68 KERNEL_INSTALL_VERBOSE=1
69 fi
70 export KERNEL_INSTALL_VERBOSE
71
72 if [[ "${0##*/}" == 'installkernel' ]]; then
73 COMMAND='add'
74 # make install doesn't pass any parameter wrt initrd handling
75 INITRD_OPTIONS=()
76 else
77 COMMAND="$1"
78 shift
79 INITRD_OPTIONS=( "${@:3}" )
80 fi
81
82 KERNEL_VERSION="$1"
83 KERNEL_IMAGE="$2"
84
85 if [[ -f /etc/machine-id ]]; then
86 read MACHINE_ID < /etc/machine-id
87 fi
88
89 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
90 echo "Not enough arguments" >&2
91 exit 1
92 fi
93
94 if ! [[ $MACHINE_ID ]]; then
95 BOOT_DIR_ABS=$(mktemp -d /tmp/kernel-install.XXXXX) || exit 1
96 trap "rm -rf '$BOOT_DIR_ABS'" EXIT INT QUIT PIPE
97 elif [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
98 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
99 elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
100 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
101 elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
102 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
103 elif mountpoint -q /efi; then
104 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
105 elif mountpoint -q /boot/efi; then
106 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
107 else
108 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
109 fi
110
111 export KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID
112
113 ret=0
114
115 readarray -t PLUGINS <<<"$(
116 dropindirs_sort ".install" \
117 "/etc/kernel/install.d" \
118 "/usr/lib/kernel/install.d"
119 )"
120
121 case $COMMAND in
122 add)
123 if [[ ! "$KERNEL_IMAGE" ]]; then
124 echo "Command 'add' requires an argument" >&2
125 exit 1
126 fi
127
128 mkdir -p "$BOOT_DIR_ABS" || {
129 echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
130 exit 1
131 }
132
133 for f in "${PLUGINS[@]}"; do
134 if [[ -x $f ]]; then
135 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
136 echo "+$f add $KERNEL_VERSION $BOOT_DIR_ABS $KERNEL_IMAGE ${INITRD_OPTIONS[@]}"
137 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
138 x=$?
139 if [[ $x == $SKIP_REMAINING ]]; then
140 ret=0
141 break
142 fi
143 ((ret+=$x))
144 fi
145 done
146
147 if ! [[ $MACHINE_ID ]] && ! rmdir "$BOOT_DIR_ABS"; then
148 echo "Warning: In kernel-install plugins, requiring BOOT_DIR_ABS to be preset is deprecated." >&2
149 echo " All plugins should not put anything in BOOT_DIR_ABS if the environment" >&2
150 echo " variable KERNEL_INSTALL_MACHINE_ID is empty." >&2
151 rm -rf "$BOOT_DIR_ABS"
152 ((ret+=$?))
153 fi
154 ;;
155
156 remove)
157 for f in "${PLUGINS[@]}"; do
158 if [[ -x $f ]]; then
159 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
160 echo "+$f remove $KERNEL_VERSION $BOOT_DIR_ABS"
161 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
162 x=$?
163 if [[ $x == $SKIP_REMAINING ]]; then
164 ret=0
165 break
166 fi
167 ((ret+=$x))
168 fi
169 done
170
171 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
172 echo "Removing $BOOT_DIR_ABS"
173
174 rm -rf "$BOOT_DIR_ABS"
175 ((ret+=$?))
176 ;;
177
178 *)
179 echo "Unknown command '$COMMAND'" >&2
180 exit 1
181 ;;
182 esac
183
184 exit $ret