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