Difference between revisions of "Working with VMware Tools Scripts and Power States"

From Virtuatopia
Jump to: navigation, search
(VMware Tools Scripts)
(Creating Custom VMware Tools Scripts)
Line 48: Line 48:
  
 
The ''Advanced'' settings allow the VMware Tools installation on the guest operating system to be automatically updated if a newer version is available, and to specify whether the guest operating system time should be synchronized with that of the host.
 
The ''Advanced'' settings allow the VMware Tools installation on the guest operating system to be automatically updated if a newer version is available, and to specify whether the guest operating system time should be synchronized with that of the host.
 +
 +
== The Default VMware Tools Scripts ==
 +
 +
If a virtual machine is configured to use VMware Tools scripts then either the default scripts installed with Vmware Tools, or custom scripts may be used. On Windows based guest operating systems, the default scripts take the form of Windows Batch files and are located in \Program Files\VMware\VMware Tools. On Linux guests the default scripts are shell scripts and reside in /etc/vmware-tools. On both guest types the files are descriptively named ''poweroff-vm-default'', ''poweron-vm-default'', ''resume-vm-default'' and ''suspend-vm-default''.
 +
 +
A review of the content of these scripts using a text or programmers editor will reveal that by default they actually do very little. For example, with the exception of suspend and resume, the Windows VMware Tools scripts contain nothing but a couple of comment lines. The real power of the Vmware Tools scripts is the ability to configure custom scripts.
  
 
== Creating Custom VMware Tools Scripts ==
 
== Creating Custom VMware Tools Scripts ==
 +
 +
As described in the previous section, the default scripts provided may be overridden using custom scripts. Custom scripts may be created either by modifying the default scripts, or by creating new scripts and configuring VMware Tools to use the new script in place of the default script. Custom scripts may be edited using any suitable text or programming editor, or by using the ''Edit...'' button located on the ''Scripts'' tab of the Vmware Tools control panel (for more details on the VMware control panel read [[The VMware Tools Control Panel]] chapter of this book). Note that the use of the control panel script editing feature on Linux will require knowledge of the ''vi'' editor. Those lacking the necessary skills with ''vi'' should consider directly creating and editing the custom scripts using their preferred editor outside the context of the control panel.
 +
 +
To create a custom script using the VMware Tools control panel, click on the ''Scripts'' tab and select the power change event for which the custom script is to be created from the menu. Ensure that the ''Use script'' option is selected and click on ''Custom Script'' at which point the text box containing the path to the script and the ''Browse'' and ''Edit'' buttons will be enabled. To edit the default script simply click on the ''Edit'' button and make the modification in the resulting edit window. To edit an existing custom script or create an entirely new script, enter the path and name of the script file into the the text field and press the ''Edit'' button. Once the script is complete, save the file and exit the editor to return to the control panel.
 +
 +
If custom scripts are being created outside of the VMware Tools control panel (and are new files rather than modifications to the existing default scripts) it will be necessary to configure VMware Tools to use these scripts. To achieve this, open the VMware Tools control panel, select the ''Scripts'' tab and choose the appropriate power state from the drop down menu. Ensure that ''Use scripts'' and ''Custom Script'' are both selected, use the ''Browse'' feature  to navigate to and select the new custom script and click on ''OK'' to apply the change.
 +
 +
On Solaris, FreeBSD and Linux systems, it will be necessary to make new script files executable before they will run. To do this, use the ''chomd'' command. For example:
 +
 +
<pre>
 +
chmod 755 /etc/vmware-tools/poweroff-vm-custom
 +
</pre>
 +
 +
To test a custom script at any time, use the ''Run Now'' button on the control panel ''Scripts'' screen.
 +
 +
== Passing a String from the Host to the Guest ==
 +
 +
A string (in other words a sequence of characters) may be passed from the host system to the guest where it may be used within a VMware Tools script, or any other script for that matter. This is achieved by assigning the string to be passed to the guest to the ''machine.id'' value in the ''.vmx'' configuration file of the virtual machine in which the guest is running. For example, to pass the string "Virtuatopia Labs" through to the guest, the ''machine.id'' line in the .vmx file would read as follows:
 +
 +
<pre>
 +
machine.id = "Virtuatopia Labs"
 +
</pre>
 +
 +
The string may subsequently be obtained from within the guest operating system on Linux using the following command:
 +
 +
<pre>
 +
