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