Skip to content

Commit 4d89510

Browse files
authored
[reboot] User-friendly reboot cause message for kernel panic (sonic-net#1486)
Signed-off-by: Yong Zhao [email protected] What I did If the rebooting of SONiC device was caused by kernel panic, then the CLI command show reboot-cause should show Kernel Panic. How I did it Currently if kernel was panicked, then the device would be rebooted. The reboot script wrote a message into reboot-cause.txt. I just updated the content of this message. How to verify it I verified this change on the virtual switch in the following steps: Trigger kernel panic: echo c > /proc/sysrq-trigger After device was rebooted, run the CLI show reboot-cause: admin@vlab-01:~$ show reboot-cause Kernel Panic [Time: Tue 09 Mar 2021 03:03:56 AM UTC] Previous command output (if the output of a command-line utility has changed) admin@vlab-01:~$ show reboot-cause User issued 'kdump' command [User: kdump, Time: Mon 08 Mar 2021 01:47:43 AM UTC] New command output (if the output of a command-line utility has changed) admin@vlab-01:~$ show reboot-cause Kernel Panic [Time: Tue 09 Mar 2021 03:03:56 AM UTC]
1 parent 1f1696a commit 4d89510

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

scripts/reboot

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ REBOOT_TIME=$(date)
99
VMCORE_FILE=/proc/vmcore
1010
if [ -e $VMCORE_FILE -a -s $VMCORE_FILE ]; then
1111
echo "We have a /proc/vmcore, then we just kdump'ed"
12-
echo "User issued 'kdump' command [User: kdump, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE}
12+
echo "Kernel Panic [Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE}
1313
sync
1414
PLATFORM=$(grep -oP 'sonic_platform=\K\S+' /proc/cmdline)
1515
if [ ! -z "${PLATFORM}" -a -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then

show/reboot_cause.py

+40-14
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
import utilities_common.cli as clicommon
99

1010

11-
PREVIOUS_REBOOT_CAUSE_FILE = "/host/reboot-cause/previous-reboot-cause.json"
12-
USER_ISSUED_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]"
11+
PREVIOUS_REBOOT_CAUSE_FILE_PATH = "/host/reboot-cause/previous-reboot-cause.json"
12+
1313

1414
def read_reboot_cause_file():
15-
result = ""
16-
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
17-
with open(PREVIOUS_REBOOT_CAUSE_FILE) as f:
18-
result = json.load(f)
19-
return result
15+
reboot_cause_dict = {}
16+
17+
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE_PATH):
18+
with open(PREVIOUS_REBOOT_CAUSE_FILE_PATH) as prev_reboot_cause_file:
19+
try:
20+
reboot_cause_dict = json.load(prev_reboot_cause_file)
21+
except json.JSONDecodeError as err:
22+
click.echo("Failed to load JSON file '{}'!".format(PREVIOUS_REBOOT_CAUSE_FILE_PATH), err=True)
23+
24+
return reboot_cause_dict
25+
2026

2127
#
2228
# 'reboot-cause' group ("show reboot-cause")
@@ -26,15 +32,35 @@ def read_reboot_cause_file():
2632
def reboot_cause(ctx):
2733
"""Show cause of most recent reboot"""
2834
if ctx.invoked_subcommand is None:
29-
reboot_cause = ""
35+
reboot_cause_str = ""
36+
3037
# Read the previous reboot cause
31-
data = read_reboot_cause_file()
32-
if data['user'] == "N/A":
33-
reboot_cause = "{}".format(data['cause'])
38+
reboot_cause_dict = read_reboot_cause_file()
39+
40+
reboot_cause = reboot_cause_dict.get("cause", "Unknown")
41+
reboot_user = reboot_cause_dict.get("user", "N/A")
42+
reboot_time = reboot_cause_dict.get("time", "N/A")
43+
44+
if reboot_user != "N/A":
45+
reboot_cause_str = "User issued '{}' command".format(reboot_cause)
3446
else:
35-
reboot_cause = USER_ISSUED_REBOOT_CAUSE_REGEX.format(data['cause'], data['user'], data['time'])
47+
reboot_cause_str = reboot_cause
48+
49+
if reboot_user != "N/A" or reboot_time != "N/A":
50+
reboot_cause_str += " ["
51+
52+
if reboot_user != "N/A":
53+
reboot_cause_str += "User: {}".format(reboot_user)
54+
if reboot_time != "N/A":
55+
reboot_cause_str += ", "
56+
57+
if reboot_time != "N/A":
58+
reboot_cause_str += "Time: {}".format(reboot_time)
59+
60+
reboot_cause_str += "]"
61+
62+
click.echo(reboot_cause_str)
3663

37-
click.echo(reboot_cause)
3864

3965
# 'history' subcommand ("show reboot-cause history")
4066
@reboot_cause.command()
@@ -54,7 +80,7 @@ def history():
5480
for tk in table_keys:
5581
entry = db.get_all(db.STATE_DB, tk)
5682
r = []
57-
r.append(tk.replace(prefix,""))
83+
r.append(tk.replace(prefix, ""))
5884
r.append(entry['cause'] if 'cause' in entry else "")
5985
r.append(entry['time'] if 'time' in entry else "")
6086
r.append(entry['user'] if 'user' in entry else "")

tests/reboot_cause_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def setup_class(cls):
3131

3232
# Test 'show reboot-cause' without previous-reboot-cause.json
3333
def test_reboot_cause_no_history_file(self):
34-
expected_output = ""
34+
expected_output = "Unknown\n"
3535
runner = CliRunner()
3636
result = runner.invoke(show.cli.commands["reboot-cause"], [])
3737
assert result.output == expected_output

0 commit comments

Comments
 (0)