Skip to content

Instantly share code, notes, and snippets.

@mritd
Last active January 27, 2021 06:13
Show Gist options
  • Save mritd/bca81deb5e3a6ddc5c417229a69f3380 to your computer and use it in GitHub Desktop.
Save mritd/bca81deb5e3a6ddc5c417229a69f3380 to your computer and use it in GitHub Desktop.
VMware Fusion helper tool
#!/usr/bin/env bash
set -e
VMP_DIR=${VMP_DIR:-"/Volumes/Data/vms"}
main(){
if [ "${1}" == "" ]; then
print_help
exit 0
fi
case "${1}" in
"start")
if [ "${2}" == "" ]; then
print_help
exit 0
fi
for v in ${@:2}; do
info "start vm => [${v}]..."
vmrun start ${VMP_DIR}/${v}.vmwarevm/${v}.vmx nogui
done
;;
"stop")
if [ "${2}" == "" ]; then
print_help
exit 0
fi
for v in ${@:2}; do
info "stop vm => [${v}]..."
vmrun stop ${VMP_DIR}/${v}.vmwarevm/${v}.vmx
done
;;
"sp"|"snapshot")
if [ "${2}" == "" ] || [ "${3}" == "" ]; then
print_help
exit 0
fi
for v in ${@:2:(($#-2))}; do
info "create snapshot ${v} => ${!#}"
vmrun snapshot ${VMP_DIR}/${v}.vmwarevm/${v}.vmx "${!#}"
done
;;
"re"|"revert")
if [ "${2}" == "" ] || [ "${3}" == "" ]; then
print_help
exit 0
fi
for v in ${@:2:(($#-2))}; do
info "revert snapshot ${v} => ${!#}"
vmrun revertToSnapshot ${VMP_DIR}/${v}.vmwarevm/${v}.vmx "${!#}"
done
;;
"ls"|"list"|"status")
IFS=''
all_vms=(${VMP_DIR}/*.vmwarevm/*.vmx)
running_vms=(`vmrun list | grep ${VMP_DIR} || true`)
printf "NAME STATUS \n"
printf -- "------------------------------\n"
for v in ${all_vms[@]}; do
if [ ${#running_vms[@]} -ge 0 ] && [[ "${running_vms[@]}" =~ "${v}" ]]; then
printf "%-20s Running\n" $(basename ${v} | sed 's@.vmx@@g')
else
printf "%-20s Stoped\n" $(basename ${v} | sed 's@.vmx@@g')
fi
done
;;
*)
print_help
exit 0
;;
esac
}
print_help(){
cat <<EOF
NAME:
$(basename ${0}) - VMware Fusion helper tool
USAGE:
$(basename ${0}) command vmname [args]
AUTHOR:
mritd <mritd@linux.com>
COMMANDS:
start Start VM Instance
stop Stop VM Instance
snapshot Create VM Snapshot(alias "sp")
status Print VM Status(alias "ls" "list")
COPYRIGHT:
Copyright (c) $(date "+%Y") mritd, All rights reserved.
EOF
}
function info(){
echo -e "\033[1;32mINFO: $@\033[0m"
}
function warn(){
echo -e "\033[1;33mWARN: $@\033[0m"
}
function err(){
echo -e "\033[1;31mERROR: $@\033[0m"
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment