Petr Šabata f5bf49
#!/bin/sh
Petr Šabata f5bf49
Petr Šabata f5bf49
# continue here only if we have to save dump.
Petr Šabata f5bf49
if [ -f /etc/fadump.initramfs ] && [ ! -f /proc/device-tree/rtas/ibm,kernel-dump ] && [ ! -f /proc/device-tree/ibm,opal/dump/mpipl-boot ]; then
Petr Šabata f5bf49
    exit 0
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
Petr Šabata f5bf49
. /lib/dracut-lib.sh
Petr Šabata f5bf49
. /lib/kdump-lib-initramfs.sh
Petr Šabata f5bf49
Petr Šabata f5bf49
set -o pipefail
Petr Šabata f5bf49
DUMP_RETVAL=0
Petr Šabata f5bf49
Petr Šabata f5bf49
export PATH=$PATH:$KDUMP_SCRIPT_DIR
Petr Šabata f5bf49
Petr Šabata f5bf49
do_dump()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _ret
Petr Šabata f5bf49
Petr Šabata f5bf49
    eval $DUMP_INSTRUCTION
Petr Šabata f5bf49
    _ret=$?
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ $_ret -ne 0 ]; then
DistroBaker 5cac7c
        derror "saving vmcore failed"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    return $_ret
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
do_kdump_pre()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _ret
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -n "$KDUMP_PRE" ]; then
Petr Šabata f5bf49
        "$KDUMP_PRE"
Petr Šabata f5bf49
        _ret=$?
Petr Šabata f5bf49
        if [ $_ret -ne 0 ]; then
DistroBaker 5cac7c
            derror "$KDUMP_PRE exited with $_ret status"
Petr Šabata f5bf49
            return $_ret
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # if any script fails, it just raises warning and continues
Petr Šabata f5bf49
    if [ -d /etc/kdump/pre.d ]; then
