23ef29
#!/bin/sh
23ef29
23ef29
# continue here only if we have to save dump.
23ef29
if [ -f /etc/fadump.initramfs ] && [ ! -f /proc/device-tree/rtas/ibm,kernel-dump ]; then
23ef29
	exit 0
23ef29
fi
23ef29
23ef29
exec &> /dev/console
23ef29
. /lib/dracut-lib.sh
23ef29
. /lib/kdump-lib-initramfs.sh
23ef29
23ef29
set -o pipefail
23ef29
DUMP_RETVAL=0
23ef29
23ef29
export PATH=$PATH:$KDUMP_SCRIPT_DIR
23ef29
23ef29
do_dump()
23ef29
{
23ef29
    local _ret
23ef29
23ef29
    eval $DUMP_INSTRUCTION
23ef29
    _ret=$?
23ef29
23ef29
    if [ $_ret -ne 0 ]; then
23ef29
        echo "kdump: saving vmcore failed"
23ef29
    fi
23ef29
23ef29
    return $_ret
23ef29
}
23ef29
23ef29
do_kdump_pre()
23ef29
{
23ef29
    if [ -n "$KDUMP_PRE" ]; then
23ef29
        "$KDUMP_PRE"
23ef29
    fi
23ef29
}
23ef29
23ef29
do_kdump_post()
23ef29
{
23ef29
    if [ -n "$KDUMP_POST" ]; then
23ef29
        "$KDUMP_POST" "$1"
23ef29
    fi
23ef29
}
23ef29
23ef29
add_dump_code()
23ef29
{
23ef29
    DUMP_INSTRUCTION=$1
23ef29
}
23ef29
23ef29
dump_raw()
23ef29
{
23ef29
    local _raw=$1
23ef29
23ef29
    [ -b "$_raw" ] || return 1
23ef29
23ef29
    echo "kdump: saving to raw disk $_raw"
23ef29
23ef29
    if ! $(echo -n $CORE_COLLECTOR|grep -q makedumpfile); then
23ef29
        _src_size=`ls -l /proc/vmcore | cut -d' ' -f5`
23ef29
        _src_size_mb=$(($_src_size / 1048576))
23ef29
        monitor_dd_progress $_src_size_mb &
23ef29
    fi
23ef29
23ef29
    echo "kdump: saving vmcore"
23ef29
    $CORE_COLLECTOR /proc/vmcore | dd of=$_raw bs=$DD_BLKSIZE >> /tmp/dd_progress_file 2>&1 || return 1
23ef29
    sync
23ef29
23ef29
    echo "kdump: saving vmcore complete"
23ef29
    return 0
23ef29
}
23ef29
23ef29
dump_ssh()
23ef29
{
23ef29
    local _opt="-i $1 -o BatchMode=yes -o StrictHostKeyChecking=yes"
23ef29
    local _dir="$KDUMP_PATH/$HOST_IP-$DATEDIR"
23ef29
    local _host=$2
23ef29
23ef29
    echo "kdump: saving to $_host:$_dir"
23ef29
23ef29
    cat /var/lib/random-seed > /dev/urandom
23ef29
    ssh -q $_opt $_host mkdir -p $_dir || return 1
23ef29
23ef29
    save_vmcore_dmesg_ssh ${DMESG_COLLECTOR} ${_dir} "${_opt}" $_host
23ef29
23ef29
    echo "kdump: saving vmcore"
23ef29
23ef29
    if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then
23ef29
        scp -q $_opt /proc/vmcore "$_host:$_dir/vmcore-incomplete" || return 1
23ef29
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore" || return 1
23ef29
    else
23ef29
        $CORE_COLLECTOR /proc/vmcore | ssh $_opt $_host "dd bs=512 of=$_dir/vmcore-incomplete" || return 1
23ef29
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore.flat" || return 1
23ef29
    fi
23ef29
23ef29
    echo "kdump: saving vmcore complete"
23ef29
    return 0
23ef29
}
23ef29
23ef29
save_vmcore_dmesg_ssh() {
23ef29
    local _dmesg_collector=$1
23ef29
    local _path=$2
23ef29
    local _opts="$3"
23ef29
    local _location=$4
23ef29
23ef29
    echo "kdump: saving vmcore-dmesg.txt"
23ef29
    $_dmesg_collector /proc/vmcore | ssh $_opts $_location "dd of=$_path/vmcore-dmesg-incomplete.txt"
23ef29
    _exitcode=$?
23ef29
23ef29
    if [ $_exitcode -eq 0 ]; then
23ef29
        ssh -q $_opts $_location mv $_path/vmcore-dmesg-incomplete.txt $_path/vmcore-dmesg.txt
23ef29
        echo "kdump: saving vmcore-dmesg.txt complete"
23ef29
    else
23ef29
        echo "kdump: saving vmcore-dmesg.txt failed"
23ef29
    fi
23ef29
}
23ef29
23ef29
get_host_ip()
23ef29
{
23ef29
    local _host
23ef29
    if is_nfs_dump_target || is_ssh_dump_target
23ef29
    then
23ef29
        kdumpnic=$(getarg kdumpnic=)
23ef29
        [ -z "$kdumpnic" ] && echo "kdump: failed to get kdumpnic!" && return 1
23ef29
        _host=`ip addr show dev $kdumpnic|grep '[ ]*inet'`
23ef29
        [ $? -ne 0 ] && echo "kdump: wrong kdumpnic: $kdumpnic" && return 1
23ef29
        _host=`echo $_host | head -n 1 | cut -d' ' -f2`
23ef29
        _host="${_host%%/*}"
23ef29
        [ -z "$_host" ] && echo "kdump: wrong kdumpnic: $kdumpnic" && return 1
23ef29
        HOST_IP=$_host
23ef29
    fi
23ef29
    return 0
23ef29
}
23ef29
23ef29
read_kdump_conf()
23ef29
{
23ef29
    if [ ! -f "$KDUMP_CONF" ]; then
23ef29
        echo "kdump: $KDUMP_CONF not found"
23ef29
        return
23ef29
    fi
23ef29
23ef29
    get_kdump_confs
23ef29
23ef29
    # rescan for add code for dump target
23ef29
    while read config_opt config_val;
23ef29
    do
23ef29
        # remove inline comments after the end of a directive.
23ef29
        config_val=$(strip_comments $config_val)
23ef29
        case "$config_opt" in
23ef29
        dracut_args)
