Have you ever clicked the Power Off or Reset icon on a virtual machine (VM) only to have it power off in a way...
that resembles pulling the plug on a physical server instead of shutting down gracefully? When this happens, it can cause data corruption.
Both the VMware Infrastructure Client (VI Client) and the Web Access interface have configurable power option icons that you can use to alter the way a VM is shut down, restarted or suspended, which we'll go over in this tip.
The power options are shown in the remote console windows of a VM, as well as in the command section when you select a VM, as seen below.
VI Client power options:
Web Access power options:
These power options are configurable and work with VMware Tools to control the behavior of the various power options that can be performed on a virtual machine (Power Off, Power On, Suspend and Reset). By default the power options are "hard," which means the power from a VM is forcibly removed without any regard to gracefully shutting down the operating system. A hard power off is the equivalent of pulling the power cable on a physical server, and the reset action is the equivalent of pulling the power cable and plugging it back in.
These options can be changed so that the power off and reset actions are "soft," which means the operating system will be either gracefully shutdown for a power-off action or gracefully shut down and restarted for a reset action. So why would you want to change the default power options? Simply put, doing so can prevent data corruption that can occur when a VM is forcibly shut down. The custom power options are part of VMware Tools, so you must have VMware Tools installed on your VMs for the soft option to work. If you do not have VMware Tools installed, you can still set the options to soft, but the action that will be performed will be hard instead.
You can also set the suspend action to soft. A default or hard suspend action does not suspend the operating system of the VM but instead preserves the memory contents of the VM and powers it off so it can be resumed in the same state when it is powered back on. Since the operating system is not aware of the suspend operation, this hard suspend can potentially cause data corruption. A soft suspend, on the other hand, leaves the VM powered on but initiates a suspend operation through the guest operating system.
The default for all new virtual machines is for all power options to be set to hard. While you can't change the default setting, you can change the options after you create a virtual machine. One important thing to note is that while the power option icons and commands that are available in the remote console and commands window are configurable, both the right-click menu and VM top menu in the VI Client and the virtual machine menu in the Web Access client are not affected by this customization. Both the hard (i.e. Power Off) and soft (i.e. Shutdown Guest) options are available when accessing power options in this manner as seen below:
To change the power options for a VM using the VI Client you must first power off the VM and then edit the settings of the VM. Then, on the Options tab, select the VMware Tools setting, as shown below.
In the power controls section you can change the power off action to shutdown guest and also the reset action to restart guest. These options will initiate a soft, graceful shutdown of the operating system instead of a hard shutdown. The settings for these options are stored in the .vmx configuration file for each VM. You can also edit the .vmx file directly to change the options, but the VM must be powered off or the changes will get overwritten.
The suspend action cannot be set to soft using the VI Client. The only way to change it is by editing the .vmx file or by using the VI Toolkit (PowerShell). Once you change it and go back into the VI Client to edit the VM settings again, you will see a new suspend option called Put Guest on Standby, which will initiate the suspend through the guest operating system instead.
Below are the four settings that are in the .vmx file for power controls:
powerType.powerOff = "soft"
powerType.powerOn = "default"
powerType.suspend = "default"
powerType.reset = "soft"
If your environment has a large number of VMs, changing them individually can be tedious and time-consuming. Fortunately you can change many VMs at once using a simple PowerShell function that Hugo Peeters wrote which is available at peetersonline.nl. This function only changes the Power Off action, and not the Reset or Suspend actions, so I added separate functions for them. All the functions check to see if the action is already set to Soft, and if so it does not change it. One advantage of this method is that it can be done while the VMs are running unlike the VI Client method which does not allow this.
To use these functions, follow these steps:
- Type out the above code and save it as a .ps1 extension.
- Start the VMware Toolkit.
- Type a dot and a space then the path and name of the .ps1 file you saved, i.e. (. c:\powershell\powersoft.ps1), the dot/space allows you to use the function after the script runs.
- Set a variable with the names of the VMs that you want to change the power options for. Variables in PowerShell begin with a dollar sign followed by the name of the variable. This variable can be set to either a single, multiple or all VMs; to set a single or multiple type $variable name=GetVM "VM name1", "VM name2", etc. (i.e. $MyVMs=GetVM "Orange", "Green"), to set to all VMs type $variable name=GetVM. Once you set a variable you can check to see what it is set to by typing the variable name at the PowerShell command line.
- Once you have defined your target VMs to run against you simply type the function name followed by the variable name, i.e. Set-SoftReset $MyVMs.
Here is a sample PowerShell session:
And here is what the power option settings for the VM look like after the functions have been run against the VM:
When you click a power option icon you will know whether or not it will be a hard or soft action by the confirmation message that is displayed. For example, if a Reset is set to soft, you will see a message like below.
The following is the message you'll receive if it will be a hard action.
As you can see, using PowerShell makes it very easy to change the power options settings for many VMs at once. Just remember that changing these settings only affects the remote console icons and the command windows; you can still perform a forced shut down or reset by using the menus instead. Changing these options so they are soft can be good insurance against someone accidently doing a hard shutdown and causing corruption inside the VM's guest operating system.
|Eric Siebert is a 25-year IT veteran with experience in programming, networking, telecom and systems administration. He is a guru-status moderator on the VMware community VMTN forums and maintains VMware-land.com, a VI3 information site.|