]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-04.bsod.sh
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / test / units / testsuite-04.bsod.sh
CommitLineData
ebefce6e
FS
1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6if systemd-detect-virt -cq; then
7 echo "This test requires a VM, skipping the test"
8 exit 0
9fi
10
11# shellcheck disable=SC2317
12at_exit() {
13 local EC=$?
14
15 if [[ $EC -ne 0 ]] && [[ -e /tmp/console.dump ]]; then
16 cat /tmp/console.dump
17 fi
18
19 if mountpoint -q /var/log/journal; then
20 journalctl --relinquish-var
21 umount /var/log/journal
22 journalctl --flush
23 fi
24
8f7c876b
FS
25 rm -f /run/systemd/journald.conf.d/99-forward-to-console.conf
26 systemctl restart systemd-journald
27
ebefce6e
FS
28 return 0
29}
30
31vcs_dump_and_check() {
32 local expected_message="${1:?}"
33
34 # It might take a while before the systemd-bsod stuff appears on the VCS,
35 # so try it a couple of times
36 for _ in {0..9}; do
37 setterm --term linux --dump --file /tmp/console.dump
32c376a4
FS
38 if grep -aq "Press any key to exit" /tmp/console.dump &&
39 grep -aq "$expected_message" /tmp/console.dump &&
40 grep -aq "The current boot has failed" /tmp/console.dump; then
ebefce6e
FS
41
42 return 0
43 fi
44
45 sleep .5
46 done
47
48 return 1
49}
50
51# Since systemd-bsod always fetches only the first emergency message from the
52# current boot, let's temporarily overmount /var/log/journal with a tmpfs,
53# as we're going to wipe it multiple times, but we need to keep the original
54# journal intact for the other tests to work correctly.
8f7c876b
FS
55#
56# Also, since we'll eventually lose the journal from this test, let's temporarily
57# forward everything to console, to make potential fails debug-able.
ebefce6e 58trap at_exit EXIT
8f7c876b
FS
59mkdir -p /run/systemd/journald.conf.d/
60echo -ne '[Journal]\nForwardToConsole=yes' >/run/systemd/journald.conf.d/99-forward-to-console.conf
ebefce6e
FS
61mount -t tmpfs tmpfs /var/log/journal
62systemctl restart systemd-journald
63
64systemctl stop systemd-bsod
65
66# Since we just wiped the journal, there should be no emergency messages and
67# systemd-bsod should be just a no-op
68timeout 10s /usr/lib/systemd/systemd-bsod
69setterm --term linux --dump --file /tmp/console.dump
70(! grep "The current boot has failed" /tmp/console.dump)
71
72# systemd-bsod should pick up emergency messages only with UID=0, so let's check
73# that as well
74systemd-run --user --machine testuser@ --wait --pipe systemd-cat -p emerg echo "User emergency message"
75systemd-cat -p emerg echo "Root emergency message"
76journalctl --sync
77# Set $SYSTEMD_COLORS so systemd-bsod also prints out the QR code
78SYSTEMD_COLORS=256 /usr/lib/systemd/systemd-bsod &
79PID=$!
80vcs_dump_and_check "Root emergency message"
81grep -aq "Scan the QR code" /tmp/console.dump
82# TODO: check if systemd-bsod exits on a key press (didn't figure this one out yet)
83kill $PID
84timeout 10 bash -c "while kill -0 $PID; do sleep .5; done"
85
86# Wipe the journal
87journalctl --vacuum-size=1 --rotate
88(! journalctl -q -b -p emerg --grep .)
89
90# Check the systemd-bsod.service as well
91# Note: the systemd-bsod.service unit has ConditionVirtualization=no, so let's
92# temporarily override it just for the test
93mkdir /run/systemd/system/systemd-bsod.service.d
94printf '[Unit]\nConditionVirtualization=\n' >/run/systemd/system/systemd-bsod.service.d/99-override.conf
95systemctl daemon-reload
96systemctl start systemd-bsod
97systemd-cat -p emerg echo "Service emergency message"
98vcs_dump_and_check "Service emergency message"
7ec22d7d
FS
99systemctl stop systemd-bsod
100
101# Wipe the journal
102journalctl --vacuum-size=1 --rotate
103(! journalctl -q -b -p emerg --grep .)
104
105# Same as above, but make sure the service responds to signals even when there are
106# no "emerg" messages, see systemd/systemd#30084
107(! systemctl is-active systemd-bsod)
108systemctl start systemd-bsod
109timeout 5s bash -xec 'until systemctl is-active systemd-bsod; do sleep .5; done'
110timeout 5s systemctl stop systemd-bsod
111timeout 5s bash -xec 'while systemctl is-active systemd-bsod; do sleep .5; done'