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