3 ################################################################################
4 # Copyright (c) 2016 Wind River Systems, Inc.
6 # SPDX-License-Identifier: Apache-2.0
8 ################################################################################
11 # Purpose of this script is to copy the puppet-built
12 # network config file from the puppet dir to the /etc/network/interfaces
13 # Only copied when difference detected
15 # Please note: function is_eq_ifcfg() is used to determine if
16 # cfg files are different
22 if [ ! -f /var/run/interfaces.puppet ] ; then
23 # No puppet file? Nothing to do!
33 log_it "Bringing $iface up"
39 log_it "Bringing $iface down"
45 log_it "Removing $theFile"
52 log_it "copying network cfg $srcFile to $dstFile"
59 log_it "Moving network cfg $srcFile to $dstFile"
63 # Return items in list1 that are not in list2
70 for item in ${list1[@]}; do
71 if [[ ! $l2 =~ " $item " ]] ; then
79 function normalized_cfg_attr_value {
83 attr_value=$(cat $cfg | grep $attr_name= | awk -F "=" {'print $2'})
87 # Special case BONDING_OPTS attribute.
89 # The BONDING_OPTS attribute contains '=' characters, so is not correctly
90 # parsed by splitting on '=' as done above. This results in changes to
91 # BONDING_OPTS not causing the interface to be restarted, so the old
92 # BONDING_OPTS still be used. Because this is only checking for changes,
93 # rather than actually using the returned value, we can return the whole
96 if [[ "${attr_name}" == "BONDING_OPTS" ]]; then
97 echo "$(cat $cfg | grep $attr_name=)"
101 if [[ "${attr_name}" != "BOOTPROTO" ]]; then
106 # Special case BOOTPROTO attribute.
108 # The BOOTPROTO attribute is not populated consistently by various aspects
109 # of the system. Different values are used to indicate a manually
110 # configured interfaces (i.e., one that does not expect to have an IP
111 # address) and so to avoid reconfiguring an interface that has different
112 # values with the same meaning we normalize them here before making any
115 # From a user perspective the values "manual", "none", and "" all have the
116 # same meaning - an interface without an IP address while "dhcp" and
117 # "static" are distinct values with a separate meaning. In practice
118 # however, the only value that matters from a ifup/ifdown script point of
119 # view is "dhcp". All other values are ignored.
121 # In our system we set BOOTPROTO to "static" to indicate that IP address
122 # attributes exist and to "manual"/"none" to indicate that no IP address
123 # attributes exist. These are not needed by ifup/ifdown as it looks for
124 # the "IPADDR" attribute whenever BOOTPROTO is set to anything other than
127 if [[ "${attr_value}" == "none" ]]; then
130 if [[ "${attr_value}" == "manual" ]]; then
133 if [[ "${attr_value}" == "" ]]; then
141 # returns $(true) if cfg file ( $1 ) has property propName ( $2 ) with a value of propValue ( $3 )
143 function cfg_has_property_with_value {
148 if [[ "$(normalized_cfg_attr_value $cfg $propname)" == "${propvalue}" ]]; then
156 # returns $(true) if cfg file is configured as a slave
159 cfg_has_property_with_value $1 "SLAVE" "yes"
164 # returns $(true) if cfg file is configured for DHCP
167 cfg_has_property_with_value $1 "BOOTPROTO" "dhcp"
171 # returns $(true) if cfg file is configured as a VLAN interface
174 cfg_has_property_with_value $1 "VLAN" "yes"
179 # returns $(true) if cfg file is configured as an ethernet interface. For the
180 # purposes of this script "ethernet" is considered as any interface that is not
181 # a vlan or a slave. This includes both regular ethernet interfaces and bonded
184 function is_ethernet {
185 if ! is_vlan $1; then
186 if ! is_slave $1; then
194 # returns $(true) if cfg file represents an interface of the specified type.
196 function iftype_filter {
199 return $(is_$iftype $2)
203 # returns $(true) if ifcfg files have the same number of VFs
206 function is_eq_sriov_numvfs {
210 sriov_numvfs_1=$(grep -o 'echo *[1-9].*sriov_numvfs' $cfg_1 | awk {'print $2'})
212 sriov_numvfs_2=$(grep -o 'echo *[1-9].*sriov_numvfs' $cfg_2 | awk {'print $2'})
214 sriov_numvfs_1=${sriov_numvfs_1:-0}
215 sriov_numvfs_2=${sriov_numvfs_2:-0}
217 if [[ "${sriov_numvfs_1}" != "${sriov_numvfs_2}" ]]; then
218 log_it "$cfg_1 and $cfg_2 differ on attribute sriov_numvfs [${sriov_numvfs_1}:${sriov_numvfs_2}]"
226 # returns $(true) if ifcfg files are equal
228 # Warning: Only compares against cfg file attributes:
229 # BOOTPROTO DEVICE IPADDR NETMASK GATEWAY MTU BONDING_OPTS SRIOV_NUMVFS
231 function is_eq_ifcfg {
235 for attr in BOOTPROTO DEVICE IPADDR NETMASK GATEWAY MTU BONDING_OPTS; do
237 attr_value1=$(normalized_cfg_attr_value $cfg_1 $attr)
239 attr_value2=$(normalized_cfg_attr_value $cfg_2 $attr)
240 if [[ "${attr_value1}" != "${attr_value2}" ]]; then
241 log_it "$cfg_1 and $cfg_2 differ on attribute $attr"
246 is_eq_sriov_numvfs $1 $2
250 # Synchronize with sysinv-agent audit (ifup/down to query link speed).
251 function sysinv_agent_lock {
254 local lock_file="/var/run/apply_network_config.lock"
255 # Lock file should be the same as defined in sysinv agent code
260 exec {LOCK_FD}>$lock_file
261 while [[ $n -le $max ]]; do
263 flock -w $lock_timeout $LOCK_FD && break
264 log_it "Failed to get lock($LOCK_FD) after $lock_timeout seconds ($n/$max), will retry"
268 if [[ $n -gt $max ]]; then
269 log_it "Failed to acquire lock($LOCK_FD) even after $max retries"
274 [[ $LOCK_FD -gt 0 ]] && flock -u $LOCK_FD
280 # synchronize with sysinv-agent audit
281 sysinv_agent_lock $ACQUIRE_LOCK
283 # check if this is a duplicated configuration
284 if ! diff -I '^#' "/var/run/interfaces.puppet" "/etc/network/interfaces" > /dev/null; then
285 # now copy the puppet changed interfaces to /etc/network/interfaces
286 do_mv /var/run/interfaces.puppet /etc/network/interfaces
288 # now restart networking service
289 /etc/init.d/networking restart
293 # need to remove this file also
294 do_rm /var/run/interfaces.puppet
298 # workaround the loopback label addresses cannot be configured as scope of host
299 ip addr show lo | egrep "inet.*global.*lo:" > /tmp/loop$$
301 while read addr_info; do
303 log_it "replace $addr_info with scope host"
304 addr=`echo $addr_info | cut -d' ' -f 2`
305 ifname=`echo $addr_info | cut -d' ' -f 5`
306 ip addr del $addr dev lo label $ifname
307 ip addr add $addr dev lo scope host label $ifname
311 # unlock: synchronize with sysinv-agent audit
312 sysinv_agent_lock $RELEASE_LOCK