| #!/bin/bash | 
 |  | 
 | ERROR_MSG="KDUMP_ERROR" | 
 |  | 
 | find /sys/fs/pstore -name 'dmesg-*' -print -exec cat {} \; -exec rm -f {} \; | 
 |  | 
 | # Dump the core file to the ram | 
 | dumpdir="/run/log/kdump" | 
 | mkdir -p ${dumpdir} | 
 |  | 
 | pmsgs=$(ls /sys/fs/pstore/pmsg-*) || exit 0 | 
 |  | 
 | # there should always be 1 pmsg file | 
 | # if not, only process the first one and delete them all at the end | 
 | pmsg=$(echo "$pmsgs" | head -n 1) | 
 |  | 
 | # Parsing the pmsg file | 
 | # First line msg_len is a single number represents the dmesg buffer | 
 | # Following msg_len bytes of dmesg | 
 | # Rest of the buffer is the kdump, or error message starting with "KDUMP_ERROR" | 
 | msg_len=$(head -n 1 $pmsg) | 
 | msg_len_size=$(echo $msg_len | wc -c) | 
 | error_msg_len=$(echo -n $ERROR_MSG | wc -c) | 
 |  | 
 | core_offset=$(( msg_len + msg_len_size )) | 
 |  | 
 | # print the version info | 
 | cat /etc/os-release | 
 |  | 
 | # dump the dmesg to stdout | 
 | dd if=$pmsg bs=1 skip=$msg_len_size count=$msg_len | 
 |  | 
 | if [ $(dd if=$pmsg bs=1 skip=$core_offset count=$error_msg_len) == $ERROR_MSG ]; then | 
 |   # dump the error msg to stdout | 
 |   dd if=$pmsg bs=$core_offset skip=1 | 
 | else | 
 |   # process the core file | 
 |   dd if=$pmsg bs=$core_offset skip=1 | makedumpfile -R ${dumpdir}/kcore-$(/bin/date +%Y_%m_%d_%H_%M_%S) | 
 | fi | 
 |  | 
 | echo "removing pmsg file "$pmsgs | 
 |  | 
 | rm -f $pmsgs |