vmware-guestd --cmd 'machine.id.get'
 +
Virtuatopia Labs
 +
</pre>
 +
 +
The equivalent command on a Windows guest is as follows:
 +
 +
<pre>
 +
VMwareService --cmd machine.id.get'
 +
Virtuatopia Labs
 +
</pre>
 +
 +
This technique can be particularly useful in situations where the hostname or IP address to be used by the guest operating system needs to be provided by the host system at system start up time.
 +
 +
== Linux, Solaris and FreeBSD Reboot and Shutdown Commands ==
 +
 +
In addition to the scripts outlined above, an additional mechanism is provided on Linux, Solaris and FreeBSD guests for executing commands when a virtual machine is either shut down or rebooted. These are specified in the ''/etc'vmware-tools.conf'' file using the '''halt-command ='' and '''reboot-command = ''' directives.

Revision as of 15:07, 13 October 2008

The power state of a VMware Server virtual machine can be changed either from within the guest operating system itself (for example by initiating the operating system's shutdown command from within the VMware Remote Console) or by using the VI Web Access interface.

In the case of changing the power state from within the VI Web Access interface, a number of configuration options are available in terms of what actually happens when the power state is changed (for example when the VI Web Access power off button is clicked) and also which VMware Tools scripts are triggered within the guest operating system.

In this chapter, the concept of power states and the use and configuration of the VMware Tools scripts will be covered in detail.




Changing Virtual Machine Power State from VI Web Access

When a virtual machine is powered on or off, suspended, resumed or restarted it is said to have undergone a change in power state. The VI Web Access interface provides no less than five locations where the power state of a virtual machine may be changed. The most prominent of these are located in the tool bar which runs along the top of the interface. Toolbar buttons are provided to power on, power off, suspend and restart the virtual machine currently selected from the Inventory panel.

In addition, power state options are listed in the Commands panel of the virtual machine workspace area when a virtual machine is selected from the inventory. Similarly, power state options are listed for the currently selected virtual machine within the Virtual Machines menu option in the menu bar at the top of the VI Web Access interface.

If the host system is selected from the inventory panel and the Virtual Machines tab of the workspace selected, a list of virtual machines appears together with a Commands panel. As each virtual machine is selected from this list, the Commands panel updates with a list of suitable commands, including power state changes, for the selected machine.

Finally, power state change controls are available in the VMware Remote Console of a running virtual machine within the Remote Console->Troubleshoot menu option.


Configuring Virtual Machine Power Options

To configure the power options of a virtual machine, select it from the Inventory panel, click on Configure VM from the Commands panel in the workspace area and choose the Power tab from the resulting dialog.


Configuring Virtual Machine Power Options


As illustrated in the preceding figure, the power state configuration settings are divided into a number of different categories, each of which is detailed below:

Power Controls

The Power Controls section of the configuration panel specifies the action performed when the VI Web Access power off, suspend and restart options are chosen for a virtual machine:

  • Power Off - The Power Off command can be configured to immediately power down the virtual guest (an effect similar to simply removing the power from a physical computer system). The effect is immediate but also poses the risk that data will be lost given that the guest operating system running within the virtual machine is not provided the opportunity to perform a clean shutdown. If VMware Tools have been installed on the guest operating system, the Shut Down Guest option may be selected ensure an orderly shutdown of the guest operating system prior to the removal of "virtual power" to the virtual machine.
  • Suspend - As with the Power Off command, two options are available for suspending virtual machines from within the VI Web Access management interface. If Suspend is selected, the virtual machine will be suspended without triggering the suspend process of the guest operating system (assuming the guest includes such a feature). If VMware Tools have been installed on the guest, the Suspend Guest option will first trigger the guest operating system's suspend mechanism prior to suspending the virtual machine.
  • Reset - The reset option may similarly be configured to reset the virtual machine without regard to the guest operating system or, assuming the prior installation of VMware Tools, using the guest operating system's reboot procedure by selecting Reset or Restart Guest respectively.

VMware Tools Scripts

The previous section described how the guest operating system running within a VMware Server virtual machine may be cleanly shutdown, restarted or suspended through the installation on VMware Tools on the guest. When a user instructs VI Web Access to change the power state of a virtual machine, VMware Server communicates with a VMware Tools process running on the guest operating system and instructs it to execute a script. The script which is executed depends on the nature of the power state change, together with script configuration settings made within the VMware Tools control panel (covered in detail later in this chapter).

the VMware Tools Scripts section of the power configuration panel allows VMware Tools script execution to be disabled for each of the power state changes. To prevent a script running at a particular point, simply deselect the check box next to the corresponding description.

BIOS Setup

The BIOS setup section provides the ability to configure the selected virtual machine to automatically enter the BIOS setup screen next time it is powered on. This is a transient setting which is reset after the first power on.

Advanced

The Advanced settings allow the VMware Tools installation on the guest operating system to be automatically updated if a newer version is available, and to specify whether the guest operating system time should be synchronized with that of the host.

The Default VMware Tools Scripts

If a virtual machine is configured to use VMware Tools scripts then either the default scripts installed with Vmware Tools, or custom scripts may be used. On Windows based guest operating systems, the default scripts take the form of Windows Batch files and are located in \Program Files\VMware\VMware Tools. On Linux guests the default scripts are shell scripts and reside in /etc/vmware-tools. On both guest types the files are descriptively named poweroff-vm-default, poweron-vm-default, resume-vm-default and suspend-vm-default.

A review of the content of these scripts using a text or programmers editor will reveal that by default they actually do very little. For example, with the exception of suspend and resume, the Windows VMware Tools scripts contain nothing but a couple of comment lines. The real power of the Vmware Tools scripts is the ability to configure custom scripts.

Creating Custom VMware Tools Scripts

As described in the previous section, the default scripts provided may be overridden using custom scripts. Custom scripts may be created either by modifying the default scripts, or by creating new scripts and configuring VMware Tools to use the new script in place of the default script. Custom scripts may be edited using any suitable text or programming editor, or by using the Edit... button located on the Scripts tab of the Vmware Tools control panel (for more details on the VMware control panel read The VMware Tools Control Panel chapter of this book). Note that the use of the control panel script editing feature on Linux will require knowledge of the vi editor. Those lacking the necessary skills with vi should consider directly creating and editing the custom scripts using their preferred editor outside the context of the control panel.

To create a custom script using the VMware Tools control panel, click on the Scripts tab and select the power change event for which the custom script is to be created from the menu. Ensure that the Use script option is selected and click on Custom Script at which point the text box containing the path to the script and the Browse and Edit buttons will be enabled. To edit the default script simply click on the Edit button and make the modification in the resulting edit window. To edit an existing custom script or create an entirely new script, enter the path and name of the script file into the the text field and press the Edit button. Once the script is complete, save the file and exit the editor to return to the control panel.

If custom scripts are being created outside of the VMware Tools control panel (and are new files rather than modifications to the existing default scripts) it will be necessary to configure VMware Tools to use these scripts. To achieve this, open the VMware Tools control panel, select the Scripts tab and choose the appropriate power state from the drop down menu. Ensure that Use scripts and Custom Script are both selected, use the Browse feature to navigate to and select the new custom script and click on OK to apply the change.

On Solaris, FreeBSD and Linux systems, it will be necessary to make new script files executable before they will run. To do this, use the chomd command. For example:

chmod 755 /etc/vmware-tools/poweroff-vm-custom

To test a custom script at any time, use the Run Now button on the control panel Scripts screen.

Passing a String from the Host to the Guest

A string (in other words a sequence of characters) may be passed from the host system to the guest where it may be used within a VMware Tools script, or any other script for that matter. This is achieved by assigning the string to be passed to the guest to the machine.id value in the .vmx configuration file of the virtual machine in which the guest is running. For example, to pass the string "Virtuatopia Labs" through to the guest, the machine.id line in the .vmx file would read as follows:

machine.id = "Virtuatopia Labs"

The string may subsequently be obtained from within the guest operating system on Linux using the following command:

vmware-guestd --cmd 'machine.id.get'
Virtuatopia Labs

The equivalent command on a Windows guest is as follows:

VMwareService --cmd machine.id.get'
Virtuatopia Labs

This technique can be particularly useful in situations where the hostname or IP address to be used by the guest operating system needs to be provided by the host system at system start up time.

Linux, Solaris and FreeBSD Reboot and Shutdown Commands

In addition to the scripts outlined above, an additional mechanism is provided on Linux, Solaris and FreeBSD guests for executing commands when a virtual machine is either shut down or rebooted. These are specified in the /etc'vmware-tools.conf file using the 'halt-command = and reboot-command = directives.