Petr Šabata f5bf49
        for file in /etc/kdump/pre.d/*; do
Petr Šabata f5bf49
            "$file"
Petr Šabata f5bf49
            _ret=$?
Petr Šabata f5bf49
            if [ $_ret -ne 0 ]; then
DistroBaker 5cac7c
                derror "$file exited with $_ret status"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
do_kdump_post()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _ret
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -d /etc/kdump/post.d ]; then
Petr Šabata f5bf49
        for file in /etc/kdump/post.d/*; do
Petr Šabata f5bf49
            "$file" "$1"
Petr Šabata f5bf49
            _ret=$?
Petr Šabata f5bf49
            if [ $_ret -ne 0 ]; then
DistroBaker 5cac7c
                derror "$file exited with $_ret status"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -n "$KDUMP_POST" ]; then
Petr Šabata f5bf49
        "$KDUMP_POST" "$1"
Petr Šabata f5bf49
        _ret=$?
Petr Šabata f5bf49
        if [ $_ret -ne 0 ]; then
DistroBaker 5cac7c
            derror "$KDUMP_POST exited with $_ret status"
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
add_dump_code()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    DUMP_INSTRUCTION=$1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
dump_raw()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _raw=$1
Petr Šabata f5bf49
Petr Šabata f5bf49
    [ -b "$_raw" ] || return 1
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving to raw disk $_raw"
Petr Šabata f5bf49
Petr Šabata f5bf49
    if ! $(echo -n $CORE_COLLECTOR|grep -q makedumpfile); then
Petr Šabata f5bf49
        _src_size=`ls -l /proc/vmcore | cut -d' ' -f5`
Petr Šabata f5bf49
        _src_size_mb=$(($_src_size / 1048576))
Petr Šabata f5bf49
        monitor_dd_progress $_src_size_mb &
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving vmcore"
Petr Šabata f5bf49
    $CORE_COLLECTOR /proc/vmcore | dd of=$_raw bs=$DD_BLKSIZE >> /tmp/dd_progress_file 2>&1 || return 1
Petr Šabata f5bf49
    sync
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving vmcore complete"
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
dump_ssh()
Petr Šabata f5bf49
{
DistroBaker 5cac7c
    local ret
Petr Šabata f5bf49
    local _opt="-i $1 -o BatchMode=yes -o StrictHostKeyChecking=yes"
Petr Šabata f5bf49
    local _dir="$KDUMP_PATH/$HOST_IP-$DATEDIR"
Petr Šabata f5bf49
    local _host=$2
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving to $_host:$_dir"
Petr Šabata f5bf49
Petr Šabata f5bf49
    cat /var/lib/random-seed > /dev/urandom
Petr Šabata f5bf49
    ssh -q $_opt $_host mkdir -p $_dir || return 1
Petr Šabata f5bf49
Petr Šabata f5bf49
    save_vmcore_dmesg_ssh ${DMESG_COLLECTOR} ${_dir} "${_opt}" $_host
Petr Šabata f5bf49
    save_opalcore_ssh ${_dir} "${_opt}" $_host
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving vmcore"
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ "${CORE_COLLECTOR%%[[:blank:]]*}" = "scp" ]; then
DistroBaker 5cac7c
        scp -q $_opt /proc/vmcore "$_host:$_dir/vmcore-incomplete"
DistroBaker 5cac7c
        ret=$?
DistroBaker 5cac7c
        save_log
DistroBaker 5cac7c
        scp -q $_opt $KDUMP_LOG_FILE "$_host:$_dir/"
DistroBaker 5cac7c
        if [ $ret -ne 0 ]; then
DistroBaker 5cac7c
            return 1
DistroBaker 5cac7c
        fi
Petr Šabata f5bf49
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore" || return 1
Petr Šabata f5bf49
    else
DistroBaker 5cac7c
        $CORE_COLLECTOR /proc/vmcore | ssh $_opt $_host "dd bs=512 of=$_dir/vmcore-incomplete"
DistroBaker 5cac7c
        ret=$?
DistroBaker 5cac7c
        save_log
DistroBaker 5cac7c
        scp -q $_opt $KDUMP_LOG_FILE "$_host:$_dir/"
DistroBaker 5cac7c
        if [ $ret -ne 0 ]; then
DistroBaker 5cac7c
            return 1
DistroBaker 5cac7c
        fi
Petr Šabata f5bf49
        ssh $_opt $_host "mv $_dir/vmcore-incomplete $_dir/vmcore.flat" || return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving vmcore complete"
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
save_opalcore_ssh() {
Petr Šabata f5bf49
    local _path=$1
Petr Šabata f5bf49
    local _opts="$2"
Petr Šabata f5bf49
    local _location=$3
Petr Šabata f5bf49
DistroBaker 5cac7c
    ddebug "_path=$_path _opts=$_opts _location=$_location"
DistroBaker 5cac7c
Petr Šabata f5bf49
    if [ ! -f $OPALCORE ]; then
Petr Šabata f5bf49
        # Check if we are on an old kernel that uses a different path
Petr Šabata f5bf49
        if [ -f /sys/firmware/opal/core ]; then
Petr Šabata f5bf49
            OPALCORE="/sys/firmware/opal/core"
Petr Šabata f5bf49
        else
Petr Šabata f5bf49
            return 0
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving opalcore:$OPALCORE to $_location:$_path"
Petr Šabata f5bf49
    scp $_opts $OPALCORE $_location:$_path/opalcore-incomplete
Petr Šabata f5bf49
    if [ $? -ne 0 ]; then
DistroBaker 5cac7c
        derror "saving opalcore failed"
Petr Šabata f5bf49
       return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    ssh $_opts $_location mv $_path/opalcore-incomplete $_path/opalcore
DistroBaker 5cac7c
    dinfo "saving opalcore complete"
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
save_vmcore_dmesg_ssh() {
Petr Šabata f5bf49
    local _dmesg_collector=$1
Petr Šabata f5bf49
    local _path=$2
Petr Šabata f5bf49
    local _opts="$3"
Petr Šabata f5bf49
    local _location=$4
Petr Šabata f5bf49
DistroBaker 5cac7c
    dinfo "saving vmcore-dmesg.txt to $_location:$_path"
Petr Šabata f5bf49
    $_dmesg_collector /proc/vmcore | ssh $_opts $_location "dd of=$_path/vmcore-dmesg-incomplete.txt"
Petr Šabata f5bf49
    _exitcode=$?
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ $_exitcode -eq 0 ]; then
Petr Šabata f5bf49
        ssh -q $_opts $_location mv $_path/vmcore-dmesg-incomplete.txt $_path/vmcore-dmesg.txt
DistroBaker 5cac7c
        dinfo "saving vmcore-dmesg.txt complete"
Petr Šabata f5bf49
    else
DistroBaker 5cac7c
        derror "saving vmcore-dmesg.txt failed"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_host_ip()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _host
Petr Šabata f5bf49
    if is_nfs_dump_target || is_ssh_dump_target
Petr Šabata f5bf49
    then
Petr Šabata f5bf49
        kdumpnic=$(getarg kdumpnic=)
DistroBaker 5cac7c
        [ -z "$kdumpnic" ] && derror "failed to get kdumpnic!" && return 1
Petr Šabata f5bf49
        _host=`ip addr show dev $kdumpnic|grep '[ ]*inet'`
DistroBaker 5cac7c
        [ $? -ne 0 ] && derror "wrong kdumpnic: $kdumpnic" && return 1
Petr Šabata f5bf49
        _host=`echo $_host | head -n 1 | cut -d' ' -f2`
Petr Šabata f5bf49
        _host="${_host%%/*}"
DistroBaker 5cac7c
        [ -z "$_host" ] && derror "wrong kdumpnic: $kdumpnic" && return 1
Petr Šabata f5bf49
        HOST_IP=$_host
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
read_kdump_conf()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if [ ! -f "$KDUMP_CONF" ]; then
DistroBaker 5cac7c
        derror "$KDUMP_CONF not found"
Petr Šabata f5bf49
        return
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    get_kdump_confs
Petr Šabata f5bf49
Petr Šabata f5bf49
    # rescan for add code for dump target
Petr Šabata f5bf49
    while read config_opt config_val;
Petr Šabata f5bf49
    do
Petr Šabata f5bf49
        # remove inline comments after the end of a directive.
Petr Šabata f5bf49
        case "$config_opt" in
Petr Šabata f5bf49
        dracut_args)
Petr Šabata f5bf49
            config_val=$(get_dracut_args_target "$config_val")
Petr Šabata f5bf49
            if [ -n "$config_val" ]; then
Petr Šabata f5bf49
                config_val=$(get_mntpoint_from_target "$config_val")
Petr Šabata f5bf49
                add_dump_code "dump_fs $config_val"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
            ;;
Petr Šabata f5bf49
        ext[234]|xfs|btrfs|minix|nfs)
Petr Šabata f5bf49
            config_val=$(get_mntpoint_from_target "$config_val")
Petr Šabata f5bf49
            add_dump_code "dump_fs $config_val"
Petr Šabata f5bf49
            ;;
Petr Šabata f5bf49
        raw)
Petr Šabata f5bf49
            add_dump_code "dump_raw $config_val"
Petr Šabata f5bf49
            ;;
Petr Šabata f5bf49
        ssh)
Petr Šabata f5bf49
            add_dump_code "dump_ssh $SSH_KEY_LOCATION $config_val"
Petr Šabata f5bf49
            ;;
Petr Šabata f5bf49
        esac
Petr Šabata f5bf49
    done <<< "$(read_strip_comments $KDUMP_CONF)"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
fence_kdump_notify()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if [ -n "$FENCE_KDUMP_NODES" ]; then
Petr Šabata f5bf49
        $FENCE_KDUMP_SEND $FENCE_KDUMP_ARGS $FENCE_KDUMP_NODES &
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
read_kdump_conf
Petr Šabata f5bf49
fence_kdump_notify
Petr Šabata f5bf49
Petr Šabata f5bf49
get_host_ip
Petr Šabata f5bf49
if [ $? -ne 0 ]; then
DistroBaker 5cac7c
    derror "get_host_ip exited with non-zero status!"
Petr Šabata f5bf49
    exit 1
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
Petr Šabata f5bf49
if [ -z "$DUMP_INSTRUCTION" ]; then
Petr Šabata f5bf49
    add_dump_code "dump_fs $NEWROOT"
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
Petr Šabata f5bf49
do_kdump_pre
Petr Šabata f5bf49
if [ $? -ne 0 ]; then
DistroBaker 5cac7c
    derror "kdump_pre script exited with non-zero status!"
Petr Šabata f5bf49
    do_final_action
Petr Šabata f5bf49
    # During systemd service to reboot the machine, stop this shell script running
Petr Šabata f5bf49
    exit 1
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
make_trace_mem "kdump saving vmcore" '1:shortmem' '2+:mem' '3+:slab'
Petr Šabata f5bf49
do_dump
Petr Šabata f5bf49
DUMP_RETVAL=$?
Petr Šabata f5bf49
Petr Šabata f5bf49
do_kdump_post $DUMP_RETVAL
Petr Šabata f5bf49
if [ $? -ne 0 ]; then
DistroBaker 5cac7c
    derror "kdump_post script exited with non-zero status!"
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
Petr Šabata f5bf49
if [ $DUMP_RETVAL -ne 0 ]; then
Petr Šabata f5bf49
    exit 1
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
Petr Šabata f5bf49
do_final_action