ab224c
#!/bin/bash
ab224c
ab224c
. $dracutfunctions
ab224c
. /lib/kdump/kdump-lib.sh
ab224c
ab224c
check() {
ab224c
    [[ $debug ]] && set -x
ab224c
    #kdumpctl sets this explicitly
ab224c
    if [ -z "$IN_KDUMP" ] || [ ! -f /etc/kdump.conf ]
ab224c
    then
ab224c
        return 1
ab224c
    fi
ab224c
    return 0
ab224c
}
ab224c
ab224c
depends() {
ab224c
    local _dep="base shutdown"
ab224c
ab224c
    if [ -d /sys/module/drm/drivers ]; then
ab224c
        _dep="$_dep drm"
ab224c
    fi
ab224c
765b01
    if is_fence_kdump; then
765b01
        _dep="$_dep network"
765b01
    fi
765b01
ab224c
    echo $_dep
ab224c
    return 0
ab224c
}
ab224c
ab224c
kdump_to_udev_name() {
ab224c
    local dev="${1//\"/}"
ab224c
ab224c
    case "$dev" in
ab224c
    UUID=*)
ab224c
        dev=`blkid -U "${dev#UUID=}"`
ab224c
        ;;
ab224c
    LABEL=*)
ab224c
        dev=`blkid -L "${dev#LABEL=}"`
ab224c
        ;;
ab224c
    esac
ab224c
    echo $(get_persistent_dev "$dev")
