Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | #! /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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | KEXEC=/usr/sbin/kexec |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 12 | KEXEC_ARGS="-p" |
| 13 | |
| 14 | MAKEDUMPFILE=/usr/bin/makedumpfile |
| 15 | MAKEDUMPFILE_ARGS="-E -d 1" |
| 16 | |
| 17 | LOGGER="logger -p info -t kdump" |
| 18 | |
| 19 | if [ -f /etc/sysconfig/kdump.conf ]; then |
| 20 | . /etc/sysconfig/kdump.conf |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | else |
| 22 | echo "no /etc/sysconfig/kdump.conf" |
| 23 | exit 1; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 24 | fi |
| 25 | |
| 26 | do_check() |
| 27 | { |
| 28 | #check makedumpfile |
| 29 | if [ ! -e ${MAKEDUMPFILE} -o ! -x ${MAKEDUMPFILE} ] ;then |
| 30 | echo "No makedumpfile found." |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | exit 0 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 32 | fi |
| 33 | |
| 34 | #check kexec |
| 35 | if [ ! -e ${KEXEC} -o ! -x ${KEXEC} ] ;then |
| 36 | echo "No kexec found." |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | exit 0 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 38 | fi |
| 39 | |
| 40 | #check whether kdump kernel image exists on the system |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | if [ -z "${KDUMP_KIMAGE}" -o ! -f "${KDUMP_KIMAGE}" ]; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 42 | echo "No kdump kernel image found." |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | exit 0 |
| 44 | fi |
| 45 | |
| 46 | if [ "${KDUMP_CMDLINE}"x = "x" ] ; then |
| 47 | echo "KDUMP_CMDLINE is not configured" |
| 48 | exit 0 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | fi |
| 50 | } |
| 51 | |
| 52 | do_save_vmcore() |
| 53 | { |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 54 | if [ ${KDUMP_VMCORE_PATH}x = x ]; then |
| 55 | KDUMP_VMCORE_PATH="/var/crash/`date +"%Y-%m-%d"`" |
| 56 | fi |
| 57 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | 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 | |
| 72 | do_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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | #check the running kernel cmdline option,insure "crashkernel=" always set. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 93 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | #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 | |
| 112 | do_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 | |
| 124 | case "$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 |
| 143 | esac |
| 144 | |
| 145 | exit $? |