June 2008 Archives

VMware Fusion Command Line Access

I'm a command line person -- I always have been and I always will be. I am a very big fan of VMware Fusion and use it daily. I was pleasantly surprised to discover that I could control it by the command line and decided to write a quick script to do just that:
#!/bin/bash

# Change these parameters to match your configuration
VMWARE="/Applications/VMware Fusion.app/Contents/MacOS/vmware"
VMSTORE="/Users/<YOURHOME>/Documents/Virtual Machines"

function vm_list()
{
    local VMDIR="${1}"

    IFS=$'\n'
    [[ -z "${VMDIR}" ]] && exit 1
    VMLIST=$(/bin/ls -1 ${VMDIR})
    for VM in ${VMLIST}; do
      # echo ${VM} | sed -e 's/\.vmwarevm$//'
      echo ${VM}
    done
}

[[ -x ${VMWARE} ]] || \
    { echo "** ${VMWARE} not found or not executable"; exit 1; }

# If user doesn't specify a command line option,
# show what's available, then exit

if [[ ${#} == 0 ]]; then
    echo "-- Available VMs --"
    vm_list "${VMSTORE}"
    exit 0
fi

OLDIFS=${IFS}; IFS=$'\n'
USERVM=$(echo ${1} | /usr/bin/tr '[:upper:]' '[:lower:]')
VMLIST=$(vm_list "${VMSTORE}")

for VM in ${VMLIST}; do
    VMLOWER=$(echo ${VM} | /usr/bin/tr '[:upper:]' '[:lower:]')
    if [[ $(/opt/local/bin/gexpr match "${VMLOWER}" ${USERVM}) > 0 ]]; then
        MATCH=1
        break;
    fi
done

if [[ -n ${MATCH} ]]; then
    echo "-- Starting ${VM} Virtual Machine"
    ${VMWARE} "${VMSTORE}/${VM}"
    RC=${?}
else
    echo "-- No matching VMs"
    RC=1
fi

echo "-- Exiting with code ${RC}"
exit ${RC}
One of the unintended benefits of using such a method of controlling VMs is the ability to easily break out of the VMware Fusion configuration process. If your VM doesn't require a GUI to interact with it, simply press CTRL-C and the configuration process quietly goes away leaving your VM running and freeing some valuable memory.