893e0b
#!/bin/bash
893e0b
8f4abc
kdump_module_init() {
8f4abc
    if ! [[ -d "${initdir}/tmp" ]]; then
8f4abc
        mkdir -p "${initdir}/tmp"
8f4abc
    fi
893e0b
8f4abc
    . /lib/kdump/kdump-lib.sh
8f4abc
}
893e0b
893e0b
check() {
893e0b
    [[ $debug ]] && set -x
893e0b
    #kdumpctl sets this explicitly
893e0b
    if [ -z "$IN_KDUMP" ] || [ ! -f /etc/kdump.conf ]
893e0b
    then
893e0b
        return 1
893e0b
    fi
893e0b
    return 0
893e0b
}
893e0b
893e0b
depends() {
893e0b
    local _dep="base shutdown"
893e0b
8f4abc
    kdump_module_init
8f4abc
8f4abc
    add_opt_module() {
8f4abc
        [[ " $omit_dracutmodules " != *\ $1\ * ]] && _dep="$_dep $1"
8f4abc
    }
8f4abc
893e0b
    is_squash_available() {
893e0b
        for kmodule in squashfs overlay loop; do
893e0b
            if [ -z "$KDUMP_KERNELVER" ]; then
893e0b
                modprobe --dry-run $kmodule &>/dev/null || return 1
893e0b
            else
893e0b
                modprobe -S $KDUMP_KERNELVER --dry-run $kmodule &>/dev/null || return 1
893e0b
            fi
893e0b
        done
893e0b
    }
893e0b
8c3ceb
    if is_squash_available && ! is_fadump_capable; then
8f4abc
        add_opt_module squash
893e0b
    else
893e0b
        dwarning "Required modules to build a squashed kdump image is missing!"
893e0b
    fi
893e0b
8f4abc
    add_opt_module watchdog-modules
8f4abc
    if is_wdt_active; then
8f4abc
        add_opt_module watchdog
8f4abc
    fi
8f4abc
8f4abc
    if is_ssh_dump_target; then
8f4abc
        _dep="$_dep ssh-client"
8f4abc
    fi
8f4abc
8f4abc
    if [ "$(uname -m)" = "s390x" ]; then
8f4abc
        _dep="$_dep znet"
8f4abc
    fi
8f4abc
893e0b
    if [ -n "$( find /sys/devices -name drm )" ] || [ -d /sys/module/hyperv_fb ]; then
8f4abc
        add_opt_module drm
893e0b
    fi
893e0b
7a865b
    if is_generic_fence_kdump || is_pcs_fence_kdump; then
893e0b
        _dep="$_dep network"
893e0b
    fi
893e0b
893e0b
    echo $_dep
893e0b
}
893e0b
893e0b
kdump_is_bridge() {
893e0b
     [ -d /sys/class/net/"$1"/bridge ]
893e0b
}
893e0b
893e0b
kdump_is_bond() {
893e0b
     [ -d /sys/class/net/"$1"/bonding ]
893e0b
}
893e0b
893e0b
kdump_is_team() {
893e0b
     [ -f /usr/bin/teamnl ] && teamnl $1 ports &> /dev/null
893e0b
}
893e0b
893e0b
kdump_is_vlan() {
893e0b
     [ -f /proc/net/vlan/"$1" ]
893e0b
}
893e0b
893e0b
# $1: netdev name
893e0b
source_ifcfg_file() {
893e0b
    local ifcfg_file
893e0b
893e0b
    ifcfg_file=$(get_ifcfg_filename $1)
893e0b
    if [ -f "${ifcfg_file}" ]; then
893e0b
        . ${ifcfg_file}
893e0b
    else
893e0b
        dwarning "The ifcfg file of $1 is not found!"
893e0b
    fi
893e0b
}
893e0b
893e0b
# $1: netdev name
893e0b
kdump_setup_dns() {
893e0b
    local _nameserver _dns
893e0b
    local _dnsfile=${initdir}/etc/cmdline.d/42dns.conf
893e0b
893e0b
    source_ifcfg_file $1
893e0b
893e0b
    [ -n "$DNS1" ] && echo "nameserver=$DNS1" > "$_dnsfile"
893e0b
    [ -n "$DNS2" ] && echo "nameserver=$DNS2" >> "$_dnsfile"
893e0b
893e0b
    while read content;
893e0b
    do
893e0b
        _nameserver=$(echo $content | grep ^nameserver)
893e0b
        [ -z "$_nameserver" ] && continue
893e0b
893e0b
        _dns=$(echo $_nameserver | cut -d' ' -f2)
893e0b
        [ -z "$_dns" ] && continue
893e0b
893e0b
        if [ ! -f $_dnsfile ] || [ ! $(cat $_dnsfile | grep -q $_dns) ]; then
893e0b
            echo "nameserver=$_dns" >> "$_dnsfile"
893e0b
        fi
893e0b
    done < "/etc/resolv.conf"
893e0b
}
893e0b
893e0b
#$1: netdev name
893e0b
#$2: srcaddr
893e0b
#if it use static ip echo it, or echo null
893e0b
kdump_static_ip() {
893e0b
    local _netdev="$1" _srcaddr="$2" _ipv6_flag
893e0b
    local _netmask _gateway _ipaddr _target _nexthop
893e0b
893e0b
    _ipaddr=$(ip addr show dev $_netdev permanent | awk "/ $_srcaddr\/.* /{print \$2}")
893e0b
893e0b
    if is_ipv6_address $_srcaddr; then
893e0b
        _ipv6_flag="-6"
893e0b
    fi
893e0b
893e0b
    if [ -n "$_ipaddr" ]; then
893e0b
        _gateway=$(ip $_ipv6_flag route list dev $_netdev | \
893e0b
                awk '/^default /{print $3}' | head -n 1)
893e0b
893e0b
        if [ "x" !=  "x"$_ipv6_flag ]; then
893e0b
            # _ipaddr="2002::56ff:feb6:56d5/64", _netmask is the number after "/"
893e0b
            _netmask=${_ipaddr#*\/}
893e0b
            _srcaddr="[$_srcaddr]"
893e0b
            _gateway="[$_gateway]"
893e0b
        else
893e0b
            _netmask=$(ipcalc -m $_ipaddr | cut -d'=' -f2)
893e0b
        fi
893e0b
        echo -n "${_srcaddr}::${_gateway}:${_netmask}::"
893e0b
    fi
893e0b
893e0b
    /sbin/ip $_ipv6_flag route show | grep -v default | grep ".*via.* $_netdev " |\
893e0b
    while read _route; do
893e0b
        _target=`echo $_route | cut -d ' ' -f1`
893e0b
        _nexthop=`echo $_route | cut -d ' ' -f3`
893e0b
        if [ "x" !=  "x"$_ipv6_flag ]; then
893e0b
            _target="[$_target]"
893e0b
            _nexthop="[$_nexthop]"
893e0b
        fi
893e0b
        echo "rd.route=$_target:$_nexthop:$_netdev"
893e0b
    done >> ${initdir}/etc/cmdline.d/45route-static.conf
893e0b
}
893e0b
893e0b
kdump_get_mac_addr() {
893e0b
    cat /sys/class/net/$1/address
893e0b
}
893e0b
893e0b
#Bonding or team master modifies the mac address
893e0b
#of its slaves, we should use perm address
893e0b
kdump_get_perm_addr() {
893e0b
    local addr=$(ethtool -P $1 | sed -e 's/Permanent address: //')
893e0b
    if [ -z "$addr" ] || [ "$addr" = "00:00:00:00:00:00" ]
893e0b
    then
893e0b
        derror "Can't get the permanent address of $1"
893e0b
    else
893e0b
        echo "$addr"
893e0b
    fi
893e0b
}
893e0b
893e0b
# Prefix kernel assigned names with "kdump-". EX: eth0 -> kdump-eth0
893e0b
# Because kernel assigned names are not persistent between 1st and 2nd
893e0b
# kernel. We could probably end up with eth0 being eth1, eth0 being
893e0b
# eth1, and naming conflict happens.
893e0b
kdump_setup_ifname() {
893e0b
    local _ifname
893e0b
893e0b
    # If ifname already has 'kdump-' prefix, we must be switching from
893e0b
    # fadump to kdump. Skip prefixing 'kdump-' in this case as adding
893e0b
    # another prefix may truncate the ifname. Since an ifname with
893e0b
    # 'kdump-' is already persistent, this should be fine.
893e0b
    if [[ $1 =~ eth* ]] && [[ ! $1 =~ ^kdump-* ]]; then
893e0b
        _ifname="kdump-$1"
893e0b
    else
893e0b
        _ifname="$1"
893e0b
    fi
893e0b
893e0b
    echo "$_ifname"
893e0b
}
893e0b
893e0b
kdump_setup_bridge() {
893e0b
    local _netdev=$1
893e0b
    local _brif _dev _mac _kdumpdev
893e0b
    for _dev in `ls /sys/class/net/$_netdev/brif/`; do
893e0b
        _kdumpdev=$_dev
893e0b
        if kdump_is_bond "$_dev"; then
893e0b
            kdump_setup_bond "$_dev"
893e0b
        elif kdump_is_team "$_dev"; then
893e0b
            kdump_setup_team "$_dev"
893e0b
        elif kdump_is_vlan "$_dev"; then
893e0b
            kdump_setup_vlan "$_dev"
893e0b
        else
893e0b
            _mac=$(kdump_get_mac_addr $_dev)
893e0b
            _kdumpdev=$(kdump_setup_ifname $_dev)
893e0b
            echo -n " ifname=$_kdumpdev:$_mac" >> ${initdir}/etc/cmdline.d/41bridge.conf
893e0b
        fi
893e0b
        _brif+="$_kdumpdev,"
893e0b
    done
893e0b
    echo " bridge=$_netdev:$(echo $_brif | sed -e 's/,$//')" >> ${initdir}/etc/cmdline.d/41bridge.conf
893e0b
}
893e0b
893e0b
kdump_setup_bond() {
893e0b
    local _netdev=$1
893e0b
    local _dev _mac _slaves _kdumpdev
893e0b
    for _dev in `cat /sys/class/net/$_netdev/bonding/slaves`; do
893e0b
        _mac=$(kdump_get_perm_addr $_dev)
893e0b
        _kdumpdev=$(kdump_setup_ifname $_dev)
893e0b
        echo -n " ifname=$_kdumpdev:$_mac" >> ${initdir}/etc/cmdline.d/42bond.conf
893e0b
        _slaves+="$_kdumpdev,"
893e0b
    done
893e0b
    echo -n " bond=$_netdev:$(echo $_slaves | sed 's/,$//')" >> ${initdir}/etc/cmdline.d/42bond.conf
893e0b
    # Get bond options specified in ifcfg
893e0b
893e0b
    source_ifcfg_file $_netdev
893e0b
7a865b
    bondoptions=":$(echo $BONDING_OPTS | xargs echo | tr " " ",")"
893e0b
    echo "$bondoptions" >> ${initdir}/etc/cmdline.d/42bond.conf
893e0b
}
893e0b
893e0b
kdump_setup_team() {
893e0b
    local _netdev=$1
893e0b
    local _dev _mac _slaves _kdumpdev
893e0b
    for _dev in `teamnl $_netdev ports | awk -F':' '{print $2}'`; do
893e0b
        _mac=$(kdump_get_perm_addr $_dev)
893e0b
        _kdumpdev=$(kdump_setup_ifname $_dev)
893e0b
        echo -n " ifname=$_kdumpdev:$_mac" >> ${initdir}/etc/cmdline.d/44team.conf
893e0b
        _slaves+="$_kdumpdev,"
893e0b
    done
893e0b
    echo " team=$_netdev:$(echo $_slaves | sed -e 's/,$//')" >> ${initdir}/etc/cmdline.d/44team.conf
893e0b
    #Buggy version teamdctl outputs to stderr!
893e0b
    #Try to use the latest version of teamd.
893e0b
    teamdctl "$_netdev" config dump > ${initdir}/tmp/$$-$_netdev.conf
893e0b
    if [ $? -ne 0 ]
893e0b
    then
893e0b
        derror "teamdctl failed."
893e0b
        exit 1
893e0b
    fi
893e0b
    inst_dir /etc/teamd
893e0b
    inst_simple ${initdir}/tmp/$$-$_netdev.conf "/etc/teamd/$_netdev.conf"
893e0b
    rm -f ${initdir}/tmp/$$-$_netdev.conf
893e0b
}
893e0b
893e0b
kdump_setup_vlan() {
893e0b
    local _netdev=$1
893e0b
    local _phydev="$(awk '/^Device:/{print $2}' /proc/net/vlan/"$_netdev")"
893e0b
    local _netmac="$(kdump_get_mac_addr $_phydev)"
893e0b
    local _kdumpdev
893e0b
8f4abc
    #Just support vlan over bond and team
893e0b
    if kdump_is_bridge "$_phydev"; then
893e0b
        derror "Vlan over bridge is not supported!"
893e0b
        exit 1
893e0b
    elif kdump_is_bond "$_phydev"; then
893e0b
        kdump_setup_bond "$_phydev"
7a865b
	echo " vlan=$(kdump_setup_ifname $_netdev):$_phydev" > ${initdir}/etc/cmdline.d/43vlan.conf
893e0b
    else
893e0b
        _kdumpdev="$(kdump_setup_ifname $_phydev)"
7a865b
	echo " vlan=$(kdump_setup_ifname $_netdev):$_kdumpdev ifname=$_kdumpdev:$_netmac" > ${initdir}/etc/cmdline.d/43vlan.conf
893e0b
    fi
893e0b
}
893e0b
893e0b
# setup s390 znet cmdline
893e0b
# $1: netdev name
893e0b
kdump_setup_znet() {
893e0b
    local _options=""
f8bec6
    local _netdev=$1
893e0b
f8bec6
    source_ifcfg_file $_netdev
893e0b
893e0b
    for i in $OPTIONS; do
893e0b
        _options=${_options},$i
893e0b
    done
f8bec6
    echo rd.znet=${NETTYPE},${SUBCHANNELS}${_options} rd.znet_ifname=$_netdev:${SUBCHANNELS} > ${initdir}/etc/cmdline.d/30znet.conf
893e0b
}
893e0b
893e0b
# Setup dracut to bringup a given network interface
893e0b
kdump_setup_netdev() {
893e0b
    local _netdev=$1 _srcaddr=$2
893e0b
    local _static _proto _ip_conf _ip_opts _ifname_opts
893e0b
    local _netmac=$(kdump_get_mac_addr $_netdev)
893e0b
893e0b
    if [ "$(uname -m)" = "s390x" ]; then
893e0b
        kdump_setup_znet $_netdev
893e0b
    fi
893e0b
893e0b
    _static=$(kdump_static_ip $_netdev $_srcaddr)
893e0b
    if [ -n "$_static" ]; then
893e0b
        _proto=none
893e0b
    elif is_ipv6_address $_srcaddr; then
8f4abc
        _proto=auto6
893e0b
    else
893e0b
        _proto=dhcp
893e0b
    fi
893e0b
893e0b
    _ip_conf="${initdir}/etc/cmdline.d/40ip.conf"
893e0b
    _ip_opts=" ip=${_static}$(kdump_setup_ifname $_netdev):${_proto}"
893e0b
893e0b
    # dracut doesn't allow duplicated configuration for same NIC, even they're exactly the same.
893e0b
    # so we have to avoid adding duplicates
893e0b
    # We should also check /proc/cmdline for existing ip=xx arg.
893e0b
    # For example, iscsi boot will specify ip=xxx arg in cmdline.
893e0b
    if [ ! -f $_ip_conf ] || ! grep -q $_ip_opts $_ip_conf &&\
893e0b
        ! grep -q "ip=[^[:space:]]*$_netdev" /proc/cmdline; then
893e0b
        echo "$_ip_opts" >> $_ip_conf
893e0b
    fi
893e0b
893e0b
    if kdump_is_bridge "$_netdev"; then
893e0b
        kdump_setup_bridge "$_netdev"
893e0b
    elif kdump_is_bond "$_netdev"; then
893e0b
        kdump_setup_bond "$_netdev"
893e0b
    elif kdump_is_team "$_netdev"; then
893e0b
        kdump_setup_team "$_netdev"
893e0b
    elif kdump_is_vlan "$_netdev"; then
893e0b
        kdump_setup_vlan "$_netdev"
893e0b
    else
893e0b
        _ifname_opts=" ifname=$(kdump_setup_ifname $_netdev):$_netmac"
893e0b
        echo "$_ifname_opts" >> $_ip_conf
893e0b
    fi
893e0b
893e0b
    kdump_setup_dns "$_netdev"
f8bec6
f8bec6
    if [ ! -f ${initdir}/etc/cmdline.d/50neednet.conf ]; then
f8bec6
        # network-manager module needs this parameter
f8bec6
        echo "rd.neednet" >> ${initdir}/etc/cmdline.d/50neednet.conf
f8bec6
    fi
893e0b
}
893e0b
893e0b
get_ip_route_field()
893e0b
{
893e0b
    if `echo $1 | grep -q $2`; then
893e0b
        echo ${1##*$2} | cut -d ' ' -f1
893e0b
    fi
893e0b
}
893e0b
893e0b
#Function:kdump_install_net
893e0b
#$1: config values of net line in kdump.conf
893e0b
#$2: srcaddr of network device
893e0b
kdump_install_net() {
893e0b
    local _server _netdev _srcaddr _route _serv_tmp
893e0b
    local config_val="$1"
893e0b
893e0b
    _server=$(get_remote_host $config_val)
893e0b
893e0b
    if is_hostname $_server; then
893e0b
        _serv_tmp=`getent ahosts $_server | grep -v : | head -n 1`
893e0b
        if [ -z "$_serv_tmp" ]; then
893e0b
            _serv_tmp=`getent ahosts $_server | head -n 1`
893e0b
        fi
893e0b
        _server=`echo $_serv_tmp | cut -d' ' -f1`
893e0b
    fi
893e0b
893e0b
    _route=`/sbin/ip -o route get to $_server 2>&1`
893e0b
    [ $? != 0 ] && echo "Bad kdump location: $config_val" && exit 1
893e0b
893e0b
    #the field in the ip output changes if we go to another subnet
893e0b
    _srcaddr=$(get_ip_route_field "$_route" "src")
893e0b
    _netdev=$(get_ip_route_field "$_route" "dev")
893e0b
893e0b
    kdump_setup_netdev "${_netdev}" "${_srcaddr}"
893e0b
893e0b
    #save netdev used for kdump as cmdline
893e0b
    # Whoever calling kdump_install_net() is setting up the default gateway,
893e0b
    # ie. bootdev/kdumpnic. So don't override the setting if calling
893e0b
    # kdump_install_net() for another time. For example, after setting eth0 as
893e0b
    # the default gate way for network dump, eth1 in the fence kdump path will
893e0b
    # call kdump_install_net again and we don't want eth1 to be the default
893e0b
    # gateway.
893e0b
    if [ ! -f ${initdir}/etc/cmdline.d/60kdumpnic.conf ] &&
893e0b
       [ ! -f ${initdir}/etc/cmdline.d/70bootdev.conf ]; then
893e0b
        echo "kdumpnic=$(kdump_setup_ifname $_netdev)" > ${initdir}/etc/cmdline.d/60kdumpnic.conf
893e0b
        echo "bootdev=$(kdump_setup_ifname $_netdev)" > ${initdir}/etc/cmdline.d/70bootdev.conf
893e0b
    fi
893e0b
}
893e0b
f8bec6
# install etc/kdump/pre.d and /etc/kdump/post.d
f8bec6
kdump_install_pre_post_conf() {
f8bec6
    if [ -d /etc/kdump/pre.d ]; then
f8bec6
        for file in /etc/kdump/pre.d/*; do
f8bec6
            if [ -x "$file" ]; then
f8bec6
                dracut_install $file
f8bec6
            elif [ $file != "/etc/kdump/pre.d/*" ]; then
f8bec6
               echo "$file is not executable"
f8bec6
            fi
f8bec6
        done
f8bec6
    fi
f8bec6
f8bec6
    if [ -d /etc/kdump/post.d ]; then
f8bec6
        for file in /etc/kdump/post.d/*; do
f8bec6
            if [ -x "$file" ]; then
f8bec6
                dracut_install $file
f8bec6
            elif [ $file != "/etc/kdump/post.d/*" ]; then
f8bec6
                echo "$file is not executable"
f8bec6
            fi
f8bec6
        done
f8bec6
    fi
f8bec6
}
f8bec6
893e0b
default_dump_target_install_conf()
893e0b
{
893e0b
    local _target _fstype
893e0b
    local _mntpoint _save_path
893e0b
893e0b
    is_user_configured_dump_target && return
893e0b
f8bec6
    _save_path=$(get_bind_mount_source $(get_save_path))
893e0b
    _target=$(get_target_from_path $_save_path)
f8bec6
    _mntpoint=$(get_mntpoint_from_target $_target)
893e0b
893e0b
    _fstype=$(get_fs_type_from_target $_target)
893e0b
    if is_fs_type_nfs $_fstype; then
893e0b
        kdump_install_net "$_target"
893e0b
        _fstype="nfs"
893e0b
    else
893e0b
        _target=$(kdump_get_persistent_dev $_target)
893e0b
    fi
893e0b
893e0b
    echo "$_fstype $_target" >> ${initdir}/tmp/$$-kdump.conf
893e0b
893e0b
    # don't touch the path under root mount
893e0b
    if [ "$_mntpoint" != "/" ]; then
893e0b
        _save_path=${_save_path##"$_mntpoint"}
893e0b
    fi
893e0b
893e0b
    #erase the old path line, then insert the parsed path
893e0b
    sed -i "/^path/d" ${initdir}/tmp/$$-kdump.conf
893e0b
    echo "path $_save_path" >> ${initdir}/tmp/$$-kdump.conf
893e0b
}
893e0b
893e0b
#install kdump.conf and what user specifies in kdump.conf
893e0b
kdump_install_conf() {
893e0b
    local _opt _val _pdev
893e0b
    sed -ne '/^#/!p' /etc/kdump.conf > ${initdir}/tmp/$$-kdump.conf
893e0b
893e0b
    while read _opt _val;
893e0b
    do
893e0b
        # remove inline comments after the end of a directive.
893e0b
        case "$_opt" in
893e0b
        raw)
893e0b
            _pdev=$(persistent_policy="by-id" kdump_get_persistent_dev $_val)
893e0b
            sed -i -e "s#^$_opt[[:space:]]\+$_val#$_opt $_pdev#" ${initdir}/tmp/$$-kdump.conf
893e0b
            ;;
893e0b
        ext[234]|xfs|btrfs|minix)
893e0b
            _pdev=$(kdump_get_persistent_dev $_val)
893e0b
            sed -i -e "s#^$_opt[[:space:]]\+$_val#$_opt $_pdev#" ${initdir}/tmp/$$-kdump.conf
893e0b
            ;;
893e0b
        ssh|nfs)
893e0b
            kdump_install_net "$_val"
893e0b
            ;;
893e0b
        dracut_args)
893e0b
            if [[ $(get_dracut_args_fstype "$_val") = nfs* ]] ; then
893e0b
                kdump_install_net "$(get_dracut_args_target "$_val")"
893e0b
            fi
893e0b
            ;;
893e0b
        kdump_pre|kdump_post|extra_bins)
893e0b
            dracut_install $_val
893e0b
            ;;
893e0b
        core_collector)
893e0b
            dracut_install "${_val%%[[:blank:]]*}"
893e0b
            ;;
893e0b
        esac
603de6
    done <<< "$(read_strip_comments /etc/kdump.conf)"
893e0b
f8bec6
    kdump_install_pre_post_conf
f8bec6
893e0b
    default_dump_target_install_conf
893e0b
893e0b
    kdump_configure_fence_kdump  "${initdir}/tmp/$$-kdump.conf"
893e0b
    inst "${initdir}/tmp/$$-kdump.conf" "/etc/kdump.conf"
893e0b
    rm -f ${initdir}/tmp/$$-kdump.conf
893e0b
}
893e0b
893e0b
# Default sysctl parameters should suffice for kdump kernel.
893e0b
# Remove custom configurations sysctl.conf & sysctl.d/*
893e0b
remove_sysctl_conf() {
893e0b
893e0b
    # As custom configurations like vm.min_free_kbytes can lead
893e0b
    # to OOM issues in kdump kernel, avoid them
893e0b
    rm -f "${initdir}/etc/sysctl.conf"
893e0b
    rm -rf "${initdir}/etc/sysctl.d"
893e0b
    rm -rf "${initdir}/run/sysctl.d"
893e0b
    rm -rf "${initdir}/usr/lib/sysctl.d"
893e0b
}
893e0b
893e0b
kdump_iscsi_get_rec_val() {
893e0b
893e0b
    local result
893e0b
893e0b
    # The open-iscsi 742 release changed to using flat files in
893e0b
    # /var/lib/iscsi.
893e0b
893e0b
    result=$(/sbin/iscsiadm --show -m session -r ${1} | grep "^${2} = ")
893e0b
    result=${result##* = }
893e0b
    echo $result
893e0b
}
893e0b
893e0b
kdump_get_iscsi_initiator() {
893e0b
    local _initiator
893e0b
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
893e0b
893e0b
    [ -f "$initiator_conf" ] || return 1
893e0b
893e0b
    while read _initiator; do
893e0b
        [ -z "${_initiator%%#*}" ] && continue # Skip comment lines
893e0b
893e0b
        case $_initiator in
893e0b
            InitiatorName=*)
893e0b
                initiator=${_initiator#InitiatorName=}
893e0b
                echo "rd.iscsi.initiator=${initiator}"
893e0b
                return 0;;
893e0b
            *) ;;
893e0b
        esac
893e0b
    done < ${initiator_conf}
893e0b
893e0b
    return 1
893e0b
}
893e0b
893e0b
# Figure out iBFT session according to session type
893e0b
is_ibft() {
893e0b
    [ "$(kdump_iscsi_get_rec_val $1 "node.discovery_type")" = fw ]
893e0b
}
893e0b
893e0b
kdump_setup_iscsi_device() {
893e0b
    local path=$1
893e0b
    local tgt_name; local tgt_ipaddr;
893e0b
    local username; local password; local userpwd_str;
893e0b
    local username_in; local password_in; local userpwd_in_str;
893e0b
    local netdev
893e0b
    local srcaddr
893e0b
    local idev
893e0b
    local netroot_str ; local initiator_str;
893e0b
    local netroot_conf="${initdir}/etc/cmdline.d/50iscsi.conf"
893e0b
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
893e0b
893e0b
    dinfo "Found iscsi component $1"
893e0b
893e0b
    # Check once before getting explicit values, so we can bail out early,
893e0b
    # e.g. in case of pure-hardware(all-offload) iscsi.
893e0b
    if ! /sbin/iscsiadm -m session -r ${path} &>/dev/null ; then
893e0b
        return 1
893e0b
    fi
893e0b
893e0b
    if is_ibft ${path}; then
893e0b
        return
893e0b
    fi
893e0b
893e0b
    # Remove software iscsi cmdline generated by 95iscsi,
893e0b
    # and let kdump regenerate here.
893e0b
    rm -f ${initdir}/etc/cmdline.d/95iscsi.conf
893e0b
893e0b
    tgt_name=$(kdump_iscsi_get_rec_val ${path} "node.name")
893e0b
    tgt_ipaddr=$(kdump_iscsi_get_rec_val ${path} "node.conn\[0\].address")
893e0b
893e0b
    # get and set username and password details
893e0b
    username=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.username")
893e0b
    [ "$username" == "<empty>" ] && username=""
893e0b
    password=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.password")
893e0b
    [ "$password" == "<empty>" ] && password=""
893e0b
    username_in=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.username_in")
893e0b
    [ -n "$username" ] && userpwd_str="$username:$password"
893e0b
893e0b
    # get and set incoming username and password details
893e0b
    [ "$username_in" == "<empty>" ] && username_in=""
893e0b
    password_in=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.password_in")
893e0b
    [ "$password_in" == "<empty>" ] && password_in=""
893e0b
893e0b
    [ -n "$username_in" ] && userpwd_in_str=":$username_in:$password_in"
893e0b
893e0b
    netdev=$(/sbin/ip route get to ${tgt_ipaddr} | \
893e0b
        sed 's|.*dev \(.*\).*|\1|g')
893e0b
    srcaddr=$(echo $netdev | awk '{ print $3; exit }')
893e0b
    netdev=$(echo $netdev | awk '{ print $1; exit }')
893e0b
893e0b
    kdump_setup_netdev $netdev $srcaddr
893e0b
893e0b
    # prepare netroot= command line
893e0b
    # FIXME: Do we need to parse and set other parameters like protocol, port
893e0b
    #        iscsi_iface_name, netdev_name, LUN etc.
893e0b
893e0b
    if is_ipv6_address $tgt_ipaddr; then
893e0b
        tgt_ipaddr="[$tgt_ipaddr]"
893e0b
    fi
893e0b
    netroot_str="netroot=iscsi:${userpwd_str}${userpwd_in_str}@$tgt_ipaddr::::$tgt_name"
893e0b
893e0b
    [[ -f $netroot_conf ]] || touch $netroot_conf
893e0b
893e0b
    # If netroot target does not exist already, append.
893e0b
    if ! grep -q $netroot_str $netroot_conf; then
893e0b
         echo $netroot_str >> $netroot_conf
893e0b
         dinfo "Appended $netroot_str to $netroot_conf"
893e0b
    fi
893e0b
893e0b
    # Setup initator
893e0b
    initiator_str=$(kdump_get_iscsi_initiator)
893e0b
    [ $? -ne "0" ] && derror "Failed to get initiator name" && return 1
893e0b
893e0b
    # If initiator details do not exist already, append.
893e0b
    if ! grep -q "$initiator_str" $netroot_conf; then
893e0b
         echo "$initiator_str" >> $netroot_conf
893e0b
         dinfo "Appended "$initiator_str" to $netroot_conf"
893e0b
    fi
893e0b
}
893e0b
893e0b
kdump_check_iscsi_targets () {
893e0b
    # If our prerequisites are not met, fail anyways.
893e0b
    type -P iscsistart >/dev/null || return 1
893e0b
893e0b
    kdump_check_setup_iscsi() (
893e0b
        local _dev
893e0b
        _dev=$1
893e0b
893e0b
        [[ -L /sys/dev/block/$_dev ]] || return
893e0b
        cd "$(readlink -f /sys/dev/block/$_dev)"
893e0b
        until [[ -d sys || -d iscsi_session ]]; do
893e0b
            cd ..
893e0b
        done
893e0b
        [[ -d iscsi_session ]] && kdump_setup_iscsi_device "$PWD"
893e0b
    )
893e0b
893e0b
    [[ $hostonly ]] || [[ $mount_needs ]] && {
893e0b
        for_each_host_dev_and_slaves_all kdump_check_setup_iscsi
893e0b
    }
893e0b
}
893e0b
7a865b
# hostname -a is deprecated, do it by ourself
7a865b
get_alias() {
7a865b
    local ips
7a865b
    local entries
7a865b
    local alias_set
7a865b
7a865b
    ips=$(hostname -I)
7a865b
    for ip in $ips
7a865b
    do
f8bec6
            # in /etc/hosts, alias can come at the 2nd column
f8bec6
            entries=$(grep $ip /etc/hosts | awk '{ $1=""; print $0 }')
7a865b
            if [ $? -eq 0 ]; then
7a865b
                    alias_set="$alias_set $entries"
7a865b
            fi
7a865b
    done
7a865b
7a865b
    echo $alias_set
7a865b
}
7a865b
7a865b
is_localhost() {
7a865b
    local hostnames=$(hostname -A)
7a865b
    local shortnames=$(hostname -A -s)
7a865b
    local aliasname=$(get_alias)
7a865b
    local nodename=$1
7a865b
7a865b
    hostnames="$hostnames $shortnames $aliasname"
7a865b
7a865b
    for name in ${hostnames}; do
7a865b
        if [ "$name" == "$nodename" ]; then
7a865b
            return 0
7a865b
        fi
7a865b
    done
7a865b
    return 1
7a865b
}
7a865b
893e0b
# retrieves fence_kdump nodes from Pacemaker cluster configuration
893e0b
get_pcs_fence_kdump_nodes() {
893e0b
    local nodes
893e0b
893e0b
    # get cluster nodes from cluster cib, get interface and ip address
893e0b
    nodelist=`pcs cluster cib | xmllint --xpath "/cib/status/node_state/@uname" -`
893e0b
893e0b
    # nodelist is formed as 'uname="node1" uname="node2" ... uname="nodeX"'
893e0b
    # we need to convert each to node1, node2 ... nodeX in each iteration
893e0b
    for node in ${nodelist}; do
893e0b
        # convert $node from 'uname="nodeX"' to 'nodeX'
893e0b
        eval $node
893e0b
        nodename=$uname
893e0b
        # Skip its own node name
893e0b
        if [ "$nodename" = `hostname` -o "$nodename" = `hostname -s` ]; then
893e0b
            continue
893e0b
        fi
893e0b
        nodes="$nodes $nodename"
893e0b
    done
893e0b
893e0b
    echo $nodes
893e0b
}
893e0b
893e0b
# retrieves fence_kdump args from config file
893e0b
get_pcs_fence_kdump_args() {
893e0b
    if [ -f $FENCE_KDUMP_CONFIG_FILE ]; then
893e0b
        . $FENCE_KDUMP_CONFIG_FILE
893e0b
        echo $FENCE_KDUMP_OPTS
893e0b
    fi
893e0b
}
893e0b
7a865b
get_generic_fence_kdump_nodes() {
7a865b
    local filtered
7a865b
    local nodes
7a865b
7a865b
    nodes=$(get_option_value "fence_kdump_nodes")
7a865b
    for node in ${nodes}; do
7a865b
        # Skip its own node name
7a865b
        if is_localhost $node; then
7a865b
            continue
7a865b
        fi
7a865b
        filtered="$filtered $node"
7a865b
    done
7a865b
    echo $filtered
7a865b
}
7a865b
893e0b
# setup fence_kdump in cluster
893e0b
# setup proper network and install needed files
893e0b
kdump_configure_fence_kdump () {
893e0b
    local kdump_cfg_file=$1
893e0b
    local nodes
893e0b
    local args
893e0b
893e0b
    if is_generic_fence_kdump; then
7a865b
        nodes=$(get_generic_fence_kdump_nodes)
893e0b
893e0b
    elif is_pcs_fence_kdump; then
893e0b
        nodes=$(get_pcs_fence_kdump_nodes)
893e0b
893e0b
        # set appropriate options in kdump.conf
893e0b
        echo "fence_kdump_nodes $nodes" >> ${kdump_cfg_file}
893e0b
893e0b
        args=$(get_pcs_fence_kdump_args)
893e0b
        if [ -n "$args" ]; then
893e0b
            echo "fence_kdump_args $args" >> ${kdump_cfg_file}
893e0b
        fi
893e0b
893e0b
    else
893e0b
        # fence_kdump not configured
893e0b
        return 1
893e0b
    fi
893e0b
893e0b
    # setup network for each node
893e0b
    for node in ${nodes}; do
893e0b
        kdump_install_net $node
893e0b
    done
893e0b
893e0b
    dracut_install /etc/hosts
893e0b
    dracut_install /etc/nsswitch.conf
893e0b
    dracut_install $FENCE_KDUMP_SEND
893e0b
}
893e0b
893e0b
# Install a random seed used to feed /dev/urandom
893e0b
# By the time kdump service starts, /dev/uramdom is already fed by systemd
893e0b
kdump_install_random_seed() {
893e0b
    local poolsize=`cat /proc/sys/kernel/random/poolsize`
893e0b
893e0b
    if [ ! -d ${initdir}/var/lib/ ]; then
893e0b
        mkdir -p ${initdir}/var/lib/
893e0b
    fi
893e0b
893e0b
    dd if=/dev/urandom of=${initdir}/var/lib/random-seed \
893e0b
       bs=$poolsize count=1 2> /dev/null
893e0b
}
893e0b
893e0b
remove_cpu_online_rule() {
893e0b
    local file=${initdir}/usr/lib/udev/rules.d/40-redhat.rules
893e0b
893e0b
    sed -i '/SUBSYSTEM=="cpu"/d' $file
893e0b
}
893e0b
8f4abc
kdump_install_systemd_conf() {
8f4abc
    local failure_action=$(get_option_value "failure_action")
8f4abc
8f4abc
    # Kdump turns out to require longer default systemd mount timeout
8f4abc
    # than 1st kernel(90s by default), we use default 300s for kdump.
8f4abc
    grep -r "^[[:space:]]*DefaultTimeoutStartSec=" ${initdir}/etc/systemd/system.conf* &>/dev/null
8f4abc
    if [ $? -ne 0 ]; then
8f4abc
        mkdir -p ${initdir}/etc/systemd/system.conf.d
8f4abc
        echo "[Manager]" > ${initdir}/etc/systemd/system.conf.d/kdump.conf
8f4abc
        echo "DefaultTimeoutStartSec=300s" >> ${initdir}/etc/systemd/system.conf.d/kdump.conf
8f4abc
    fi
8f4abc
8f4abc
    # Forward logs to console directly, and don't read Kmsg, this avoids
8f4abc
    # unneccessary memory consumption and make console output more useful.
8f4abc
    # Only do so for non fadump image.
8f4abc
    mkdir -p ${initdir}/etc/systemd/journald.conf.d
8f4abc
    echo "[Journal]" > ${initdir}/etc/systemd/journald.conf.d/kdump.conf
8f4abc
    echo "Storage=volatile" >> ${initdir}/etc/systemd/journald.conf.d/kdump.conf
8f4abc
    echo "ReadKMsg=no" >> ${initdir}/etc/systemd/journald.conf.d/kdump.conf
8f4abc
    echo "ForwardToConsole=yes" >> ${initdir}/etc/systemd/journald.conf.d/kdump.conf
8f4abc
}
8f4abc
893e0b
install() {
893e0b
    local arch
893e0b
8f4abc
    kdump_module_init
893e0b
    kdump_install_conf
893e0b
    remove_sysctl_conf
893e0b
893e0b
    # Onlining secondary cpus breaks kdump completely on KVM on Power hosts
893e0b
    # Though we use maxcpus=1 by default but 40-redhat.rules will bring up all
893e0b
    # possible cpus by default. (rhbz1270174 rhbz1266322)
893e0b
    # Thus before we get the kernel fix and the systemd rule fix let's remove
893e0b
    # the cpu online rule in kdump initramfs.
893e0b
    arch=$(uname -m)
893e0b
    if [[ "$arch" = "ppc64le" ]] || [[ "$arch" = "ppc64" ]]; then
893e0b
        remove_cpu_online_rule
893e0b
    fi
893e0b
893e0b
    if is_ssh_dump_target; then
893e0b
        kdump_install_random_seed
893e0b
    fi
893e0b
    dracut_install -o /etc/adjtime /etc/localtime
893e0b
    inst "$moddir/monitor_dd_progress" "/kdumpscripts/monitor_dd_progress"
893e0b
    chmod +x ${initdir}/kdumpscripts/monitor_dd_progress
893e0b
    inst "/bin/dd" "/bin/dd"
893e0b
    inst "/bin/tail" "/bin/tail"
893e0b
    inst "/bin/date" "/bin/date"
893e0b
    inst "/bin/sync" "/bin/sync"
893e0b
    inst "/bin/cut" "/bin/cut"
893e0b
    inst "/bin/head" "/bin/head"
893e0b
    inst "/sbin/makedumpfile" "/sbin/makedumpfile"
893e0b
    inst "/sbin/vmcore-dmesg" "/sbin/vmcore-dmesg"
8f4abc
    inst "/usr/bin/printf" "/sbin/printf"
8f4abc
    inst "/usr/bin/logger" "/sbin/logger"
893e0b
    inst "/lib/kdump/kdump-lib.sh" "/lib/kdump-lib.sh"
893e0b
    inst "/lib/kdump/kdump-lib-initramfs.sh" "/lib/kdump-lib-initramfs.sh"
8f4abc
    inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh"
893e0b
    inst "$moddir/kdump.sh" "/usr/bin/kdump.sh"
893e0b
    inst "$moddir/kdump-capture.service" "$systemdsystemunitdir/kdump-capture.service"
8f4abc
    systemctl -q --root "$initdir" add-wants initrd.target kdump-capture.service
893e0b
    inst "$moddir/kdump-error-handler.sh" "/usr/bin/kdump-error-handler.sh"
893e0b
    inst "$moddir/kdump-error-handler.service" "$systemdsystemunitdir/kdump-error-handler.service"
893e0b
    # Replace existing emergency service and emergency target
893e0b
    cp "$moddir/kdump-emergency.service" "$initdir/$systemdsystemunitdir/emergency.service"
893e0b
    cp "$moddir/kdump-emergency.target" "$initdir/$systemdsystemunitdir/emergency.target"
893e0b
    # Also redirect dracut-emergency to kdump error handler
893e0b
    ln_r "$systemdsystemunitdir/emergency.service" "$systemdsystemunitdir/dracut-emergency.service"
893e0b
893e0b
    # Check for all the devices and if any device is iscsi, bring up iscsi
893e0b
    # target. Ideally all this should be pushed into dracut iscsi module
893e0b
    # at some point of time.
893e0b
    kdump_check_iscsi_targets
893e0b
8f4abc
    kdump_install_systemd_conf
8f4abc
893e0b
    # For the lvm type target under kdump, in /etc/lvm/lvm.conf we can
893e0b
    # safely replace "reserved_memory=XXXX"(default value is 8192) with
893e0b
    # "reserved_memory=1024" to lower memory pressure under kdump. We do
893e0b
    # it unconditionally here, if "/etc/lvm/lvm.conf" doesn't exist, it
893e0b
    # actually does nothing.
893e0b
    sed -i -e \
893e0b
      's/\(^[[:space:]]*reserved_memory[[:space:]]*=\)[[:space:]]*[[:digit:]]*/\1 1024/' \
893e0b
      ${initdir}/etc/lvm/lvm.conf &>/dev/null
893e0b
8f4abc
    # Save more memory by dropping switch root capability
603de6
    if ! is_fadump_capable; then
603de6
        dracut_no_switch_root
603de6
    fi
893e0b
}