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