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