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