Using vmrun on VMWare Fusion

I had the need to create a bunch of very small Linux VMs on my Mac. Here are some example commands using 'vmrun' to help people out. If you didn't know, the vmrun command allows users to do some administration of their VMWare guests from the command line. My understanding is that it is the VIX API as an executable. It does some pretty cool things - I only wish it allowed you to clone/create VMs - apparently you can do this with one of the available APIs, but I don't think Fusion is supported with that (I'm not sure but that's what I gathered - if you know differently please let me know.).

List running systems

/Library/Application\ Support/VMware\ Fusion/vmrun list

Start, Stop, Pause a System

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom start ~/Documents/Virtual\ Machines.localized/nagiospool/payloadprocessor-test.vmwarevm/payloadprocessor-test.vmx

replace 'start' with 'pause' or 'stop' in the command above

Reading and writing variables

from the looks of it, runtimeConfig basically means, log in and get me the real value. Here we write a variable and read it back out.

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom writeVariable ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx runtimeConfig SUPERTOM cool

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom readVariable ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx runtimeConfig SUPERTOM

Running a command or script inside the guest

Note, you won't get any response back. But if you follow the example below, you'll see that the file is created on the box

Write a file on the guest OS

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom runScriptInGuest ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx "/bin/bash" "date > ~/foo.txt"

Write some complicated text

Nothing too complicated here, but we write some text that should be interpreted as a command to be run (backticks), so we need to escape the backticks 3 times.

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom runScriptInGuest ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx "/bin/bash" "echo \"export MYOUTPUT=\\\`cat ~/foo.txt\\\`\" >> ~/myvars"

Running SUDO commands

I ran some sudo commands - these are VMs on my personal laptop which only need to access each other, so I added this to the /etc/sudoers file of each guest. You can certainly get more restrictive:

supertom ALL=(ALL) NOPASSWD: ALL

Changing network info

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom runScriptInGuest ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx "/bin/bash" "sudo bash -c 'sed \"s/130/145/\" /etc/network/interfaces > /etc/network/interfaces.sav2 && sudo mv /etc/network/interfaces.sav2 /etc/network/interfaces'"

restart networking

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu supertom -gp supertom runScriptInGuest ~/Documents/Virtual\ Machines.localized/nagiospool/mercury-fb.vmwarevm/mercury-fb.vmx "/bin/bash" "sudo /etc/init.d/networking restart"

References

Back to Code