wok view open-vm-tools/stuff/network @ rev 18391

Add: WIP open-vm-tools (10.0.0)
author Nathan Neulinger <nneul@neulinger.org>
date Fri Sep 18 23:31:50 2015 +0000 (2015-09-18)
parents
children 16220d9eae10
line source
1 #!/bin/sh
2 ##########################################################
3 # Copyright (C) 2001-2010 VMware, Inc. All rights reserved.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation version 2.1 and no later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 #
18 ##########################################################
21 #
22 # network (Linux)
23 #
24 # Using a combination of a system networking script, ifconfig, and ifup,
25 # attempt to release and renew DHCP leases upon receipt of suspend and resume
26 # events, respectively.
27 #
30 echo `date` ": Executing '$0'"
31 echo
33 . `dirname "$0"`/../../statechange.subr
36 #
37 # find_networking_script --
38 #
39 # Searches common Linux distro init/rc paths to find a singular network
40 # services script.
41 #
42 # Result:
43 # Returns a valid networking script path on success or "error" on failure.
44 #
45 # Side effects:
46 # None.
47 #
49 find_networking_script() {
50 echo "network.sh"
51 }
54 #
55 # run_network_script --
56 #
57 # Finds out how to run the system's script used to control networking, and
58 # runs it with the given argument (which should be one of the usual SysV
59 # init script arguments).
60 #
61 run_network_script()
62 {
63 script=`find_networking_script`
64 [ "$script" != "error" ] || Panic "Cannot find system networking script."
66 "$script" "$1"
67 }
70 #
71 # save_active_NIC_list --
72 #
73 # Records a list of every active NIC to /var/run/vmware-active-nics.
74 #
75 # XXX What's the story on aliases? Should they still be included, or will
76 # they be recreated automatically upon resume?
77 #
78 # Results:
79 # $activeList has, one per line, a list of all active NICs.
80 #
81 # Side effects:
82 # None.
83 #
85 save_active_NIC_list() {
86 >$activeList
88 for nic in `ifconfig | awk '/^eth/ { print $1 }'`; do
89 ifconfig $nic | egrep -q '\bUP\b' && echo $nic >> $activeList
90 exitCode=`expr $exitCode \| $?`
91 done
92 }
95 #
96 # rescue_NIC --
97 #
98 # For each NIC recorded in $activeList that is not currently "up", run
99 # "INTERFACE=$nic network.sh start".
100 #
101 # Results:
102 # All downed NICs should be active.
103 #
105 rescue_NIC() {
106 if [ -f "$activeList" ]; then
107 while read nic; do
108 if ifconfig $nic | egrep -q '\bUP\b'; then
109 echo `date` "[rescue_nic] $nic is already active."
110 else
111 echo `date` "[rescue_nic] activating $nic ..."
113 INTERFACE=$nic /etc/init.d/network.sh start
114 exitCode=`expr $exitCode \| $?`
115 fi
116 done < $activeList
118 rm -f $activeList
119 fi
120 }
123 #
124 # main --
125 #
126 # Main entry point. Perform some sanity checking, then map state change
127 # events to relevant networking operations.
128 #
129 # Results:
130 # See comment at top of file.
131 #
133 main() {
134 exitCode=0
135 activeList=/var/run/vmware-active-nics
137 # XXX Are these really necessary? If so, we should have seen customer
138 # complaints by now.
139 which ifconfig >/dev/null 2>&1 || Panic "ifconfig not in search path."
141 case "$1" in
142 poweron-vm)
143 rm -f $activeList
144 ;;
145 suspend-vm)
146 exitCode=$?
147 if [ $exitCode != 0 ]; then
148 save_active_NIC_list
149 run_network_script stop
150 exitCode=$?
151 fi
152 ;;
153 resume-vm)
154 WakeNetworkManager
155 exitCode=$?
156 if [ $exitCode != 0 ]; then
157 # According to hfu, "/etc/init.d/networking restart" on Debian 5.0
158 # may bring down ethernet interfaces tagged as "allow-hotplug" without
159 # bringing them back up.
160 #
161 # This is especially a problem when reverting to a live, running
162 # VM snapshot where an active NIC list hadn't yet been generated,
163 # resulting in sudden loss of an otherwise operational NIC.
164 #
165 # So, if the active list doesn't exist, assume we're coming back to
166 # a live snapshot and capture the current active list now for
167 # rescue later.
168 if [ ! -s $activeList ]; then
169 save_active_NIC_list
170 fi
172 # We shall use start not restart here. Otherwise we may not be able
173 # to bring back active list on distros like sles11sp2
174 # -- PR 816791
175 run_network_script start
176 rescue_NIC
177 exitCode=$?
178 fi
179 ;;
180 *) ;;
181 esac
183 return $exitCode
184 }
186 main "$@"