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