b9e861
# These variables and functions are useful in 2nd kernel
b9e861
b9e861
. /lib/kdump-lib.sh
fe2ad6
. /lib/kdump-logger.sh
b9e861
b9e861
KDUMP_PATH="/var/crash"
fe2ad6
KDUMP_LOG_FILE="/run/initramfs/kexec-dmesg.log"
b9e861
CORE_COLLECTOR=""
fe2ad6
DEFAULT_CORE_COLLECTOR="makedumpfile -l --message-level 7 -d 31"
b9e861
DMESG_COLLECTOR="/sbin/vmcore-dmesg"
b9e861
FAILURE_ACTION="systemctl reboot -f"
b9e861
DATEDIR=`date +%Y-%m-%d-%T`
b9e861
HOST_IP='127.0.0.1'
b9e861
DUMP_INSTRUCTION=""
b9e861
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
b9e861
KDUMP_SCRIPT_DIR="/kdumpscripts"
b9e861
DD_BLKSIZE=512
b9e861
FINAL_ACTION="systemctl reboot -f"
b9e861
KDUMP_CONF="/etc/kdump.conf"
b9e861
KDUMP_PRE=""
b9e861
KDUMP_POST=""
b9e861
NEWROOT="/sysroot"
73ea9d
OPALCORE="/sys/firmware/opal/mpipl/core"
b9e861
fe2ad6
#initiate the kdump logger
fe2ad6
dlog_init
fe2ad6
if [ $? -ne 0 ]; then
fe2ad6
    echo "failed to initiate the kdump logger."
fe2ad6
    exit 1
fe2ad6
fi
fe2ad6
b9e861
get_kdump_confs()
b9e861
{
b9e861
    local config_opt config_val
b9e861
b9e861
    while read config_opt config_val;
b9e861
    do
b9e861
        # remove inline comments after the end of a directive.
b9e861
        case "$config_opt" in
b9e861
            path)
b9e861
                KDUMP_PATH="$config_val"
b9e861
            ;;
b9e861
            core_collector)
b9e861
                [ -n "$config_val" ] && CORE_COLLECTOR="$config_val"
b9e861
            ;;
b9e861
            sshkey)
b9e861
                if [ -f "$config_val" ]; then
b9e861
                    SSH_KEY_LOCATION=$config_val
b9e861
                fi
b9e861
            ;;
b9e861
            kdump_pre)
b9e861
                KDUMP_PRE="$config_val"
b9e861
            ;;
b9e861
            kdump_post)
b9e861
                KDUMP_POST="$config_val"
b9e861
            ;;
b9e861
            fence_kdump_args)
b9e861
                FENCE_KDUMP_ARGS="$config_val"
b9e861
            ;;
b9e861
            fence_kdump_nodes)
b9e861
                FENCE_KDUMP_NODES="$config_val"
b9e861
            ;;
b9e861
            failure_action|default)
b9e861
                case $config_val in
b9e861
                    shell)
b9e861
                        FAILURE_ACTION="kdump_emergency_shell"
b9e861
                    ;;
b9e861
                    reboot)
bda30f
                        FAILURE_ACTION="systemctl reboot -f && exit"
b9e861
                    ;;
b9e861
                    halt)
bda30f
                        FAILURE_ACTION="halt && exit"
b9e861
                    ;;
b9e861
                    poweroff)
bda30f
                        FAILURE_ACTION="systemctl poweroff -f && exit"
b9e861
                    ;;
b9e861
                    dump_to_rootfs)
b9e861
                        FAILURE_ACTION="dump_to_rootfs"
b9e861
                    ;;
b9e861
                esac
b9e861
            ;;
b9e861
            final_action)
b9e861
                case $config_val in
b9e861
                    reboot)
b9e861
                        FINAL_ACTION="systemctl reboot -f"
b9e861
                    ;;
b9e861
                    halt)
b9e861
                        FINAL_ACTION="halt"
b9e861
                    ;;
b9e861
                    poweroff)
b9e861
                        FINAL_ACTION="systemctl poweroff -f"
b9e861
                    ;;
b9e861
                esac
b9e861
            ;;
b9e861
        esac
b9e861
    done <<< "$(read_strip_comments $KDUMP_CONF)"
b9e861
b9e861
    if [ -z "$CORE_COLLECTOR" ]; then
b9e861
        CORE_COLLECTOR="$DEFAULT_CORE_COLLECTOR"
b9e861
        if is_ssh_dump_target || is_raw_dump_target; then
b9e861
            CORE_COLLECTOR="$CORE_COLLECTOR -F"
b9e861
        fi
b9e861
    fi
