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