de80c6
# These variables and functions are useful in 2nd kernel
de80c6
de80c6
. /lib/kdump-lib.sh
de80c6
de80c6
KDUMP_PATH="/var/crash"
de80c6
CORE_COLLECTOR=""
de80c6
DEFAULT_CORE_COLLECTOR="makedumpfile -l --message-level 1 -d 31"
de80c6
DMESG_COLLECTOR="/sbin/vmcore-dmesg"
de80c6
DEFAULT_ACTION="reboot"
de80c6
DATEDIR=`date +%Y.%m.%d-%T`
de80c6
HOST_IP='127.0.0.1'
de80c6
DUMP_INSTRUCTION=""
de80c6
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
de80c6
KDUMP_SCRIPT_DIR="/kdumpscripts"
de80c6
DD_BLKSIZE=512
de80c6
FINAL_ACTION="reboot"
de80c6
KDUMP_CONF="/etc/kdump.conf"
de80c6
KDUMP_PRE=""
de80c6
KDUMP_POST=""
de80c6
NEWROOT="/sysroot"
de80c6
de80c6
get_kdump_confs()
de80c6
{
de80c6
    local config_opt config_val
de80c6
de80c6
    while read config_opt config_val;
de80c6
    do
de80c6
        # remove inline comments after the end of a directive.
de80c6
        config_val=$(strip_comments $config_val)
de80c6
        case "$config_opt" in
de80c6
            path)
de80c6
                KDUMP_PATH="$config_val"
de80c6
            ;;
de80c6
            core_collector)
de80c6
                [ -n "$config_val" ] && CORE_COLLECTOR="$config_val"
de80c6
            ;;
de80c6
            sshkey)
de80c6
                if [ -f "$config_val" ]; then
de80c6
                    SSH_KEY_LOCATION=$config_val
de80c6
                fi
de80c6
            ;;
de80c6
            kdump_pre)
de80c6
                KDUMP_PRE="$config_val"
de80c6
            ;;
de80c6
            kdump_post)
de80c6
                KDUMP_POST="$config_val"
de80c6
            ;;
de80c6
            fence_kdump_args)
de80c6
                FENCE_KDUMP_ARGS="$config_val"
de80c6
            ;;
de80c6
            fence_kdump_nodes)
de80c6
                FENCE_KDUMP_NODES="$config_val"
de80c6
            ;;
de80c6
            default)
de80c6
                case $config_val in
de80c6
                    shell)
de80c6
                        DEFAULT_ACTION="kdump_emergency_shell"
de80c6
                    ;;
de80c6
                    reboot)
de80c6
                        DEFAULT_ACTION="reboot"
de80c6
                    ;;
de80c6
                    halt)
de80c6
                        DEFAULT_ACTION="halt"
de80c6
                    ;;
de80c6
                    poweroff)
de80c6
                        DEFAULT_ACTION="poweroff"
de80c6
                    ;;
de80c6
                    dump_to_rootfs)
de80c6
                        DEFAULT_ACTION="dump_to_rootfs"
de80c6
                    ;;
de80c6
                esac
de80c6
            ;;
de80c6
        esac
de80c6
    done < $KDUMP_CONF
de80c6
de80c6
    if [ -z "$CORE_COLLECTOR" ]; then
de80c6
        CORE_COLLECTOR="$DEFAULT_CORE_COLLECTOR"
de80c6
        if is_ssh_dump_target || is_raw_dump_target; then
de80c6
            CORE_COLLECTOR="$CORE_COLLECTOR -F"
de80c6
        fi
de80c6
    fi
de80c6
}
de80c6
de80c6
# dump_fs <mount point| device>
de80c6
dump_fs()
de80c6
{
de80c6
de80c6
    local _dev=$(findmnt -k -f -n -r -o SOURCE $1)
de80c6
    local _mp=$(findmnt -k -f -n -r -o TARGET $1)
de80c6
de80c6
    echo "kdump: dump target is $_dev"
de80c6
de80c6
    if [ -z "$_mp" ]; then
de80c6
        echo "kdump: error: Dump target $_dev is not mounted."
de80c6
        return 1
de80c6
    fi
de80c6
de80c6
    # Remove -F in makedumpfile case. We don't want a flat format dump here.
de80c6
    [[ $CORE_COLLECTOR = *makedumpfile* ]] && CORE_COLLECTOR=`echo $CORE_COLLECTOR | sed -e "s/-F//g"`
de80c6
de80c6
    echo "kdump: saving to $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
de80c6
de80c6
    mount -o remount,rw $_mp || return 1
de80c6
    mkdir -p $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR || return 1
de80c6
de80c6
    save_vmcore_dmesg_fs ${DMESG_COLLECTOR} "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
de80c6
de80c6
    echo "kdump: saving vmcore"
de80c6
    $CORE_COLLECTOR /proc/vmcore $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete || return 1
de80c6
    mv $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore
de80c6
    sync
de80c6
de80c6
    echo "kdump: saving vmcore complete"
de80c6
}
de80c6
de80c6
save_vmcore_dmesg_fs() {
de80c6
    local _dmesg_collector=$1
de80c6
    local _path=$2
de80c6
de80c6
    echo "kdump: saving vmcore-dmesg.txt"
de80c6
    $_dmesg_collector /proc/vmcore > ${_path}/vmcore-dmesg-incomplete.txt
de80c6
    _exitcode=$?
de80c6
    if [ $_exitcode -eq 0 ]; then
de80c6
        mv ${_path}/vmcore-dmesg-incomplete.txt ${_path}/vmcore-dmesg.txt
de80c6
de80c6
        # Make sure file is on disk. There have been instances where later
de80c6
        # saving vmcore failed and system rebooted without sync and there
de80c6
        # was no vmcore-dmesg.txt available.
de80c6
        sync
de80c6
        echo "kdump: saving vmcore-dmesg.txt complete"
de80c6
    else
de80c6
        echo "kdump: saving vmcore-dmesg.txt failed"
de80c6
    fi
de80c6
}
de80c6
de80c6
dump_to_rootfs()
de80c6
{
de80c6
de80c6
    echo "Kdump: trying to bring up rootfs device"
de80c6
    systemctl start dracut-initqueue
de80c6
    echo "Kdump: waiting for rootfs mount, will timeout after 90 seconds"
de80c6
    systemctl start sysroot.mount
de80c6
de80c6
    dump_fs $NEWROOT
de80c6
}
de80c6
de80c6
kdump_emergency_shell()
de80c6
{
de80c6
    echo "PS1=\"kdump:\\\${PWD}# \"" >/etc/profile
de80c6
    /bin/dracut-emergency
de80c6
    rm -f /etc/profile
de80c6
}
de80c6
de80c6
do_default_action()
de80c6
{
de80c6
    echo "Kdump: Executing default action $DEFAULT_ACTION"
de80c6
    eval $DEFAULT_ACTION
de80c6
}
de80c6
de80c6
do_final_action()
de80c6
{
de80c6
    eval $FINAL_ACTION
de80c6
}