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