893e0b
# These variables and functions are useful in 2nd kernel
893e0b
893e0b
. /lib/kdump-lib.sh
8f4abc
. /lib/kdump-logger.sh
893e0b
893e0b
KDUMP_PATH="/var/crash"
8f4abc
KDUMP_LOG_FILE="/run/initramfs/kexec-dmesg.log"
893e0b
CORE_COLLECTOR=""
8f4abc
DEFAULT_CORE_COLLECTOR="makedumpfile -l --message-level 7 -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
8f4abc
#initiate the kdump logger
8f4abc
dlog_init
8f4abc
if [ $? -ne 0 ]; then
8f4abc
    echo "failed to initiate the kdump logger."
8f4abc
    exit 1
8f4abc
fi
8f4abc
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
8f4abc
# store the kexec kernel log to a file.
8f4abc
save_log()
8f4abc
{
8f4abc
    dmesg -T > $KDUMP_LOG_FILE
8f4abc
8f4abc
    if command -v journalctl > /dev/null; then
8f4abc
        journalctl -ab >> $KDUMP_LOG_FILE
8f4abc
    fi
d43fe6
    chmod 600 $KDUMP_LOG_FILE
8f4abc
}
8f4abc
f8bec6
# dump_fs <mount point>
893e0b
dump_fs()
893e0b
{
8f4abc
    local _exitcode
f8bec6
    local _mp=$1
8f4abc
    ddebug "dump_fs _mp=$_mp"
8f4abc
8f4abc
    if ! is_mounted "$_mp"; then
8f4abc
        dinfo "dump path \"$_mp\" is not mounted, trying to mount..."
8f4abc
        mount --target $_mp
8f4abc
        if [ $? -ne 0 ]; then
8f4abc
            derror "failed to dump to \"$_mp\", it's not a mount point!"
8f4abc
            return 1
8c3ceb
        fi
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
8f4abc
    dinfo "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
8f4abc
    dinfo "saving vmcore"
8f4abc
    $CORE_COLLECTOR /proc/vmcore $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete
8f4abc
    _exitcode=$?
8f4abc
    if [ $_exitcode -eq 0 ]; then
063c48
        sync -f "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete"
063c48
        _sync_exitcode=$?
063c48
        if [ $_sync_exitcode -eq 0 ]; then
063c48
            mv "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore-incomplete" "$_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/vmcore"
063c48
            dinfo "saving vmcore complete"
063c48
        else
063c48
            derror "sync vmcore failed, _exitcode:$_sync_exitcode"
063c48
            return 1
063c48
        fi
8f4abc
    else
8f4abc
        derror "saving vmcore failed, _exitcode:$_exitcode"
8f4abc
    fi
893e0b
8f4abc
    dinfo "saving the $KDUMP_LOG_FILE to $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/"
8f4abc
    save_log
8f4abc
    mv $KDUMP_LOG_FILE $_mp/$KDUMP_PATH/$HOST_IP-$DATEDIR/
8f4abc
    if [ $_exitcode -ne 0 ]; then
8f4abc
        return 1
8f4abc
    fi
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
8f4abc
    dinfo "saving vmcore-dmesg.txt to ${_path}"
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
d43fe6
        chmod 600 ${_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
8f4abc
        dinfo "saving vmcore-dmesg.txt complete"
893e0b
    else
8f4abc
        derror "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
8f4abc
    dinfo "saving opalcore:$OPALCORE to ${_path}/opalcore"
7a865b
    cp $OPALCORE ${_path}/opalcore
7a865b
    if [ $? -ne 0 ]; then
8f4abc
        derror "saving opalcore failed"
7a865b
        return 1
7a865b
    fi
7a865b
7a865b
    sync
8f4abc
    dinfo "saving opalcore complete"
7a865b
    return 0
7a865b
}
7a865b
893e0b
dump_to_rootfs()
893e0b
{
893e0b
8f4abc
    dinfo "Trying to bring up rootfs device"
893e0b
    systemctl start dracut-initqueue
8f4abc
    dinfo "Waiting for rootfs mount, will timeout after 90 seconds"
893e0b
    systemctl start sysroot.mount
893e0b
8f4abc
    ddebug "NEWROOT=$NEWROOT"
8f4abc
893e0b
    dump_fs $NEWROOT
893e0b
}
893e0b
893e0b
kdump_emergency_shell()
893e0b
{
893e0b
    echo "PS1=\"kdump:\\\${PWD}# \"" >/etc/profile
8f4abc
    ddebug "Switching to dracut emergency..."
893e0b
    /bin/dracut-emergency
893e0b
    rm -f /etc/profile
893e0b
}
893e0b
603de6
do_failure_action()
893e0b
{
8f4abc
    dinfo "Executing failure action $FAILURE_ACTION"
603de6
    eval $FAILURE_ACTION
893e0b
}
893e0b
893e0b
do_final_action()
893e0b
{
8f4abc
    dinfo "Executing final action $FINAL_ACTION"
893e0b
    eval $FINAL_ACTION
893e0b
}