Petr Šabata f5bf49
#!/bin/bash
Petr Šabata f5bf49
DistroBaker 624a64
kdump_module_init() {
DistroBaker 624a64
    if ! [[ -d "${initdir}/tmp" ]]; then
DistroBaker 624a64
        mkdir -p "${initdir}/tmp"
DistroBaker 624a64
    fi
Petr Šabata f5bf49
DistroBaker 624a64
    . /lib/kdump/kdump-lib.sh
DistroBaker 624a64
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check() {
Petr Šabata f5bf49
    [[ $debug ]] && set -x
Petr Šabata f5bf49
    #kdumpctl sets this explicitly
b494b7
    if [[ -z $IN_KDUMP ]] || [[ ! -f /etc/kdump.conf ]]; then
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
depends() {
Petr Šabata f5bf49
    local _dep="base shutdown"
Petr Šabata f5bf49
DistroBaker 624a64
    kdump_module_init
DistroBaker 624a64
DistroBaker 17a515
    add_opt_module() {
DistroBaker 17a515
        [[ " $omit_dracutmodules " != *\ $1\ * ]] && _dep="$_dep $1"
DistroBaker 17a515
    }
DistroBaker 17a515
Kairui Song 96a3fc
    if is_squash_available; then
DistroBaker 17a515
        add_opt_module squash
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        dwarning "Required modules to build a squashed kdump image is missing!"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 17a515
    if is_wdt_active; then
DistroBaker 17a515
        add_opt_module watchdog
DistroBaker 17a515
    fi
DistroBaker 17a515
DistroBaker 17a515
    if is_ssh_dump_target; then
DistroBaker 17a515
        _dep="$_dep ssh-client"
DistroBaker 17a515
    fi
DistroBaker 17a515
b494b7
    if [[ "$(uname -m)" == "s390x" ]]; then
DistroBaker 17a515
        _dep="$_dep znet"
DistroBaker 17a515
    fi
DistroBaker 17a515
b494b7
    if [[ -n "$(ls -A /sys/class/drm 2> /dev/null)" ]] || [[ -d /sys/module/hyperv_fb ]]; then
DistroBaker 17a515
        add_opt_module drm
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_generic_fence_kdump || is_pcs_fence_kdump; then
Petr Šabata f5bf49
        _dep="$_dep network"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
bf4667
    echo "$_dep"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_is_bridge() {
b494b7
    [[ -d /sys/class/net/"$1"/bridge ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_is_bond() {
b494b7
    [[ -d /sys/class/net/"$1"/bonding ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_is_team() {
b494b7
    [[ -f /usr/bin/teamnl ]] && teamnl "$1" ports &> /dev/null
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_is_vlan() {
b494b7
    [[ -f /proc/net/vlan/"$1" ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
source_ifcfg_file() {
Petr Šabata f5bf49
    local ifcfg_file
Petr Šabata f5bf49
Coiby Xu b5c915
    dwarning "Network Scripts are deprecated. You are encouraged to set up network by NetworkManager."
bf4667
    ifcfg_file=$(get_ifcfg_filename "$1")
b494b7
    if [[ -f ${ifcfg_file} ]]; then
bf4667
        . "${ifcfg_file}"
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        dwarning "The ifcfg file of $1 is not found!"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu f06949
# $1: nmcli connection show output
Petr Šabata f5bf49
kdump_setup_dns() {
Coiby Xu f06949
    local _netdev="$1"
Coiby Xu f06949
    local _nm_show_cmd="$2"
Coiby Xu f06949
    local _nameserver _dns _tmp array
Petr Šabata f5bf49
    local _dnsfile=${initdir}/etc/cmdline.d/42dns.conf
Petr Šabata f5bf49
Coiby Xu f06949
    _tmp=$(get_nmcli_value_by_field "$_nm_show_cmd" "IP4.DNS")
bf4667
    # shellcheck disable=SC2206
b494b7
    array=(${_tmp//|/ })
c8fadd
    if [[ ${array[*]} ]]; then
b494b7
        for _dns in "${array[@]}"; do
Coiby Xu f06949
            echo "nameserver=$_dns" >> "$_dnsfile"
Coiby Xu f06949
        done
Coiby Xu f06949
    else
Coiby Xu f06949
        dwarning "Failed to get DNS info via nmcli output. Now try sourcing ifcfg script"
Coiby Xu f06949
        source_ifcfg_file "$_netdev"
b494b7
        [[ -n $DNS1 ]] && echo "nameserver=$DNS1" > "$_dnsfile"
b494b7
        [[ -n $DNS2 ]] && echo "nameserver=$DNS2" >> "$_dnsfile"
Coiby Xu f06949
    fi
Petr Šabata f5bf49
b494b7
    while read -r content; do
bf4667
        _nameserver=$(echo "$content" | grep ^nameserver)
b494b7
        [[ -z $_nameserver ]] && continue
Petr Šabata f5bf49
bf4667
        _dns=$(echo "$_nameserver" | awk '{print $2}')
b494b7
        [[ -z $_dns ]] && continue
Petr Šabata f5bf49
b494b7
        if [[ ! -f $_dnsfile ]] || ! grep -q "$_dns" "$_dnsfile"; then
Petr Šabata f5bf49
            echo "nameserver=$_dns" >> "$_dnsfile"
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    done < "/etc/resolv.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu 54611c
# $1: repeat times
Coiby Xu 54611c
# $2: string to be repeated
Coiby Xu 54611c
# $3: separator
Coiby Xu 54611c
repeatedly_join_str() {
Coiby Xu 54611c
    local _count="$1"
Coiby Xu 54611c
    local _str="$2"
Coiby Xu 54611c
    local _separator="$3"
Coiby Xu 54611c
    local i _res
Coiby Xu 54611c
b494b7
    if [[ $_count -le 0 ]]; then
Coiby Xu 54611c
        echo -n ""
Coiby Xu 54611c
        return
Coiby Xu 54611c
    fi
Coiby Xu 54611c
Coiby Xu 54611c
    i=0
Coiby Xu 54611c
    _res="$_str"
Coiby Xu 54611c
    ((_count--))
Coiby Xu 54611c
b494b7
    while [[ $i -lt $_count ]]; do
Coiby Xu 54611c
        ((i++))
Coiby Xu 54611c
        _res="${_res}${_separator}${_str}"
Coiby Xu 54611c
    done
Coiby Xu 54611c
    echo -n "$_res"
Coiby Xu 54611c
}
Coiby Xu 54611c
Coiby Xu 54611c
# $1: prefix
Coiby Xu 54611c
# $2: ipv6_flag="-6" indicates it's IPv6
Coiby Xu 54611c
# Given a prefix, calculate the netmask (equivalent of "ipcalc -m")
Coiby Xu 54611c
# by concatenating three parts,
Coiby Xu 54611c
#  1) the groups with all bits set 1
Coiby Xu 54611c
#  2) a group with partial bits set to 0
Coiby Xu 54611c
#  3) the groups with all bits set to 0
Coiby Xu 54611c
cal_netmask_by_prefix() {
Coiby Xu 54611c
    local _prefix="$1"
Coiby Xu 54611c
    local _ipv6_flag="$2" _ipv6
Coiby Xu 54611c
    local _bits_per_octet=8
Coiby Xu 54611c
    local _count _res _octets_per_group _octets_total _seperator _total_groups
Coiby Xu 54611c
    local _max_group_value _max_group_value_repr _bits_per_group _tmp _zero_bits
Coiby Xu 54611c
b494b7
    if [[ $_ipv6_flag == "-6" ]]; then
Coiby Xu 54611c
        _ipv6=1
Coiby Xu 54611c
    else
Coiby Xu 54611c
        _ipv6=0
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    if [[ $_prefix -lt 0 || $_prefix -gt 128 ]] \
b494b7
        || ( ((!_ipv6)) && [[ $_prefix -gt 32 ]]); then
Coiby Xu 54611c
        derror "Bad prefix:$_prefix for calculating netmask"
Coiby Xu 54611c
        exit 1
Coiby Xu 54611c
    fi
Coiby Xu 54611c
Coiby Xu 54611c
    if ((_ipv6)); then
Coiby Xu 54611c
        _octets_per_group=2
Coiby Xu 54611c
        _octets_total=16
Coiby Xu 54611c
        _seperator=":"
Coiby Xu 54611c
    else
Coiby Xu 54611c
        _octets_per_group=1
Coiby Xu 54611c
        _octets_total=4
Coiby Xu 54611c
        _seperator="."
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    _total_groups=$((_octets_total / _octets_per_group))
Coiby Xu 54611c
    _bits_per_group=$((_octets_per_group * _bits_per_octet))
Coiby Xu 54611c
    _max_group_value=$(((1 << _bits_per_group) - 1))
Coiby Xu 54611c
Coiby Xu 54611c
    if ((_ipv6)); then
Coiby Xu 54611c
        _max_group_value_repr=$(printf "%x" $_max_group_value)
Coiby Xu 54611c
    else
Coiby Xu 54611c
        _max_group_value_repr="$_max_group_value"
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    _count=$((_prefix / _octets_per_group / _bits_per_octet))
Coiby Xu 54611c
    _first_part=$(repeatedly_join_str "$_count" "$_max_group_value_repr" "$_seperator")
Coiby Xu 54611c
    _res="$_first_part"
Coiby Xu 54611c
b494b7
    _tmp=$((_octets_total * _bits_per_octet - _prefix))
480de7
    _zero_bits=$((_tmp % _bits_per_group))
b494b7
    if [[ $_zero_bits -ne 0 ]]; then
Coiby Xu 54611c
        _second_part=$((_max_group_value >> _zero_bits << _zero_bits))
Coiby Xu 54611c
        if ((_ipv6)); then
Coiby Xu 54611c
            _second_part=$(printf "%x" $_second_part)
Coiby Xu 54611c
        fi
Coiby Xu 54611c
        ((_count++))
b494b7
        if [[ -z $_first_part ]]; then
Coiby Xu 54611c
            _res="$_second_part"
Coiby Xu 54611c
        else
Coiby Xu 54611c
            _res="${_first_part}${_seperator}${_second_part}"
Coiby Xu 54611c
        fi
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    _count=$((_total_groups - _count))
b494b7
    if [[ $_count -eq 0 ]]; then
Coiby Xu 54611c
        echo -n "$_res"
Coiby Xu 54611c
        return
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    if ((_ipv6)) && [[ $_count -gt 1 ]]; then
Coiby Xu 54611c
        # use condensed notion for IPv6
Coiby Xu 54611c
        _third_part=":"
Coiby Xu 54611c
    else
Coiby Xu 54611c
        _third_part=$(repeatedly_join_str "$_count" "0" "$_seperator")
Coiby Xu 54611c
    fi
Coiby Xu 54611c
b494b7
    if [[ -z $_res ]] && ((!_ipv6)); then
Coiby Xu 54611c
        echo -n "${_third_part}"
Coiby Xu 54611c
    else
Coiby Xu 54611c
        echo -n "${_res}${_seperator}${_third_part}"
Coiby Xu 54611c
    fi
Coiby Xu 54611c
}
Coiby Xu 54611c
Petr Šabata f5bf49
#$1: netdev name
Petr Šabata f5bf49
#$2: srcaddr
Petr Šabata f5bf49
#if it use static ip echo it, or echo null
Petr Šabata f5bf49
kdump_static_ip() {
Coiby Xu 69c605
    local _netdev="$1" _srcaddr="$2" kdumpnic="$3" _ipv6_flag
Coiby Xu 54611c
    local _netmask _gateway _ipaddr _target _nexthop _prefix
Petr Šabata f5bf49
bf4667
    _ipaddr=$(ip addr show dev "$_netdev" permanent | awk "/ $_srcaddr\/.* /{print \$2}")
Petr Šabata f5bf49
bf4667
    if is_ipv6_address "$_srcaddr"; then
Petr Šabata f5bf49
        _ipv6_flag="-6"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
b494b7
    if [[ -n $_ipaddr ]]; then
b494b7
        _gateway=$(ip $_ipv6_flag route list dev "$_netdev" \
b494b7
            | awk '/^default /{print $3}' | head -n 1)
Petr Šabata f5bf49
b494b7
        if [[ "x" != "x"$_ipv6_flag ]]; then
Petr Šabata f5bf49
            # _ipaddr="2002::56ff:feb6:56d5/64", _netmask is the number after "/"
Petr Šabata f5bf49
            _netmask=${_ipaddr#*\/}
Petr Šabata f5bf49
            _srcaddr="[$_srcaddr]"
Petr Šabata f5bf49
            _gateway="[$_gateway]"
Petr Šabata f5bf49
        else
Coiby Xu 54611c
            _prefix=$(cut -d'/' -f2 <<< "$_ipaddr")
f6d6b6
            if ! _netmask=$(cal_netmask_by_prefix "$_prefix" "$_ipv6_flag"); then
Coiby Xu 54611c
                derror "Failed to calculate netmask for $_ipaddr"
Coiby Xu 54611c
                exit 1
Coiby Xu 54611c
            fi
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
        echo -n "${_srcaddr}::${_gateway}:${_netmask}::"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
b494b7
    /sbin/ip $_ipv6_flag route show | grep -v default \
b494b7
        | grep ".*via.* $_netdev " | grep -v "^[[:space:]]*nexthop" \
b494b7
        | while read -r _route; do
b494b7
            _target=$(echo "$_route" | awk '{print $1}')
b494b7
            _nexthop=$(echo "$_route" | awk '{print $3}')
b494b7
            if [[ "x" != "x"$_ipv6_flag ]]; then
b494b7
                _target="[$_target]"
b494b7
                _nexthop="[$_nexthop]"
b494b7
            fi
b494b7
            echo "rd.route=$_target:$_nexthop:$kdumpnic"
b494b7
        done >> "${initdir}/etc/cmdline.d/45route-static.conf"
Petr Šabata f5bf49
bf4667
    kdump_handle_mulitpath_route "$_netdev" "$_srcaddr" "$kdumpnic"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_handle_mulitpath_route() {
Coiby Xu 69c605
    local _netdev="$1" _srcaddr="$2" kdumpnic="$3" _ipv6_flag
Petr Šabata f5bf49
    local _target _nexthop _route _weight _max_weight _rule
Petr Šabata f5bf49
bf4667
    if is_ipv6_address "$_srcaddr"; then
Petr Šabata f5bf49
        _ipv6_flag="-6"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
67611b
    while IFS="" read -r _route; do
b494b7
        if [[ $_route =~ [[:space:]]+nexthop ]]; then
d07b20
            _route=${_route##[[:space:]]}
Petr Šabata f5bf49
            # Parse multipath route, using previous _target
b494b7
            [[ $_target == 'default' ]] && continue
b494b7
            [[ $_route =~ .*via.*\ $_netdev ]] || continue
Petr Šabata f5bf49
16c282
            _weight=$(echo "$_route" | cut -d ' ' -f7)
b494b7
            if [[ $_weight -gt $_max_weight ]]; then
16c282
                _nexthop=$(echo "$_route" | cut -d ' ' -f3)
Petr Šabata f5bf49
                _max_weight=$_weight
b494b7
                if [[ "x" != "x"$_ipv6_flag ]]; then
Coiby Xu 69c605
                    _rule="rd.route=[$_target]:[$_nexthop]:$kdumpnic"
Petr Šabata f5bf49
                else
Coiby Xu 69c605
                    _rule="rd.route=$_target:$_nexthop:$kdumpnic"
Petr Šabata f5bf49
                fi
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        else
b494b7
            [[ -n $_rule ]] && echo "$_rule"
16c282
            _target=$(echo "$_route" | cut -d ' ' -f1)
Petr Šabata f5bf49
            _rule="" _max_weight=0 _weight=0
Petr Šabata f5bf49
        fi
b494b7
    done >> "${initdir}/etc/cmdline.d/45route-static.conf" \
Petr Šabata f5bf49
        <<< "$(/sbin/ip $_ipv6_flag route show)"
Petr Šabata f5bf49
bf4667
    [[ -n $_rule ]] && echo "$_rule" >> "${initdir}/etc/cmdline.d/45route-static.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_get_mac_addr() {
bf4667
    cat "/sys/class/net/$1/address"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#Bonding or team master modifies the mac address
Petr Šabata f5bf49
#of its slaves, we should use perm address
Petr Šabata f5bf49
kdump_get_perm_addr() {
633084
    local addr
633084
    addr=$(ethtool -P "$1" | sed -e 's/Permanent address: //')
b494b7
    if [[ -z $addr ]] || [[ $addr == "00:00:00:00:00:00" ]]; then
Petr Šabata f5bf49
        derror "Can't get the permanent address of $1"
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        echo "$addr"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Prefix kernel assigned names with "kdump-". EX: eth0 -> kdump-eth0
Petr Šabata f5bf49
# Because kernel assigned names are not persistent between 1st and 2nd
Petr Šabata f5bf49
# kernel. We could probably end up with eth0 being eth1, eth0 being
Petr Šabata f5bf49
# eth1, and naming conflict happens.
Petr Šabata f5bf49
kdump_setup_ifname() {
Petr Šabata f5bf49
    local _ifname
Petr Šabata f5bf49
Petr Šabata f5bf49
    # If ifname already has 'kdump-' prefix, we must be switching from
Petr Šabata f5bf49
    # fadump to kdump. Skip prefixing 'kdump-' in this case as adding
Petr Šabata f5bf49
    # another prefix may truncate the ifname. Since an ifname with
Petr Šabata f5bf49
    # 'kdump-' is already persistent, this should be fine.
Petr Šabata f5bf49
    if [[ $1 =~ eth* ]] && [[ ! $1 =~ ^kdump-* ]]; then
Petr Šabata f5bf49
        _ifname="kdump-$1"
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        _ifname="$1"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo "$_ifname"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_bridge() {
Petr Šabata f5bf49
    local _netdev=$1
Petr Šabata f5bf49
    local _brif _dev _mac _kdumpdev
72c3be
    for _dev in "/sys/class/net/$_netdev/brif/"*; do
72c3be
        [[ -e $_dev ]] || continue
72c3be
        _dev=${_dev##*/}
Petr Šabata f5bf49
        _kdumpdev=$_dev
Petr Šabata f5bf49
        if kdump_is_bond "$_dev"; then
f6d6b6
            (kdump_setup_bond "$_dev" "$(get_nmcli_connection_show_cmd_by_ifname "$_dev")") || exit 1
Petr Šabata f5bf49
        elif kdump_is_team "$_dev"; then
Petr Šabata f5bf49
            kdump_setup_team "$_dev"
Petr Šabata f5bf49
        elif kdump_is_vlan "$_dev"; then
Petr Šabata f5bf49
            kdump_setup_vlan "$_dev"
Petr Šabata f5bf49
        else
bf4667
            _mac=$(kdump_get_mac_addr "$_dev")
bf4667
            _kdumpdev=$(kdump_setup_ifname "$_dev")
bf4667
            echo -n " ifname=$_kdumpdev:$_mac" >> "${initdir}/etc/cmdline.d/41bridge.conf"
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
        _brif+="$_kdumpdev,"
Petr Šabata f5bf49
    done
d07b20
    echo " bridge=$_netdev:${_brif%,}" >> "${initdir}/etc/cmdline.d/41bridge.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu c23189
# drauct takes bond=<bondname>[:<bondslaves>:[:<options>]] syntax to parse
Coiby Xu c23189
#    bond. For example:
Coiby Xu c23189
#     bond=bond0:eth0,eth1:mode=balance-rr
Petr Šabata f5bf49
kdump_setup_bond() {
Coiby Xu c23189
    local _netdev="$1"
Coiby Xu c23189
    local _nm_show_cmd="$2"
Coiby Xu c23189
    local _dev _mac _slaves _kdumpdev _bondoptions
bf4667
    for _dev in $(cat "/sys/class/net/$_netdev/bonding/slaves"); do
bf4667
        _mac=$(kdump_get_perm_addr "$_dev")
bf4667
        _kdumpdev=$(kdump_setup_ifname "$_dev")
bf4667
        echo -n " ifname=$_kdumpdev:$_mac" >> "${initdir}/etc/cmdline.d/42bond.conf"
Petr Šabata f5bf49
        _slaves+="$_kdumpdev,"
Petr Šabata f5bf49
    done
d07b20
    echo -n " bond=$_netdev:${_slaves%,}" >> "${initdir}/etc/cmdline.d/42bond.conf"
Petr Šabata f5bf49
Coiby Xu c23189
    _bondoptions=$(get_nmcli_value_by_field "$_nm_show_cmd" "bond.options")
Coiby Xu c23189
b494b7
    if [[ -z $_bondoptions ]]; then
Coiby Xu c23189
        dwarning "Failed to get bond configuration via nmlci output. Now try sourcing ifcfg script."
bf4667
        source_ifcfg_file "$_netdev"
bf4667
        _bondoptions="$(echo "$BONDING_OPTS" | xargs echo | tr " " ",")"
Coiby Xu c23189
    fi
Coiby Xu c23189
b494b7
    if [[ -z $_bondoptions ]]; then
Coiby Xu c23189
        derror "Get empty bond options"
Coiby Xu c23189
        exit 1
Coiby Xu c23189
    fi
Petr Šabata f5bf49
bf4667
    echo ":$_bondoptions" >> "${initdir}/etc/cmdline.d/42bond.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_team() {
Petr Šabata f5bf49
    local _netdev=$1
Petr Šabata f5bf49
    local _dev _mac _slaves _kdumpdev
bf4667
    for _dev in $(teamnl "$_netdev" ports | awk -F':' '{print $2}'); do
bf4667
        _mac=$(kdump_get_perm_addr "$_dev")
bf4667
        _kdumpdev=$(kdump_setup_ifname "$_dev")
bf4667
        echo -n " ifname=$_kdumpdev:$_mac" >> "${initdir}/etc/cmdline.d/44team.conf"
Petr Šabata f5bf49
        _slaves+="$_kdumpdev,"
Petr Šabata f5bf49
    done
d07b20
    echo " team=$_netdev:${_slaves%,}" >> "${initdir}/etc/cmdline.d/44team.conf"
Petr Šabata f5bf49
    #Buggy version teamdctl outputs to stderr!
Petr Šabata f5bf49
    #Try to use the latest version of teamd.
f6d6b6
    if ! teamdctl "$_netdev" config dump > "${initdir}/tmp/$$-$_netdev.conf"; then
Petr Šabata f5bf49
        derror "teamdctl failed."
Petr Šabata f5bf49
        exit 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    inst_dir /etc/teamd
bf4667
    inst_simple "${initdir}/tmp/$$-$_netdev.conf" "/etc/teamd/$_netdev.conf"
bf4667
    rm -f "${initdir}/tmp/$$-$_netdev.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_vlan() {
Petr Šabata f5bf49
    local _netdev=$1
633084
    local _phydev
633084
    local _netmac
Petr Šabata f5bf49
    local _kdumpdev
Petr Šabata f5bf49
633084
    _phydev="$(awk '/^Device:/{print $2}' /proc/net/vlan/"$_netdev")"
633084
    _netmac="$(kdump_get_mac_addr "$_phydev")"
633084
DistroBaker a10140
    #Just support vlan over bond and team
Petr Šabata f5bf49
    if kdump_is_bridge "$_phydev"; then
Petr Šabata f5bf49
        derror "Vlan over bridge is not supported!"
Petr Šabata f5bf49
        exit 1
Petr Šabata f5bf49
    elif kdump_is_bond "$_phydev"; then
f6d6b6
        (kdump_setup_bond "$_phydev" "$(get_nmcli_connection_show_cmd_by_ifname "$_phydev")") || exit 1
bf4667
        echo " vlan=$(kdump_setup_ifname "$_netdev"):$_phydev" > "${initdir}/etc/cmdline.d/43vlan.conf"
Petr Šabata f5bf49
    else
bf4667
        _kdumpdev="$(kdump_setup_ifname "$_phydev")"
bf4667
        echo " vlan=$(kdump_setup_ifname "$_netdev"):$_kdumpdev ifname=$_kdumpdev:$_netmac" > "${initdir}/etc/cmdline.d/43vlan.conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu f0ecf8
# find online znet device
Coiby Xu f0ecf8
# return ifname (_netdev)
Coiby Xu f0ecf8
# code reaped from the list_configured function of
Coiby Xu f0ecf8
# https://github.com/hreinecke/s390-tools/blob/master/zconf/znetconf
Coiby Xu f0ecf8
find_online_znet_device() {
b494b7
    local CCWGROUPBUS_DEVICEDIR="/sys/bus/ccwgroup/devices"
b494b7
    local NETWORK_DEVICES d ifname ONLINE
b494b7
b494b7
    [[ ! -d $CCWGROUPBUS_DEVICEDIR ]] && return
b494b7
    NETWORK_DEVICES=$(find $CCWGROUPBUS_DEVICEDIR)
b494b7
    for d in $NETWORK_DEVICES; do
b494b7
        [[ ! -f "$d/online" ]] && continue
b494b7
        read -r ONLINE < "$d/online"
b494b7
        if [[ $ONLINE -ne 1 ]]; then
b494b7
            continue
b494b7
        fi
b494b7
        # determine interface name, if there (only for qeth and if
b494b7
        # device is online)
b494b7
        if [[ -f $d/if_name ]]; then
b494b7
            read -r ifname < "$d/if_name"
b494b7
        elif [[ -d $d/net ]]; then
b494b7
            ifname=$(ls "$d/net/")
b494b7
        fi
b494b7
        [[ -n $ifname ]] && break
b494b7
    done
b494b7
    echo -n "$ifname"
Coiby Xu f0ecf8
}
Coiby Xu f0ecf8
Petr Šabata f5bf49
# setup s390 znet cmdline
Coiby Xu d408ec
# $1: netdev (ifname)
Coiby Xu d408ec
# $2: nmcli connection show output
Petr Šabata f5bf49
kdump_setup_znet() {
Coiby Xu d408ec
    local _netdev="$1"
Coiby Xu d408ec
    local _nmcli_cmd="$2"
Coiby Xu d408ec
    local s390_prefix="802-3-ethernet.s390-"
Petr Šabata f5bf49
    local _options=""
Coiby Xu d408ec
    local NETTYPE
Coiby Xu d408ec
    local SUBCHANNELS
Coiby Xu d408ec
Coiby Xu d408ec
    NETTYPE=$(get_nmcli_value_by_field "$_nmcli_cmd" "${s390_prefix}nettype")
Coiby Xu d408ec
    SUBCHANNELS=$(get_nmcli_value_by_field "$_nmcli_cmd" "${s390_prefix}subchannels")
Coiby Xu d408ec
    _options=$(get_nmcli_value_by_field "$_nmcli_cmd" "${s390_prefix}options")
Coiby Xu d408ec
b494b7
    if [[ -z $NETTYPE || -z $SUBCHANNELS || -z $_options ]]; then
Coiby Xu d408ec
        dwarning "Failed to get znet configuration via nmlci output. Now try sourcing ifcfg script."
bf4667
        source_ifcfg_file "$_netdev"
Coiby Xu d408ec
        for i in $OPTIONS; do
Coiby Xu d408ec
            _options=${_options},$i
Coiby Xu d408ec
        done
Coiby Xu d408ec
    fi
Petr Šabata f5bf49
b494b7
    if [[ -z $NETTYPE || -z $SUBCHANNELS || -z $_options ]]; then
Coiby Xu d408ec
        exit 1
Coiby Xu d408ec
    fi
Petr Šabata f5bf49
bf4667
    echo "rd.znet=${NETTYPE},${SUBCHANNELS},${_options} rd.znet_ifname=$_netdev:${SUBCHANNELS}" > "${initdir}/etc/cmdline.d/30znet.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
b494b7
kdump_get_ip_route() {
f6d6b6
    local _route
f6d6b6
    if ! _route=$(/sbin/ip -o route get to "$1" 2>&1;; then
c373a2
        derror "Bad kdump network destination: $1"
c373a2
        exit 1
c373a2
    fi
bf4667
    echo "$_route"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
b494b7
kdump_get_ip_route_field() {
37b61e
    echo "$1" | sed -n -e "s/^.*\<$2\>\s\+\(\S\+\).*$/\1/p"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
b494b7
kdump_get_remote_ip() {
633084
    local _remote _remote_temp
633084
    _remote=$(get_remote_host "$1")
bf4667
    if is_hostname "$_remote"; then
bf4667
        _remote_temp=$(getent ahosts "$_remote" | grep -v : | head -n 1)
b494b7
        if [[ -z $_remote_temp ]]; then
bf4667
            _remote_temp=$(getent ahosts "$_remote" | head -n 1)
Petr Šabata f5bf49
        fi
bf4667
        _remote=$(echo "$_remote_temp" | awk '{print $1}')
Petr Šabata f5bf49
    fi
bf4667
    echo "$_remote"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Setup dracut to bring up network interface that enable
Petr Šabata f5bf49
# initramfs accessing giving destination
Petr Šabata f5bf49
# $1: destination host
Petr Šabata f5bf49
kdump_install_net() {
Coiby Xu d408ec
    local _destaddr _srcaddr _route _netdev _nm_show_cmd kdumpnic
Petr Šabata f5bf49
    local _static _proto _ip_conf _ip_opts _ifname_opts
Coiby Xu f0ecf8
    local _znet_netdev _nm_show_cmd_znet
Petr Šabata f5bf49
bf4667
    _destaddr=$(kdump_get_remote_ip "$1")
bf4667
    _route=$(kdump_get_ip_route "$_destaddr")
Petr Šabata f5bf49
    _srcaddr=$(kdump_get_ip_route_field "$_route" "src")
Petr Šabata f5bf49
    _netdev=$(kdump_get_ip_route_field "$_route" "dev")
Coiby Xu d408ec
    _nm_show_cmd=$(get_nmcli_connection_show_cmd_by_ifname "$_netdev")
bf4667
    _netmac=$(kdump_get_mac_addr "$_netdev")
bf4667
    kdumpnic=$(kdump_setup_ifname "$_netdev")
Petr Šabata f5bf49
Coiby Xu f0ecf8
    _znet_netdev=$(find_online_znet_device)
b494b7
    if [[ -n $_znet_netdev ]]; then
Coiby Xu f0ecf8
        _nm_show_cmd_znet=$(get_nmcli_connection_show_cmd_by_ifname "$_znet_netdev")
f6d6b6
        if ! (kdump_setup_znet "$_znet_netdev" "$_nm_show_cmd_znet"); then
Coiby Xu d408ec
            derror "Failed to set up znet"
Coiby Xu d408ec
            exit 1
Coiby Xu d408ec
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
bf4667
    _static=$(kdump_static_ip "$_netdev" "$_srcaddr" "$kdumpnic")
b494b7
    if [[ -n $_static ]]; then
Petr Šabata f5bf49
        _proto=none
bf4667
    elif is_ipv6_address "$_srcaddr"; then
DistroBaker 7d0122
        _proto=auto6
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        _proto=dhcp
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    _ip_conf="${initdir}/etc/cmdline.d/40ip.conf"
Coiby Xu 6a8e08
    _ip_opts=" ip=${_static}$kdumpnic:${_proto}"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # dracut doesn't allow duplicated configuration for same NIC, even they're exactly the same.
Petr Šabata f5bf49
    # so we have to avoid adding duplicates
Petr Šabata f5bf49
    # We should also check /proc/cmdline for existing ip=xx arg.
Petr Šabata f5bf49
    # For example, iscsi boot will specify ip=xxx arg in cmdline.
b494b7
    if [[ ! -f $_ip_conf ]] || ! grep -q "$_ip_opts" "$_ip_conf" \
b494b7
        && ! grep -q "ip=[^[:space:]]*$_netdev" /proc/cmdline; then
bf4667
        echo "$_ip_opts" >> "$_ip_conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    if kdump_is_bridge "$_netdev"; then
Petr Šabata f5bf49
        kdump_setup_bridge "$_netdev"
Petr Šabata f5bf49
    elif kdump_is_bond "$_netdev"; then
f6d6b6
        (kdump_setup_bond "$_netdev" "$_nm_show_cmd") || exit 1
Petr Šabata f5bf49
    elif kdump_is_team "$_netdev"; then
Petr Šabata f5bf49
        kdump_setup_team "$_netdev"
Petr Šabata f5bf49
    elif kdump_is_vlan "$_netdev"; then
Petr Šabata f5bf49
        kdump_setup_vlan "$_netdev"
Petr Šabata f5bf49
    else
Coiby Xu 6a8e08
        _ifname_opts=" ifname=$kdumpnic:$_netmac"
bf4667
        echo "$_ifname_opts" >> "$_ip_conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Coiby Xu f06949
    kdump_setup_dns "$_netdev" "$_nm_show_cmd"
Petr Šabata f5bf49
dcb59c
    if [[ ! -f ${initdir}/etc/cmdline.d/50neednet.conf ]]; then
Petr Šabata f5bf49
        # network-manager module needs this parameter
bf4667
        echo "rd.neednet" >> "${initdir}/etc/cmdline.d/50neednet.conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Save netdev used for kdump as cmdline
Petr Šabata f5bf49
    # Whoever calling kdump_install_net() is setting up the default gateway,
Petr Šabata f5bf49
    # ie. bootdev/kdumpnic. So don't override the setting if calling
Petr Šabata f5bf49
    # kdump_install_net() for another time. For example, after setting eth0 as
Petr Šabata f5bf49
    # the default gate way for network dump, eth1 in the fence kdump path will
Petr Šabata f5bf49
    # call kdump_install_net again and we don't want eth1 to be the default
Petr Šabata f5bf49
    # gateway.
b494b7
    if [[ ! -f ${initdir}/etc/cmdline.d/60kdumpnic.conf ]] \
b494b7
        && [[ ! -f ${initdir}/etc/cmdline.d/70bootdev.conf ]]; then
bf4667
        echo "kdumpnic=$kdumpnic" > "${initdir}/etc/cmdline.d/60kdumpnic.conf"
bf4667
        echo "bootdev=$kdumpnic" > "${initdir}/etc/cmdline.d/70bootdev.conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# install etc/kdump/pre.d and /etc/kdump/post.d
Petr Šabata f5bf49
kdump_install_pre_post_conf() {
dcb59c
    if [[ -d /etc/kdump/pre.d ]]; then
Petr Šabata f5bf49
        for file in /etc/kdump/pre.d/*; do
b494b7
            if [[ -x $file ]]; then
bf4667
                dracut_install "$file"
dcb59c
            elif [[ $file != "/etc/kdump/pre.d/*" ]]; then
b494b7
                echo "$file is not executable"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
dcb59c
    if [[ -d /etc/kdump/post.d ]]; then
Petr Šabata f5bf49
        for file in /etc/kdump/post.d/*; do
b494b7
            if [[ -x $file ]]; then
bf4667
                dracut_install "$file"
dcb59c
            elif [[ $file != "/etc/kdump/post.d/*" ]]; then
Petr Šabata f5bf49
                echo "$file is not executable"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
b494b7
default_dump_target_install_conf() {
Petr Šabata f5bf49
    local _target _fstype
Petr Šabata f5bf49
    local _mntpoint _save_path
Petr Šabata f5bf49
Petr Šabata f5bf49
    is_user_configured_dump_target && return
Petr Šabata f5bf49
bf4667
    _save_path=$(get_bind_mount_source "$(get_save_path)")
bf4667
    _target=$(get_target_from_path "$_save_path")
bf4667
    _mntpoint=$(get_mntpoint_from_target "$_target")
Petr Šabata f5bf49
bf4667
    _fstype=$(get_fs_type_from_target "$_target")
bf4667
    if is_fs_type_nfs "$_fstype"; then
Petr Šabata f5bf49
        kdump_install_net "$_target"
Petr Šabata f5bf49
        _fstype="nfs"
Petr Šabata f5bf49
    else
bf4667
        _target=$(kdump_get_persistent_dev "$_target")
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
bf4667
    echo "$_fstype $_target" >> "${initdir}/tmp/$$-kdump.conf"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # don't touch the path under root mount
b494b7
    if [[ $_mntpoint != "/" ]]; then
Petr Šabata f5bf49
        _save_path=${_save_path##"$_mntpoint"}
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    #erase the old path line, then insert the parsed path
bf4667
    sed -i "/^path/d" "${initdir}/tmp/$$-kdump.conf"
bf4667
    echo "path $_save_path" >> "${initdir}/tmp/$$-kdump.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#install kdump.conf and what user specifies in kdump.conf
Petr Šabata f5bf49
kdump_install_conf() {
Petr Šabata f5bf49
    local _opt _val _pdev
67b8dd
67b8dd
    kdump_read_conf > "${initdir}/tmp/$$-kdump.conf"
Petr Šabata f5bf49
b494b7
    while read -r _opt _val; do
Petr Šabata f5bf49
        # remove inline comments after the end of a directive.
Petr Šabata f5bf49
        case "$_opt" in
b494b7
            raw)
b494b7
                _pdev=$(persistent_policy="by-id" kdump_get_persistent_dev "$_val")
b494b7
                sed -i -e "s#^${_opt}[[:space:]]\+$_val#$_opt $_pdev#" "${initdir}/tmp/$$-kdump.conf"
b494b7
                ;;
b494b7
            ext[234] | xfs | btrfs | minix)
b494b7
                _pdev=$(kdump_get_persistent_dev "$_val")
b494b7
                sed -i -e "s#^${_opt}[[:space:]]\+$_val#$_opt $_pdev#" "${initdir}/tmp/$$-kdump.conf"
b494b7
                ;;
b494b7
            ssh | nfs)
b494b7
                kdump_install_net "$_val"
b494b7
                ;;
b494b7
            dracut_args)
b494b7
                if [[ $(get_dracut_args_fstype "$_val") == nfs* ]]; then
b494b7
                    kdump_install_net "$(get_dracut_args_target "$_val")"
b494b7
                fi
b494b7
                ;;
b494b7
            kdump_pre | kdump_post | extra_bins)
b494b7
                dracut_install "$_val"
b494b7
                ;;
b494b7
            core_collector)
b494b7
                dracut_install "${_val%%[[:blank:]]*}"
b494b7
                ;;
Petr Šabata f5bf49
        esac
67b8dd
    done <<< "$(kdump_read_conf)"
Petr Šabata f5bf49
Petr Šabata f5bf49
    kdump_install_pre_post_conf
Petr Šabata f5bf49
Petr Šabata f5bf49
    default_dump_target_install_conf
Petr Šabata f5bf49
b494b7
    kdump_configure_fence_kdump "${initdir}/tmp/$$-kdump.conf"
Petr Šabata f5bf49
    inst "${initdir}/tmp/$$-kdump.conf" "/etc/kdump.conf"
bf4667
    rm -f "${initdir}/tmp/$$-kdump.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Kairui Song de11eb
# Default sysctl parameters should suffice for kdump kernel.
Kairui Song de11eb
# Remove custom configurations sysctl.conf & sysctl.d/*
Kairui Song de11eb
remove_sysctl_conf() {
Kairui Song de11eb
Petr Šabata f5bf49
    # As custom configurations like vm.min_free_kbytes can lead
Petr Šabata f5bf49
    # to OOM issues in kdump kernel, avoid them
Petr Šabata f5bf49
    rm -f "${initdir}/etc/sysctl.conf"
Petr Šabata f5bf49
    rm -rf "${initdir}/etc/sysctl.d"
Petr Šabata f5bf49
    rm -rf "${initdir}/run/sysctl.d"
Petr Šabata f5bf49
    rm -rf "${initdir}/usr/lib/sysctl.d"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_iscsi_get_rec_val() {
Petr Šabata f5bf49
Petr Šabata f5bf49
    local result
Petr Šabata f5bf49
Petr Šabata f5bf49
    # The open-iscsi 742 release changed to using flat files in
Petr Šabata f5bf49
    # /var/lib/iscsi.
Petr Šabata f5bf49
bf4667
    result=$(/sbin/iscsiadm --show -m session -r "$1" | grep "^${2} = ")
Petr Šabata f5bf49
    result=${result##* = }
bf4667
    echo "$result"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_get_iscsi_initiator() {
Petr Šabata f5bf49
    local _initiator
Petr Šabata f5bf49
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
Petr Šabata f5bf49
b494b7
    [[ -f $initiator_conf ]] || return 1
Petr Šabata f5bf49
67611b
    while read -r _initiator; do
b494b7
        [[ -z ${_initiator%%#*} ]] && continue # Skip comment lines
Petr Šabata f5bf49
Petr Šabata f5bf49
        case $_initiator in
Petr Šabata f5bf49
            InitiatorName=*)
Petr Šabata f5bf49
                initiator=${_initiator#InitiatorName=}
Petr Šabata f5bf49
                echo "rd.iscsi.initiator=${initiator}"
b494b7
                return 0
b494b7
                ;;
Petr Šabata f5bf49
            *) ;;
Petr Šabata f5bf49
        esac
Petr Šabata f5bf49
    done < ${initiator_conf}
Petr Šabata f5bf49
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Figure out iBFT session according to session type
Petr Šabata f5bf49
is_ibft() {
b494b7
    [[ "$(kdump_iscsi_get_rec_val "$1" "node.discovery_type")" == fw ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_iscsi_device() {
Petr Šabata f5bf49
    local path=$1
b494b7
    local tgt_name
b494b7
    local tgt_ipaddr
b494b7
    local username
b494b7
    local password
b494b7
    local userpwd_str
b494b7
    local username_in
b494b7
    local password_in
b494b7
    local userpwd_in_str
b494b7
    local netroot_str
b494b7
    local initiator_str
Petr Šabata f5bf49
    local netroot_conf="${initdir}/etc/cmdline.d/50iscsi.conf"
Petr Šabata f5bf49
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
Petr Šabata f5bf49
Petr Šabata f5bf49
    dinfo "Found iscsi component $1"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Check once before getting explicit values, so we can bail out early,
Petr Šabata f5bf49
    # e.g. in case of pure-hardware(all-offload) iscsi.
b494b7
    if ! /sbin/iscsiadm -m session -r "$path" &> /dev/null; then
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
bf4667
    if is_ibft "$path"; then
Petr Šabata f5bf49
        return
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Remove software iscsi cmdline generated by 95iscsi,
Petr Šabata f5bf49
    # and let kdump regenerate here.
bf4667
    rm -f "${initdir}/etc/cmdline.d/95iscsi.conf"
Petr Šabata f5bf49
bf4667
    tgt_name=$(kdump_iscsi_get_rec_val "$path" "node.name")
bf4667
    tgt_ipaddr=$(kdump_iscsi_get_rec_val "$path" "node.conn\[0\].address")
Petr Šabata f5bf49
Petr Šabata f5bf49
    # get and set username and password details
bf4667
    username=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.username")
b494b7
    [[ $username == "<empty>" ]] && username=""
bf4667
    password=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.password")
b494b7
    [[ $password == "<empty>" ]] && password=""
bf4667
    username_in=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.username_in")
b494b7
    [[ -n $username ]] && userpwd_str="$username:$password"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # get and set incoming username and password details
b494b7
    [[ $username_in == "<empty>" ]] && username_in=""
bf4667
    password_in=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.password_in")
b494b7
    [[ $password_in == "<empty>" ]] && password_in=""
Petr Šabata f5bf49
b494b7
    [[ -n $username_in ]] && userpwd_in_str=":$username_in:$password_in"
Petr Šabata f5bf49
Petr Šabata f5bf49
    kdump_install_net "$tgt_ipaddr"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # prepare netroot= command line
Petr Šabata f5bf49
    # FIXME: Do we need to parse and set other parameters like protocol, port
Petr Šabata f5bf49
    #        iscsi_iface_name, netdev_name, LUN etc.
Petr Šabata f5bf49
bf4667
    if is_ipv6_address "$tgt_ipaddr"; then
Petr Šabata f5bf49
        tgt_ipaddr="[$tgt_ipaddr]"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    netroot_str="netroot=iscsi:${userpwd_str}${userpwd_in_str}@$tgt_ipaddr::::$tgt_name"
Petr Šabata f5bf49
bf4667
    [[ -f $netroot_conf ]] || touch "$netroot_conf"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # If netroot target does not exist already, append.
bf4667
    if ! grep -q "$netroot_str" "$netroot_conf"; then
b494b7
        echo "$netroot_str" >> "$netroot_conf"
b494b7
        dinfo "Appended $netroot_str to $netroot_conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Setup initator
f6d6b6
    if ! initiator_str=$(kdump_get_iscsi_initiator); then
f6d6b6
        derror "Failed to get initiator name"
f6d6b6
        return 1
f6d6b6
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # If initiator details do not exist already, append.
bf4667
    if ! grep -q "$initiator_str" "$netroot_conf"; then
b494b7
        echo "$initiator_str" >> "$netroot_conf"
b494b7
        dinfo "Appended $initiator_str to $netroot_conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
b494b7
kdump_check_iscsi_targets() {
Petr Šabata f5bf49
    # If our prerequisites are not met, fail anyways.
b494b7
    type -P iscsistart > /dev/null || return 1
Petr Šabata f5bf49
Petr Šabata f5bf49
    kdump_check_setup_iscsi() (
Petr Šabata f5bf49
        local _dev
Petr Šabata f5bf49
        _dev=$1
Petr Šabata f5bf49
Petr Šabata f5bf49
        [[ -L /sys/dev/block/$_dev ]] || return
9abf44
        cd "$(readlink -f "/sys/dev/block/$_dev")" || return 1
Petr Šabata f5bf49
        until [[ -d sys || -d iscsi_session ]]; do
Petr Šabata f5bf49
            cd ..
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
        [[ -d iscsi_session ]] && kdump_setup_iscsi_device "$PWD"
Petr Šabata f5bf49
    )
Petr Šabata f5bf49
Petr Šabata f5bf49
    [[ $hostonly ]] || [[ $mount_needs ]] && {
Petr Šabata f5bf49
        for_each_host_dev_and_slaves_all kdump_check_setup_iscsi
Petr Šabata f5bf49
    }
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# hostname -a is deprecated, do it by ourself
Petr Šabata f5bf49
get_alias() {
Petr Šabata f5bf49
    local ips
Petr Šabata f5bf49
    local entries
Petr Šabata f5bf49
    local alias_set
Petr Šabata f5bf49
Petr Šabata f5bf49
    ips=$(hostname -I)
b494b7
    for ip in $ips; do
b494b7
        # in /etc/hosts, alias can come at the 2nd column
b494b7
        if entries=$(grep "$ip" /etc/hosts | awk '{ $1=""; print $0 }'); then
b494b7
            alias_set="$alias_set $entries"
b494b7
        fi
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
bf4667
    echo "$alias_set"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_localhost() {
633084
    local hostnames
633084
    local shortnames
633084
    local aliasname
Petr Šabata f5bf49
    local nodename=$1
Petr Šabata f5bf49
633084
    hostnames=$(hostname -A)
633084
    shortnames=$(hostname -A -s)
633084
    aliasname=$(get_alias)
Petr Šabata f5bf49
    hostnames="$hostnames $shortnames $aliasname"
Petr Šabata f5bf49
Petr Šabata f5bf49
    for name in ${hostnames}; do
b494b7
        if [[ $name == "$nodename" ]]; then
Petr Šabata f5bf49
            return 0
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# retrieves fence_kdump nodes from Pacemaker cluster configuration
Petr Šabata f5bf49
get_pcs_fence_kdump_nodes() {
Petr Šabata f5bf49
    local nodes
Petr Šabata f5bf49
Petr Šabata f5bf49
    pcs cluster sync > /dev/null 2>&1 && pcs cluster cib-upgrade > /dev/null 2>&1
Petr Šabata f5bf49
    # get cluster nodes from cluster cib, get interface and ip address
16c282
    nodelist=$(pcs cluster cib | xmllint --xpath "/cib/status/node_state/@uname" -)
Petr Šabata f5bf49
Petr Šabata f5bf49
    # nodelist is formed as 'uname="node1" uname="node2" ... uname="nodeX"'
Petr Šabata f5bf49
    # we need to convert each to node1, node2 ... nodeX in each iteration
Petr Šabata f5bf49
    for node in ${nodelist}; do
Petr Šabata f5bf49
        # convert $node from 'uname="nodeX"' to 'nodeX'
bf4667
        eval "$node"
bf4667
        nodename="$uname"
Petr Šabata f5bf49
        # Skip its own node name
bf4667
        if is_localhost "$nodename"; then
Petr Šabata f5bf49
            continue
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
        nodes="$nodes $nodename"
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
bf4667
    echo "$nodes"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# retrieves fence_kdump args from config file
Petr Šabata f5bf49
get_pcs_fence_kdump_args() {
dcb59c
    if [[ -f $FENCE_KDUMP_CONFIG_FILE ]]; then
bf4667
        . "$FENCE_KDUMP_CONFIG_FILE"
bf4667
        echo "$FENCE_KDUMP_OPTS"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_generic_fence_kdump_nodes() {
Petr Šabata f5bf49
    local filtered
Petr Šabata f5bf49
    local nodes
Petr Šabata f5bf49
14205c
    nodes=$(kdump_get_conf_val "fence_kdump_nodes")
Petr Šabata f5bf49
    for node in ${nodes}; do
Petr Šabata f5bf49
        # Skip its own node name
bf4667
        if is_localhost "$node"; then
Petr Šabata f5bf49
            continue
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
        filtered="$filtered $node"
Petr Šabata f5bf49
    done
bf4667
    echo "$filtered"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# setup fence_kdump in cluster
Petr Šabata f5bf49
# setup proper network and install needed files
b494b7
kdump_configure_fence_kdump() {
Petr Šabata f5bf49
    local kdump_cfg_file=$1
Petr Šabata f5bf49
    local nodes
Petr Šabata f5bf49
    local args
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_generic_fence_kdump; then
Petr Šabata f5bf49
        nodes=$(get_generic_fence_kdump_nodes)
Petr Šabata f5bf49
Petr Šabata f5bf49
    elif is_pcs_fence_kdump; then
Petr Šabata f5bf49
        nodes=$(get_pcs_fence_kdump_nodes)
Petr Šabata f5bf49
Petr Šabata f5bf49
        # set appropriate options in kdump.conf
bf4667
        echo "fence_kdump_nodes $nodes" >> "${kdump_cfg_file}"
Petr Šabata f5bf49
Petr Šabata f5bf49
        args=$(get_pcs_fence_kdump_args)
b494b7
        if [[ -n $args ]]; then
bf4667
            echo "fence_kdump_args $args" >> "${kdump_cfg_file}"
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        # fence_kdump not configured
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # setup network for each node
Petr Šabata f5bf49
    for node in ${nodes}; do
bf4667
        kdump_install_net "$node"
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
Petr Šabata f5bf49
    dracut_install /etc/hosts
Petr Šabata f5bf49
    dracut_install /etc/nsswitch.conf
bf4667
    dracut_install "$FENCE_KDUMP_SEND"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Install a random seed used to feed /dev/urandom
Petr Šabata f5bf49
# By the time kdump service starts, /dev/uramdom is already fed by systemd
Petr Šabata f5bf49
kdump_install_random_seed() {
e07098
    local poolsize
e07098
b494b7
    poolsize=$(< /proc/sys/kernel/random/poolsize)
Petr Šabata f5bf49
bf4667
    if [[ ! -d "${initdir}/var/lib/" ]]; then
bf4667
        mkdir -p "${initdir}/var/lib/"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
bf4667
    dd if=/dev/urandom of="${initdir}/var/lib/random-seed" \
b494b7
        bs="$poolsize" count=1 2> /dev/null
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_install_systemd_conf() {
Petr Šabata f5bf49
    # Kdump turns out to require longer default systemd mount timeout
Petr Šabata f5bf49
    # than 1st kernel(90s by default), we use default 300s for kdump.
f6d6b6
    if ! grep -q -r "^[[:space:]]*DefaultTimeoutStartSec=" "${initdir}/etc/systemd/system.conf"*; then
bf4667
        mkdir -p "${initdir}/etc/systemd/system.conf.d"
bf4667
        echo "[Manager]" > "${initdir}/etc/systemd/system.conf.d/kdump.conf"
bf4667
        echo "DefaultTimeoutStartSec=300s" >> "${initdir}/etc/systemd/system.conf.d/kdump.conf"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    # Forward logs to console directly, and don't read Kmsg, this avoids
DistroBaker 5cac7c
    # unneccessary memory consumption and make console output more useful.
Petr Šabata f5bf49
    # Only do so for non fadump image.
bf4667
    mkdir -p "${initdir}/etc/systemd/journald.conf.d"
bf4667
    echo "[Journal]" > "${initdir}/etc/systemd/journald.conf.d/kdump.conf"
bf4667
    echo "Storage=volatile" >> "${initdir}/etc/systemd/journald.conf.d/kdump.conf"
bf4667
    echo "ReadKMsg=no" >> "${initdir}/etc/systemd/journald.conf.d/kdump.conf"
bf4667
    echo "ForwardToConsole=yes" >> "${initdir}/etc/systemd/journald.conf.d/kdump.conf"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
install() {
DistroBaker 624a64
    kdump_module_init
Petr Šabata f5bf49
    kdump_install_conf
Kairui Song de11eb
    remove_sysctl_conf
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_ssh_dump_target; then
Petr Šabata f5bf49
        kdump_install_random_seed
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    dracut_install -o /etc/adjtime /etc/localtime
Petr Šabata f5bf49
    inst "$moddir/monitor_dd_progress" "/kdumpscripts/monitor_dd_progress"
bf4667
    chmod +x "${initdir}/kdumpscripts/monitor_dd_progress"
Petr Šabata f5bf49
    inst "/bin/dd" "/bin/dd"
Petr Šabata f5bf49
    inst "/bin/tail" "/bin/tail"
Petr Šabata f5bf49
    inst "/bin/date" "/bin/date"
Petr Šabata f5bf49
    inst "/bin/sync" "/bin/sync"
Petr Šabata f5bf49
    inst "/bin/cut" "/bin/cut"
Petr Šabata f5bf49
    inst "/bin/head" "/bin/head"
Petr Šabata f5bf49
    inst "/bin/awk" "/bin/awk"
Petr Šabata f5bf49
    inst "/bin/sed" "/bin/sed"
Petr Šabata f5bf49
    inst "/sbin/makedumpfile" "/sbin/makedumpfile"
Petr Šabata f5bf49
    inst "/sbin/vmcore-dmesg" "/sbin/vmcore-dmesg"
DistroBaker 5cac7c
    inst "/usr/bin/printf" "/sbin/printf"
DistroBaker 5cac7c
    inst "/usr/bin/logger" "/sbin/logger"
337158
    inst "/usr/bin/chmod" "/sbin/chmod"
Petr Šabata f5bf49
    inst "/lib/kdump/kdump-lib-initramfs.sh" "/lib/kdump-lib-initramfs.sh"
DistroBaker 5cac7c
    inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh"
Petr Šabata f5bf49
    inst "$moddir/kdump.sh" "/usr/bin/kdump.sh"
Petr Šabata f5bf49
    inst "$moddir/kdump-capture.service" "$systemdsystemunitdir/kdump-capture.service"
DistroBaker 7d0122
    systemctl -q --root "$initdir" add-wants initrd.target kdump-capture.service
Petr Šabata f5bf49
    # Replace existing emergency service and emergency target
Petr Šabata f5bf49
    cp "$moddir/kdump-emergency.service" "$initdir/$systemdsystemunitdir/emergency.service"
Petr Šabata f5bf49
    cp "$moddir/kdump-emergency.target" "$initdir/$systemdsystemunitdir/emergency.target"
Petr Šabata f5bf49
    # Also redirect dracut-emergency to kdump error handler
Petr Šabata f5bf49
    ln_r "$systemdsystemunitdir/emergency.service" "$systemdsystemunitdir/dracut-emergency.service"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Check for all the devices and if any device is iscsi, bring up iscsi
Petr Šabata f5bf49
    # target. Ideally all this should be pushed into dracut iscsi module
Petr Šabata f5bf49
    # at some point of time.
Petr Šabata f5bf49
    kdump_check_iscsi_targets
Petr Šabata f5bf49
Petr Šabata f5bf49
    kdump_install_systemd_conf
Petr Šabata f5bf49
Petr Šabata f5bf49
    # nfs/ssh dump will need to get host ip in second kernel and need to call 'ip' tool, see get_host_ip for more detail
Petr Šabata f5bf49
    if is_nfs_dump_target || is_ssh_dump_target; then
Petr Šabata f5bf49
        inst "ip"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # For the lvm type target under kdump, in /etc/lvm/lvm.conf we can
Petr Šabata f5bf49
    # safely replace "reserved_memory=XXXX"(default value is 8192) with
Petr Šabata f5bf49
    # "reserved_memory=1024" to lower memory pressure under kdump. We do
Petr Šabata f5bf49
    # it unconditionally here, if "/etc/lvm/lvm.conf" doesn't exist, it
Petr Šabata f5bf49
    # actually does nothing.
Petr Šabata f5bf49
    sed -i -e \
b494b7
        's/\(^[[:space:]]*reserved_memory[[:space:]]*=\)[[:space:]]*[[:digit:]]*/\1 1024/' \
b494b7
        "${initdir}/etc/lvm/lvm.conf" &> /dev/null
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Save more memory by dropping switch root capability
Kairui Song 96a3fc
    dracut_no_switch_root
Petr Šabata f5bf49
}