wok diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/open-vm-tools/stuff/network Fri Sep 18 23:31:50 2015 +0000 1.3 @@ -0,0 +1,186 @@ 1.4 +#!/bin/sh 1.5 +########################################################## 1.6 +# Copyright (C) 2001-2010 VMware, Inc. All rights reserved. 1.7 +# 1.8 +# This program is free software; you can redistribute it and/or modify it 1.9 +# under the terms of the GNU Lesser General Public License as published 1.10 +# by the Free Software Foundation version 2.1 and no later version. 1.11 +# 1.12 +# This program is distributed in the hope that it will be useful, but 1.13 +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 1.14 +# or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public 1.15 +# License for more details. 1.16 +# 1.17 +# You should have received a copy of the GNU Lesser General Public License 1.18 +# along with this program; if not, write to the Free Software Foundation, Inc., 1.19 +# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.20 +# 1.21 +########################################################## 1.22 + 1.23 + 1.24 +# 1.25 +# network (Linux) 1.26 +# 1.27 +# Using a combination of a system networking script, ifconfig, and ifup, 1.28 +# attempt to release and renew DHCP leases upon receipt of suspend and resume 1.29 +# events, respectively. 1.30 +# 1.31 + 1.32 + 1.33 +echo `date` ": Executing '$0'" 1.34 +echo 1.35 + 1.36 +. `dirname "$0"`/../../statechange.subr 1.37 + 1.38 + 1.39 +# 1.40 +# find_networking_script -- 1.41 +# 1.42 +# Searches common Linux distro init/rc paths to find a singular network 1.43 +# services script. 1.44 +# 1.45 +# Result: 1.46 +# Returns a valid networking script path on success or "error" on failure. 1.47 +# 1.48 +# Side effects: 1.49 +# None. 1.50 +# 1.51 + 1.52 +find_networking_script() { 1.53 + echo "network.sh" 1.54 +} 1.55 + 1.56 + 1.57 +# 1.58 +# run_network_script -- 1.59 +# 1.60 +# Finds out how to run the system's script used to control networking, and 1.61 +# runs it with the given argument (which should be one of the usual SysV 1.62 +# init script arguments). 1.63 +# 1.64 +run_network_script() 1.65 +{ 1.66 + script=`find_networking_script` 1.67 + [ "$script" != "error" ] || Panic "Cannot find system networking script." 1.68 + 1.69 + "$script" "$1" 1.70 +} 1.71 + 1.72 + 1.73 +# 1.74 +# save_active_NIC_list -- 1.75 +# 1.76 +# Records a list of every active NIC to /var/run/vmware-active-nics. 1.77 +# 1.78 +# XXX What's the story on aliases? Should they still be included, or will 1.79 +# they be recreated automatically upon resume? 1.80 +# 1.81 +# Results: 1.82 +# $activeList has, one per line, a list of all active NICs. 1.83 +# 1.84 +# Side effects: 1.85 +# None. 1.86 +# 1.87 + 1.88 +save_active_NIC_list() { 1.89 + >$activeList 1.90 + 1.91 + for nic in `ifconfig | awk '/^eth/ { print $1 }'`; do 1.92 + ifconfig $nic | egrep -q '\bUP\b' && echo $nic >> $activeList 1.93 + exitCode=`expr $exitCode \| $?` 1.94 + done 1.95 +} 1.96 + 1.97 + 1.98 +# 1.99 +# rescue_NIC -- 1.100 +# 1.101 +# For each NIC recorded in $activeList that is not currently "up", run 1.102 +# "INTERFACE=$nic network.sh start". 1.103 +# 1.104 +# Results: 1.105 +# All downed NICs should be active. 1.106 +# 1.107 + 1.108 +rescue_NIC() { 1.109 + if [ -f "$activeList" ]; then 1.110 + while read nic; do 1.111 + if ifconfig $nic | egrep -q '\bUP\b'; then 1.112 + echo `date` "[rescue_nic] $nic is already active." 1.113 + else 1.114 + echo `date` "[rescue_nic] activating $nic ..." 1.115 + 1.116 + INTERFACE=$nic /etc/init.d/network.sh start 1.117 + exitCode=`expr $exitCode \| $?` 1.118 + fi 1.119 + done < $activeList 1.120 + 1.121 + rm -f $activeList 1.122 + fi 1.123 +} 1.124 + 1.125 + 1.126 +# 1.127 +# main -- 1.128 +# 1.129 +# Main entry point. Perform some sanity checking, then map state change 1.130 +# events to relevant networking operations. 1.131 +# 1.132 +# Results: 1.133 +# See comment at top of file. 1.134 +# 1.135 + 1.136 +main() { 1.137 + exitCode=0 1.138 + activeList=/var/run/vmware-active-nics 1.139 + 1.140 + # XXX Are these really necessary? If so, we should have seen customer 1.141 + # complaints by now. 1.142 + which ifconfig >/dev/null 2>&1 || Panic "ifconfig not in search path." 1.143 + 1.144 + case "$1" in 1.145 + poweron-vm) 1.146 + rm -f $activeList 1.147 + ;; 1.148 + suspend-vm) 1.149 + exitCode=$? 1.150 + if [ $exitCode != 0 ]; then 1.151 + save_active_NIC_list 1.152 + run_network_script stop 1.153 + exitCode=$? 1.154 + fi 1.155 + ;; 1.156 + resume-vm) 1.157 + WakeNetworkManager 1.158 + exitCode=$? 1.159 + if [ $exitCode != 0 ]; then 1.160 + # According to hfu, "/etc/init.d/networking restart" on Debian 5.0 1.161 + # may bring down ethernet interfaces tagged as "allow-hotplug" without 1.162 + # bringing them back up. 1.163 + # 1.164 + # This is especially a problem when reverting to a live, running 1.165 + # VM snapshot where an active NIC list hadn't yet been generated, 1.166 + # resulting in sudden loss of an otherwise operational NIC. 1.167 + # 1.168 + # So, if the active list doesn't exist, assume we're coming back to 1.169 + # a live snapshot and capture the current active list now for 1.170 + # rescue later. 1.171 + if [ ! -s $activeList ]; then 1.172 + save_active_NIC_list 1.173 + fi 1.174 + 1.175 + # We shall use start not restart here. Otherwise we may not be able 1.176 + # to bring back active list on distros like sles11sp2 1.177 + # -- PR 816791 1.178 + run_network_script start 1.179 + rescue_NIC 1.180 + exitCode=$? 1.181 + fi 1.182 + ;; 1.183 + *) ;; 1.184 + esac 1.185 + 1.186 + return $exitCode 1.187 +} 1.188 + 1.189 +main "$@"