893e0b
#!/bin/sh
893e0b
893e0b
# continue here only if we have to save dump.
893e0b
if [ -f /etc/fadump.initramfs ] && [ ! -f /proc/device-tree/rtas/ibm,kernel-dump ]; then
893e0b
	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
{
893e0b
    if [ -n "$KDUMP_PRE" ]; then
893e0b
        "$KDUMP_PRE"
893e0b
    fi
893e0b
}
893e0b
893e0b
do_kdump_post()
893e0b
{
893e0b
    if [ -n "$KDUMP_POST" ]; then
893e0b
        "$KDUMP_POST" "$1"
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
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
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
        config_val=$(strip_comments $config_val)
893e0b
        case "$config_opt" in
893e0b
        dracut_args)
893e0b
            config_val=$(get_dracut_args_target "$config_val")
893e0b
            [ -n "$config_val" ] && add_dump_code "dump_fs $config_val"
893e0b
            ;;
893e0b
        ext[234]|xfs|btrfs|minix|nfs)
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
893e0b
    done < $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
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