2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-06-03 03:50:08 +00:00

Merge pull request #6110 from xcat2/configvlan_with_nmcli_update

configvlan with nmcli
This commit is contained in:
Yuan Bai 2019-03-18 16:34:41 +08:00 committed by GitHub
commit b04532cfaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 6 deletions

View File

@ -574,10 +574,15 @@ function configure_nicdevice {
vlanid=`echo "$nic_dev" | $sed -e 's/^\(.*\)vla\?n\?\([0-9]\+\)$/\2/'`
vlanname=`echo "$nic_dev" | $sed -e 's/^\(.*\)vla\?n\?\([0-9]\+\)$/\1/'`
fi
ipaddrs=$(find_nic_ips $nic_dev)
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
create_vlan_interface_nmcli ifname=$vlanname vlanid=$vlanid ipaddrs=$ipaddrs
fi
if [ $? -ne 0 ]; then
log_error "configure VLAN failed."
errorcode=1
fi
#configure bond
elif [ x"$nic_dev_type" = "xbond" ]; then

View File

@ -1191,9 +1191,9 @@ function create_vlan_interface {
while [ ! -f /proc/net/vlan/$ifname.$vlanid ];
do
if [ $i -eq 0 ]; then
# alternative cmd to "vconfig add $ifname $vlanid"
$ip link add link $ifname name $ifname.$vlanid type vlan id $(( 10#$vlanid ))
log_info "$ip link add link $ifname name $ifname.$vlanid type vlan id $(( 10#$vlanid ))"
cmd="$ip link add link $ifname name $ifname.$vlanid type vlan id $(( 10#$vlanid ))"
$cmd
log_info "$cmd"
fi
$sleep 0.5
((i+=1))
@ -1204,7 +1204,6 @@ function create_vlan_interface {
return 1
fi
# setup interface
[ -n "$_mtu" ] && $ip link set $ifname.$vlanid mtu $_mtu
$ip link set $ifname.$vlanid up
log_info "$ip link set $ifname.$vlanid up"
@ -1678,12 +1677,129 @@ function get_first_addr_ipv4 {
#
# create vlan using nmcli
#
# input : ifname=<ifname> slave_ports=<ports> xcatnet=<xcatnetwork> _ipaddr=<ip> _netmask=<netmask> _mtu=<mtu> _bridge=<bridge_name> vlanid=<vlanid>
# input : ifname=<ifname> vlanid=<vlanid> ipaddrs=<ipaddrs>
# return : 0 success
#
###############################################################################
function create_vlan_interface_nmcli {
log_info "create_vlan_interface_nmcli $@"
local ifname=""
local vlanid=""
local ipaddrs=""
local _ipaddrs=""
local _xcatnet=""
local _netmask=""
local _mtu=""
# in case it's on top of bond, we need to migrate ip from its
# member vlan ports.
# parser input arguments
while [ -n "$1" ];
do
key=`echo "$1" | $cut -s -d= -f1`
if [ "$key" = "ifname" ] || \
[ "$key" = "ipaddrs" ] || \
[ "$key" = "vlanid" ]; then
eval "$1"
fi
shift
done
if [ -z "$vlanid" ]; then
log_error "No \"vlanid\" specificd for vlan interface. Abort!"
return 1
fi
_xcatnet=$(query_nicnetworks_net $ifname.$vlanid)
log_info "Pickup xcatnet, \"$_xcatnet\", from NICNETWORKS for interface \"$ifname\"."
_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 [ $? -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
fi
check_and_set_device_managed $ifname
if [ $? -ne 0 ]; then
log_error "The parent interface $ifname is unmanaged, so skip $ifname.$vlanid"
retrun 1
fi
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"
tmp_con_name=""
is_nmcli_connection_exist $con_name
if [ $? -eq 0 ]; then
tmp_con_name=$con_name"-tmp"
$nmcli con modify $con_name connection.id $tmp_con_name
fi
$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
fi
return 1
elif [ ! -z "$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
return 0
}
###############################################################################
#
# is_connection_activate_intime
#
# input : connection_name
# time_out (optional, 40 seconds by default)
# return : 1 active
# 0 failed
#
###############################################################################
function is_connection_activate_intime {
con_name=$1
time_out=40
if [ ! -z "$2" ]; then
time_out=$2
fi
i=0
while [ $i -lt "$time_out" ]; do
con_state=$($nmcli con show $con_name | grep -i state| awk '{print $2}');
if [ ! -z "$con_state" -a "$con_state" = "activated" ]; then
break
fi
sleep 1
i=$((i+1))
done
if [ $i -ge "$time_out" ]; then
return 0
else
return 1
fi
}
###############################################################################