ab224c
}
ab224c
ab224c
kdump_is_bridge() {
ab224c
     [ -d /sys/class/net/"$1"/bridge ]
ab224c
}
ab224c
ab224c
kdump_is_bond() {
ab224c
     [ -d /sys/class/net/"$1"/bonding ]
ab224c
}
ab224c
ab224c
kdump_is_team() {
ab224c
     [ -f /usr/bin/teamnl ] && teamnl $1 ports &> /dev/null
ab224c
}
ab224c
ab224c
kdump_is_vlan() {
ab224c
     [ -f /proc/net/vlan/"$1" ]
ab224c
}
ab224c
ab224c
# $1: netdev name
ab224c
kdump_setup_dns() {
ab224c
    _dnsfile=${initdir}/etc/cmdline.d/42dns.conf
ab224c
    . /etc/sysconfig/network-scripts/ifcfg-$1
ab224c
    [ -n "$DNS1" ] && echo "nameserver=$DNS1" > "$_dnsfile"
ab224c
    [ -n "$DNS2" ] && echo "nameserver=$DNS2" >> "$_dnsfile"
ab224c
}
ab224c
ab224c
#$1: netdev name
ab224c
#checking /etc/sysconfig/network-scripts/ifcfg-$1,
ab224c
#if it use static ip echo it, or echo null
ab224c
kdump_static_ip() {
ab224c
    . /etc/sysconfig/network-scripts/ifcfg-$1
ab224c
    if [ -n "$IPADDR" ]; then
ab224c
       [ -z "$NETMASK" -a -n "$PREFIX" ] && \
ab224c
           NETMASK=$(ipcalc -m $IPADDR/$PREFIX | cut -d'=' -f2)
ab224c
       echo -n "${IPADDR}::${GATEWAY}:${NETMASK}::"
ab224c
    fi
ab224c
}
ab224c
ab224c
kdump_get_mac_addr() {
ab224c
    cat /sys/class/net/$1/address
ab224c
}
ab224c
ab224c
#Bonding or team master modifies the mac address
ab224c
#of its slaves, we should use perm address
ab224c
kdump_get_perm_addr() {
ab224c
    local addr=$(ethtool -P $1 | sed -e 's/Permanent address: //')
ab224c
    if [ -z "$addr" ] || [ "$addr" = "00:00:00:00:00:00" ]
ab224c
    then
ab224c
        derror "Can't get the permanent address of $1"
ab224c
    else
ab224c
        echo "$addr"
ab224c
    fi
ab224c
}
ab224c
ab224c
kdump_setup_bridge() {
ab224c
    local _netdev=$1
ab224c
    local _brif _dev
ab224c
    for _dev in `ls /sys/class/net/$_netdev/brif/`; do
ab224c
        if kdump_is_bond "$_dev"; then
ab224c
            kdump_setup_bond "$_dev"
ab224c
        elif kdump_is_team "$_dev"; then
ab224c
            kdump_setup_team "$_dev"
ab224c
        elif kdump_is_vlan "$_dev"; then
ab224c
            kdump_setup_vlan "$_dev"
ab224c
        else
ab224c
            echo -n " ifname=$_dev:$(kdump_get_mac_addr $_dev)" >> ${initdir}/etc/cmdline.d/41bridge.conf
ab224c
        fi
ab224c
        _brif+="$_dev,"
ab224c
    done
ab224c
    echo " bridge=$_netdev:$(echo $_brif | sed -e 's/,$//')" >> ${initdir}/etc/cmdline.d/41bridge.conf
ab224c
}
ab224c
ab224c
kdump_setup_bond() {
ab224c
    local _netdev=$1
ab224c
    local _dev
ab224c
    for _dev in `cat /sys/class/net/$_netdev/bonding/slaves`; do
ab224c
        echo -n " ifname=$_dev:$(kdump_get_perm_addr $_dev)" >> ${initdir}/etc/cmdline.d/42bond.conf
ab224c
    done
ab224c
    echo -n " bond=$_netdev:$(sed -e 's/ /,/g' /sys/class/net/$_netdev/bonding/slaves)" >> ${initdir}/etc/cmdline.d/42bond.conf
ab224c
    # Get bond options specified in ifcfg
ab224c
    . /etc/sysconfig/network-scripts/ifcfg-$_netdev
ab224c
    bondoptions="$(echo :$BONDING_OPTS | sed 's/\s\+/,/')"
ab224c
    echo "$bondoptions" >> ${initdir}/etc/cmdline.d/42bond.conf
ab224c
}
ab224c
ab224c
kdump_setup_team() {
ab224c
    local _netdev=$1
ab224c
    local slaves _dev
ab224c
    for _dev in `teamnl $_netdev ports | awk -F':' '{print $2}'`; do
ab224c
        echo -n " ifname=$_dev:$(kdump_get_perm_addr $_dev)" >> ${initdir}/etc/cmdline.d/44team.conf
ab224c
        slaves+="$_dev,"
ab224c
    done
ab224c
    echo " team=$_netdev:$(echo $slaves | sed -e 's/,$//')" >> ${initdir}/etc/cmdline.d/44team.conf
ab224c
    #Buggy version teamdctl outputs to stderr!
ab224c
    #Try to use the latest version of teamd.
ab224c
    teamdctl "$_netdev" config dump > /tmp/$$-$_netdev.conf
ab224c
    if [ $? -ne 0 ]
ab224c
    then
ab224c
        derror "teamdctl failed."
ab224c
        exit 1
ab224c
    fi
ab224c
    inst_dir /etc/teamd
ab224c
    inst_simple /tmp/$$-$_netdev.conf "/etc/teamd/$_netdev.conf"
ab224c
    rm -f /tmp/$$-$_netdev.conf
ab224c
}
ab224c
ab224c
kdump_setup_vlan() {
ab224c
    local _netdev=$1
ab224c
    local _phydev="$(awk '/^Device:/{print $2}' /proc/net/vlan/"$_netdev")"
ab224c
    local _netmac="$(kdump_get_mac_addr $_phydev)"
ab224c
ab224c
    echo " vlan=$_netdev:$_phydev" > ${initdir}/etc/cmdline.d/43vlan.conf
ab224c
ab224c
    #Just support vlan over bond, it is not easy
ab224c
    #to support all other complex setup
ab224c
    if kdump_is_bridge "$_phydev"; then
ab224c
        derror "Vlan over bridge is not supported!"
ab224c
        exit 1
ab224c
    elif kdump_is_team "$_phydev"; then
ab224c
        derror "Vlan over team is not supported!"
ab224c
        exit 1
ab224c
    elif kdump_is_bond "$_phydev"; then
ab224c
        kdump_setup_bond "$_phydev"
ab224c
    else
ab224c
        echo " vlan=$_netdev:$_phydev ifname=$_phydev:$_netmac" > ${initdir}/etc/cmdline.d/43vlan.conf
ab224c
    fi
ab224c
}
ab224c
ab224c
# setup s390 znet cmdline
ab224c
# $1: netdev name
ab224c
kdump_setup_znet() {
ab224c
    local _options=""
ab224c
    . /etc/sysconfig/network-scripts/ifcfg-$1
ab224c
    for i in $OPTIONS; do
ab224c
        _options=${_options},$i
ab224c
    done
ab224c
    echo rd.znet=${NETTYPE},${SUBCHANNELS}${_options} > ${initdir}/etc/cmdline.d/30znet.conf
ab224c
}
ab224c
ab224c
# Setup dracut to bringup a given network interface
ab224c
kdump_setup_netdev() {
ab224c
    local _netdev=$1
765b01
    local _static _proto _ip_conf _ip_opts _ifname_opts
ab224c
ab224c
    if [ "$(uname -m)" = "s390x" ]; then
ab224c
        kdump_setup_znet $_netdev
ab224c
    fi
ab224c
ab224c
    _netmac=$(kdump_get_mac_addr $_netdev)
ab224c
    _static=$(kdump_static_ip $_netdev)
ab224c
    if [ -n "$_static" ]; then
ab224c
        _proto=none
ab224c
    else
ab224c
        _proto=dhcp
ab224c
    fi
ab224c
765b01
    _ip_conf="${initdir}/etc/cmdline.d/40ip.conf"
765b01
    _ip_opts=" ip=${_static}$_netdev:${_proto}"
765b01
765b01
    # dracut doesn't allow duplicated configuration for same NIC, even they're exactly the same.
765b01
    # so we have to avoid adding duplicates
765b01
    if [ ! -f $_ip_conf ] || ! grep -q $_ip_opts $_ip_conf; then
765b01
        echo "$_ip_opts" >> $_ip_conf
765b01
    fi
ab224c
ab224c
    if kdump_is_bridge "$_netdev"; then
ab224c
        kdump_setup_bridge "$_netdev"
ab224c
    elif kdump_is_bond "$_netdev"; then
ab224c
        kdump_setup_bond "$_netdev"
ab224c
    elif kdump_is_team "$_netdev"; then
ab224c
        kdump_setup_team "$_netdev"
ab224c
    elif kdump_is_vlan "$_netdev"; then
ab224c
        kdump_setup_vlan "$_netdev"
ab224c
    else
765b01
        _ifname_opts=" ifname=$_netdev:$(kdump_get_mac_addr $_netdev)"
765b01
        echo "$_ifname_opts" >> $_ip_conf
ab224c
    fi
ab224c
ab224c
    kdump_setup_dns "$_netdev"
ab224c
}
ab224c
ab224c
#Function:kdump_install_net
ab224c
#$1: config values of net line in kdump.conf
ab224c
kdump_install_net() {
ab224c
    local _server _netdev
ab224c
    local config_val="$1"
ab224c
ab224c
    _server=`echo $config_val | sed 's/.*@//' | cut -d':' -f1`
ab224c
ab224c
    _need_dns=`echo $_server|grep "[a-zA-Z]"`
ab224c
    [ -n "$_need_dns" ] && _server=`getent hosts $_server|cut -d' ' -f1`
ab224c
ab224c
    _netdev=`/sbin/ip route get to $_server 2>&1`
ab224c
    [ $? != 0 ] && echo "Bad kdump location: $config_val" && exit 1
ab224c
ab224c
    #the field in the ip output changes if we go to another subnet
ab224c
    if [ -n "`echo $_netdev | grep via`" ]
ab224c
    then
ab224c
        # we are going to a different subnet
ab224c
        _netdev=`echo $_netdev|awk '{print $5;}'|head -n 1`
ab224c
    else
ab224c
        # we are on the same subnet
ab224c
        _netdev=`echo $_netdev|awk '{print $3}'|head -n 1`
ab224c
    fi
ab224c
ab224c
    kdump_setup_netdev "${_netdev}"
765b01
ab224c
    #save netdev used for kdump as cmdline
765b01
    # Whoever calling kdump_install_net() is setting up the default gateway,
765b01
    # ie. bootdev/kdumpnic. So don't override the setting if calling
765b01
    # kdump_install_net() for another time. For example, after setting eth0 as
765b01
    # the default gate way for network dump, eth1 in the fence kdump path will
765b01
    # call kdump_install_net again and we don't want eth1 to be the default
765b01
    # gateway.
765b01
    if [ ! -f ${initdir}${initdir}/etc/cmdline.d/60kdumpnic.conf ] &&
765b01
       [ ! -f ${initdir}/etc/cmdline.d/70bootdev.conf ]; then
765b01
        echo "kdumpnic=${_netdev}" > ${initdir}/etc/cmdline.d/60kdumpnic.conf
765b01
        echo "bootdev=${_netdev}" > ${initdir}/etc/cmdline.d/70bootdev.conf
765b01
    fi
ab224c
}
ab224c
ab224c
#install kdump.conf and what user specifies in kdump.conf
ab224c
kdump_install_conf() {
ab224c
    sed -ne '/^#/!p' /etc/kdump.conf > /tmp/$$-kdump.conf
ab224c
ab224c
    while read config_opt config_val;
ab224c
    do
ab224c
        # remove inline comments after the end of a directive.
ab224c
        config_val=$(strip_comments $config_val)
ab224c
        case "$config_opt" in
ab224c
        ext[234]|xfs|btrfs|minix|raw)
