Virtualization has been a key technology for me as a contractor. Because I have multiple clients, keeping their projects sequestered from each other is a snap: one or more VMs (virtual machines) per project. Development, testing, and in some cases deployment is all on separate VMs.
I’ve been using VMware Workstation since it first came out 1999. The ability for me to seamlessly move VMs between Windows, Linux, and OS X has been a key reason I’ve stuck with them for so long.
Automating tasks via scripting one of many reasons I insist that my workstation run Un*x of some flavour. I’ve been using OS X for quite a while quite happily as a software developer, however VMware Fusion (for OS X) doesn’t present the full set of features available on VMware Workstation (for Linux and Windows). Fortunately hidden under the hood are the same tools.
The Executable vmrun
VMware provides a tool called vmrun that allows common operations to be performed on VMs — starting, suspending, taking snapshots, etc. On OS X it’s tucked away in the VMware Fusion bundle under Contents/Library.
In my shell startup I add the directory to PATH:
# VMware Fusion
if [ -d "/Applications/VMware Fusion.app/Contents/Library" ]; then
export PATH=$PATH:"/Applications/VMware Fusion.app/Contents/Library"
Find the Virtual Machine’s .vmx File
To use vmrun you need to have the path to the .vmx file that resides inside of the VM bundle on OS X. For example, I have a VM with an install of RedHat Enterprise Linux 7:
This is actually not a single file, but a directory called RHEL7.vmwarevm. The contents can be seen in the Finder by right clicking:
This will open up the directory and show the various files that make up the virtual machine.
As can be seen, the .vmx file is prominently displayed.
Put the Pieces Together
To start the VM from the command line one uses the “start” parameter. For example, if the above virtual machine were in one’s home directory one can type:
$ vmrun start ~/RHEL7.vmwarevm/RHEL7.vmx
If one runs vmrun without parameters it gives a farily long summary of commands that it accepts. One can:
- control the power state of the VM,
- control snapshots,
- perform various operations inside a running VM, and
- other operations such as installing tools and cloning.
A short list of common operations:
|List running VMs||list|
|Start a VM||start||/path/to/vmx/file|
|Suspend a VM||suspend||/path/to/vmx/file|
|Take a snapshot||snapshot||/path/to/vmx/file||snapshot name|
VMware has a PDF on vmrun here.