| 1 |
#!/usr/bin/env bash |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
# shellcheck disable=SC2235 |
| 4 |
set -eux |
| 5 |
set -o pipefail |
| 6 |
|
| 7 |
export SYSTEMD_LOG_LEVEL=debug |
| 8 |
|
| 9 |
kernel_install="${1:?}" |
| 10 |
loaderentry_install="${2:?}" |
| 11 |
uki_copy_install="${3:?}" |
| 12 |
ukify="${4:-}" |
| 13 |
ukify_install="${5:-}" |
| 14 |
boot_stub="${6:-}" |
| 15 |
if [[ -d "${PROJECT_BUILD_ROOT:-}" ]]; then |
| 16 |
bootctl="${PROJECT_BUILD_ROOT}/bootctl" |
| 17 |
else |
| 18 |
bootctl= |
| 19 |
fi |
| 20 |
|
| 21 |
D="$(mktemp --tmpdir --directory "test-kernel-install.XXXXXXXXXX")" |
| 22 |
|
| 23 |
# shellcheck disable=SC2064 |
| 24 |
trap "rm -rf '$D'" EXIT INT QUIT PIPE |
| 25 |
mkdir -p "$D/boot" |
| 26 |
mkdir -p "$D/efi" |
| 27 |
mkdir -p "$D/sources" |
| 28 |
|
| 29 |
echo 'buzy image' >"$D/sources/linux" |
| 30 |
echo 'the initrd' >"$D/sources/initrd" |
| 31 |
echo 'the-token' >"$D/sources/entry-token" |
| 32 |
|
| 33 |
cat >"$D/sources/cmdline" <<EOF |
| 34 |
opt1 |
| 35 |
# commented line |
| 36 |
opt2 |
| 37 |
EOF |
| 38 |
|
| 39 |
cat >"$D/sources/install.conf" <<EOF |
| 40 |
layout=bls |
| 41 |
initrd_generator=none |
| 42 |
# those are overridden by envvars |
| 43 |
BOOT_ROOT="$D/badboot" |
| 44 |
MACHINE_ID=badbadbadbadbadbad6abadbadbadbad |
| 45 |
EOF |
| 46 |
|
| 47 |
# Create a 'devicetree' config file that points to a fake dtb file |
| 48 |
echo 'subdir/whatever.dtb' >"$D/sources/devicetree" |
| 49 |
mkdir "$D/sources/subdir" |
| 50 |
echo 'DTBDTBDTBDTB' >"$D/sources/subdir/whatever.dtb" |
| 51 |
|
| 52 |
export KERNEL_INSTALL_CONF_ROOT="$D/sources" |
| 53 |
# We "install" multiple plugins, but control which ones will be active via install.conf. |
| 54 |
KERNEL_INSTALL_PLUGINS="'${loaderentry_install}' '${uki_copy_install}'" |
| 55 |
if [[ -n "$ukify_install" ]]; then |
| 56 |
# shellcheck disable=SC2089 |
| 57 |
KERNEL_INSTALL_PLUGINS="'${ukify_install}' $KERNEL_INSTALL_PLUGINS" |
| 58 |
fi |
| 59 |
# shellcheck disable=SC2090 |
| 60 |
export KERNEL_INSTALL_PLUGINS |
| 61 |
export BOOT_ROOT="$D/boot" |
| 62 |
export BOOT_MNT="$D/boot" |
| 63 |
export MACHINE_ID='3e0484f3634a418b8e6a39e8828b03e3' |
| 64 |
export KERNEL_INSTALL_UKIFY="$ukify" |
| 65 |
export KERNEL_INSTALL_BOOT_STUB="$boot_stub" |
| 66 |
export KERNEL_INSTALL_READ_MACHINE_INFO="no" |
| 67 |
export KERNEL_INSTALL_BYPASS="no" |
| 68 |
|
| 69 |
# Test type#1 installation |
| 70 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 71 |
|
| 72 |
entry="$BOOT_ROOT/loader/entries/the-token-1.1.1.conf" |
| 73 |
test -f "$entry" |
| 74 |
grep -qE '^title ' "$entry" |
| 75 |
grep -qE '^version +1.1.1' "$entry" |
| 76 |
grep -qE '^options +opt1 opt2' "$entry" |
| 77 |
grep -qE '^linux .*/the-token/1.1.1/linux' "$entry" |
| 78 |
grep -qE '^initrd .*/the-token/1.1.1/initrd' "$entry" |
| 79 |
grep -qE '^devicetree .*/the-token/1.1.1/whatever.dtb' "$entry" |
| 80 |
|
| 81 |
grep -qE 'image' "$BOOT_ROOT/the-token/1.1.1/linux" |
| 82 |
grep -qE 'initrd' "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 83 |
grep -qE 'DTBDTB' "$BOOT_ROOT/the-token/1.1.1/whatever.dtb" |
| 84 |
|
| 85 |
test -f /usr/lib/modules/"$(uname -r)"/vmlinuz && "$kernel_install" inspect |
| 86 |
"$kernel_install" inspect "$D/sources/linux" |
| 87 |
|
| 88 |
"$kernel_install" -v remove 1.1.1 |
| 89 |
test ! -e "$entry" |
| 90 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 91 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 92 |
|
| 93 |
# Test again with too many arguments for 'remove' command. See #28448. |
| 94 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 95 |
|
| 96 |
test -f "$entry" |
| 97 |
test -f "$BOOT_ROOT/the-token/1.1.1/linux" |
| 98 |
test -f "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 99 |
|
| 100 |
"$kernel_install" -v remove 1.1.1 hoge foo bar |
| 101 |
test ! -e "$entry" |
| 102 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 103 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 104 |
|
| 105 |
# Test --entry-type options |
| 106 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 107 |
"$kernel_install" -v remove 1.1.1 --entry-type=type1 |
| 108 |
test ! -e "$entry" |
| 109 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 110 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 111 |
|
| 112 |
# Invoke kernel-install as installkernel |
| 113 |
ln -s --relative -v "$kernel_install" "$D/sources/installkernel" |
| 114 |
"$D/sources/installkernel" -v 1.1.2 "$D/sources/linux" System.map /somedirignored |
| 115 |
|
| 116 |
entry="$BOOT_ROOT/loader/entries/the-token-1.1.2.conf" |
| 117 |
test -f "$entry" |
| 118 |
grep -qE '^title ' "$entry" |
| 119 |
grep -qE '^version +1.1.2' "$entry" |
| 120 |
grep -qE '^options +opt1 opt2' "$entry" |
| 121 |
grep -qE '^linux .*/the-token/1.1.2/linux' "$entry" |
| 122 |
( ! grep -qE '^initrd' "$entry" ) |
| 123 |
grep -qE '^devicetree .*/the-token/1.1.2/whatever.dtb' "$entry" |
| 124 |
|
| 125 |
grep -qE 'image' "$BOOT_ROOT/the-token/1.1.2/linux" |
| 126 |
test ! -e "$BOOT_ROOT/the-token/1.1.2/initrd" |
| 127 |
grep -qE 'DTBDTB' "$BOOT_ROOT/the-token/1.1.2/whatever.dtb" |
| 128 |
|
| 129 |
# Check installation with boot counting |
| 130 |
echo '56' >"$D/sources/tries" |
| 131 |
|
| 132 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 133 |
entry="$BOOT_ROOT/loader/entries/the-token-1.1.1+56.conf" |
| 134 |
test -f "$entry" |
| 135 |
grep -qE '^title ' "$entry" |
| 136 |
grep -qE '^version +1.1.1' "$entry" |
| 137 |
grep -qE '^options +opt1 opt2' "$entry" |
| 138 |
grep -qE '^linux .*/the-token/1.1.1/linux' "$entry" |
| 139 |
grep -qE '^initrd .*/the-token/1.1.1/initrd' "$entry" |
| 140 |
|
| 141 |
grep -qE 'image' "$BOOT_ROOT/the-token/1.1.1/linux" |
| 142 |
grep -qE 'initrd' "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 143 |
|
| 144 |
# Install UKI |
| 145 |
if [ -f "$ukify" ]; then |
| 146 |
mkdir "$D/sources/install.conf.d" |
| 147 |
cat >>"$D/sources/install.conf.d/override.conf" <<EOF |
| 148 |
layout=uki |
| 149 |
uki_generator=ukify |
| 150 |
EOF |
| 151 |
|
| 152 |
"$kernel_install" -v add 1.1.3 "$D/sources/linux" "$D/sources/initrd" |
| 153 |
uki="${BOOT_ROOT}/EFI/Linux/the-token-1.1.3+56.efi" |
| 154 |
test -f "$uki" |
| 155 |
|
| 156 |
if [ -x "$bootctl" ]; then |
| 157 |
"$bootctl" kernel-inspect "$uki" | grep -E 'Kernel Type: +uki$' >/dev/null |
| 158 |
"$bootctl" kernel-inspect "$uki" | grep -E 'Version: +1\.1\.3$' >/dev/null |
| 159 |
"$bootctl" kernel-inspect "$uki" | grep -E 'Cmdline: +opt1 opt2$' >/dev/null |
| 160 |
fi |
| 161 |
|
| 162 |
"$ukify" inspect "$uki" | grep -E '^.sbat' >/dev/null |
| 163 |
"$ukify" inspect "$uki" | grep -E '^.cmdline' >/dev/null |
| 164 |
"$ukify" inspect "$uki" | grep -E '^.uname' >/dev/null |
| 165 |
"$ukify" inspect "$uki" | grep -E '^.initrd' >/dev/null |
| 166 |
"$ukify" inspect "$uki" | grep -E '^.linux' >/dev/null |
| 167 |
"$ukify" inspect "$uki" | grep -E '^.dtb' >/dev/null |
| 168 |
|
| 169 |
rm "$D/sources/install.conf.d/override.conf" |
| 170 |
fi |
| 171 |
|
| 172 |
# Test bootctl |
| 173 |
if [ -x "$bootctl" ]; then |
| 174 |
echo "Testing bootctl" |
| 175 |
e2="${entry%+*}_2.conf" |
| 176 |
cp "$entry" "$e2" |
| 177 |
export SYSTEMD_ESP_PATH=/boot |
| 178 |
# We use --root so strip the root prefix from KERNEL_INSTALL_CONF_ROOT |
| 179 |
export KERNEL_INSTALL_CONF_ROOT="sources" |
| 180 |
|
| 181 |
# create file that is not referenced. Check if cleanup removes |
| 182 |
# it but leaves the rest alone |
| 183 |
:> "$BOOT_ROOT/the-token/1.1.2/initrd" |
| 184 |
"$bootctl" --root="$D" cleanup |
| 185 |
test ! -e "$BOOT_ROOT/the-token/1.1.2/initrd" |
| 186 |
test -e "$BOOT_ROOT/the-token/1.1.2/linux" |
| 187 |
test -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 188 |
test -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 189 |
|
| 190 |
# now remove duplicated entry and make sure files are left over |
| 191 |
"$bootctl" --root="$D" unlink "${e2##*/}" |
| 192 |
test -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 193 |
test -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 194 |
test -e "$entry" |
| 195 |
test ! -e "$e2" |
| 196 |
# remove last entry referencing those files |
| 197 |
entry_id="${entry##*/}" |
| 198 |
entry_id="${entry_id%+*}.conf" |
| 199 |
"$bootctl" --root="$D" unlink "$entry_id" |
| 200 |
test ! -e "$entry" |
| 201 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/linux" |
| 202 |
test ! -e "$BOOT_ROOT/the-token/1.1.1/initrd" |
| 203 |
fi |
| 204 |
|
| 205 |
########################################### |
| 206 |
# tests for --make-entry-directory= |
| 207 |
########################################### |
| 208 |
|
| 209 |
# disable all dropins |
| 210 |
cat >"$D/00-skip.install" <<EOF |
| 211 |
#!/bin/bash |
| 212 |
exit 77 |
| 213 |
EOF |
| 214 |
chmod +x "$D/00-skip.install" |
| 215 |
export KERNEL_INSTALL_PLUGINS="$D/00-skip.install" |
| 216 |
|
| 217 |
# drop layout= from install.conf |
| 218 |
cat >"$D/sources/install.conf" <<EOF |
| 219 |
initrd_generator=none |
| 220 |
# those are overridden by envvars |
| 221 |
BOOT_ROOT="$D/badboot" |
| 222 |
MACHINE_ID=badbadbadbadbadbad6abadbadbadbad |
| 223 |
EOF |
| 224 |
export KERNEL_INSTALL_CONF_ROOT="$D/sources" |
| 225 |
|
| 226 |
rm -rf "$BOOT_ROOT" |
| 227 |
mkdir -p "$BOOT_ROOT" |
| 228 |
|
| 229 |
# 1. defaults to 'auto', and the entry directory is created only when the layout is BLS |
| 230 |
# 1.1 token directory does not exist -> layout is other. |
| 231 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 232 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 233 |
"$kernel_install" -v remove 1.1.1 |
| 234 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 235 |
|
| 236 |
# 1.2 token directory exists -> layout is BLS |
| 237 |
mkdir -p "$BOOT_ROOT/the-token" |
| 238 |
"$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 239 |
test -d "$BOOT_ROOT/the-token/1.1.1" |
| 240 |
"$kernel_install" -v remove 1.1.1 |
| 241 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 242 |
rmdir "$BOOT_ROOT/the-token" |
| 243 |
|
| 244 |
# 2. --make-entry-directory=yes |
| 245 |
# 2.1 token directory does not exist -> layout is other. |
| 246 |
"$kernel_install" -v --make-entry-directory=yes add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 247 |
test -d "$BOOT_ROOT/the-token/1.1.1" |
| 248 |
"$kernel_install" -v --make-entry-directory=yes remove 1.1.1 |
| 249 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 250 |
test -d "$BOOT_ROOT/the-token" |
| 251 |
|
| 252 |
# 2.2 token directory exists -> layout is BLS |
| 253 |
mkdir -p "$BOOT_ROOT/the-token" |
| 254 |
"$kernel_install" -v --make-entry-directory=yes add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 255 |
test -d "$BOOT_ROOT/the-token/1.1.1" |
| 256 |
"$kernel_install" -v --make-entry-directory=yes remove 1.1.1 |
| 257 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 258 |
test -d "$BOOT_ROOT/the-token" |
| 259 |
rmdir "$BOOT_ROOT/the-token" |
| 260 |
|
| 261 |
# 3. --make-entry-directory=no |
| 262 |
# 3.1 token directory does not exist -> layout is other. |
| 263 |
"$kernel_install" -v --make-entry-directory=no add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 264 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 265 |
"$kernel_install" -v --make-entry-directory=no remove 1.1.1 |
| 266 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 267 |
|
| 268 |
# 3.2 token directory exists -> layout is BLS |
| 269 |
mkdir -p "$BOOT_ROOT/the-token" |
| 270 |
"$kernel_install" -v --make-entry-directory=no add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 271 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 272 |
"$kernel_install" -v --make-entry-directory=no remove 1.1.1 |
| 273 |
test ! -e "$BOOT_ROOT/the-token/1.1.1" |
| 274 |
test -d "$BOOT_ROOT/the-token" |
| 275 |
rmdir "$BOOT_ROOT/the-token" |
| 276 |
|
| 277 |
########################################### |
| 278 |
# tests for --entry-token= |
| 279 |
########################################### |
| 280 |
"$kernel_install" -v --make-entry-directory=yes --entry-token=machine-id add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 281 |
test -d "$BOOT_ROOT/$MACHINE_ID/1.1.1" |
| 282 |
"$kernel_install" -v --make-entry-directory=yes --entry-token=machine-id remove 1.1.1 |
| 283 |
test ! -e "$BOOT_ROOT/$MACHINE_ID/1.1.1" |
| 284 |
test -d "$BOOT_ROOT/$MACHINE_ID" |
| 285 |
rmdir "$BOOT_ROOT/$MACHINE_ID" |
| 286 |
|
| 287 |
"$kernel_install" -v --make-entry-directory=yes --entry-token=literal:hoge add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 288 |
test -d "$BOOT_ROOT/hoge/1.1.1" |
| 289 |
"$kernel_install" -v --make-entry-directory=yes --entry-token=literal:hoge remove 1.1.1 |
| 290 |
test ! -e "$BOOT_ROOT/hoge/1.1.1" |
| 291 |
test -d "$BOOT_ROOT/hoge" |
| 292 |
rmdir "$BOOT_ROOT/hoge" |
| 293 |
|
| 294 |
########################################### |
| 295 |
# tests for --json= |
| 296 |
########################################### |
| 297 |
output="$("$kernel_install" -v --json=pretty inspect 1.1.1 "$D/sources/linux")" |
| 298 |
|
| 299 |
diff -u <(echo "$output") - >&2 <<EOF |
| 300 |
{ |
| 301 |
"MachineID" : "3e0484f3634a418b8e6a39e8828b03e3", |
| 302 |
"KernelImageType" : "unknown", |
| 303 |
"Layout" : "other", |
| 304 |
"BootRoot" : "$BOOT_ROOT", |
| 305 |
"EntryTokenType" : "literal", |
| 306 |
"EntryToken" : "the-token", |
| 307 |
"EntryDirectory" : "$BOOT_ROOT/the-token/1.1.1", |
| 308 |
"KernelVersion" : "1.1.1", |
| 309 |
"Kernel" : "$D/sources/linux", |
| 310 |
"Initrds" : null, |
| 311 |
"InitrdGenerator" : "none", |
| 312 |
"UKIGenerator" : null, |
| 313 |
"Plugins" : [ |
| 314 |
"$D/00-skip.install" |
| 315 |
], |
| 316 |
"PluginEnvironment" : [ |
| 317 |
"LC_COLLATE=$SYSTEMD_DEFAULT_LOCALE", |
| 318 |
"KERNEL_INSTALL_VERBOSE=1", |
| 319 |
"KERNEL_INSTALL_IMAGE_TYPE=unknown", |
| 320 |
"KERNEL_INSTALL_MACHINE_ID=3e0484f3634a418b8e6a39e8828b03e3", |
| 321 |
"KERNEL_INSTALL_ENTRY_TOKEN=the-token", |
| 322 |
"KERNEL_INSTALL_BOOT_ROOT=$BOOT_ROOT", |
| 323 |
"KERNEL_INSTALL_LAYOUT=other", |
| 324 |
"KERNEL_INSTALL_INITRD_GENERATOR=none", |
| 325 |
"KERNEL_INSTALL_UKI_GENERATOR=", |
| 326 |
"KERNEL_INSTALL_STAGING_AREA=${TMPDIR:-/var/tmp}/kernel-install.staging.XXXXXX" |
| 327 |
] |
| 328 |
} |
| 329 |
EOF |
| 330 |
|
| 331 |
########################################### |
| 332 |
# tests for propagation of plugin failure (issue #30087) |
| 333 |
########################################### |
| 334 |
cat >"$D/00-plugin-skip" <<EOF |
| 335 |
#!/usr/bin/env bash |
| 336 |
exit 77 |
| 337 |
EOF |
| 338 |
chmod +x "$D/00-plugin-skip" |
| 339 |
|
| 340 |
cat >"$D/10-plugin-fail" <<EOF |
| 341 |
#!/usr/bin/env bash |
| 342 |
exit 42 |
| 343 |
EOF |
| 344 |
chmod +x "$D/10-plugin-fail" |
| 345 |
|
| 346 |
# Exit code 77 means remaining plugins will be skipped. |
| 347 |
KERNEL_INSTALL_PLUGINS="$D/00-plugin-skip $D/10-plugin-fail" "$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 348 |
|
| 349 |
# Other non-zero exit code will be propagated. |
| 350 |
set +e |
| 351 |
KERNEL_INSTALL_PLUGINS="$D/10-plugin-fail" "$kernel_install" -v add 1.1.1 "$D/sources/linux" "$D/sources/initrd" |
| 352 |
ret=$? |
| 353 |
set -e |
| 354 |
test "$ret" -eq "42" |