From 1165a99610e1db3fe7b5b46c192a773cbf1599a1 Mon Sep 17 00:00:00 2001 From: bybai Date: Wed, 20 Mar 2019 05:33:21 -0400 Subject: [PATCH 1/4] configure bond support using nmcli --- xCAT/postscripts/confignetwork | 6 +- xCAT/postscripts/nicutils.sh | 255 +++++++++++++++++++++++++++++++-- 2 files changed, 246 insertions(+), 15 deletions(-) diff --git a/xCAT/postscripts/confignetwork b/xCAT/postscripts/confignetwork index 14b829bc3..3e5587d68 100755 --- a/xCAT/postscripts/confignetwork +++ b/xCAT/postscripts/confignetwork @@ -592,7 +592,11 @@ function configure_nicdevice { if [ "$networkmanager_active" = "0" ]; then create_bond_interface ifname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type elif [ "$networkmanager_active" = "1" ]; then - create_bond_interface_nmcli ifname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type + create_bond_interface_nmcli bondname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type _ipaddr=$ipaddrs + fi + if [ $? -ne 0 ]; then + log_error "configure bond $nic_dev failed." + errorcode=1 fi elif [ x"$nic_dev_type" = "xinfiniband" ] || [ x"$nic_dev_type" = "xOmnipath" ]; then log_info "Call configib for IB nics: $nic_dev, ports: $num_iba_ports" diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh index a6a32a2a8..ce7b6db07 100755 --- a/xCAT/postscripts/nicutils.sh +++ b/xCAT/postscripts/nicutils.sh @@ -1878,7 +1878,9 @@ function create_bridge_interface_nmcli { fi fi log_info "create bridge connection $xcat_con_name" - $nmcli con add type bridge con-name $xcat_con_name ifname $ifname + cmd="$nmcli con add type bridge con-name $xcat_con_name ifname $ifname $_mtu" + log_info $cmd + $cmd if [ $? -ne 0 ]; then log_error "nmcli failed to add bridge $ifname" is_nmcli_connection_exist $tmp_con_name @@ -1906,7 +1908,9 @@ function create_bridge_interface_nmcli { fi fi log_info "create $_pretype slaves connetcion $xcat_slave_con for bridge" - $nmcli con add type $_pretype con-name $xcat_slave_con ifname $_port master $ifname $_mtu + cmd="$nmcli con add type $_pretype con-name $xcat_slave_con ifname $_port master $ifname $_mtu" + log_info "$cmd" + $cmd if [ $? -ne 0 ]; then log_error "nmcli failed to add bridge slave $_port" is_nmcli_connection_exist $tmp_slave_con_name @@ -1926,15 +1930,6 @@ function create_bridge_interface_nmcli { $nmcli con mod $xcat_con_name ipv4.method manual ipv4.addresses $ipv4_addr/$str_prefix; fi - # Configure MTU - if [ -n "$_mtu" ]; then - $nmcli con mod $xcat_con_name $_mtu - if [ $? -ne 0 ]; then - log_error "$nmcli con mod $xcat_con_name $_mtu failed" - rc=1 - fi - fi - # bring up interface formally log_info "$nmcli con up $xcat_slave_con; $nmcli con up $xcat_con_name" lines=`$nmcli con up $xcat_slave_con; $nmcli con up $xcat_con_name` @@ -1976,12 +1971,244 @@ function create_bridge_interface_nmcli { ############################################################################################################################# # # create bond or bond->vlan interface -# https://www.kernel.org/doc/Documentation/networking/bonding.txt -# https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/sec-Using_Channel_Bonding.html # -# input : ifname= xcatnet= _ipaddr= _netmask= _bonding_opts= _mtu= slave_ports= +# input : bondname= _ipaddr= slave_ports= slave_type= +# return : 0 successful +# other unsuccessful # ############################################################################################################################ function create_bond_interface_nmcli { log_info "create_bond_interface $@" + local lines="" + local bondname="" + local xcatnet="" + local _ipaddr="" + local _netmask="" + local _bonding_opts="" + local _mtu="" + local slave_ports="" #bond slaves + local slave_type="" + local rc=0 + local cleanup=0 + # parser input arguments + while [ -n "$1" ]; + do + key=$(echo "$1" | $cut -s -d= -f1) + if [ "$key" = "bondname" ] || \ + [ "$key" = "_ipaddr" ] || \ + [ "$key" = "slave_ports" ] || \ + [ "$key" = "slave_type" ]; then + eval "$1" + fi + shift + done + if [ "$slave_type" = "ethernet" ]; then + slave_type="Ethernet" + # - "802.3ad" mode requires a switch that is 802.3ad compliant. + _bonding_opts="mode=802.3ad,miimon=100" + elif [ "$slave_type" = "infiniband" ]; then + slave_type="Infiniband" + _bonding_opts="mode=1,miimon=100,fail_over_mac=1" + fi + # query "nicnetworks" table about its target "xcatnet" + xcatnet=$(query_nicnetworks_net $bondname) + log_info "Pickup xcatnet, \"$xcatnet\", from NICNETWORKS for interface \"$bondname\"." + + # Query mtu value from "networks" table + _mtu_num=$(get_network_attr $xcatnet mtu) + if [ $? -ne 0 ]; then + _mtu="" + else + _mtu="mtu $_mtu_num" + fi + + # Query mask value from "networks" table + _netmask=$(get_network_attr $xcatnet mask) + if [ $? -ne 0 ]; then + log_error "No valid netmask get for $bondname" + return 1 + fi + + # Calculate prefix based on mask + str_prefix=$(v4mask2prefix $_netmask) + + # Get first valid ip from nics.nicips + ipv4_addr=$(get_first_addr_ipv4 $_ipaddr) + if [ $? -ne 0 ]; then + log_error "No valid IP address get for $bondname, please check $ipaddrs" + return 1 + fi + + # check if all slave dev managed or not by nmcli + for ifslave in $(echo "$slave_ports" | $sed -e 's/,/ /g') + do + check_and_set_device_managed $ifslave + if [ $? -ne 0 ]; then + return 1 + fi + + done + + # check if bond connection exist or not + xcat_con_name="xcat-"$bondname + tmp_con_name="" + is_nmcli_connection_exist $xcat_con_name + if [ $? -eq 0 ]; then + is_connection_activate_intime $xcat_con_name 10 + if [ $? -eq 1 ]; then + $nmcli con down $xcat_con_name + wait_for_ifstate $bondname DOWN 40 1 + fi + tmp_con_name="xcat-bond-$xcat_con_name-tmp" + log_info "$xcat_con_name exists, rename old $xcat_con_name to $tmp_con_name" + $nmcli con modify $xcat_con_name connection.id $tmp_con_name + if [ $? -ne 0 ] ; then + log_error "$nmcli rename $xcat_con_name failed" + return 1 + fi + fi + + # create raw bond device + log_info "create bond connection $xcat_con_name" + cmd="$nmcli con add type bond con-name $xcat_con_name ifname $bondname bond.options $_bonding_opts method none ipv4.method manual ipv4.addresses $ipv4_addr/$str_prefix $_mtu" + log_info $cmd + $cmd + if [ $? -ne 0 ]; then + log_error "nmcli failed to add bond connection $xcat_con_name" + if [ -n "$tmp_con_name" ] ; then + $nmcli con modify $tmp_con_name connection.id $xcat_con_name + fi + return 1 + fi + + # Create slaves connection + num=1 + xcat_slave_con_names="" + tmp_slave_con_names="" + for ifslave in $(echo "$slave_ports" | $sed -e 's/,/ /g') + do + tmp_slave_con_name="" + xcat_slave_con="xcat-bond-slave-"$ifslave + is_nmcli_connection_exist $xcat_slave_con + if [ $? -eq 0 ] ; then + tmp_slave_con_name=$xcat_slave_con"-tmp-"$num + log_info "rename $xcat_slave_con to $tmp_slave_con_name" + $nmcli con modify $xcat_slave_con connection.id $tmp_slave_con_name + if [ -n "$tmp_slave_con_names" ]; then + tmp_slave_con_names="$tmp_slave_con_names $tmp_slave_con_name" + else + tmp_slave_con_names=$tmp_slave_con_name + fi + + fi + cmd="$nmcli con add type $slave_type con-name $xcat_slave_con $_mtu method none ifname $ifslave master $xcat_con_name" + log_info $cmd + $cmd + if [ $? -ne 0 ]; then + log_error "nmcli failed to add bond slave connection $xcat_slave_name" + if [ -n "$tmp_slave_con_name" ] ; then + $nmcli con modify $tmp_slave_con_name connection.id $xcat_slave_name + fi + rc=1 + break + else + if [ -n "$xcat_slave_con_names" ]; then + xcat_slave_con_names="$xcat_slave_con_names $xcat_slave_con" + else + xcat_slave_con_names=$xcat_slave_con + fi + fi + if [ -n "$tmp_slave_con_name" ]; then + is_connection_activate_intime $tmp_slave_con_name 10 + if [ $? -eq 1 ]; then + $nmcli con down $tmp_slave_con_name + fi + fi + cmd="$nmcli con up $xcat_slave_con" + log_info $cmd + $cmd + is_connection_activate_intime $xcat_slave_con + is_active=$? + if [ "$is_active" -eq 0 ]; then + log_error "The bond slave connection $xcat_slave_con can not be booted up" + $nmcli con delete $xcat_slave_con + if [ -n "$tmp_slave_con_name" ]; then + $nmcli con modify $tmp_slave_con_name connection.id $xcat_slave_con + fi + rc=1 + break + fi + done + # bring up interface formally + if [ $rc -ne 1 ]; then + log_info "$nmcli con up $xcat_con_name" + lines=$($nmcli con up $xcat_con_name) + is_connection_activate_intime $xcat_con_name + is_active=$? + if [ "$is_active" -eq 0 ]; then + log_error "connection $xcat_con_name failed to activate" + rc=1 + else + wait_for_ifstate $bondname UP 20 10 + if [ $? -ne 0 ]; then + rc=1 + else + $ip address show dev $bondname| $sed -e 's/^/[bond] >> /g' | log_lines info + fi + fi + fi + + if [ $rc -eq 1 ]; then + # delete all bond slave and master which is created by xCAT + if [ -n "$xcat_slave_con_names" ]; then + log_info "delete connection $xcat_slave_con_names" + delete_bond_slaves_con "$xcat_slave_con_names" + fi + if [ -n "$tmp_slave_con_names" ]; then + for tmpslave in $tmp_slave_con_names + do + slavecon=$(echo $tmpslave|awk -F"-" '{print $1"-"$2"-"$3"-"$4}') + log_info "restore connection $slavecon" + $nmcli con modify $tmpslave connection.id $slavecon + done + fi + is_nmcli_connection_exist $xcat_con_name + if [ $? -eq 0 ] ; then + log_info "delete bond connection $xcat_con_name" + $nmcli con delete $xcat_con_name + fi + if [ -n "$tmp_con_name" ] ; then + log_info "restore bond connection $tmp_con_name" + $nmcli con modify $tmp_con_name connection.id $xcat_con_name + fi + else + # delete tmp master and tmp slaves + if [ -n "$tmp_con_name" ] ; then + $nmcli con delete $tmp_con_name + fi + if [ -n "$tmp_slave_con_names" ]; then + delete_bond_slaves_con "$tmp_slave_con_names" + fi + fi + return $rc +} +###################################################################### +# +# delete bond slaves connection +# imput format: ... ... +# +###################################################################### +function delete_bond_slaves_con { + slaves_con_names=$1 + if [ -z "$slaves_con_names" ]; then + log_error "bond slaves connection list is empty" + return + fi + for slave in $slaves_con_names + do + is_nmcli_connection_exist $slave + if [ $? -eq 0 ] ; then + $nmcli con delete $slave + fi + done } From 91c02af9eec941613e00092c59cf606f96312fdb Mon Sep 17 00:00:00 2001 From: bybai Date: Thu, 21 Mar 2019 13:00:06 -0400 Subject: [PATCH 2/4] support eth->bond->vlan based on testcase confignetwork_2eth_bridge_br0 --- xCAT/postscripts/confignetwork | 9 +- xCAT/postscripts/nicutils.sh | 175 ++++++++++++++++++++------------- 2 files changed, 111 insertions(+), 73 deletions(-) diff --git a/xCAT/postscripts/confignetwork b/xCAT/postscripts/confignetwork index 3e5587d68..519c0e311 100755 --- a/xCAT/postscripts/confignetwork +++ b/xCAT/postscripts/confignetwork @@ -524,6 +524,7 @@ function configure_nicdevice { nic_pair=`echo "$nics_pair" |sed -n "${num}p"` echo "configure nic and its device : $nic_pair" ipaddrs=$(find_nic_ips $nic_dev) + next_nic=$(echo "$nics_pair"|awk /${nic_dev}$/'{ print $1}') #ignore bmc interfaces. They're allowed in the nics table to generate DNS/hostname records, but they #can't be configured here (it's done in bmcsetup if [ x"$nic_dev_type" = "xbmc" ]; then @@ -592,7 +593,7 @@ function configure_nicdevice { if [ "$networkmanager_active" = "0" ]; then create_bond_interface ifname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type elif [ "$networkmanager_active" = "1" ]; then - create_bond_interface_nmcli bondname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type _ipaddr=$ipaddrs + create_bond_interface_nmcli bondname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type _ipaddr=$ipaddrs next_nic=$next_nic fi if [ $? -ne 0 ]; then log_error "configure bond $nic_dev failed." @@ -627,13 +628,13 @@ errorcode=0 utolcmd="sed -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" #back up all network interface configure files -nwdirbak=$nwdir".xcatbak" +nwdirbak="/tmp/" ls $nwdirbak > /dev/null 2>/dev/null if [ $? -ne 0 ]; then - log_info "back up $nwdir to $newdirbak" + log_info "back up $nwdir to $nwdirbak" cp -rf $nwdir $nwdirbak > /dev/null 2>/dev/null if [ $? -ne 0 ]; then - log_warn "back up $nwdir to $newdirbak failed." + log_warn "back up $nwdir to $nwdirbak failed." fi fi diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh index ce7b6db07..49118481f 100755 --- a/xCAT/postscripts/nicutils.sh +++ b/xCAT/postscripts/nicutils.sh @@ -1737,7 +1737,7 @@ function create_vlan_interface_nmcli { check_and_set_device_managed $ifname if [ $? -ne 0 ]; then log_error "The parent interface $ifname is unmanaged, so skip $ifname.$vlanid" - retrun 1 + return 1 fi log_info "check parent interface $ifname whether it is managed by NetworkManager" #load the 8021q module if not loaded. @@ -1865,13 +1865,19 @@ function create_bridge_interface_nmcli { return 1 fi # Create bridge connection - xcat_con_name="xcat"$ifname + xcat_con_name="xcat-bridge-"$ifname tmp_con_name=$xcat_con_name"-tmp" if [ x"$_brtype" = "xbridge" ]; then is_nmcli_connection_exist $xcat_con_name if [ $? -eq 0 ] ; then + is_connection_activate_intime $xcat_con_name 1 + if [ $? -eq 1 ]; then + log_info "$xcat_con_name exists, down it first" + $nmcli con down $xcat_con_name + $ip link set dev $ifname down + fi log_info "$xcat_con_name exists, rename old $xcat_con_name to $tmp_con_name" - $nmcli con modify $xcat_con_name connection.id $tmp_con_name + $nmcli con modify $xcat_con_name connection.id $tmp_con_name autoconnect no if [ $? -ne 0 ] ; then log_error "$nmcli rename $xcat_con_name failed" return 1 @@ -1895,20 +1901,31 @@ function create_bridge_interface_nmcli { fi # Create slaves connection - xcat_slave_con="xcat_br_"$_port + xcat_slave_con="xcat-br-slave-"$_port tmp_slave_con_name=$xcat_slave_con"-tmp" if [ x"$_pretype" = "xethernet" -o x"$_pretype" = "xvlan" -o x"$_pretype" = "xbond" ]; then is_nmcli_connection_exist $xcat_slave_con if [ $? -eq 0 ] ; then + is_connection_activate_intime $xcat_slave_con 1 + if [ $? -eq 1 ]; then + $nmcli con down $xcat_slave_con + $ip link set dev $_port down + fi log_info "$xcat_slave_con exists, rename old connetion $xcat_slave_con to $tmp_slave_con_name" - $nmcli con modify $xcat_slave_con connection.id $tmp_slave_con_name + $nmcli con modify $xcat_slave_con connection.id $tmp_slave_con_name autoconnect no if [ $? -ne 0 ] ; then log_error "$nmcli rename $xcat_slave_con failed" return 1 fi fi + con_use_same_dev=$(nmcli dev show $_port|grep GENERAL.CONNECTION|awk -F: '{print $2}'|sed 's/^[ \t]*//g') + if [ "$con_use_same_dev" != "--" -a -n "$con_use_same_dev" ]; then + cmd="$nmcli con mod "$con_use_same_dev" master $ifname $_mtu" + xcat_slave_con=$con_use_same_dev + else + cmd="$nmcli con add type $_pretype con-name $xcat_slave_con ifname $_port master $ifname $_mtu" + fi log_info "create $_pretype slaves connetcion $xcat_slave_con for bridge" - cmd="$nmcli con add type $_pretype con-name $xcat_slave_con ifname $_port master $ifname $_mtu" log_info "$cmd" $cmd if [ $? -ne 0 ]; then @@ -1931,10 +1948,11 @@ function create_bridge_interface_nmcli { fi # bring up interface formally - log_info "$nmcli con up $xcat_slave_con; $nmcli con up $xcat_con_name" - lines=`$nmcli con up $xcat_slave_con; $nmcli con up $xcat_con_name` + log_info "$nmcli con up $xcat_con_name" + $nmcli con up $xcat_con_name rc=$? - + log_info "$nmcli con up $xcat_slave_con" + $nmcli con up $xcat_slave_con # If bridge interface is active, delete tmp old connection # If bridge interface is not active, delete new bridge and slave connection, and restore old connection is_connection_activate_intime $xcat_con_name @@ -1972,14 +1990,13 @@ function create_bridge_interface_nmcli { # # create bond or bond->vlan interface # -# input : bondname= _ipaddr= slave_ports= slave_type= +# input : bondname= _ipaddr= slave_ports= slave_type= next_nic= # return : 0 successful # other unsuccessful # ############################################################################################################################ function create_bond_interface_nmcli { - log_info "create_bond_interface $@" - local lines="" + log_info "create_bond_interface_nmcli $@" local bondname="" local xcatnet="" local _ipaddr="" @@ -1989,7 +2006,7 @@ function create_bond_interface_nmcli { local slave_ports="" #bond slaves local slave_type="" local rc=0 - local cleanup=0 + local next_nic="" # parser input arguments while [ -n "$1" ]; do @@ -1997,6 +2014,7 @@ function create_bond_interface_nmcli { if [ "$key" = "bondname" ] || \ [ "$key" = "_ipaddr" ] || \ [ "$key" = "slave_ports" ] || \ + [ "$key" = "next_nic" ] || \ [ "$key" = "slave_type" ]; then eval "$1" fi @@ -2009,38 +2027,41 @@ function create_bond_interface_nmcli { elif [ "$slave_type" = "infiniband" ]; then slave_type="Infiniband" _bonding_opts="mode=1,miimon=100,fail_over_mac=1" - fi - # query "nicnetworks" table about its target "xcatnet" - xcatnet=$(query_nicnetworks_net $bondname) - log_info "Pickup xcatnet, \"$xcatnet\", from NICNETWORKS for interface \"$bondname\"." - - # Query mtu value from "networks" table - _mtu_num=$(get_network_attr $xcatnet mtu) - if [ $? -ne 0 ]; then - _mtu="" else - _mtu="mtu $_mtu_num" + _bonding_opts="mode=active-backup" fi + if [ -z "$next_nic" ]; then + # query "nicnetworks" table about its target "xcatnet" + xcatnet=$(query_nicnetworks_net $bondname) + log_info "Pickup xcatnet, \"$xcatnet\", from NICNETWORKS for interface \"$bondname\"." - # Query mask value from "networks" table - _netmask=$(get_network_attr $xcatnet mask) - if [ $? -ne 0 ]; then - log_error "No valid netmask get for $bondname" - return 1 + # Query mtu value from "networks" table + _mtu_num=$(get_network_attr $xcatnet mtu) + if [ -n "$_mtu_num" ]; then + _mtu="mtu $_mtu_num" + fi + + # Query mask value from "networks" table + _netmask=$(get_network_attr $xcatnet mask) + if [ $? -ne 0 ]; then + log_error "No valid netmask get for $bondname" + return 1 + fi + + # Calculate prefix based on mask + str_prefix=$(v4mask2prefix $_netmask) + + # Get first valid ip from nics.nicips + ipv4_addr=$(get_first_addr_ipv4 $_ipaddr) + if [ $? -ne 0 ]; then + log_warn "No valid IP address get for $bondname, please check $ipaddrs" + return 1 + fi + fi - - # Calculate prefix based on mask - str_prefix=$(v4mask2prefix $_netmask) - - # Get first valid ip from nics.nicips - ipv4_addr=$(get_first_addr_ipv4 $_ipaddr) - if [ $? -ne 0 ]; then - log_error "No valid IP address get for $bondname, please check $ipaddrs" - return 1 - fi - # check if all slave dev managed or not by nmcli - for ifslave in $(echo "$slave_ports" | $sed -e 's/,/ /g') + xcat_slave_ports=$(echo "$slave_ports" | $sed -e 's/,/ /g') + for ifslave in $xcat_slave_ports do check_and_set_device_managed $ifslave if [ $? -ne 0 ]; then @@ -2050,18 +2071,19 @@ function create_bond_interface_nmcli { done # check if bond connection exist or not - xcat_con_name="xcat-"$bondname + xcat_con_name="xcat-bond-"$bondname tmp_con_name="" is_nmcli_connection_exist $xcat_con_name if [ $? -eq 0 ]; then - is_connection_activate_intime $xcat_con_name 10 + is_connection_activate_intime $xcat_con_name 1 if [ $? -eq 1 ]; then $nmcli con down $xcat_con_name + $ip link set dev $bondname down wait_for_ifstate $bondname DOWN 40 1 fi - tmp_con_name="xcat-bond-$xcat_con_name-tmp" + tmp_con_name="$xcat_con_name-tmp" log_info "$xcat_con_name exists, rename old $xcat_con_name to $tmp_con_name" - $nmcli con modify $xcat_con_name connection.id $tmp_con_name + $nmcli con modify $xcat_con_name connection.id $tmp_con_name autoconnect no if [ $? -ne 0 ] ; then log_error "$nmcli rename $xcat_con_name failed" return 1 @@ -2070,7 +2092,12 @@ function create_bond_interface_nmcli { # create raw bond device log_info "create bond connection $xcat_con_name" - cmd="$nmcli con add type bond con-name $xcat_con_name ifname $bondname bond.options $_bonding_opts method none ipv4.method manual ipv4.addresses $ipv4_addr/$str_prefix $_mtu" + cmd="" + if [ -n "$next_nic" ]; then + cmd="$nmcli con add type bond con-name $xcat_con_name ifname $bondname bond.options $_bonding_opts autoconnect yes" + else + cmd="$nmcli con add type bond con-name $xcat_con_name ifname $bondname bond.options $_bonding_opts method none ipv4.method manual ipv4.addresses $ipv4_addr/$str_prefix $_mtu" + fi log_info $cmd $cmd if [ $? -ne 0 ]; then @@ -2082,18 +2109,23 @@ function create_bond_interface_nmcli { fi # Create slaves connection - num=1 xcat_slave_con_names="" tmp_slave_con_names="" - for ifslave in $(echo "$slave_ports" | $sed -e 's/,/ /g') + for ifslave in $xcat_slave_ports do tmp_slave_con_name="" xcat_slave_con="xcat-bond-slave-"$ifslave is_nmcli_connection_exist $xcat_slave_con if [ $? -eq 0 ] ; then - tmp_slave_con_name=$xcat_slave_con"-tmp-"$num + is_connection_activate_intime $xcat_slave_con 1 + if [ $? -eq 1 ]; then + $nmcli con down $xcat_con_name + ip link set dev $ifslave down + wait_for_ifstate $ifslave DOWN 40 1 + fi + tmp_slave_con_name=$xcat_slave_con"-tmp" log_info "rename $xcat_slave_con to $tmp_slave_con_name" - $nmcli con modify $xcat_slave_con connection.id $tmp_slave_con_name + $nmcli con modify $xcat_slave_con connection.id $tmp_slave_con_name autoconnect no if [ -n "$tmp_slave_con_names" ]; then tmp_slave_con_names="$tmp_slave_con_names $tmp_slave_con_name" else @@ -2101,13 +2133,20 @@ function create_bond_interface_nmcli { fi fi - cmd="$nmcli con add type $slave_type con-name $xcat_slave_con $_mtu method none ifname $ifslave master $xcat_con_name" + con_use_same_dev=$(nmcli dev show $ifslave|grep GENERAL.CONNECTION|awk -F: '{print $2}'|sed 's/^[ \t]*//g') + if [ "$con_use_same_dev" != "--" -a "$con_use_same_dev" != "$xcat_slave_con" ]; then + $nmcli con down "$con_use_same_dev" + $nmcli con mod "$con_use_same_dev" autoconnect no + $ip link set dev $ifslave down + wait_for_ifstate $ifslave DOWN 20 2 + fi + cmd="$nmcli con add type $slave_type con-name $xcat_slave_con $_mtu method none ifname $ifslave master $xcat_con_name autoconnect yes" log_info $cmd $cmd if [ $? -ne 0 ]; then - log_error "nmcli failed to add bond slave connection $xcat_slave_name" + log_error "nmcli failed to add bond slave connection $xcat_slave_con" if [ -n "$tmp_slave_con_name" ] ; then - $nmcli con modify $tmp_slave_con_name connection.id $xcat_slave_name + $nmcli con modify $tmp_slave_con_name connection.id $xcat_slave_con fi rc=1 break @@ -2118,12 +2157,6 @@ function create_bond_interface_nmcli { xcat_slave_con_names=$xcat_slave_con fi fi - if [ -n "$tmp_slave_con_name" ]; then - is_connection_activate_intime $tmp_slave_con_name 10 - if [ $? -eq 1 ]; then - $nmcli con down $tmp_slave_con_name - fi - fi cmd="$nmcli con up $xcat_slave_con" log_info $cmd $cmd @@ -2142,19 +2175,23 @@ function create_bond_interface_nmcli { # bring up interface formally if [ $rc -ne 1 ]; then log_info "$nmcli con up $xcat_con_name" - lines=$($nmcli con up $xcat_con_name) - is_connection_activate_intime $xcat_con_name - is_active=$? - if [ "$is_active" -eq 0 ]; then - log_error "connection $xcat_con_name failed to activate" - rc=1 - else - wait_for_ifstate $bondname UP 20 10 - if [ $? -ne 0 ]; then + $nmcli con up $xcat_con_name + if [ -z "$next_nic" ]; then + is_connection_activate_intime $xcat_con_name + is_active=$? + if [ "$is_active" -eq 0 ]; then + log_error "connection $xcat_con_name failed to activate" rc=1 else - $ip address show dev $bondname| $sed -e 's/^/[bond] >> /g' | log_lines info + wait_for_ifstate $bondname UP 20 10 + if [ $? -ne 0 ]; then + rc=1 + else + $ip address show dev $bondname| $sed -e 's/^/[bond] >> /g' | log_lines info + fi fi + else + rc=0 fi fi @@ -2167,7 +2204,7 @@ function create_bond_interface_nmcli { if [ -n "$tmp_slave_con_names" ]; then for tmpslave in $tmp_slave_con_names do - slavecon=$(echo $tmpslave|awk -F"-" '{print $1"-"$2"-"$3"-"$4}') + slavecon=$(echo $tmpslave|sed 's/-tmp$//') log_info "restore connection $slavecon" $nmcli con modify $tmpslave connection.id $slavecon done From 8e1adba4fbd335dcc8e562207302813ff1cc89f6 Mon Sep 17 00:00:00 2001 From: bybai Date: Thu, 21 Mar 2019 23:40:01 -0400 Subject: [PATCH 3/4] enhance configure vlan support eth->vlan->bridge --- xCAT/postscripts/confignetwork | 2 +- xCAT/postscripts/nicutils.sh | 66 ++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/xCAT/postscripts/confignetwork b/xCAT/postscripts/confignetwork index 519c0e311..39fce3b8d 100755 --- a/xCAT/postscripts/confignetwork +++ b/xCAT/postscripts/confignetwork @@ -582,7 +582,7 @@ function configure_nicdevice { if [ "$networkmanager_active" = "0" ]; then create_vlan_interface ifname=$vlanname vlanid=$vlanid elif [ "$networkmanager_active" = "1" ]; then - create_vlan_interface_nmcli ifname=$vlanname vlanid=$vlanid ipaddrs=$ipaddrs + create_vlan_interface_nmcli ifname=$vlanname vlanid=$vlanid ipaddrs=$ipaddrs next_nic=$next_nic fi if [ $? -ne 0 ]; then log_error "configure VLAN failed." diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh index 49118481f..cd52a76b1 100755 --- a/xCAT/postscripts/nicutils.sh +++ b/xCAT/postscripts/nicutils.sh @@ -1677,7 +1677,7 @@ function get_first_addr_ipv4 { # # create vlan using nmcli # -# input : ifname= vlanid= ipaddrs= +# input : ifname= vlanid= ipaddrs= next_nic= # return : 0 success # ############################################################################### @@ -1690,6 +1690,7 @@ function create_vlan_interface_nmcli { local _xcatnet="" local _netmask="" local _mtu="" + local next_nic="" # in case it's on top of bond, we need to migrate ip from its # member vlan ports. # parser input arguments @@ -1698,6 +1699,7 @@ function create_vlan_interface_nmcli { key=`echo "$1" | $cut -s -d= -f1` if [ "$key" = "ifname" ] || \ [ "$key" = "ipaddrs" ] || \ + [ "$key" = "next_nic" ] || \ [ "$key" = "vlanid" ]; then eval "$1" fi @@ -1708,30 +1710,29 @@ function create_vlan_interface_nmcli { log_error "No \"vlanid\" specificd for vlan interface. Abort!" return 1 fi + if [ -z "$next_nic" ]; then + _xcatnet=$(query_nicnetworks_net $ifname.$vlanid) + log_info "Pickup xcatnet, \"$_xcatnet\", from NICNETWORKS for interface \"$ifname\"." - _xcatnet=$(query_nicnetworks_net $ifname.$vlanid) - log_info "Pickup xcatnet, \"$_xcatnet\", from NICNETWORKS for interface \"$ifname\"." + _mtu_num=$(get_network_attr $xcatnet mtu) + if [ -n "$_mtu_num" ]; then + _mtu="mtu $_mtu_num" + fi - _mtu_num=$(get_network_attr $xcatnet mtu) - if [ $? -ne 0 ]; then - _mtu="" - else - _mtu="mtu $_mtu_num" - fi - - if [ ! -z "$ipaddrs" ]; then - _netmask_long=$(get_network_attr $_xcatnet mask) - if [ $? -ne 0 ]; then - log_error "No valid netmask get for $ifname.$vlanid" - return 1 - else - ipaddr=$(get_first_addr_ipv4 $ipaddrs) + if [ ! -z "$ipaddrs" ]; then + _netmask_long=$(get_network_attr $_xcatnet mask) if [ $? -ne 0 ]; then - log_error "No valid IP address get for $ifname.$vlanid, please check $ipaddrs" + log_error "No valid netmask get for $ifname.$vlanid" return 1 + else + ipaddr=$(get_first_addr_ipv4 $ipaddrs) + if [ $? -ne 0 ]; then + log_error "No valid IP address get for $ifname.$vlanid, please check $ipaddrs" + return 1 + fi + _netmask=$(v4mask2prefix $_netmask_long) + _ipaddrs="method none ip4 $ipaddr/$_netmask" fi - _netmask=$(v4mask2prefix $_netmask_long) - _ipaddrs="method none ip4 $ipaddr/$_netmask" fi fi check_and_set_device_managed $ifname @@ -1742,7 +1743,7 @@ function create_vlan_interface_nmcli { log_info "check parent interface $ifname whether it is managed by NetworkManager" #load the 8021q module if not loaded. load_kmod module=8021q retry=10 interval=0.5 - con_name="xcat.$ifname.$vlanid" + con_name="xcat-vlan-$ifname.$vlanid" tmp_con_name="" is_nmcli_connection_exist $con_name if [ $? -eq 0 ]; then @@ -1752,17 +1753,20 @@ function create_vlan_interface_nmcli { $nmcli con add type vlan con-name $con_name dev $ifname id $(( 10#$vlanid )) $_ipaddrs $_mtu log_info "create NetworkManager connection for $ifname.$vlanid" - $nmcli con up $con_name - is_connection_activate_intime $con_name - is_active=$? - if [ "$is_active" -eq 0 ]; then - log_error "The vlan configuration for $ifname.$vlanid can not be booted up" - $nmcli con delete $con_name - if [ ! -z "$tmp_con_name" ]; then - $nmcli con modify $tmp_con_name connection.id $con_name + if [ -z "$next_nic" ]; then + $nmcli con up $con_name + is_connection_activate_intime $con_name + is_active=$? + if [ "$is_active" -eq 0 ]; then + log_error "The vlan configuration for $ifname.$vlanid can not be booted up" + $nmcli con delete $con_name + if [ ! -z "$tmp_con_name" ]; then + $nmcli con modify $tmp_con_name connection.id $con_name + fi + return 1 fi - return 1 - elif [ ! -z "$tmp_con_name" ]; then + fi + if [ -n "$tmp_con_name" ]; then $nmcli con delete $tmp_con_name fi $ip address show dev $ifname.$vlanid | $sed -e 's/^/[vlan] >> /g' | log_lines info From f0fb356f7c5e3226d2fc703f08e068278bb3d8cc Mon Sep 17 00:00:00 2001 From: bybai Date: Fri, 22 Mar 2019 02:28:29 -0400 Subject: [PATCH 4/4] add comments and change nic configure files backup dir --- xCAT/postscripts/confignetwork | 11 +++++++---- xCAT/postscripts/nicutils.sh | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/xCAT/postscripts/confignetwork b/xCAT/postscripts/confignetwork index 39fce3b8d..47f0e75c5 100755 --- a/xCAT/postscripts/confignetwork +++ b/xCAT/postscripts/confignetwork @@ -524,7 +524,10 @@ function configure_nicdevice { nic_pair=`echo "$nics_pair" |sed -n "${num}p"` echo "configure nic and its device : $nic_pair" ipaddrs=$(find_nic_ips $nic_dev) - next_nic=$(echo "$nics_pair"|awk /${nic_dev}$/'{ print $1}') + # if a device is middle device, it may have no IP, for example, configure eth0->vlan.1->br0, vlan1.1 is middle device + # is_mid_device is to label if ${nic_dev} is middle device + # if $is_mid_device has value, the ${nic_dev} is middle device, or else, it is not middle device + is_mid_device=$(echo "$nics_pair"|awk /${nic_dev}$/'{ print $1}') #ignore bmc interfaces. They're allowed in the nics table to generate DNS/hostname records, but they #can't be configured here (it's done in bmcsetup if [ x"$nic_dev_type" = "xbmc" ]; then @@ -582,7 +585,7 @@ function configure_nicdevice { if [ "$networkmanager_active" = "0" ]; then create_vlan_interface ifname=$vlanname vlanid=$vlanid elif [ "$networkmanager_active" = "1" ]; then - create_vlan_interface_nmcli ifname=$vlanname vlanid=$vlanid ipaddrs=$ipaddrs next_nic=$next_nic + create_vlan_interface_nmcli ifname=$vlanname vlanid=$vlanid ipaddrs=$ipaddrs next_nic=$is_mid_device fi if [ $? -ne 0 ]; then log_error "configure VLAN failed." @@ -593,7 +596,7 @@ function configure_nicdevice { if [ "$networkmanager_active" = "0" ]; then create_bond_interface ifname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type elif [ "$networkmanager_active" = "1" ]; then - create_bond_interface_nmcli bondname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type _ipaddr=$ipaddrs next_nic=$next_nic + create_bond_interface_nmcli bondname=$nic_dev slave_ports=$base_nic_for_bond slave_type=$base_nic_type _ipaddr=$ipaddrs next_nic=$is_mid_device fi if [ $? -ne 0 ]; then log_error "configure bond $nic_dev failed." @@ -628,7 +631,7 @@ errorcode=0 utolcmd="sed -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" #back up all network interface configure files -nwdirbak="/tmp/" +nwdirbak=$nwdir".xcatbak" ls $nwdirbak > /dev/null 2>/dev/null if [ $? -ne 0 ]; then log_info "back up $nwdir to $nwdirbak" diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh index cd52a76b1..70088a4a4 100755 --- a/xCAT/postscripts/nicutils.sh +++ b/xCAT/postscripts/nicutils.sh @@ -2123,7 +2123,7 @@ function create_bond_interface_nmcli { if [ $? -eq 0 ] ; then is_connection_activate_intime $xcat_slave_con 1 if [ $? -eq 1 ]; then - $nmcli con down $xcat_con_name + $nmcli con down $xcat_slave_con ip link set dev $ifslave down wait_for_ifstate $ifslave DOWN 40 1 fi