[PATCHv2] hv_set_ifconfig.sh double check before setting ip

From: Eduardo Otubo
Date: Mon Aug 28 2017 - 06:01:35 EST


v2: The script is now a little bit safer so it doesn't conflicts with
network daemon trying to set configurations at the same time.

This patch fixes the behavior of the hv_set_ifconfig script when setting
the interface ip. Sometimes the interface has already been configured by
network daemon, in this case hv_set_ifconfig causes "RTNETLINK: file
exists error"; in order to avoid this error this patch makes sure double
checks the interface before trying anything.

Signed-off-by: Eduardo Otubo <otubo@xxxxxxxxxx>
---
tools/hv/hv_set_ifconfig.sh | 44 ++++++++++++++++++++++++++++++++++++--------
1 file changed, 36 insertions(+), 8 deletions(-)

diff --git a/tools/hv/hv_set_ifconfig.sh b/tools/hv/hv_set_ifconfig.sh
index 735aafd64a3f..3a04b91f61e3 100755
--- a/tools/hv/hv_set_ifconfig.sh
+++ b/tools/hv/hv_set_ifconfig.sh
@@ -46,19 +46,47 @@
# is expected to return the configuration that is set via the SET
# call.
#
+interface=$(echo $1 | awk -F - '{ print $2 }')

+current_ip=$(ip addr show $interface|grep "inet ");
+config_file_ip=$(grep IPADDR $1|cut -d"=" -f2);

+current_ipv6=$(ip addr show $interface|grep "inet6 ");
+config_file_ipv6=$(grep IPV6ADDR $1|cut -d"=" -f2);
+config_file_ipv6_netmask=$(grep IPV6NETMASK $1|cut -d"=" -f2);
+config_file_ipv6=${config_file_ipv6}/${config_file_ipv6_netmask};

-echo "IPV6INIT=yes" >> $1
-echo "NM_CONTROLLED=no" >> $1
-echo "PEERDNS=yes" >> $1
-echo "ONBOOT=yes" >> $1
+network_service_state=$(/bin/systemctl is-active network);

+while [[ ${network_service_state} == "activating" \
+ || ${network_service_state} == "deactivating" ]]; do
+ # Network script is still working. let's wait a bit.
+ # The default timeout for systemd is 90s.
+ sleep 30s;
+ ((i++));
+ network_service_state=$(/bin/systemctl is-active network);

-cp $1 /etc/sysconfig/network-scripts/
+ # If network service doens't come up or down in 90s we log the
+ # error and give up.
+ if [[ $i == 3 ]]; then
+ logger "Couldn't set IP address for fail-over interface"\
+ " because network daemon might be busy. Try to"\
+ " if-down $interface && if-up $interface"\
+ " manually later.";
+ exit 1;
+ fi
+done

+# Only set the IP if it's not configured yet.
+if [[ $(test "${current_ip#*$config_file_ip}") == "$config_file_ip" \
+ || $(test "${current_ipv6#*$config_file_ipv6}") == "$current_ipv6" ]]; then
+ echo "IPV6INIT=yes" >> $1
+ echo "NM_CONTROLLED=no" >> $1
+ echo "PEERDNS=yes" >> $1
+ echo "ONBOOT=yes" >> $1

-interface=$(echo $1 | awk -F - '{ print $2 }')
+ cp $1 /etc/sysconfig/network-scripts/

-/sbin/ifdown $interface 2>/dev/null
-/sbin/ifup $interface 2>/dev/null
+ /sbin/ifdown $interface 2>/dev/null
+ /sbin/ifup $interface 2>/dev/null
+fi
--
2.13.5