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