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