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
exec &> /dev/console
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
893e0b
        echo "kdump: 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
f8bec6
            echo "kdump: $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
f8bec6
                echo "kdump: $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
f8bec6
                echo "kdump: $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
f8bec6
            echo "kdump: $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
893e0b
    echo "kdump: 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
893e0b
    echo "kdump: saving vmcore"
893e0b
    $CORE_COLLECTOR /proc/vmcore | dd of=$_raw bs=$DD_BLKSIZE >> /tmp/dd_progress_file 2>&1 || return 1
893e0b
    sync
893e0b
893e0b
    echo "kdump: saving vmcore complete"
893e0b
    return 0
893e0b
}
893e0b
893e0b
dump_ssh()
893e0b
{
893e0b
    local _opt="-i $1 -o BatchMode=yes -o StrictHostKeyChecking=yes"
893e0b
    local _dir="$KDUMP_PATH/$HOST_IP-$DATEDIR"
893e0b
    local _host=$2
893e0b
893e0b
    echo "kdump: 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
893e0b
    echo "kdump: saving vmcore"
893e0b
893e0b
    if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then
893e0b
        scp -q $_opt /proc/vmcore "$_host:$_dir/vmcore-incomplete" || return 1
893e0b
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore" || return 1
893e0b
    else
893e0b
        $CORE_COLLECTOR /proc/vmcore | ssh $_opt $_host "dd bs=512 of=$_dir/vmcore-incomplete" || return 1
893e0b
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore.flat" || return 1
893e0b
    fi
893e0b
893e0b
    echo "kdump: saving vmcore complete"
893e0b
    return 0
893e0b
}
893e0b
7a865b
save_opalcore_ssh() {
7a865b
    local _path=$1
7a865b
    local _opts="$2"
7a865b
    local _location=$3
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
7a865b
    echo "kdump: saving opalcore"
7a865b
    scp $_opts $OPALCORE $_location:$_path/opalcore-incomplete
7a865b
    if [ $? -ne 0 ]; then
7a865b
        echo "kdump: saving opalcore failed"
7a865b
       return 1
7a865b
    fi
7a865b
7a865b
    ssh $_opts $_location mv $_path/opalcore-incomplete $_path/opalcore
7a865b
    echo "kdump: 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
893e0b
    echo "kdump: saving vmcore-dmesg.txt"
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
893e0b
        echo "kdump: saving vmcore-dmesg.txt complete"
893e0b
    else
893e0b
        echo "kdump: 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=)
893e0b
        [ -z "$kdumpnic" ] && echo "kdump: failed to get kdumpnic!" && return 1
893e0b
        _host=`ip addr show dev $kdumpnic|grep '[ ]*inet'`
893e0b
        [ $? -ne 0 ] && echo "kdump: wrong kdumpnic: $kdumpnic" && return 1
893e0b
        _host=`echo $_host | head -n 1 | cut -d' ' -f2`
893e0b
        _host="${_host%%/*}"
893e0b
        [ -z "$_host" ] && echo "kdump: 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
893e0b
        echo "kdump: $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
893e0b
    echo "kdump: 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
893e0b
    echo "kdump: 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
893e0b
    echo "kdump: 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