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