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"
f4cf83
OPALCORE="/sys/firmware/opal/mpipl/core"
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
399b37
# dump_fs <mount point>
b9e861
dump_fs()
b9e861
{
399b37
    local _mp=$1
399b37
    local _dev=$(get_mount_info SOURCE target $_mp -f)
399b37
    local _op=$(get_mount_info OPTIONS target $_mp -f)
b9e861
399b37
    # If dump path have a corresponding device entry but not mounted, mount it.
399b37
    if [ -n "$_dev" ] || [ "$_dev" == "rootfs" ]; then
399b37
        if ! is_mounted "$_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
        fi
bda30f
    else
399b37
        echo "kdump: failed to dump to \"$_mp\", it's not a mount point!"
399b37
        return 1
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/"
f4cf83
    save_opalcore_fs "$_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
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
f4cf83
save_opalcore_fs() {
f4cf83
    local _path=$1
f4cf83
f4cf83
    if [ ! -f $OPALCORE ]; then
f4cf83
        # Check if we are on an old kernel that uses a different path
f4cf83
        if [ -f /sys/firmware/opal/core ]; then
f4cf83
            OPALCORE="/sys/firmware/opal/core"
f4cf83
        else
f4cf83
            return 0
f4cf83
        fi
f4cf83
    fi
f4cf83
f4cf83
    echo "kdump: saving opalcore"
f4cf83
    cp $OPALCORE ${_path}/opalcore
f4cf83
    if [ $? -ne 0 ]; then
f4cf83
        echo "kdump: saving opalcore failed"
f4cf83
        return 1
f4cf83
    fi
f4cf83
f4cf83
    sync
f4cf83
    echo "kdump: saving opalcore complete"
f4cf83
    return 0
f4cf83
}
f4cf83
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
}