From 31ae6e4be97a09be41b8bee8f4e6cf741abfa849 Mon Sep 17 00:00:00 2001 From: bybai Date: Fri, 15 Mar 2019 03:11:01 -0400 Subject: [PATCH] add common functions for confignetwork using nmcli --- xCAT/postscripts/confignetwork | 11 +++++ xCAT/postscripts/nicutils.sh | 76 +++++++++++++++++++++++++++++++++- xCAT/postscripts/xcatlib.sh | 7 ++-- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/xCAT/postscripts/confignetwork b/xCAT/postscripts/confignetwork index 602919b5c..48911ecea 100755 --- a/xCAT/postscripts/confignetwork +++ b/xCAT/postscripts/confignetwork @@ -614,6 +614,17 @@ errorcode=0 #nictypes should support capital letters, for example, Ethernet and ethernet utolcmd="sed -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" +#back up all network interface configure files +nwdirbak=$nwdir".xcatbak" +ls $nwdirbak > /dev/null 2>/dev/null +if [ $? -ne 0 ]; then + log_info "back up $nwdir to $newdirbak" + cp -rf $nwdir $nwdirbak > /dev/null 2>/dev/null + if [ $? -ne 0 ]; then + log_warn "back up $nwdir to $newdirbak failed." + fi +fi + #get for installnic installnic='' installnic=`get_installnic` diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh index bc6dc1b47..fcba10372 100755 --- a/xCAT/postscripts/nicutils.sh +++ b/xCAT/postscripts/nicutils.sh @@ -746,13 +746,32 @@ function check_brctl() { ############################################################################### # # check and set device managed -# input: ifname +# input: network interface # output: 0 managed # 1 umanaged # ############################################################################### function check_and_set_device_managed() { - ifname=$1 + tname=$1 + rc=1 + $nmcli device show $tname >/dev/null 2>/dev/null + if [ $? -ne 0 ]; then + log_error "Device $tname not found" + else + $nmcli -g GENERAL.STATE device show $tname|grep unmanaged >/dev/null 2>/dev/null + if [ $? -eq 0 ]; then + log_info "$nmcli device set $tname managed yes" + $nmcli device set $tname managed yes + if [ $? -eq 0 ]; then + rc=0 + else + log_error "nmcli fail to set device $tname managed" + fi + else + rc=0 + fi + fi + return $rc } ############################################################################### @@ -1602,6 +1621,59 @@ function check_NetworkManager_or_network_service() { return 2 } +############################################################################### +# +# get nmcli connection name +# input: network connetion +# return: 0 connection exists +# 1 connection does not exist +# +############################################################################## +function get_nmcli_connection_name { + + str_con_name=$1 + # the device str_if_name active connectin + nmcli -g NAME connection show |grep -w $str_con_name >/dev/null 2>/dev/null + if [ $? -eq 0 ]; then + return 0 + else + return 1 + fi + +} + +############################################################################### +# +# get first ipv4 addr from nicips +# input: nics.nicips for one nic +# return 0, output: ipv4 addr +# return 1, output error: "IP: $IP not available" or "$IP: IP format error" +# +############################################################################### +function get_first_valid_ipv4_addr { + + str_ips=$1 + res=1 + if [ -n "$str_ips" ]; then + IP=$(echo "$str_ips"|awk -F"|" '{print $1}') + if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then + FIELD1=$(echo $IP|cut -d. -f1) + FIELD2=$(echo $IP|cut -d. -f2) + FIELD3=$(echo $IP|cut -d. -f3) + FIELD4=$(echo $IP|cut -d. -f4) + if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then + echo "$IP" + res=0 + else + log_error "IP: $IP not available" + fi + else + log_error "$IP: IP format error" + fi + fi + return $res +} + ############################################################################### # # create vlan using nmcli diff --git a/xCAT/postscripts/xcatlib.sh b/xCAT/postscripts/xcatlib.sh index e1c688402..f97233c85 100755 --- a/xCAT/postscripts/xcatlib.sh +++ b/xCAT/postscripts/xcatlib.sh @@ -463,9 +463,9 @@ function stopservice { elif [ -n "$svcd" ];then cmd="service $svcd stop" fi - - echo $cmd - + if [ -n "$cmd" ]; then + echo $cmd + fi if [ -z "$cmd" ];then return 127 fi @@ -477,7 +477,6 @@ function stopservice { retmsg=`$cmd 2>&1` retval=$? [ "$retval" = "0" ] && (echo "$retmsg" | grep -i "Running in chroot,\s*ignoring request.*" >/dev/null 2>&1) && retval=1 - return $retval }