Last active
May 31, 2017 03:22
-
-
Save bunchc/ab2658bb2a880a8841e3b5ccb54348ef to your computer and use it in GitHub Desktop.
Script to reset VMware Fusion networking for 7.x and 8.x releases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# @script damnit_fusion.sh | |
# @description Fusion Networking hates me | |
# | |
# $ ~/damnit_fusion.sh | |
# | |
# Be default will perform a gentle restart of VMware Fusion networking. | |
# When that is not enough set FROM_ORBIT to true and try again. | |
# | |
# Borrowed in part from https://gist.github.com/fnichol/8982147 | |
# and https://vbrownbag.com/2014/05/kicking-vmware-fusion-networking-in-the-teeth/ | |
# also https://gist.github.com/bdwyertech/43161ef53ac0c9b5e998d7842a881b32 | |
# also https://github.com/henri/vmware-fusion-scripts/blob/master/suspend_and_resume/suspend_running_vms.bash | |
## | |
export FROM_ORBIT=${FROM_ORBIT:-false} | |
banner() { printf -- "-----> $*\n\n"; } | |
# | |
# Kills all vmware & vmnet processees & deletes networking files | |
# | |
from_orbit() { | |
stop_networking | |
banner "Killing vmware and vmnet processes" | |
sudo pkill vmware | |
sudo pkill vmnet | |
banner "Removing cruft" | |
sudo rm -rf /Library/Preferences/VMware\ Fusion/networkin* | |
sudo rm -rf /Library/Preferences/VMware\ Fusion/lastlocation* | |
sudo rm -rf /Library/Preferences/VMware\ Fusion/location* | |
sudo rm -rf /Library/Preferences/VMware\ Fusion/vmnet*/ | |
} | |
# | |
# Gets a list of running vms | |
# @return runningVMs - List of running VMs per vmrun | |
# | |
list_vms() { | |
runningVMs=$(vmrun list | awk 'NR>=2') | |
} | |
# | |
# Takes a list of running VMs supplied by list_vms and either gracefully | |
# stops them, or shoots them in the face | |
# @param runningVMs - A list of running vms from list_vms | |
# | |
stop_vms() { | |
local runningVMs=$1; shift | |
if [ -z "$runningVMs" ]; then { | |
echo "No VMs to stop." | |
return | |
} else { | |
arrRunningVMs=($runningVMs) | |
if [ "$FROM_ORBIT" = true ]; then { | |
banner "The current blast radius is: ${#arrRunningVMs[@]}" | |
for vm in "{$arrRunningVMs[@]}"; do | |
echo "Currently Nuking: $vm" | |
vmrun -T fusion stop "${vm}" hard | |
done | |
}; else { | |
banner "Suspending VMs" | |
for vm in "{$arrRunningVMs[@]}"; do | |
echo "Currently Suspending: $vm" | |
vmrun -T fusion suspend "${vm}" hard | |
done | |
}; fi | |
}; fi | |
} | |
# | |
# Takes the list of VMs that were running, and attempts to start them | |
# @param runningVMs - The list of running VMs from list_vms | |
# | |
resume_vms() { | |
local runningVMs=$1; shift | |
if [ -z "$runningVMs" ]; then { | |
echo "No VMs to start." | |
return | |
} else { | |
arrRunningVMs=($runningVMs) | |
if [ "$FROM_ORBIT" = true ]; then { | |
banner "FROM_ORBIT is set, not restarting VMs." | |
printf "The following VMs were stopped: \n" | |
for vm in "{$arrRunningVMs[@]}"; do | |
echo "$vm" | |
done | |
}; else { | |
banner "Attempting to start VMs" | |
for vm in "{$arrRunningVMs[@]}"; do | |
echo "Currently Starting: $vm" | |
vmrun -T fusion start "${vm}" nogui | |
done | |
}; fi | |
}; fi | |
} | |
# | |
# start_networking | |
# | |
start_networking() { | |
banner "Starting networking" | |
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --start | |
} | |
# | |
# stop_networking | |
# | |
stop_networking() { | |
banner "Stopping networking" | |
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --stop | |
} | |
# | |
# configure_networking | |
# | |
configure_networking() { | |
banner "Configuring networking" | |
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --configure | |
} | |
# | |
# Runs the network restart process | |
# | |
restart_networking() { | |
configure_networking | |
stop_networking | |
banner "Sleeping a moment" | |
sleep 30 | |
start_networking | |
} | |
# | |
# Do the things | |
# | |
main() { | |
# https://en.wikipedia.org/wiki/Kinetic_bombardment | |
banner "Positioning rod from god" | |
banner "Stopping running VMs" | |
runningVMs=$(list_vms) | |
stop_vms "$runningVMs" | |
if [ "$FROM_ORBIT" = true ]; then { | |
from_orbit | |
}; fi | |
banner "Restarting networking" | |
restart_networking | |
banner "Restarting VMs" | |
resume_vms "$runningVMs" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment