Created
January 18, 2023 06:12
-
-
Save hedzr/956cb892069b0353f915b395a9504ebf to your computer and use it in GitHub Desktop.
run a vmware VM from command-line - chs post at https://hedzr.com/devops/linux/run-vmware-vm-from-command-line/
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
vmware_run() { | |
local VMDIR="${VMWARE_DIR:-$HOME/Downloads/VMs/vmware}" | |
local VMTYPE="${VMWARE_TYPE:-fusion}" # or "ws" | |
# local VMDIR="$HOME/Documents/Virtual Machines.localized" | |
which vmrun >/dev/null 2>&1 || ln -s "/Applications/VMware Fusion.app/Contents/Library/vmrun" /usr/local/bin/vmrun | |
vmware_run_usage() { | |
cat <<-EOF | |
Usage: | |
$(basename $0) <VM-name> <start|up|stop|halt|suspend|reset|pause|unpause> | |
$(basename $0) <VM-name> <start_with_gui|start-with-gui> | |
Examples: | |
$(basename $0) u20.local start | |
# start vm without gui (headless mode) | |
EOF | |
} | |
vmware_run_start() { | |
local cmd=$1 | |
local vmname=$2 | |
shift | |
shift | |
vmrun list | grep -q "$vmname" && { | |
echo "VM '$vmname' already running" | |
} || { | |
[ "$cmd" != "start" ] && cmd=start | |
echo "vmrun -T $VMTYPE $cmd $VMDIR/$vmname.vmwarevm/$vmname.vmx" "$@" | |
vmrun -T $VMTYPE $cmd $VMDIR/$vmname.vmwarevm/$vmname.vmx "$@" | |
} | |
} | |
vmware_run_stop() { | |
local cmd=$1 | |
local vmname=$2 | |
vmrun list | grep -q "$vmname" || { | |
echo "VM '$vmname' not running" | |
} && { | |
[ "$cmd" != "stop" ] && cmd=stop | |
vmrun -T $VMTYPE $cmd $VMDIR/$vmname.vmwarevm/$vmname.vmx | |
} | |
} | |
case ${2:-start} in | |
start | start-without-gui | up) | |
local cmd=${2:-start} && local vmn=$1 | |
(($#)) && shift | |
(($#)) && shift | |
dbg "vmn=$wmn" && | |
vmware_run_start $cmd $vmn nogui "$@" | |
;; | |
start_with_gui | start-with-gui | unpause) | |
local cmd=$2 && local vmn=$1 | |
(($#)) && shift | |
(($#)) && shift | |
vmware_run_start start $vmn "$@" | |
;; | |
stop | suspend | reset | pause | halt) | |
local cmd=$2 && local vmn=$1 | |
(($#)) && shift | |
(($#)) && shift | |
vmware_run_stop $cmd $vmn "$@" | |
;; | |
usage | help | -h | -H | --help) | |
vmware_run_usage | |
;; | |
*) | |
case $1 in | |
usage | help | -h | -H | --help) | |
vmware_run_usage | |
;; | |
*) | |
vmrun "$@" | |
;; | |
esac | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment