b9e861
#!/bin/sh
b9e861
b9e861
# continue here only if we have to save dump.
f4cf83
if [ -f /etc/fadump.initramfs ] && [ ! -f /proc/device-tree/rtas/ibm,kernel-dump ] && [ ! -f /proc/device-tree/ibm,opal/dump/mpipl-boot ]; then
f4cf83
    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
54b5ae
        derror "saving vmcore failed"
b9e861
    fi
b9e861
b9e861
    return $_ret
b9e861
}
b9e861
b9e861
do_kdump_pre()
b9e861
{
a6b1d2
    local _ret
a6b1d2
b9e861
    if [ -n "$KDUMP_PRE" ]; then
b9e861
        "$KDUMP_PRE"
a6b1d2
        _ret=$?
a6b1d2
        if [ $_ret -ne 0 ]; then
54b5ae
            derror "$KDUMP_PRE exited with $_ret status"
a6b1d2
            return $_ret
a6b1d2
        fi
b9e861
    fi
a6b1d2
e7b8b8
    # if any script fails, it just raises warning and continues
a6b1d2
    if [ -d /etc/kdump/pre.d ]; then
a6b1d2
        for file in /etc/kdump/pre.d/*; do
a6b1d2
            "$file"
a6b1d2
            _ret=$?
a6b1d2
            if [ $_ret -ne 0 ]; then
54b5ae
                derror "$file exited with $_ret status"
a6b1d2
            fi
a6b1d2
        done
a6b1d2
    fi
a6b1d2
    return 0
b9e861
}
b9e861
b9e861
do_kdump_post()
b9e861
{
a6b1d2
    local _ret
a6b1d2
a6b1d2
    if [ -d /etc/kdump/post.d ]; then
a6b1d2
        for file in /etc/kdump/post.d/*; do
a6b1d2
            "$file" "$1"
a6b1d2
            _ret=$?
a6b1d2
            if [ $_ret -ne 0 ]; then
54b5ae
                derror "$file exited with $_ret status"
a6b1d2
            fi
a6b1d2
        done
a6b1d2
    fi
a6b1d2
b9e861
    if [ -n "$KDUMP_POST" ]; then
b9e861
        "$KDUMP_POST" "$1"
a6b1d2
        _ret=$?
a6b1d2
        if [ $_ret -ne 0 ]; then
54b5ae
            derror "$KDUMP_POST exited with $_ret status"
a6b1d2
        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
54b5ae
    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
54b5ae
    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
54b5ae
    dinfo "saving vmcore complete"
b9e861
    return 0
b9e861
}
b9e861
b9e861
dump_ssh()
b9e861
{
bb7919
    local _ret=0
bb7919
    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
bb7919
    local _vmcore="vmcore"
fd10de
    local _ipv6_addr="" _username=""
b9e861
54b5ae
    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
f4cf83
    save_opalcore_ssh ${_dir} "${_opt}" $_host
b9e861
54b5ae
    dinfo "saving vmcore"
b9e861
fd10de
    if is_ipv6_address "$_host"; then
fd10de
        _username=${_host%@*}
fd10de
        _ipv6_addr="[${_host#*@}]"
fd10de
    fi
fd10de
b9e861
    if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then
fd10de
        if [ -n "$_username" ] && [ -n "$_ipv6_addr" ]; then
fd10de
            scp -q $_opt /proc/vmcore "$_username@$_ipv6_addr:$_dir/vmcore-incomplete"
fd10de
        else
fd10de
            scp -q $_opt /proc/vmcore "$_host:$_dir/vmcore-incomplete"
fd10de
        fi
bb7919
        _exitcode=$?
b9e861
    else
5b78bd
        $CORE_COLLECTOR /proc/vmcore | ssh $_opt $_host "umask 0077 && dd bs=512 of=$_dir/vmcore-incomplete"
bb7919
        _exitcode=$?
bb7919
        _vmcore="vmcore.flat"
bb7919
    fi
bb7919
bb7919
    if [ $_exitcode -eq 0 ]; then
bb7919
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/$_vmcore"
bb7919
        _exitcode2=$?
bb7919
        if [ $_exitcode2 -ne 0 ]; then
bb7919
            derror "moving vmcore failed, _exitcode:$_exitcode2"
bb7919
        else
bb7919
            dinfo "saving vmcore complete"
54b5ae
        fi
bb7919
    else
bb7919
        derror "saving vmcore failed, _exitcode:$_exitcode"
bb7919
    fi
bb7919
fd10de
    dinfo "saving the $KDUMP_LOG_FILE to $_host:$_dir/"
bb7919
    save_log
fd10de
    if [ -n "$_username" ] && [ -n "$_ipv6_addr" ]; then
fd10de
        scp -q $_opt $KDUMP_LOG_FILE "$_username@$_ipv6_addr:$_dir/"
fd10de
    else
fd10de
        scp -q $_opt $KDUMP_LOG_FILE "$_host:$_dir/"
fd10de
    fi
bb7919
    _ret=$?
bb7919
    if [ $_ret -ne 0 ]; then
bb7919
        derror "saving log file failed, _exitcode:$_ret"
bb7919
    fi
bb7919
bb7919
    if [ $_exitcode -ne 0 ] || [ $_exitcode2 -ne 0 ];then
bb7919
        return 1
b9e861
    fi
b9e861
b9e861
    return 0
b9e861
}
b9e861
f4cf83
save_opalcore_ssh() {
f4cf83
    local _path=$1
f4cf83
    local _opts="$2"
f4cf83
    local _location=$3
fd10de
    local _user_name="" _ipv6addr=""
f4cf83
54b5ae
    ddebug "_path=$_path _opts=$_opts _location=$_location"
54b5ae
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
fd10de
    if is_ipv6_address "$_host"; then
fd10de
        _user_name=${_location%@*}
fd10de
        _ipv6addr="[${_location#*@}]"
fd10de
    fi
fd10de
54b5ae
    dinfo "saving opalcore:$OPALCORE to $_location:$_path"
fd10de
fd10de
    if [ -n "$_user_name" ] && [ -n "$_ipv6addr" ]; then
fd10de
        scp $_opts $OPALCORE $_user_name@$_ipv6addr:$_path/opalcore-incomplete
fd10de
    else
fd10de
        scp $_opts $OPALCORE $_location:$_path/opalcore-incomplete
fd10de
    fi
f4cf83
    if [ $? -ne 0 ]; then
54b5ae
        derror "saving opalcore failed"
f4cf83
       return 1
f4cf83
    fi
f4cf83
f4cf83
    ssh $_opts $_location mv $_path/opalcore-incomplete $_path/opalcore
54b5ae
    dinfo "saving opalcore complete"
f4cf83
    return 0
f4cf83
}
f4cf83
b9e861
save_vmcore_dmesg_ssh() {
b9e861
    local _dmesg_collector=$1
b9e861
    local _path=$2
b9e861
    local _opts="$3"
b9e861
    local _location=$4
b9e861
54b5ae
    dinfo "saving vmcore-dmesg.txt to $_location:$_path"
5b78bd
    $_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
54b5ae
        dinfo "saving vmcore-dmesg.txt complete"
b9e861
    else
54b5ae
        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=)
54b5ae
        [ -z "$kdumpnic" ] && derror "failed to get kdumpnic!" && return 1
b9e861
        _host=`ip addr show dev $kdumpnic|grep '[ ]*inet'`
54b5ae
        [ $? -ne 0 ] && derror "wrong kdumpnic: $kdumpnic" && return 1
b9e861
        _host=`echo $_host | head -n 1 | cut -d' ' -f2`
b9e861
        _host="${_host%%/*}"
54b5ae
        [ -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
54b5ae
        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")
a6b1d2
            if [ -n "$config_val" ]; then
a6b1d2
                config_val=$(get_mntpoint_from_target "$config_val")
a6b1d2
                add_dump_code "dump_fs $config_val"
a6b1d2
            fi
b9e861
            ;;
b9e861
        ext[234]|xfs|btrfs|minix|nfs)
399b37
            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
54b5ae
    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
54b5ae
    derror "kdump_pre script exited with non-zero status!"
b9e861
    do_final_action
e7b8b8
    # During systemd service to reboot the machine, stop this shell script running
e7b8b8
    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
54b5ae
    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