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