Auto-run VMWare VM’s On Boot
We had a request to deploy two dozen VM’s to datacenters running on headless Mac Pros. If the Pros ever got shut down or rebooted, the VM’s needed to come back up automatically without requiring anyone to log in to the Mac Pros. Here’s how we did that:
You would think you could create a .plist that runs VMWare using the vmrun command:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourcompanyhere.vmware</string> <key>ProgramArguments</key> <array> <string>/usr/bin/logger "=================================="</string> <string>/usr/bin/logger "Trying to start vmware VM's now..."</string> <string>/Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "/Users/Shared/v1.vmwarevm/v1.vmx" gui</string> <string>/Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "/Users/Shared/v1.vmwarevm/v2.vmx" gui</string> </array> <key>RunAtLoad</key> <true/> <key>StandardOutPath</key> <string>/var/log/jpmcvmware.log</string> <key>StandardErrorPath</key> <string>/var/log/jpmcvmware.log</string> </dict> </plist>
However, launchd only allows one argument per “ProgramArguments” key. Thus you need to move your commands to a script, and have the .plist reference that script. So do this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourcompanyhere.vmware</string> <key>ProgramArguments</key> <array> <string>/Library/Scripts/vmwarescript.sh/string> </array> <key>RunAtLoad</key> <true/> <key>StandardOutPath</key> <string>/var/log/jpmcvmware.log</string> <key>StandardErrorPath</key> <string>/var/log/jpmcvmware.log</string> </dict> </plist>
Deploy this .plist to the Mac Pros into the /Library/LaunchDaemons directory. Ensure it has 644 permissions.
Then create the script that the .plist references:
#!/bin/sh /usr/bin/logger "Trying to start vmware VM's now..." /Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "/Users/Shared/v1.vmwarevm/v1.vmx" gui sleep 5 /Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "/Users/Shared/v2.vmwarevm/v2.vmx" gui sleep 5
Save the script in the path you set in the .plist above (/Library/Scripts/vmwarescript.sh).
Notes:
- The VM’s are located in a central location, not in any particular user’s home directory.
- The VM’s are set to run with a GUI. If you never want your users to access the host Mac’s Virtual Machine Library, change “gui” to “nogui”. The VM’s will still be accessible over VNC and ssh.
Finally, enable the launchd event with the command:
launchctl load /Library/LaunchDaemons/com.yourcompany.here.plist