893e0b
#!/bin/sh
893e0b
893e0b
# continue here only if we have to save dump.
7a865b
if [ -f /etc/fadump.initramfs ] && [ ! -f /proc/device-tree/rtas/ibm,kernel-dump ] && [ ! -f /proc/device-tree/ibm,opal/dump/mpipl-boot ]; then
7a865b
    exit 0
893e0b
fi
893e0b
893e0b
. /lib/dracut-lib.sh
893e0b
. /lib/kdump-lib-initramfs.sh
893e0b
893e0b
set -o pipefail
893e0b
DUMP_RETVAL=0
893e0b
893e0b
export PATH=$PATH:$KDUMP_SCRIPT_DIR
893e0b
893e0b
do_dump()
893e0b
{
893e0b
    local _ret
893e0b
893e0b
    eval $DUMP_INSTRUCTION
893e0b
    _ret=$?
893e0b
893e0b
    if [ $_ret -ne 0 ]; then
8f4abc
        derror "saving vmcore failed"
893e0b
    fi
893e0b
893e0b
    return $_ret
893e0b
}
893e0b
893e0b
do_kdump_pre()
893e0b
{
f8bec6
    local _ret
f8bec6
893e0b
    if [ -n "$KDUMP_PRE" ]; then
893e0b
        "$KDUMP_PRE"
f8bec6
        _ret=$?
f8bec6
        if [ $_ret -ne 0 ]; then
8f4abc
            derror "$KDUMP_PRE exited with $_ret status"
f8bec6
            return $_ret
f8bec6
        fi
893e0b
    fi
f8bec6
f8bec6
    # if any script fails, it just raises warning and continues
f8bec6
    if [ -d /etc/kdump/pre.d ]; then
f8bec6
        for file in /etc/kdump/pre.d/*; do
f8bec6
            "$file"
f8bec6
            _ret=$?
f8bec6
            if [ $_ret -ne 0 ]; then
8f4abc
                derror "$file exited with $_ret status"
f8bec6
            fi
f8bec6
        done
f8bec6
    fi
f8bec6
    return 0
893e0b
}
893e0b
893e0b
do_kdump_post()
893e0b
{
f8bec6
    local _ret
f8bec6
f8bec6
    if [ -d /etc/kdump/post.d ]; then
f8bec6
        for file in /etc/kdump/post.d/*; do
f8bec6
            "$file" "$1"
f8bec6
            _ret=$?
f8bec6
            if [ $_ret -ne 0 ]; then
8f4abc
                derror "$file exited with $_ret status"
f8bec6
            fi
f8bec6
        done
f8bec6
    fi
f8bec6
893e0b
    if [ -n "$KDUMP_POST" ]; then
893e0b
        "$KDUMP_POST" "$1"
f8bec6
        _ret=$?
f8bec6
        if [ $_ret -ne 0 ]; then
8f4abc
            derror "$KDUMP_POST exited with $_ret status"
f8bec6
        fi
893e0b
    fi
893e0b
}
893e0b
893e0b
add_dump_code()
893e0b
{
893e0b
    DUMP_INSTRUCTION=$1
893e0b
}
893e0b
893e0b
dump_raw()
893e0b
{
893e0b
    local _raw=$1
893e0b
893e0b
    [ -b "$_raw" ] || return 1
893e0b
8f4abc
    dinfo "saving to raw disk $_raw"
893e0b
893e0b
    if ! $(echo -n $CORE_COLLECTOR|grep -q makedumpfile); then
893e0b
        _src_size=`ls -l /proc/vmcore | cut -d' ' -f5`
893e0b
        _src_size_mb=$(($_src_size / 1048576))
893e0b
        monitor_dd_progress $_src_size_mb &
893e0b
    fi
893e0b
8f4abc
    dinfo "saving vmcore"
893e0b
    $CORE_COLLECTOR /proc/vmcore | dd of=$_raw bs=$DD_BLKSIZE >> /tmp/dd_progress_file 2>&1 || return 1
893e0b
    sync
893e0b
8f4abc
    dinfo "saving vmcore complete"
893e0b
    return 0
893e0b
}
893e0b
893e0b
dump_ssh()
893e0b
{
8f4abc
    local _ret=0
8f4abc
    local _exitcode=0 _exitcode2=0
893e0b
    local _opt="-i $1 -o BatchMode=yes -o StrictHostKeyChecking=yes"
893e0b
    local _dir="$KDUMP_PATH/$HOST_IP-$DATEDIR"
893e0b
    local _host=$2
8f4abc
    local _vmcore="vmcore"
8f4abc
    local _ipv6_addr="" _username=""
893e0b
8f4abc
    dinfo "saving to $_host:$_dir"
893e0b
893e0b
    cat /var/lib/random-seed > /dev/urandom
893e0b
    ssh -q $_opt $_host mkdir -p $_dir || return 1
893e0b
893e0b
    save_vmcore_dmesg_ssh ${DMESG_COLLECTOR} ${_dir} "${_opt}" $_host
7a865b
    save_opalcore_ssh ${_dir} "${_opt}" $_host
893e0b
8f4abc
    dinfo "saving vmcore"
8f4abc
8f4abc
    if is_ipv6_address "$_host"; then
8f4abc
        _username=${_host%@*}
8f4abc
        _ipv6_addr="[${_host#*@}]"
8f4abc
    fi
893e0b
893e0b
    if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then
8f4abc
        if [ -n "$_username" ] && [ -n "$_ipv6_addr" ]; then
8f4abc
            scp -q $_opt /proc/vmcore "$_username@$_ipv6_addr:$_dir/vmcore-incomplete"
8f4abc
        else
8f4abc
            scp -q $_opt /proc/vmcore "$_host:$_dir/vmcore-incomplete"
8f4abc
        fi
8f4abc
        _exitcode=$?
8f4abc
    else
8f4abc
        $CORE_COLLECTOR /proc/vmcore | ssh $_opt $_host "dd bs=512 of=$_dir/vmcore-incomplete"
8f4abc
        _exitcode=$?
8f4abc
        _vmcore="vmcore.flat"
8f4abc
    fi
8f4abc
8f4abc
    if [ $_exitcode -eq 0 ]; then
8f4abc
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/$_vmcore"
8f4abc
        _exitcode2=$?
8f4abc
        if [ $_exitcode2 -ne 0 ]; then
8f4abc
            derror "moving vmcore failed, _exitcode:$_exitcode2"
8f4abc
        else
8f4abc
            dinfo "saving vmcore complete"
8f4abc
        fi
8f4abc
    else
8f4abc
        derror "saving vmcore failed, _exitcode:$_exitcode"
8f4abc
    fi
8f4abc
8f4abc
    dinfo "saving the $KDUMP_LOG_FILE to $_host:$_dir/"
8f4abc
    save_log
8f4abc
    if [ -n "$_username" ] && [ -n "$_ipv6_addr" ]; then
8f4abc
        scp -q $_opt $KDUMP_LOG_FILE "$_username@$_ipv6_addr:$_dir/"
893e0b
    else
8f4abc
        scp -q $_opt $KDUMP_LOG_FILE "$_host:$_dir/"
8f4abc
    fi
8f4abc
    _ret=$?
8f4abc
    if [ $_ret -ne 0 ]; then
8f4abc
        derror "saving log file failed, _exitcode:$_ret"
8f4abc
    fi
8f4abc
8f4abc
    if [ $_exitcode -ne 0 ] || [ $_exitcode2 -ne 0 ];then
8f4abc
        return 1
893e0b
    fi
893e0b
893e0b
    return 0
893e0b
}
893e0b
7a865b
save_opalcore_ssh() {
7a865b
    local _path=$1
7a865b
    local _opts="$2"
7a865b
    local _location=$3
8f4abc
    local _user_name="" _ipv6addr=""
8f4abc
8f4abc
    ddebug "_path=$_path _opts=$_opts _location=$_location"
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
    if is_ipv6_address "$_host"; then
8f4abc
        _user_name=${_location%@*}
8f4abc
        _ipv6addr="[${_location#*@}]"
8f4abc
    fi
8f4abc
8f4abc
    dinfo "saving opalcore:$OPALCORE to $_location:$_path"
8f4abc
8f4abc
    if [ -n "$_user_name" ] && [ -n "$_ipv6addr" ]; then
8f4abc
        scp $_opts $OPALCORE $_user_name@$_ipv6addr:$_path/opalcore-incomplete
8f4abc
    else
8f4abc
        scp $_opts $OPALCORE $_location:$_path/opalcore-incomplete
8f4abc
    fi
7a865b
    if [ $? -ne 0 ]; then
8f4abc
        derror "saving opalcore failed"
7a865b
       return 1
7a865b
    fi
7a865b
7a865b
    ssh $_opts $_location mv $_path/opalcore-incomplete $_path/opalcore
8f4abc
    dinfo "saving opalcore complete"
7a865b
    return 0
7a865b
}
7a865b
893e0b
save_vmcore_dmesg_ssh() {
893e0b
    local _dmesg_collector=$1
893e0b
    local _path=$2
893e0b
    local _opts="$3"
893e0b
    local _location=$4
893e0b
8f4abc
    dinfo "saving vmcore-dmesg.txt to $_location:$_path"
893e0b
    $_dmesg_collector /proc/vmcore | ssh $_opts $_location "dd of=$_path/vmcore-dmesg-incomplete.txt"
893e0b
    _exitcode=$?
893e0b
893e0b
    if [ $_exitcode -eq 0 ]; then
893e0b
        ssh -q $_opts $_location mv $_path/vmcore-dmesg-incomplete.txt $_path/vmcore-dmesg.txt
8f4abc
        dinfo "saving vmcore-dmesg.txt complete"
893e0b
    else
8f4abc
        derror "saving vmcore-dmesg.txt failed"
893e0b
    fi
893e0b
}
893e0b
893e0b
get_host_ip()
893e0b
{
893e0b
    local _host
893e0b
    if is_nfs_dump_target || is_ssh_dump_target
893e0b
    then
893e0b
        kdumpnic=$(getarg kdumpnic=)
8f4abc
        [ -z "$kdumpnic" ] && derror "failed to get kdumpnic!" && return 1
893e0b
        _host=`ip addr show dev $kdumpnic|grep '[ ]*inet'`
8f4abc
        [ $? -ne 0 ] && derror "wrong kdumpnic: $kdumpnic" && return 1
893e0b
        _host=`echo $_host | head -n 1 | cut -d' ' -f2`
893e0b
        _host="${_host%%/*}"
8f4abc
        [ -z "$_host" ] && derror "wrong kdumpnic: $kdumpnic" && return 1
893e0b
        HOST_IP=$_host
893e0b
    fi
893e0b
    return 0
893e0b
}
893e0b
893e0b
read_kdump_conf()
893e0b
{
893e0b
    if [ ! -f "$KDUMP_CONF" ]; then
8f4abc
        derror "$KDUMP_CONF not found"
893e0b
        return
893e0b
    fi
893e0b
893e0b
    get_kdump_confs
893e0b
893e0b
    # rescan for add code for dump target
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
        dracut_args)
893e0b
            config_val=$(get_dracut_args_target "$config_val")
f8bec6
            if [ -n "$config_val" ]; then
f8bec6
                config_val=$(get_mntpoint_from_target "$config_val")
f8bec6
                add_dump_code "dump_fs $config_val"
f8bec6
            fi
893e0b
            ;;
893e0b
        ext[234]|xfs|btrfs|minix|nfs)
f8bec6
            config_val=$(get_mntpoint_from_target "$config_val")
893e0b
            add_dump_code "dump_fs $config_val"
893e0b
            ;;
893e0b
        raw)
893e0b
            add_dump_code "dump_raw $config_val"
893e0b
            ;;
893e0b
        ssh)
893e0b
            add_dump_code "dump_ssh $SSH_KEY_LOCATION $config_val"
893e0b
            ;;
893e0b
        esac
603de6
    done <<< "$(read_strip_comments $KDUMP_CONF)"
893e0b
}
893e0b
893e0b
fence_kdump_notify()
893e0b
{
893e0b
    if [ -n "$FENCE_KDUMP_NODES" ]; then
893e0b
        $FENCE_KDUMP_SEND $FENCE_KDUMP_ARGS $FENCE_KDUMP_NODES &
893e0b
    fi
893e0b
}
893e0b
893e0b
read_kdump_conf
893e0b
fence_kdump_notify
893e0b
893e0b
get_host_ip
893e0b
if [ $? -ne 0 ]; then
8f4abc
    derror "get_host_ip exited with non-zero status!"
893e0b
    exit 1
893e0b
fi
893e0b
893e0b
if [ -z "$DUMP_INSTRUCTION" ]; then
893e0b
    add_dump_code "dump_fs $NEWROOT"
893e0b
fi
893e0b
893e0b
do_kdump_pre
893e0b
if [ $? -ne 0 ]; then
8f4abc
    derror "kdump_pre script exited with non-zero status!"
893e0b
    do_final_action
f8bec6
    # During systemd service to reboot the machine, stop this shell script running
f8bec6
    exit 1
893e0b
fi
893e0b
make_trace_mem "kdump saving vmcore" '1:shortmem' '2+:mem' '3+:slab'
893e0b
do_dump
893e0b
DUMP_RETVAL=$?
893e0b
893e0b
do_kdump_post $DUMP_RETVAL
893e0b
if [ $? -ne 0 ]; then
8f4abc
    derror "kdump_post script exited with non-zero status!"
893e0b
fi
893e0b
893e0b
if [ $DUMP_RETVAL -ne 0 ]; then
893e0b
    exit 1
893e0b
fi
893e0b
893e0b
do_final_action