ab224c
            sed -i -e "s#$config_val#$(kdump_to_udev_name $config_val)#" /tmp/$$-kdump.conf
ab224c
            ;;
ab224c
        ssh|nfs)
ab224c
            kdump_install_net "$config_val"
ab224c
            ;;
ab224c
        kdump_pre|kdump_post|extra_bins)
ab224c
            dracut_install $config_val
ab224c
            ;;
ab224c
        core_collector)
ab224c
            dracut_install "${config_val%%[[:blank:]]*}"
ab224c
            ;;
ab224c
        esac
ab224c
    done < /etc/kdump.conf
ab224c
765b01
    kdump_check_fence_kdump
ab224c
    inst "/tmp/$$-kdump.conf" "/etc/kdump.conf"
ab224c
    rm -f /tmp/$$-kdump.conf
ab224c
}
ab224c
ab224c
kdump_iscsi_get_rec_val() {
ab224c
ab224c
    local result
ab224c
ab224c
    # The open-iscsi 742 release changed to using flat files in
ab224c
    # /var/lib/iscsi.
ab224c
ab224c
    result=$(/sbin/iscsiadm --show -m session -r ${1} | grep "^${2} = ")
ab224c
    result=${result##* = }
ab224c
    echo $result
ab224c
}
ab224c
ab224c
kdump_get_iscsi_initiator() {
ab224c
    local _initiator
ab224c
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
ab224c
ab224c
    [ -f "$initiator_conf" ] || return 1
ab224c
ab224c
    while read _initiator; do
ab224c
        [ -z "${_initiator%%#*}" ] && continue # Skip comment lines
ab224c
ab224c
        case $_initiator in
ab224c
            InitiatorName=*)
ab224c
                initiator=${_initiator#InitiatorName=}
ab224c
                echo "rd.iscsi.initiator=${initiator}"
ab224c
                return 0;;
ab224c
            *) ;;
ab224c
        esac
ab224c
    done < ${initiator_conf}
ab224c
ab224c
    return 1
ab224c
}
ab224c
ab224c
# No ibft handling yet.
ab224c
kdump_setup_iscsi_device() {
ab224c
    local path=$1
ab224c
    local tgt_name; local tgt_ipaddr;
ab224c
    local username; local password; local userpwd_str;
ab224c
    local username_in; local password_in; local userpwd_in_str;
ab224c
    local netdev
ab224c
    local idev
ab224c
    local netroot_str ; local initiator_str;
ab224c
    local netroot_conf="${initdir}/etc/cmdline.d/50iscsi.conf"
ab224c
    local initiator_conf="/etc/iscsi/initiatorname.iscsi"
ab224c
ab224c
    dinfo "Found iscsi component $1"
ab224c
ab224c
    # Check once before getting explicit values, so we can output a decent
ab224c
    # error message.
ab224c
ab224c
    if ! /sbin/iscsiadm -m session -r ${path} >/dev/null ; then
ab224c
        derror "Unable to find iscsi record for $path"
ab224c
        return 1
ab224c
    fi
ab224c
ab224c
    tgt_name=$(kdump_iscsi_get_rec_val ${path} "node.name")
ab224c
    tgt_ipaddr=$(kdump_iscsi_get_rec_val ${path} "node.conn\[0\].address")
ab224c
ab224c
    # get and set username and password details
ab224c
    username=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.username")
ab224c
    [ "$username" == "<empty>" ] && username=""
ab224c
    password=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.password")
ab224c
    [ "$password" == "<empty>" ] && password=""
ab224c
    username_in=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.username_in")
ab224c
    [ -n "$username" ] && userpwd_str="$username:$password"
ab224c
ab224c
    # get and set incoming username and password details
ab224c
    [ "$username_in" == "<empty>" ] && username_in=""
ab224c
    password_in=$(kdump_iscsi_get_rec_val ${path} "node.session.auth.password_in")
ab224c
    [ "$password_in" == "<empty>" ] && password_in=""
ab224c
ab224c
    [ -n "$username_in" ] && userpwd_in_str=":$username_in:$password_in"
ab224c
ab224c
    netdev=$(/sbin/ip route get to ${tgt_ipaddr} | \
ab224c
        sed 's|.*dev \(.*\).*|\1|g' | awk '{ print $1; exit }')
ab224c
ab224c
    kdump_setup_netdev $netdev
ab224c
ab224c
    # prepare netroot= command line
ab224c
    # FIXME: IPV6 addresses require explicit [] around $tgt_ipaddr
ab224c
    # FIXME: Do we need to parse and set other parameters like protocol, port
ab224c
    #        iscsi_iface_name, netdev_name, LUN etc.
ab224c
ab224c
    netroot_str="netroot=iscsi:${userpwd_str}${userpwd_in_str}@$tgt_ipaddr::::$tgt_name"
ab224c
ab224c
    [[ -f $netroot_conf ]] || touch $netroot_conf
ab224c
ab224c
    # If netroot target does not exist already, append.
ab224c
    if ! grep -q $netroot_str $netroot_conf; then
ab224c
         echo $netroot_str >> $netroot_conf
ab224c
         dinfo "Appended $netroot_str to $netroot_conf"
ab224c
    fi
ab224c
ab224c
    # Setup initator
ab224c
    initiator_str=$(kdump_get_iscsi_initiator)
ab224c
    [ $? -ne "0" ] && derror "Failed to get initiator name" && return 1
ab224c
ab224c
    # If initiator details do not exist already, append.
ab224c
    if ! grep -q "$initiator_str" $netroot_conf; then
ab224c
         echo "$initiator_str" >> $netroot_conf
ab224c
         dinfo "Appended "$initiator_str" to $netroot_conf"
ab224c
    fi
ab224c
}
ab224c
ab224c
kdump_check_iscsi_targets () {
ab224c
    # If our prerequisites are not met, fail anyways.
ab224c
    type -P iscsistart >/dev/null || return 1
ab224c
ab224c
    kdump_check_setup_iscsi() (
ab224c
        local _dev
ab224c
        _dev=$1
ab224c
ab224c
        [[ -L /sys/dev/block/$_dev ]] || return
ab224c
        cd "$(readlink -f /sys/dev/block/$_dev)"
ab224c
        until [[ -d sys || -d iscsi_session ]]; do
ab224c
            cd ..
ab224c
        done
ab224c
        [[ -d iscsi_session ]] && kdump_setup_iscsi_device "$PWD"
ab224c
    )
ab224c
ab224c
    [[ $hostonly ]] || [[ $mount_needs ]] && {
ab224c
        for_each_host_dev_and_slaves_all kdump_check_setup_iscsi
ab224c
    }
ab224c
}
ab224c
ab224c
765b01
# setup fence_kdump in cluster
765b01
# setup proper network and install needed files
765b01
# also preserve '[node list]' for 2nd kernel /etc/fence_kdump_nodes
765b01
kdump_check_fence_kdump () {
765b01
    local nodes
765b01
    is_fence_kdump || return 1
765b01
765b01
    # get cluster nodes from cluster cib, get interface and ip address
765b01
    nodelist=`pcs cluster cib | xmllint --xpath "/cib/status/node_state/@uname" -`
765b01
765b01
    # nodelist is formed as 'uname="node1" uname="node2" ... uname="nodeX"'
765b01
    # we need to convert each to node1, node2 ... nodeX in each iteration
765b01
    for node in ${nodelist}; do
765b01
        # convert $node from 'uname="nodeX"' to 'nodeX'
765b01
        eval $node
765b01
        nodename=$uname
765b01
        # Skip its own node name
765b01
        if [ "$nodename" = `hostname` ]; then
765b01
            continue
765b01
        fi
765b01
        nodes="$nodes $nodename"
765b01
765b01
        kdump_install_net $nodename
765b01
    done
765b01
    echo
765b01
765b01
    echo "$nodes" > ${initdir}/$FENCE_KDUMP_NODES
765b01
    dracut_install $FENCE_KDUMP_SEND
765b01
    dracut_install -o $FENCE_KDUMP_CONFIG
765b01
}
765b01
765b01
# Install a random seed used to feed /dev/urandom
765b01
# By the time kdump service starts, /dev/uramdom is already fed by systemd
765b01
kdump_install_random_seed() {
765b01
    local poolsize=`cat /proc/sys/kernel/random/poolsize`
765b01
765b01
    if [ ! -d ${initdir}/var/lib/ ]; then
765b01
        mkdir -p ${initdir}/var/lib/
765b01
    fi
765b01
765b01
    dd if=/dev/urandom of=${initdir}/var/lib/random-seed \
765b01
       bs=$poolsize count=1 2> /dev/null
765b01
}
765b01
ab224c
install() {
ab224c
    kdump_install_conf
ab224c
    >"$initdir/lib/dracut/no-emergency-shell"
ab224c
ab224c
    if is_ssh_dump_target; then
765b01
        kdump_install_random_seed
ab224c
    fi
ab224c
    dracut_install -o /etc/adjtime /etc/localtime
ab224c
    inst "$moddir/monitor_dd_progress" "/kdumpscripts/monitor_dd_progress"
ab224c
    chmod +x ${initdir}/kdumpscripts/monitor_dd_progress
ab224c
    inst "/bin/dd" "/bin/dd"
ab224c
    inst "/bin/tail" "/bin/tail"
ab224c
    inst "/bin/date" "/bin/date"
ab224c
    inst "/bin/sync" "/bin/sync"
ab224c
    inst "/bin/cut" "/bin/cut"
ab224c
    inst "/sbin/makedumpfile" "/sbin/makedumpfile"
ab224c
    inst "/sbin/vmcore-dmesg" "/sbin/vmcore-dmesg"
ab224c
    inst_hook pre-pivot 9999 "$moddir/kdump.sh"
ab224c
    inst "/lib/kdump/kdump-lib.sh" "/lib/kdump-lib.sh"
ab224c
ab224c
    # Check for all the devices and if any device is iscsi, bring up iscsi
ab224c
    # target. Ideally all this should be pushed into dracut iscsi module
ab224c
    # at some point of time.
ab224c
    kdump_check_iscsi_targets
ab224c
}
765b01
765b01
installkernel() {
765b01
    wdt=$(lsmod|cut -f1 -d' '|grep "wdt$")
765b01
    if [ -n "$wdt" ]; then
765b01
        [ "$wdt" = "iTCO_wdt" ] && instmods lpc_ich
765b01
        instmods $wdt
765b01
    fi
765b01
}