Skip to content

Commit 1115c84

Browse files
jlevequeyxieca
authored andcommitted
[reboot-cause]: Move reboot cause processing to its own service, 'process-reboot-cause' (#3102)
1 parent c932302 commit 1115c84

File tree

4 files changed

+104
-32
lines changed

4 files changed

+104
-32
lines changed

files/build_templates/sonic_debian_extension.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd.service $FILESYSTEM_ROOT/etc/systemd/s
220220
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable caclmgrd.service
221221
sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd $FILESYSTEM_ROOT/usr/bin/
222222

223+
# Copy process-reboot-cause service files
224+
sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause.service $FILESYSTEM_ROOT/etc/systemd/system/
225+
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable process-reboot-cause.service
226+
sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause $FILESYSTEM_ROOT/usr/bin/
227+
223228
## Install package without starting service
224229
## ref: https://wiki.debian.org/chroot
225230
sudo tee -a $FILESYSTEM_ROOT/usr/sbin/policy-rc.d > /dev/null <<EOF

files/image_config/platform/rc.local

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,6 @@ value_extract() {
106106
done
107107
}
108108

109-
# Set up previous and next reboot cause files accordingly
110-
process_reboot_cause() {
111-
REBOOT_CAUSE_DIR="/host/reboot-cause"
112-
REBOOT_CAUSE_FILE="${REBOOT_CAUSE_DIR}/reboot-cause.txt"
113-
PREVIOUS_REBOOT_CAUSE_FILE="${REBOOT_CAUSE_DIR}/previous-reboot-cause.txt"
114-
115-
mkdir -p $REBOOT_CAUSE_DIR
116-
117-
# If this is the first boot after an image install, store that as the
118-
# previous reboot cause.
119-
if [ -f $FIRST_BOOT_FILE ]; then
120-
echo "SONiC image installation" > $PREVIOUS_REBOOT_CAUSE_FILE
121-
fi
122-
123-
# If there is an existing REBOOT_CAUSE_FILE, copy that file to
124-
# PREVIOUS_REBOOT_CAUSE_FILE.
125-
if [ -f $REBOOT_CAUSE_FILE ]; then
126-
mv -f $REBOOT_CAUSE_FILE $PREVIOUS_REBOOT_CAUSE_FILE
127-
else
128-
echo "Unknown reboot cause" > $PREVIOUS_REBOOT_CAUSE_FILE
129-
fi
130-
131-
# Log the previous reboot cause to the syslog
132-
logger "Previous reboot cause: $(cat $PREVIOUS_REBOOT_CAUSE_FILE)"
133-
134-
# Set the default cause for the next reboot
135-
echo "Unexpected reboot" > $REBOOT_CAUSE_FILE
136-
}
137-
138109
program_console_speed()
139110
{
140111
speed=$(cat /proc/cmdline | grep -Eo 'console=ttyS[0-9]+,[0-9]+' | cut -d "," -f2)
@@ -150,9 +121,6 @@ program_console_speed()
150121

151122
#### Begin Main Body ####
152123

153-
# Set up previous and next reboot cause files
154-
process_reboot_cause
155-
156124
# If the machine.conf is absent, it indicates that the unit booted
157125
# into SONiC from another NOS. Extract the machine.conf from ONIE.
158126
if [ ! -e /host/machine.conf ]; then
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
#
3+
# process-reboot-cause
4+
#
5+
# Program designed to run once, soon after system boot which will
6+
# determine the cause of the previous reboot and store it to the disk,
7+
#
8+
9+
try:
10+
import os
11+
import pwd
12+
import subprocess
13+
import sys
14+
import syslog
15+
import sonic_platform
16+
from sonic_daemon_base.daemon_base import Logger
17+
except ImportError as err:
18+
raise ImportError("%s - required module not found" % str(err))
19+
20+
VERSION = "1.0"
21+
22+
SYSLOG_IDENTIFIER = "process-reboot-cause"
23+
24+
# Global logger instance
25+
logger = Logger(SYSLOG_IDENTIFIER)
26+
27+
REBOOT_CAUSE_DIR = "/host/reboot-cause/"
28+
REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt"
29+
PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt"
30+
31+
UNKNOWN_REBOOT_CAUSE = "Unknown"
32+
33+
34+
def main():
35+
logger.log_info("Starting up...")
36+
37+
if not os.geteuid() == 0:
38+
logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
39+
sys.exit("This utility must be run as root")
40+
41+
# Create REBOOT_CAUSE_DIR if it doesn't exist
42+
if not os.path.exists(REBOOT_CAUSE_DIR):
43+
os.makedirs(REBOOT_CAUSE_DIR)
44+
45+
# Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists
46+
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
47+
os.remove(PREVIOUS_REBOOT_CAUSE_FILE)
48+
49+
# Check if the previous reboot was caused by hardware
50+
platform = sonic_platform.platform.Platform()
51+
52+
chassis = platform.get_chassis()
53+
54+
hardware_reboot_cause, optional_details = chassis.get_reboot_cause()
55+
56+
# Set a default previous reboot cause
57+
previous_reboot_cause = UNKNOWN_REBOOT_CAUSE
58+
59+
if hardware_reboot_cause == chassis.REBOOT_CAUSE_NON_HARDWARE:
60+
# The reboot was not caused by hardware. If there is a REBOOT_CAUSE_FILE, it will
61+
# contain any software-related reboot info. We will use it as the previous cause.
62+
if os.path.isfile(REBOOT_CAUSE_FILE):
63+
cause_file = open(REBOOT_CAUSE_FILE, "r")
64+
previous_reboot_cause = cause_file.readline().rstrip('\n')
65+
cause_file.close()
66+
elif hardware_reboot_cause == chassis.REBOOT_CAUSE_HARDWARE_OTHER:
67+
previous_reboot_cause = "{} ({})".format(hardware_reboot_cause, optional_details)
68+
else:
69+
previous_reboot_cause = hardware_reboot_cause
70+
71+
# Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE
72+
prev_cause_file = open(PREVIOUS_REBOOT_CAUSE_FILE, "w")
73+
prev_cause_file.write(previous_reboot_cause)
74+
prev_cause_file.close()
75+
76+
# Also log the previous reboot cause to the syslog
77+
logger.log_info("Previous reboot cause: {}".format(previous_reboot_cause))
78+
79+
# Remove the old REBOOT_CAUSE_FILE
80+
os.remove(REBOOT_CAUSE_FILE)
81+
82+
# Write a new default reboot cause file for the next reboot
83+
cause_file = open(REBOOT_CAUSE_FILE, "w")
84+
cause_file.write(UNKNOWN_REBOOT_CAUSE)
85+
cause_file.close()
86+
87+
88+
if __name__ == "__main__":
89+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Reboot cause determination service
3+
After=rc-local.service
4+
5+
[Service]
6+
Type=simple
7+
ExecStart=/usr/bin/process-reboot-cause
8+
9+
[Install]
10+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)