23ef29
            config_val=$(get_dracut_args_target "$config_val")
23ef29
            [[ -n "$config_val" ]] && add_dump_code "dump_fs $config_val"
23ef29
            ;;
23ef29
        ext[234]|xfs|btrfs|minix|nfs)
23ef29
            add_dump_code "dump_fs $config_val"
23ef29
            ;;
23ef29
        raw)
23ef29
            add_dump_code "dump_raw $config_val"
23ef29
            ;;
23ef29
        ssh)
23ef29
            add_dump_code "dump_ssh $SSH_KEY_LOCATION $config_val"
23ef29
            ;;
23ef29
        esac
23ef29
    done < $KDUMP_CONF
23ef29
}
23ef29
23ef29
fence_kdump_notify()
23ef29
{
23ef29
    if [ -n "$FENCE_KDUMP_NODES" ]; then
23ef29
        $FENCE_KDUMP_SEND $FENCE_KDUMP_ARGS $FENCE_KDUMP_NODES &
23ef29
    fi
23ef29
}
23ef29
23ef29
read_kdump_conf
23ef29
fence_kdump_notify
23ef29
23ef29
get_host_ip
23ef29
if [ $? -ne 0 ]; then
23ef29
    echo "kdump: get_host_ip exited with non-zero status!"
23ef29
    exit 1
23ef29
fi
23ef29
23ef29
if [ -z "$DUMP_INSTRUCTION" ]; then
23ef29
    add_dump_code "dump_fs $NEWROOT"
23ef29
fi
23ef29
23ef29
do_kdump_pre
23ef29
if [ $? -ne 0 ]; then
23ef29
    echo "kdump: kdump_pre script exited with non-zero status!"
23ef29
    do_final_action
23ef29
fi
23ef29
make_trace_mem "kdump saving vmcore" '1:shortmem' '2+:mem' '3+:slab'
23ef29
do_dump
23ef29
DUMP_RETVAL=$?
23ef29
23ef29
do_kdump_post $DUMP_RETVAL
23ef29
if [ $? -ne 0 ]; then
23ef29
    echo "kdump: kdump_post script exited with non-zero status!"
23ef29
fi
23ef29
23ef29
if [ $DUMP_RETVAL -ne 0 ]; then
23ef29
    exit 1
23ef29
fi
23ef29
23ef29
do_final_action