Running a VM Headless (no GUI) and Controlling Shutdown/Suspend with Mac OS X Reboot/Shutdown
The Subject says it all and here's the HOWTO:
The following has been tested and used under Mac OS X 10.5 and higher and VMware Fusion 3.x.
Running a Virtual Machine Headless (no GUI) is possible with VMware Fusion however its never been officially supported via the VMware Fusion GUI and VMware is aware that some users would like it to be supported via the GUI however until then here is how I do it.
To run a Virtual Machine Headless. ...
1. First run the Virtual Machine normally to make sure everything is configured as needed and in a manner that can be controlled via ssh, VNC or RDP, Web Browser, etc. Turn off features that aren't really needed like Accelerate 3D Graphics when applicable as it lowers the memory overhead. Do not have headless Virtual Machines automatically connect to or be connected to unneeded devices. Use an ISO Image vs connecting to the Physical CD/DVD, etc. Also note that the following parameter can be added to the Virtual Machine's .vmx configuration file and this will attempt to automatically answer with the appropriate answer various message boxes that can pop up. Obviously when running headless not having a message box answered will hang the Virtual Machine as it is not displayed when running in this manner. One should add this last after having already run the Virtual Machine normally and dispensing with any message boxes and where applicable check the 'Do not show again...' check boxes.
msg.autoAnswer = "TRUE"
2. Shutdown the Virtual Machine a then remove its entry from the Virtual Machine Library and close VMware Fusion.
3. Use vmrun to start the Virtual Machine with the nogui parameter. Have a look at: Using vmrun to Control Virtual Machines
If one then runs VMware Fusion the running Virtual Machine will remain headless since it was removed from the Virtual Machine Library. Note that you can drag and drop the target Virtual Machine Package onto the Virtual Machine Library while it's running headless in order to attach it to the GUI however understand that this can cause the Black Screen of Death in that the Virtual Machine Display will be Black and possibly not interactive through the GUI and will probably stay that way until it is shutdown via the earlier preconfigurations made to control the Virtual Machine while running headless and then closing VMware Fusion and reopening VMware Fusion. Note that this condition does not always occur however it can and does happen so it's imperative that one has a way to perform a controlled shutdown of the Guest OS via ssh or other remote access type methods.
Note: While running headless understand that if the Mac is rebooted/shutdown without first gracefully shutting down or suspending any Virtual Machines that are running headless (or normally for that matter) this can at its worst corrupt them to a point beyond repair so it is imperative that one remains cognizant of this and take appropriate measures to always suspend or shutdown all Virtual Machines that are running headless or normally before rebooting or shutting down the Mac. By themselves Virtual Machines running in headless mode require either direct user action as they will not suspend automatically like a normal running Virtual Machines when closing VMware Fusion unless one takes additional steps to enable controlled shutdown/suspend upon a controlled reboot/shutdown of the Host, Mac OS X and this will be covered within the three topic covered below.
- Starting a Virtual Machine without User Login
- Starting a Virtual Machine upon User Login
- Shutdown/Suspend Virtual Machine upon Mac OS X Reboot/Shutdown
==========
Starting a Virtual Machine without User Login
If you want to run a Virtual Machine as a Service without having to Login then a .plist file will need to be created for the target Virtual Machine and placed in the /Library/LaunchDaemons/ folder. The .plist file can be created using the example below or using a nice free GUI Tool named Lingon. (Note: This link is for the last freeware version. The latest is available at the App Store for $4.99.)
Hint: I often use TextEdit in Plain text mode as a buffer when copying/modifying/pasting blocks of code before pasting them into nano and or also use TextWrangler as it can access hidden files/folders and allow root access editing of files that are owned by or need to be owned by root.
In a Terminal (/Applications/Utilities/Terminal):
cd /Library/LaunchDaemons/
sudo nano com.wkz.headlessvm.plist
Copy and paste the code below into nano.
<?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>Disabled</key>
<false/>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>com.wkz.headlessvm</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Application Support/VMware Fusion/vmrun</string>
<string>-T</string>
<string>fusion</string>
<string>start</string>
<string>/Virtual Machines/Windows XP Professional SP3.vmwarevm/Windows XP Professional SP3.vmx</string>
<string>nogui</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>WKZ</string>
</dict>
</plist>
Save the com.wkz.headlessvm.plist file by typing Ctrl-O then Enter and then Ctrl-X to close nano.
Verify the permissions on the file using the ls -l command:
-rw-r--r-- root wheel com.wkz.headlessvm.plist
If it does not show as in the example above then use the following commands:
sudo chown root:wheel com.wkz.headlessvm.plist
sudo chmod 644 com.wkz.headlessvm.plist
Note: The fully qualified pathname to the .vmx configuration file must be used and in my working example I have my Virtual Machines folder in the root of the Macintosh HD not in the Documents folder in my Home folder, the latter being the VMware Fusion default location so adjust pathnames accordingly as well as other bits of information to match your environment and these are shown in maroon.
Additionally the KeepAlive key when being set to will restart the Virtual Machine immediately if you shutdown the Virtual Machine from within the Guest OS, suspend it with vmrun or kill the vmware-vmx process so keep that in mind when first configuring and testing your settings and as such I'd keep the value set to "false" until everything is worked out. Also if you do not need to keep it alive under any condition, except Mac OS X Reboot/Shutdown of course, and want to be able to shutdown/suspend the Virtual Machine while logged in or just leaving Mac OS X running then use "false" for the KeepAlive key. This key is really more for running headless on Mac OS X Server when the Virtual Machine is providing mission critical services and why one would want to keep it alive during normal operations if something was to kill the vmware-vmx process. The vmware-vmx process is the actual Virtual Machine vs the VMware Fusion process the UI.
Using Lingon is probably more intuitive and better then working directly with XML unless you just like to write code the old fashion way. Note: I found it better to initially create the .plist file using nano in a Terminal and then as/when needed to use Lingon to more easily edit or disable as needed. The primary difference was when starting with Lingon it did not have the UserName and WKZ key/value and this did make a difference with permissions through multiple reboots/shutdowns during my first testing with Lingon a few years ago when Eric (etung) turned me onto it and with starting the Virtual Machine in this manner and the shutdown/suspend method described in the next section.
==========
==========
Starting a Virtual Machine upon User Login
&
Shutdown/Suspend Virtual Machine upon Mac OS X Reboot/Shutdown
I have a folder in the root of the Macintosh HD named Scripts and this folder is included in the ${PATH} to make it easier to execute my scripts while keeping them separate from the OS and Application files. In my Home folder in a Terminal I created a file named .profile and added the following:
export PATH=$PATH:/Scripts
What nice about this is I also have added alias to this file which enable me to use very short commands for normally very long commands lines.
In the Scripts folder the two scripts used for the Headless Virtual Machines are, runheadless and suspendheadless and each have at a minimum the following as examples:
runheadless:
#!/bin/bash
"/Library/Application Support/VMware Fusion/vmrun" -T fusion start "/Virtual Machines/Windows XP Professional SP3.vmwarevm/Windows XP Professional SP3.vmx" nogui
suspendheadless:
#!/bin/bash
"/Library/Application Support/VMware Fusion/vmrun" -T fusion suspend "/Virtual Machines/VMware Fusion/Windows XP Professional SP3.vmwarevm/Windows XP Professional SP3.vmx"
If you need to have more then one Virtual Machine running headless just add an addition line of code for it.
These actions could be accomplished in a single script that has to be used anyway and shown below however I find it better for my needs to do it this way, so feel free to take whatever from all of this and use it to your liking. Also if you start the Virtual Machine using the "Starting a Virtual Machine without User Login" method from above then you can either leave the command out of the StartService () function leaving that line blank or leave it as is and use the target file and comment out the vmrun line in that file and this way you can more easily switch between the two startup methods that are being discussed herein.
The most important part about the following is this is what does the controlled shutdown/suspend as well as one of the startup methods if one chooses to start the Virtual Machine headless from this script too.
The following commands need to be done as root so in a Terminal you can either change to root using sudo su or preface all command with sudo it's your choice. Because of the number of commands and the time it takes I suggest using sudo su.
mkdir /Library/StartupItems/HeadlessVM
cd /Library/StartupItems/HeadlessVM
nano HeadlessVM
Copy and paste the following code into nano.
#!/bin/sh
. /etc/rc.common
StartService ()
{
/Scripts/runheadless
}
StopService ()
{
/Scripts/suspendheadless
}
RunService "$1"
Save the HeadlessVM file by typing Ctrl-O then Enter and then Ctrl-X to close nano.
Next make the HeadlessVM script executable.
chmod a+x HeadlessVM
Next create a StartupParameters.plist file for the HeadlessVM script.
nano StartupParameters.plist
Copy and paste the following code into nano.
{
Description = "HeadlessVM";
Provides = ("Runs/Suspends Virtual Machine Headless on OS X Startup/Shutdown");
Uses = ("Disks");
}
Save the StartupParameters.plist file by typing Ctrl-O then Enter and then Ctrl-X to close nano.
The permissions on these files are important and if not set properly will not execute properly so an ls -l should show as follows:
-rwxr--r-- root wheel HeadlessVM
-rw-r--r-- root wheel StartupParameters.plist
This equates to 744 and 644 respectively and if need be use the chown and or chmod command shown above eairler with these masks and the permissions for Group and Others must only be read only.
Note: There are other methods of Starting a Virtual Machine upon User Login however to do a controlled shutdown/suspend of a headless Virtual Machine, while there may be other methods, using the StopService ()function in the above script while using /Library/StartupItems is the only method that I personally know of and information show here in part was derived from Start Me Up: Writing and Understanding OS X StartupItems and direct usage on my own system and use habits.
After creating/modifying these files Mac OS X needs to be rebooted and make sure that the Virtual Machine is either manually suspended or not running when first creating the files in this last section as the Virtual Machine will not shutdown/suspend until after the reboot and then on the next reboot/shutdown cycle. Also note that I choose to suspend headless Virtual Machines vs shutting them down when rebooting/shutdown the Host because it's a faster process in either direction although it can be coded to shutdown. Be aware the shutting down takes longer and in that essence leaves more room for error if the Host decides to hiccup during a reboot/shutdown.
Needless to say even though this will handle a controlled shutdown/suspend on the Virtual Machine in order to safety reboot/shutdown the Host don't forget to make regular user data backups and backups of the Virtual Machine Package itself so as to have a clear and clean recovery plan when something go wrong and remember that it is a known fact that Time Machine is not 100% reliable under all conditions to backup/restore Virtual Machines and should not be relied upon so make a manual backup of the Machine Machine with it shutdown, not suspended and VMware Fusion closed.
Have fun running your Virtual Machines headless!
==========
WoodyZ - VMware vExpert, 2009 & 2010
Message was edited by: WoodyZ
This was originally posted in mid Nov, 2010 and the formatting was corrupted during the VMware Web Site upgrade in Dec, 2010 and has been now been cleaned up some to correct what showed as errors in coding due to the new version of software being used in the forums. Some clean up remains to be done.