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"
603de6
FAILURE_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"
7a865b
OPALCORE="/sys/firmware/opal/mpipl/core"
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
        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
            ;;
603de6
            failure_action|default)
893e0b
                case $config_val in
893e0b
                    shell)
603de6
                        FAILURE_ACTION="kdump_emergency_shell"
893e0b
                    ;;
893e0b
                    reboot)
8c3ceb
                        FAILURE_ACTION="systemctl reboot -f && exit"
893e0b
                    ;;
893e0b
                    halt)
8c3ceb
                        FAILURE_ACTION="halt && exit"
893e0b
                    ;;
893e0b
                    poweroff)
8c3ceb
                        FAILURE_ACTION="systemctl poweroff -f && exit"
893e0b
                    ;;
893e0b
                    dump_to_rootfs)
603de6
                        FAILURE_ACTION="dump_to_rootfs"
603de6
                    ;;
603de6
                esac
603de6
            ;;
603de6
            final_action)
603de6
                case $config_val in
603de6
                    reboot)
603de6
                        FINAL_ACTION="systemctl reboot -f"
603de6
                    ;;
603de6
                    halt)
603de6
                        FINAL_ACTION="halt"
603de6
                    ;;
603de6
                    poweroff)
603de6
                        FINAL_ACTION="systemctl poweroff -f"
893e0b
                    ;;
893e0b
                esac
893e0b
            ;;
893e0b
        esac
603de6
    done <<< "$(read_strip_comments $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
f8bec6
# dump_fs <mount point>
893e0b
dump_fs()
893e0b
{
f8bec6
    local _mp=$1
f8bec6
    local _dev=$(get_mount_info SOURCE target $_mp -f)
f8bec6
    local _op=$(get_mount_info OPTIONS target $_mp -f)
893e0b
f8bec6
    # If dump path have a corresponding device entry but not mounted, mount it.
f8bec6
    if [ -n "$_dev" ] || [ "$_dev" == "rootfs" ]; then
f8bec6
        if ! is_mounted "$_mp"; then
8c3ceb
            echo "kdump: dump target $_dev is not mounted, trying to mount..."
8c3ceb
            mkdir -p $_mp
8c3ceb
            mount -o $_op $_dev $_mp
8c3ceb
8c3ceb
            if [ $? -ne 0 ]; then
8c3ceb
                echo "kdump: mounting failed (mount point: $_mp, option: $_op)"
8c3ceb
                return 1
8c3ceb
            fi
8c3ceb
        fi
8c3ceb
    else
f8bec6
        echo "kdump: failed to dump to \"$_mp\", it's not a mount point!"
f8bec6
        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/"
7a865b
    save_opalcore_fs "$_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"
8c3ceb
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
7a865b
save_opalcore_fs() {
7a865b
    local _path=$1
7a865b
7a865b
    if [ ! -f $OPALCORE ]; then
7a865b
        # Check if we are on an old kernel that uses a different path
7a865b
        if [ -f /sys/firmware/opal/core ]; then
7a865b
            OPALCORE="/sys/firmware/opal/core"
7a865b
        else
7a865b
            return 0
7a865b
        fi
7a865b
    fi
7a865b
7a865b
    echo "kdump: saving opalcore"
7a865b
    cp $OPALCORE ${_path}/opalcore
7a865b
    if [ $? -ne 0 ]; then
7a865b
        echo "kdump: saving opalcore failed"
7a865b
        return 1
7a865b
    fi
7a865b
7a865b
    sync
7a865b
    echo "kdump: saving opalcore complete"
7a865b
    return 0
7a865b
}
7a865b
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
603de6
do_failure_action()
893e0b
{
603de6
    echo "Kdump: Executing failure action $FAILURE_ACTION"
603de6
    eval $FAILURE_ACTION
893e0b
}
893e0b
893e0b
do_final_action()
893e0b
{
893e0b
    eval $FINAL_ACTION
893e0b
}