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