blob: 69e3cafe5cfa7a37b5964d9dcdf67b9525fbf65d [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001#! /bin/sh
2#
3# kdump
4#
5# Description: The kdump script provides the support:
6# 1. Load a kdump kernel image into memory;
7# 2. Copy away vmcore when system panic.
8#
9
10#default
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011KEXEC=/usr/sbin/kexec
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050012KEXEC_ARGS="-p"
13
14MAKEDUMPFILE=/usr/bin/makedumpfile
15MAKEDUMPFILE_ARGS="-E -d 1"
16
17LOGGER="logger -p info -t kdump"
18
19if [ -f /etc/sysconfig/kdump.conf ]; then
20 . /etc/sysconfig/kdump.conf
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021else
22 echo "no /etc/sysconfig/kdump.conf"
23 exit 1;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024fi
25
26do_check()
27{
28 #check makedumpfile
29 if [ ! -e ${MAKEDUMPFILE} -o ! -x ${MAKEDUMPFILE} ] ;then
30 echo "No makedumpfile found."
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031 exit 0
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032 fi
33
34 #check kexec
35 if [ ! -e ${KEXEC} -o ! -x ${KEXEC} ] ;then
36 echo "No kexec found."
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037 exit 0
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038 fi
39
40 #check whether kdump kernel image exists on the system
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041 if [ -z "${KDUMP_KIMAGE}" -o ! -f "${KDUMP_KIMAGE}" ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050042 echo "No kdump kernel image found."
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043 exit 0
44 fi
45
46 if [ "${KDUMP_CMDLINE}"x = "x" ] ; then
47 echo "KDUMP_CMDLINE is not configured"
48 exit 0
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049 fi
50}
51
52do_save_vmcore()
53{
Patrick Williamsc0f7c042017-02-23 20:41:17 -060054 if [ ${KDUMP_VMCORE_PATH}x = x ]; then
55 KDUMP_VMCORE_PATH="/var/crash/`date +"%Y-%m-%d"`"
56 fi
57
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058 mkdir -p ${KDUMP_VMCORE_PATH}
59 echo "Saving a vmcore to ${KDUMP_VMCORE_PATH}."
60
61 ${MAKEDUMPFILE} ${MAKEDUMPFILE_ARGS} /proc/vmcore ${KDUMP_VMCORE_PATH}/vmcore-"`date +"%H:%M:%S"`"
62# cp --sparse=always /proc/vmcore ${KDUMP_VMCORE_PATH}/vmcore-"`date +"%H:%M:%S"`"
63 rc=$?
64 if [ ${rc} == 0 ]; then
65 ${LOGGER} "Saved a vmcore to ${KDUMP_VMCORE_PATH}."
66 else
67 ${LOGGER} "Failed to save vmcore!"
68 fi
69 return ${rc}
70}
71
72do_start()
73{
74 #check file
75 do_check
76
77 #check whether the running kernel supports kdump.
78 if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
79 echo "Kdump isn't supported on the running kernel!!!"
80 ${LOGGER} "Kdump isn't supported on the running kernel!!!"
81 return 1
82 fi
83
84 #check whether kdump kernel image has been loaded
85 rc=`cat /sys/kernel/kexec_crash_loaded`
86 if [ ${rc} != 0 ]; then
87 echo "Kdump is already running.";
88 ${LOGGER} "Kdump is already running."
89 return 0
90 fi
91
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092 #check the running kernel cmdline option,insure "crashkernel=" always set.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 grep -q crashkernel= /proc/cmdline
94 if [ $? != 0 ]; then
95 echo "Kdump isn't supported on the running kernel,please check boot option!!!"
96 ${LOGGER} "Kdump isn't supported on the running kernel,please check boot option!!!"
97 return 1
98 fi
99
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500100 #Load the kdump kernel image
101 ${KEXEC} ${KEXEC_ARGS} "${KDUMP_KIMAGE}" --append="${KDUMP_CMDLINE}"
102 if [ $? != 0 ]; then
103 echo "Failed to load kdump kernel!"
104 ${LOGGER} "Failed to load kdump kernel!"
105 return 1
106 fi
107
108 echo "Kdump started up."
109 ${LOGGER} "Kdump started up."
110}
111
112do_stop()
113{
114 ${KEXEC} -p -u 2>/dev/null
115 if [ $? == 0 ]; then
116 echo "Kdump has been stopped."
117 ${LOGGER} "Kdump has been stopped."
118 else
119 echo "Failed to stop kdump!"
120 ${LOGGER} "Failed to stop kdump!"
121 fi
122}
123
124case "$1" in
125 start)
126 if [ -s /proc/vmcore ]; then
127 do_save_vmcore
128 reboot
129 else
130 do_start
131 fi
132 ;;
133 restart)
134 do_stop
135 do_start
136 ;;
137 stop)
138 do_stop
139 ;;
140 *)
141 echo $"Usage: $0 {start|stop|restart}"
142 exit 1
143esac
144
145exit $?