b9e861
}
b9e861
fe2ad6
# store the kexec kernel log to a file.
fe2ad6
save_log()
fe2ad6
{
fe2ad6
    dmesg -T > $KDUMP_LOG_FILE
fe2ad6
fe2ad6
    if command -v journalctl > /dev/null; then
fe2ad6
        journalctl -ab >> $KDUMP_LOG_FILE
fe2ad6
    fi
cf4816
    chmod 600 $KDUMP_LOG_FILE
fe2ad6
}
fe2ad6
73ea9d
# dump_fs <mount point>
b9e861
dump_fs()
b9e861
{
fe2ad6
    local _exitcode
73ea9d
    local _mp=$1
fe2ad6
    ddebug "dump_fs _mp=$_mp"
fe2ad6
fe2ad6
    if ! is_mounted "$_mp"; then
fe2ad6
        dinfo "dump path \"$_mp\" is not mounted, trying to mount..."
fe2ad6
        mount --target $_mp
fe2ad6
        if [ $? -ne 0 ]; then
fe2ad6
            derror "failed to dump to \"$_mp\", it's not a mount point!"
fe2ad6
            return 1
bda30f
        fi
b9e861
    fi
b9e861
b9e861
    # Remove -F in makedumpfile case. We don't want a flat format dump here.
b9e861
    [[ $CORE_COLLECTOR = *makedumpfile* ]] && CORE_COLLECTOR=`echo $CORE_COLLECTOR | sed -e "s/-F//g"`
b9e861
fe2ad6
    dinfo "saving to $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
b9e861
b9e861
    mount -o remount,rw $_mp || return 1
b9e861
    mkdir -p $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR || return 1
b9e861
b9e861
    save_vmcore_dmesg_fs ${DMESG_COLLECTOR} "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
73ea9d
    save_opalcore_fs "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
b9e861
fe2ad6
    dinfo "saving vmcore"
fe2ad6
    $CORE_COLLECTOR /proc/vmcore $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete
fe2ad6
    _exitcode=$?
fe2ad6
    if [ $_exitcode -eq 0 ]; then
fe2ad6
        mv $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore
fe2ad6
        sync
fe2ad6
        dinfo "saving vmcore complete"
fe2ad6
    else
fe2ad6
        derror "saving vmcore failed, _exitcode:$_exitcode"
fe2ad6
    fi
b9e861
fe2ad6
    dinfo "saving the $KDUMP_LOG_FILE to $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
fe2ad6
    save_log
fe2ad6
    mv $KDUMP_LOG_FILE $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/
fe2ad6
    if [ $_exitcode -ne 0 ]; then
fe2ad6
        return 1
fe2ad6
    fi
bda30f
b9e861
    # improper kernel cmdline can cause the failure of echo, we can ignore this kind of failure
b9e861
    return 0
b9e861
}
b9e861
b9e861
save_vmcore_dmesg_fs() {
b9e861
    local _dmesg_collector=$1
b9e861
    local _path=$2
b9e861
fe2ad6
    dinfo "saving vmcore-dmesg.txt to ${_path}"
b9e861
    $_dmesg_collector /proc/vmcore > ${_path}/vmcore-dmesg-incomplete.txt
b9e861
    _exitcode=$?
b9e861
    if [ $_exitcode -eq 0 ]; then
b9e861
        mv ${_path}/vmcore-dmesg-incomplete.txt ${_path}/vmcore-dmesg.txt
cf4816
        chmod 600 ${_path}/vmcore-dmesg.txt
b9e861
b9e861
        # Make sure file is on disk. There have been instances where later
b9e861
        # saving vmcore failed and system rebooted without sync and there
b9e861
        # was no vmcore-dmesg.txt available.
b9e861
        sync
fe2ad6
        dinfo "saving vmcore-dmesg.txt complete"
b9e861
    else
fe2ad6
        derror "saving vmcore-dmesg.txt failed"
b9e861
    fi
b9e861
}
b9e861
73ea9d
save_opalcore_fs() {
73ea9d
    local _path=$1
73ea9d
73ea9d
    if [ ! -f $OPALCORE ]; then
73ea9d
        # Check if we are on an old kernel that uses a different path
73ea9d
        if [ -f /sys/firmware/opal/core ]; then
73ea9d
            OPALCORE="/sys/firmware/opal/core"
73ea9d
        else
73ea9d
            return 0
73ea9d
        fi
73ea9d
    fi
73ea9d
fe2ad6
    dinfo "saving opalcore:$OPALCORE to ${_path}/opalcore"
73ea9d
    cp $OPALCORE ${_path}/opalcore
73ea9d
    if [ $? -ne 0 ]; then
fe2ad6
        derror "saving opalcore failed"
73ea9d
        return 1
73ea9d
    fi
73ea9d
73ea9d
    sync
fe2ad6
    dinfo "saving opalcore complete"
73ea9d
    return 0
73ea9d
}
73ea9d
b9e861
dump_to_rootfs()
b9e861
{
b9e861
fe2ad6
    dinfo "Trying to bring up rootfs device"
b9e861
    systemctl start dracut-initqueue
fe2ad6
    dinfo "Waiting for rootfs mount, will timeout after 90 seconds"
b9e861
    systemctl start sysroot.mount
b9e861
fe2ad6
    ddebug "NEWROOT=$NEWROOT"
fe2ad6
b9e861
    dump_fs $NEWROOT
b9e861
}
b9e861
b9e861
kdump_emergency_shell()
b9e861
{
b9e861
    echo "PS1=\"kdump:\\\${PWD}# \"" >/etc/profile
fe2ad6
    ddebug "Switching to dracut emergency..."
b9e861
    /bin/dracut-emergency
b9e861
    rm -f /etc/profile
b9e861
}
b9e861
b9e861
do_failure_action()
b9e861
{
fe2ad6
    dinfo "Executing failure action $FAILURE_ACTION"
b9e861
    eval $FAILURE_ACTION
b9e861
}
b9e861
b9e861
do_final_action()
b9e861
{
fe2ad6
    dinfo "Executing final action $FINAL_ACTION"
b9e861
    eval $FINAL_ACTION
b9e861
}