Petr Šabata f5bf49
#!/bin/bash
Petr Šabata f5bf49
Coiby Xu 0aaa05
_DRACUT_KDUMP_NM_TMP_DIR="$DRACUT_TMPDIR/$$-DRACUT_KDUMP_NM"
Coiby Xu 94f8ee
Coiby Xu 420f55
_save_kdump_netifs() {
Coiby Xu 420f55
    unique_netifs[$1]=1
Coiby Xu 420f55
}
Coiby Xu 420f55
Coiby Xu 420f55
_get_kdump_netifs() {
Coiby Xu 420f55
    echo -n "${!unique_netifs[@]}"
Coiby Xu 420f55
}
Coiby Xu 420f55
DistroBaker 624a64
kdump_module_init() {
DistroBaker 624a64
    if ! [[ -d "${initdir}/tmp" ]]; then
DistroBaker 624a64
        mkdir -p "${initdir}/tmp"
DistroBaker 624a64
    fi
Petr Šabata f5bf49
Coiby Xu 94f8ee
    mkdir -p "$_DRACUT_KDUMP_NM_TMP_DIR"
Coiby Xu 94f8ee
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
10e0a5
    if is_lvm2_thinp_dump_target; then
10e0a5
        if grep -q lvmthinpool-monitor <<< $(dracut --list-modules); then
10e0a5
            add_opt_module lvmthinpool-monitor
10e0a5
        else
10e0a5
            dwarning "Required lvmthinpool-monitor modules is missing! Please upgrade dracut >= 057."
10e0a5
        fi
10e0a5
    fi
10e0a5
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 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
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
Coiby Xu 214e9d
apply_nm_initrd_generator_timeouts() {
Coiby Xu 214e9d
    local _timeout_conf
Coiby Xu 214e9d
Coiby Xu 214e9d
    _timeout_conf=$_DRACUT_KDUMP_NM_TMP_DIR/timeout_conf
Coiby Xu 214e9d
    cat << EOF > "$_timeout_conf"
Coiby Xu 214e9d
[device-95-kdump]
Coiby Xu 214e9d
carrier-wait-timeout=30000
Coiby Xu 214e9d
Coiby Xu 214e9d
[connection-95-kdump]
Coiby Xu 214e9d
ipv4.dhcp-timeout=90
Coiby Xu 214e9d
ipv6.dhcp-timeout=90
Coiby Xu 214e9d
EOF
Coiby Xu 214e9d
Coiby Xu 214e9d
    inst "$_timeout_conf" "/etc/NetworkManager/conf.d/95-kdump-timeouts.conf"
Coiby Xu 214e9d
}
Coiby Xu 214e9d
Coiby Xu 668875
use_ipv4_or_ipv6() {
Coiby Xu 668875
    local _netif=$1 _uuid=$2
Coiby Xu 668875
Coiby Xu 668875
    if [[ -v "ipv4_usage[$_netif]" ]]; then
Coiby Xu 668875
        nmcli connection modify --temporary "$_uuid" ipv4.may-fail no &> >(ddebug)
Coiby Xu 668875
    fi
Coiby Xu 668875
Coiby Xu 668875
    if [[ -v "ipv6_usage[$_netif]" ]]; then
Coiby Xu 668875
        nmcli connection modify --temporary "$_uuid" ipv6.may-fail no &> >(ddebug)
Coiby Xu 668875
    fi
Coiby Xu 668875
Coiby Xu 668875
    if [[ -v "ipv4_usage[$_netif]" ]] && [[ ! -v "ipv6_usage[$_netif]" ]]; then
Coiby Xu 668875
        nmcli connection modify --temporary "$_uuid" ipv6.method disabled &> >(ddebug)
Coiby Xu 668875
    elif [[ ! -v "ipv4_usage[$_netif]" ]] && [[ -v "ipv6_usage[$_netif]" ]]; then
Coiby Xu 668875
        nmcli connection modify --temporary "$_uuid" ipv4.method disabled &> >(ddebug)
Coiby Xu 668875
    fi
Coiby Xu 668875
}
Coiby Xu 668875
Coiby Xu 94f8ee
_clone_nmconnection() {
Coiby Xu 94f8ee
    local _clone_output _name _unique_id
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    _unique_id=$1
Coiby Xu 94f8ee
    _name=$(nmcli --get-values connection.id connection show "$_unique_id")
Coiby Xu 94f8ee
    if _clone_output=$(nmcli connection clone --temporary uuid "$_unique_id" "$_name"); then
Coiby Xu 94f8ee
        sed -E -n "s/.* \(.*\) cloned as.*\((.*)\)\.$/\1/p" <<< "$_clone_output"
Coiby Xu 94f8ee
        return 0
Coiby Xu 94f8ee
    fi
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    return 1
Coiby Xu 94f8ee
}
Coiby Xu 94f8ee
Coiby Xu d22786
_match_nmconnection_by_mac() {
Coiby Xu d22786
    local _unique_id _dev _mac _mac_field
Coiby Xu d22786
Coiby Xu d22786
    _unique_id=$1
Coiby Xu d22786
    _dev=$2
Coiby Xu d22786
Coiby Xu d22786
    _mac=$(kdump_get_perm_addr "$_dev")
Coiby Xu d22786
    [[ $_mac != 'not set' ]] || return
Coiby Xu d22786
    _mac_field=$(nmcli --get-values connection.type connection show "$_unique_id").mac-address
Coiby Xu d22786
    nmcli connection modify --temporary "$_unique_id" "$_mac_field" "$_mac" &> >(ddebug)
Coiby Xu d22786
    nmcli connection modify --temporary "$_unique_id" "connection.interface-name" "" &> >(ddebug)
Coiby Xu d22786
}
Coiby Xu d22786
Coiby Xu 94f8ee
# Clone and modify NM connection profiles
Coiby Xu 94f8ee
#
Coiby Xu 94f8ee
# This function makes use of "nmcli clone" to automatically convert ifcfg-*
Coiby Xu 94f8ee
# files to Networkmanager .nmconnection connection profiles and also modify the
Coiby Xu 94f8ee
# properties of .nmconnection if necessary.
Coiby Xu 94f8ee
clone_and_modify_nmconnection() {
Coiby Xu 94f8ee
    local _dev _cloned_nmconnection_file_path _tmp_nmconnection_file_path _old_uuid _uuid
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    _dev=$1
Coiby Xu 94f8ee
    _nmconnection_file_path=$2
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    _old_uuid=$(nmcli --get-values connection.uuid connection show filename "$_nmconnection_file_path")
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    if ! _uuid=$(_clone_nmconnection "$_old_uuid"); then
Coiby Xu 94f8ee
        derror "Failed to clone $_old_uuid"
Coiby Xu 94f8ee
        exit 1
Coiby Xu 94f8ee
    fi
Coiby Xu 94f8ee
Coiby Xu 668875
    use_ipv4_or_ipv6 "$_dev" "$_uuid"
Coiby Xu 668875
Coiby Xu 214e9d
    nmcli connection modify --temporary uuid "$_uuid" connection.wait-device-timeout 60000 &> >(ddebug)
Coiby Xu d22786
    # For physical NIC i.e. non-user created NIC, ask NM to match a
Coiby Xu d22786
    # connection profile based on MAC address
Coiby Xu d22786
    _match_nmconnection_by_mac "$_uuid" "$_dev"
Coiby Xu d22786
Coiby Xu c28d6f
    # If a value contain ":", nmcli by default escape it with "\:" because it
Coiby Xu c28d6f
    # also uses ":" as the delimiter to separate values. In our case, escaping is not needed.
Coiby Xu c28d6f
    _cloned_nmconnection_file_path=$(nmcli --escape no --get-values UUID,FILENAME connection show | sed -n "s/^${_uuid}://p")
Coiby Xu 94f8ee
    _tmp_nmconnection_file_path=$_DRACUT_KDUMP_NM_TMP_DIR/$(basename "$_nmconnection_file_path")
Coiby Xu 94f8ee
    cp "$_cloned_nmconnection_file_path" "$_tmp_nmconnection_file_path"
Coiby Xu 94f8ee
    # change uuid back to old value in case it's refered by other connection
Coiby Xu 94f8ee
    # profile e.g. connection.master could be interface name of the master
Coiby Xu 94f8ee
    # device or UUID of the master connection.
Coiby Xu 94f8ee
    sed -i -E "s/(^uuid=).*$/\1${_old_uuid}/g" "$_tmp_nmconnection_file_path"
Coiby Xu 94f8ee
    nmcli connection del "$_uuid" &> >(ddebug)
Coiby Xu 94f8ee
    echo -n "$_tmp_nmconnection_file_path"
Coiby Xu 94f8ee
}
Coiby Xu 94f8ee
Coiby Xu 94f8ee
_install_nmconnection() {
Coiby Xu 94f8ee
    local _src _nmconnection_name _dst
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    _src=$1
Coiby Xu 94f8ee
    _nmconnection_name=$(basename "$_src")
Coiby Xu 94f8ee
    _dst="/etc/NetworkManager/system-connections/$_nmconnection_name"
Coiby Xu 94f8ee
    inst "$_src" "$_dst"
Coiby Xu 94f8ee
}
Coiby Xu 94f8ee
Coiby Xu 94f8ee
kdump_install_nmconnections() {
Coiby Xu 94f8ee
    local _netif _nm_conn_path _cloned_nm_path
Coiby Xu 94f8ee
Coiby Xu 94f8ee
    while IFS=: read -r _netif _nm_conn_path; do
Coiby Xu 94f8ee
        [[ -v "unique_netifs[$_netif]" ]] || continue
Coiby Xu 94f8ee
        if _cloned_nm_path=$(clone_and_modify_nmconnection "$_netif" "$_nm_conn_path"); then
Coiby Xu 94f8ee
            _install_nmconnection "$_cloned_nm_path"
Coiby Xu 94f8ee
        else
Coiby Xu 94f8ee
            derror "Failed to install the .nmconnection for $_netif"
Coiby Xu 94f8ee
            exit 1
Coiby Xu 94f8ee
        fi
Coiby Xu 94f8ee
    done <<< "$(nmcli -t -f device,filename connection show --active)"
Coiby Xu 1141e0
Coiby Xu 1141e0
    # Stop dracut 35network-manger to calling nm-initrd-generator.
Coiby Xu 1141e0
    # Note this line of code can be removed after NetworkManager >= 1.35.2
Coiby Xu 1141e0
    # gets released.
Coiby Xu 1141e0
    echo > "${initdir}/usr/libexec/nm-initrd-generator"
Coiby Xu 94f8ee
}
Coiby Xu 94f8ee
Coiby Xu 95a39f
kdump_install_nm_netif_allowlist() {
Coiby Xu 95a39f
    local _netif _except_netif _netif_allowlist _netif_allowlist_nm_conf
Coiby Xu 95a39f
Coiby Xu 95a39f
    for _netif in $1; do
Coiby Xu 95a39f
        _per_mac=$(kdump_get_perm_addr "$_netif")
Coiby Xu 95a39f
        if [[ "$_per_mac" != 'not set' ]]; then
Coiby Xu 95a39f
            _except_netif="mac:$_per_mac"
Coiby Xu 95a39f
        else
Coiby Xu 95a39f
            _except_netif="interface-name:$_netif"
Coiby Xu 95a39f
        fi
Coiby Xu 95a39f
        _netif_allowlist="${_netif_allowlist}except:${_except_netif};"
Coiby Xu 95a39f
    done
Coiby Xu 95a39f
Coiby Xu 95a39f
    _netif_allowlist_nm_conf=$_DRACUT_KDUMP_NM_TMP_DIR/netif_allowlist_nm_conf
Coiby Xu 95a39f
    cat << EOF > "$_netif_allowlist_nm_conf"
Coiby Xu 95a39f
[device-others]
Coiby Xu 95a39f
match-device=${_netif_allowlist}
Coiby Xu 95a39f
managed=false
Coiby Xu 95a39f
EOF
Coiby Xu 95a39f
Coiby Xu 95a39f
    inst "$_netif_allowlist_nm_conf" "/etc/NetworkManager/conf.d/10-kdump-netif_allowlist.conf"
Coiby Xu 95a39f
}
Coiby Xu 95a39f
Coiby Xu 81b414
_get_nic_driver() {
Coiby Xu 81b414
    ethtool -i "$1" | sed -n -E "s/driver: (.*)/\1/p"
Coiby Xu 81b414
}
Coiby Xu 81b414
Coiby Xu 06ddf8
_get_hpyerv_physical_driver() {
Coiby Xu 06ddf8
    local _physical_nic
Coiby Xu 06ddf8
Coiby Xu 06ddf8
    _physical_nic=$(find /sys/class/net/"$1"/ -name 'lower_*' | sed -En "s/\/.*lower_(.*)/\1/p")
Coiby Xu 06ddf8
    [[ -n $_physical_nic ]] || return
Coiby Xu 06ddf8
    _get_nic_driver "$_physical_nic"
Coiby Xu 06ddf8
}
Coiby Xu 06ddf8
Coiby Xu 74e102
_get_physical_function_driver() {
Coiby Xu 74e102
    local _physfn_dir=/sys/class/net/"$1"/device/physfn
Coiby Xu 74e102
Coiby Xu 74e102
    if [[ -e "$_physfn_dir" ]]; then
Coiby Xu 74e102
        basename "$(readlink -f "$_physfn_dir"/driver)"
Coiby Xu 74e102
    fi
Coiby Xu 74e102
}
Coiby Xu 74e102
Coiby Xu 81b414
kdump_install_nic_driver() {
Coiby Xu 81b414
    local _netif _driver _drivers
Coiby Xu 81b414
Coiby Xu ce8f79
    _drivers=('=drivers/net/phy' '=drivers/net/mdio')
Coiby Xu 81b414
Coiby Xu 81b414
    for _netif in $1; do
Lichen Liu 5e6d9d
        [[ $_netif == lo ]] && continue
Coiby Xu 81b414
        _driver=$(_get_nic_driver "$_netif")
Coiby Xu 81b414
        if [[ -z $_driver ]]; then
Coiby Xu 81b414
            derror "Failed to get the driver of $_netif"
Coiby Xu 81b414
            exit 1
Coiby Xu 81b414
        fi
Coiby Xu 81b414
Coiby Xu 81b414
        if [[ $_driver == "802.1Q VLAN Support" ]]; then
Coiby Xu 81b414
            # ethtool somehow doesn't return the driver name for a VLAN NIC
Coiby Xu 81b414
            _driver=8021q
Coiby Xu 81b414
        elif [[ $_driver == "team" ]]; then
Coiby Xu 81b414
            # install the team mode drivers like team_mode_roundrobin.ko as well
Coiby Xu 81b414
            _driver='=drivers/net/team'
Coiby Xu 06ddf8
        elif [[ $_driver == "hv_netvsc" ]]; then
Coiby Xu 06ddf8
            # A Hyper-V VM may have accelerated networking
Coiby Xu 06ddf8
            # https://learn.microsoft.com/en-us/azure/virtual-network/accelerated-networking-overview
Coiby Xu 06ddf8
            # Install the driver of physical NIC as well
Coiby Xu 06ddf8
            _drivers+=("$(_get_hpyerv_physical_driver "$_netif")")
Coiby Xu 81b414
        fi
Coiby Xu 81b414
Coiby Xu 81b414
        _drivers+=("$_driver")
Coiby Xu 74e102
        # For a Single Root I/O Virtualization (SR-IOV) virtual device,
Coiby Xu 74e102
        # the driver of physical device needs to be installed as well
Coiby Xu 74e102
        _drivers+=("$(_get_physical_function_driver "$_netif")")
Coiby Xu 81b414
    done
Coiby Xu 81b414
Lichen Liu 5e6d9d
    [[ -n ${_drivers[*]} ]] || return
Coiby Xu 81b414
    instmods "${_drivers[@]}"
Coiby Xu 81b414
}
Coiby Xu 81b414
Petr Šabata f5bf49
kdump_setup_bridge() {
Petr Šabata f5bf49
    local _netdev=$1
Coiby Xu 420f55
    local _dev
72c3be
    for _dev in "/sys/class/net/$_netdev/brif/"*; do
72c3be
        [[ -e $_dev ]] || continue
72c3be
        _dev=${_dev##*/}
Petr Šabata f5bf49
        if kdump_is_bond "$_dev"; then
Coiby Xu 420f55
            kdump_setup_bond "$_dev" || return 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
        fi
Coiby Xu 420f55
        _save_kdump_netifs "$_dev"
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_bond() {
Coiby Xu c23189
    local _netdev="$1"
Coiby Xu 420f55
    local _dev
Petr Šabata f5bf49
Coiby Xu 420f55
    for _dev in $(< "/sys/class/net/$_netdev/bonding/slaves"); do
Coiby Xu 420f55
        _save_kdump_netifs "$_dev"
Coiby Xu 420f55
    done
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_team() {
Petr Šabata f5bf49
    local _netdev=$1
Coiby Xu 420f55
    local _dev
bf4667
    for _dev in $(teamnl "$_netdev" ports | awk -F':' '{print $2}'); do
Coiby Xu 420f55
        _save_kdump_netifs "$_dev"
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_setup_vlan() {
Petr Šabata f5bf49
    local _netdev=$1
Coiby Xu 420f55
    local _parent_netif
Petr Šabata f5bf49
Coiby Xu 420f55
    _parent_netif="$(awk '/^Device:/{print $2}' /proc/net/vlan/"$_netdev")"
633084
DistroBaker a10140
    #Just support vlan over bond and team
Coiby Xu 420f55
    if kdump_is_bridge "$_parent_netif"; then
Petr Šabata f5bf49
        derror "Vlan over bridge is not supported!"
Petr Šabata f5bf49
        exit 1
Coiby Xu 420f55
    elif kdump_is_bond "$_parent_netif"; then
Coiby Xu 420f55
        kdump_setup_bond "$_parent_netif" || return 1
Coiby Xu 420f55
    elif kdump_is_team "$_parent_netif"; then
Coiby Xu 420f55
        kdump_setup_team "$_parent_netif" || return 1
Petr Šabata f5bf49
    fi
Coiby Xu 420f55
Coiby Xu 420f55
    _save_kdump_netifs "$_parent_netif"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu afbb32
_find_znet_nmconnection() {
Coiby Xu afbb32
    LANG=C grep -s -E -i -l \
Coiby Xu afbb32
        "^s390-subchannels=([0-9]\.[0-9]\.[a-f0-9]+;){0,2}" \
Coiby Xu afbb32
        "$1"/*.nmconnection | LC_ALL=C sed -e "$2"
Coiby Xu f0ecf8
}
Coiby Xu f0ecf8
Coiby Xu afbb32
# setup s390 znet
Coiby Xu afbb32
#
Coiby Xu afbb32
# Note part of code is extracted from ccw_init provided by s390utils
Petr Šabata f5bf49
kdump_setup_znet() {
Coiby Xu afbb32
    local _config_file _unique_name _NM_conf_dir
Coiby Xu afbb32
    local __sed_discard_ignored_files='/\(~\|\.bak\|\.old\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
Coiby Xu d408ec
Coiby Xu afbb32
    if [[ "$(uname -m)" != "s390x" ]]; then
Coiby Xu afbb32
        return
Coiby Xu afbb32
    fi
Coiby Xu d408ec
Coiby Xu afbb32
    _NM_conf_dir="/etc/NetworkManager/system-connections"
Coiby Xu afbb32
Coiby Xu afbb32
    _config_file=$(_find_znet_nmconnection "$initdir/$_NM_conf_dir" "$__sed_discard_ignored_files")
Coiby Xu afbb32
    if [[ -n "$_config_file" ]]; then
Coiby Xu afbb32
        ddebug "$_config_file has already contained the znet config"
Coiby Xu afbb32
        return
Coiby Xu d408ec
    fi
Petr Šabata f5bf49
Coiby Xu afbb32
    _config_file=$(LANG=C grep -s -E -i -l \
Coiby Xu afbb32
        "^[[:space:]]*SUBCHANNELS=['\"]?([0-9]\.[0-9]\.[a-f0-9]+,){0,2}" \
Coiby Xu afbb32
        /etc/sysconfig/network-scripts/ifcfg-* \
Coiby Xu afbb32
        | LC_ALL=C sed -e "$__sed_discard_ignored_files")
Coiby Xu afbb32
Coiby Xu afbb32
    if [[ -z "$_config_file" ]]; then
Coiby Xu afbb32
        _config_file=$(_find_znet_nmconnection "$_NM_conf_dir" "$__sed_discard_ignored_files")
Coiby Xu afbb32
    fi
Coiby Xu afbb32
Coiby Xu afbb32
    if [[ -n "$_config_file" ]]; then
Coiby Xu afbb32
        _unique_name=$(cat /proc/sys/kernel/random/uuid)
Coiby Xu afbb32
        nmcli connection clone --temporary "$_config_file" "$_unique_name" &> >(ddebug)
Coiby Xu afbb32
        nmcli connection modify --temporary "$_unique_name" connection.autoconnect false
Coiby Xu afbb32
        inst "/run/NetworkManager/system-connections/${_unique_name}.nmconnection" "${_NM_conf_dir}/${_unique_name}.nmconnection"
Coiby Xu afbb32
        nmcli connection del "$_unique_name" &> >(ddebug)
Coiby Xu d408ec
    fi
Petr Šabata f5bf49
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
Coiby Xu 420f55
# Collect netifs needed by kdump
Petr Šabata f5bf49
# $1: destination host
Coiby Xu 420f55
kdump_collect_netif_usage() {
Coiby Xu d22786
    local _destaddr _srcaddr _route _netdev
Petr Šabata f5bf49
bf4667
    _destaddr=$(kdump_get_remote_ip "$1")
Coiby Xu a0f7f2
Coiby Xu a0f7f2
    if ! _route=$(kdump_get_ip_route "$_destaddr"); then
Coiby Xu a0f7f2
        derror "Bad kdump network destination: $_destaddr"
Coiby Xu a0f7f2
        exit 1
Coiby Xu a0f7f2
    fi
Coiby Xu a0f7f2
Petr Šabata f5bf49
    _srcaddr=$(kdump_get_ip_route_field "$_route" "src")
Petr Šabata f5bf49
    _netdev=$(kdump_get_ip_route_field "$_route" "dev")
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
Coiby Xu 420f55
        kdump_setup_bond "$_netdev" || return 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
    fi
Coiby Xu 420f55
    _save_kdump_netifs "$_netdev"
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
Coiby Xu d22786
    if [[ ! -f ${initdir}/etc/cmdline.d/60kdumpip.conf ]]; then
Coiby Xu d22786
        echo "kdump_remote_ip=$_destaddr" > "${initdir}/etc/cmdline.d/60kdumpip.conf"
Coiby Xu 420f55
    fi
Coiby Xu 420f55
Coiby Xu 420f55
    if is_ipv6_address "$_srcaddr"; then
Coiby Xu 420f55
        ipv6_usage[$_netdev]=1
Coiby Xu 420f55
    else
Coiby Xu 420f55
        ipv4_usage[$_netdev]=1
Coiby Xu 420f55
    fi
Coiby Xu 420f55
}
Coiby Xu 420f55
Coiby Xu 6e359f
kdump_install_resolv_conf() {
Coiby Xu 6e359f
    local _resolv_conf=/etc/resolv.conf _nm_conf_dir=/etc/NetworkManager/conf.d
Coiby Xu 6e359f
Coiby Xu 6e359f
    # Some users may choose to manage /etc/resolve.conf manually [1]
Coiby Xu 6e359f
    # by setting dns=none or use a symbolic link resolve.conf [2].
Coiby Xu 6e359f
    # So resolve.conf should be installed to kdump initrd as well. To prevent
Coiby Xu 6e359f
    # NM frome overwritting the user-configured resolve.conf in kdump initrd,
Coiby Xu 6e359f
    # also set dns=none for NM.
Coiby Xu 6e359f
    #
Coiby Xu 6e359f
    # Note:
Coiby Xu 6e359f
    # 1. When resolv.conf is managed by systemd-resolved.service, it could also be a
Coiby Xu 6e359f
    #    symbolic link. So exclude this case by teling if systemd-resolved is enabled.
Coiby Xu 6e359f
    #
Coiby Xu 6e359f
    # 2. It's harmless to blindly copy /etc/resolve.conf to the initrd because
Coiby Xu 6e359f
    #    by default in initramfs this file will be overwritten by
Coiby Xu 6e359f
    #    NetworkManager. If user manages it via a symbolic link, it's still
Coiby Xu 6e359f
    #    preserved because NM won't touch a symbolic link file.
Coiby Xu 6e359f
    #
Coiby Xu 6e359f
    # [1] https://bugzilla.gnome.org/show_bug.cgi?id=690404
Coiby Xu 6e359f
    # [2] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/manually-configuring-the-etc-resolv-conf-file_configuring-and-managing-networking
Coiby Xu c179f4
    systemctl -q is-enabled systemd-resolved 2> /dev/null && return 0
Coiby Xu 6e359f
    inst "$_resolv_conf"
Coiby Xu 6e359f
    if NetworkManager --print-config | grep -qs "^dns=none"; then
Coiby Xu c179f4
        printf "[main]\ndns=none\n" > "${initdir}/${_nm_conf_dir}"/90-dns-none.conf
Coiby Xu 6e359f
    fi
Coiby Xu 6e359f
}
Coiby Xu 6e359f
Coiby Xu 420f55
# Setup dracut to bring up network interface that enable
Coiby Xu 420f55
# initramfs accessing giving destination
Coiby Xu 420f55
kdump_install_net() {
Coiby Xu 420f55
    local _netifs
Coiby Xu 420f55
Coiby Xu 420f55
    _netifs=$(_get_kdump_netifs)
Coiby Xu 420f55
    if [[ -n "$_netifs" ]]; then
Coiby Xu 420f55
        kdump_install_nmconnections
Coiby Xu 420f55
        apply_nm_initrd_generator_timeouts
Coiby Xu afbb32
        kdump_setup_znet
Coiby Xu 95a39f
        kdump_install_nm_netif_allowlist "$_netifs"
Coiby Xu 81b414
        kdump_install_nic_driver "$_netifs"
Coiby Xu 6e359f
        kdump_install_resolv_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
Coiby Xu 420f55
        kdump_collect_netif_usage "$_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
                ;;
dcaec9
            ext[234] | xfs | btrfs | minix | virtiofs)
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)
Coiby Xu 420f55
                kdump_collect_netif_usage "$_val"
b494b7
                ;;
b494b7
            dracut_args)
b494b7
                if [[ $(get_dracut_args_fstype "$_val") == nfs* ]]; then
Coiby Xu 420f55
                    kdump_collect_netif_usage "$(get_dracut_args_target "$_val")"
b494b7
                fi
b494b7
                ;;
b494b7
            kdump_pre | kdump_post | extra_bins)
6a373d
                # shellcheck disable=SC2086
6a373d
                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
Coiby Xu 420f55
    kdump_collect_netif_usage "$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
Coiby Xu fa2f8f
    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"
Coiby Xu fa2f8f
    }
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
Coiby Xu 420f55
        kdump_collect_netif_usage "$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
Pingfan Liu 5ac720
remove_cpu_online_rule() {
Pingfan Liu 5ac720
    local file=${initdir}/usr/lib/udev/rules.d/40-redhat.rules
Pingfan Liu 5ac720
Pingfan Liu 5ac720
    sed -i '/SUBSYSTEM=="cpu"/d' "$file"
Pingfan Liu 5ac720
}
Pingfan Liu 5ac720
Petr Šabata f5bf49
install() {
Coiby Xu 668875
    declare -A unique_netifs ipv4_usage ipv6_usage
Pingfan Liu 5ac720
    local arch
Pingfan Liu 5ac720
DistroBaker 624a64
    kdump_module_init
Petr Šabata f5bf49
    kdump_install_conf
Kairui Song de11eb
    remove_sysctl_conf
Petr Šabata f5bf49
Pingfan Liu 5ac720
    # Onlining secondary cpus breaks kdump completely on KVM on Power hosts
Pingfan Liu 5ac720
    # Though we use maxcpus=1 by default but 40-redhat.rules will bring up all
Pingfan Liu 5ac720
    # possible cpus by default. (rhbz1270174 rhbz1266322)
Pingfan Liu 5ac720
    # Thus before we get the kernel fix and the systemd rule fix let's remove
Pingfan Liu 5ac720
    # the cpu online rule in kdump initramfs.
Pingfan Liu 5ac720
    arch=$(uname -m)
Pingfan Liu 5ac720
    if [[ "$arch" = "ppc64le" ]] || [[ "$arch" = "ppc64" ]]; then
Pingfan Liu 5ac720
        remove_cpu_online_rule
Pingfan Liu 5ac720
    fi
Pingfan Liu 5ac720
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"
039c1d
    inst "/bin/stat" "/bin/stat"
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
Coiby Xu 420f55
    kdump_install_net
Coiby Xu 420f55
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
Lichen Liu 78e19b
    # Skip initrd-cleanup.service and initrd-parse-etc.service becasue we don't
Lichen Liu 78e19b
    # need to switch root. Instead of removing them, we use ConditionPathExists
Lichen Liu 78e19b
    # to check if /proc/vmcore exists to determine if we are in kdump.
Lichen Liu 78e19b
    sed -i '/\[Unit\]/a ConditionPathExists=!\/proc\/vmcore' \
Lichen Liu 78e19b
        "${initdir}/${systemdsystemunitdir}/initrd-cleanup.service" &> /dev/null
Lichen Liu 78e19b
Lichen Liu 78e19b
    sed -i '/\[Unit\]/a ConditionPathExists=!\/proc\/vmcore' \
Lichen Liu 78e19b
        "${initdir}/${systemdsystemunitdir}/initrd-parse-etc.service" &> /dev/null
Lichen Liu 78e19b
Petr Šabata f5bf49
    # Save more memory by dropping switch root capability
Kairui Song 96a3fc
    dracut_no_switch_root
Petr Šabata f5bf49
}