Managing virtual machines in vSphere

When modifying and managing virtual machines in vSphere, solution providers should make sure to know how to use other tools and methods besides the vSphere Client.

Solution providers are probably most familiar with using the vSphere Client when managing virtual machines (VMs) in vSphere. However, some alternate methods for managing vSphere VMs are particularly useful, especially when troubleshooting VMs or doing configuration changes in bulk. In this tip, you'll find information on the various methods for managing and modifying VMs.

When changes are made to a VM's configuration using the vSphere Client, those changes are stored in the .vmx file that is located in the VM's home directory. This file is a text file that contains both virtual hardware configuration information for the VM as well as resource and advanced settings.

In most cases, you don't want to edit this file directly, but there are occasions when doing so is necessary. For example, when troubleshooting or adding settings that you cannot set through the vSphere Client, editing the file directly is necessary. Although you can edit the file when the VM is powered on, it's best to power it off first as the file is occasionally written to while the VM is running. Editing the file when the VM is off ensures your changes do not get overwritten. Solution providers should note that changes made directly to the file will not take effect until the VM is restarted.

Before editing the .vmx file, it's best to first make a backup of it, and the easiest way to do this is by using the datastore browser in the vSphere Client. Simply browse the datastore that the VM is located on and go to its home directory. Next, right-click on the .vmx file and click Download and then select a directory on your workstation to save it in. Don't be confused with the .vmxf file that's in the VM's home directory -- that file is not used with vSphere and is only there for compatibility purposes with VMware Workstation.

Now that you have the file on the workstation, you can edit it using a text editor, such as Notepad. Just be sure to first make a backup copy of the file. Once you are done editing the file, browse the datastore again. Go to the VM's home directory and click the Upload button to copy the file from your workstation to the .vmx file in the VM's home directory.

Solution providers can also edit the .vmx file by logging into the ESX Service Console or ESXi Management Console in Tech Support mode either locally or remotely using Secure Shell. To edit the file, log in to the console, navigate to the VM's home directory and then open it using either the nano (available in ESX and ESXi) or vi (available only in ESX) text editor. The nano editor is much simpler to use than the more advanced vi editor. Simply type nano .vmx to open the file in the editor and don't forget that Linux is case sensitive. Once you are done editing the file, press -O to write out or save the file and then -X to exit the editor.

In some situations, like when you're in a data center without any client workstations and working directly from the host's console, you can't use the vSphere Client and have to resort to command-line utilities to manage your customers' virtual machines. The most common command-line utility used to manage VMs is the vmware-cmd command. This single command can perform many VM-related operations -- including power management, snapshot management and configuration management -- depending on the parameters you give it.

The vmware-cmd command can be used locally in the ESX Service Console or remotely with ESX or ESXi using either the vSphere Command-Line Interface (CLI) or the vSphere Management Assistant (vMA) appliance. The vSphere CLI is a command-line version of the vSphere Client and is installed on a Windows or Linux workstation to remotely manage an ESX or ESXi host.

Commands in the vSphere CLI are similar to the VMware-specific commands used inside the ESX Service Console to manage host and VM configurations and operations. They run as Perl scripts on the workstation they are installed on and connect remotely to the host using the built-in application programming interface (API). The vMA deploys as a pre-built VM and is essentially a command-line version of vCenter Server that allows solution providers to manage multiple hosts at the same time.

It is important to note that while many command-line utilities are used with ESXi locally in Tech Support mode, vmware-cmd is not one of them. Vim-cmd is an alternate command that is used to do things like VM power operations. It's a wrapper for the vim-sh command, which is an interactive shell buffer used for advanced VM configurations and operations.

Another useful command-line utility for managing virtual machines is the vm-support command. It is useful for a variety of tasks, including forcibly powering off a VM or collecting VM information for troubleshooting. One that's very helpful in performance troubleshooting is the esxtop command that displays real-time VM performance statistics.

Finally, the vmkfstools command is the Swiss Army knife for managing VM disks. It allows you clone, create and shrink disks, among other things. Both commands are in the ESX Service Console and are run remotely (resxtop) with the vSphere CLI or vMA on both ESX and ESXi hosts. Esxtop is only available when run in the ESX Service Console; the remote version of the command that is in the vSphere CLI is called resxtop.

The table below describes some common uses for these commands when working with and managing virtual machines.



vmware-cmd -l

Lists all virtual machines registered on a host and outputs the path to their .vmx file, which is used with other vmware-cmd VM operations <cfg>

vmware-cmd <cfg> getstate

Returns the power state of the selected VM

vmware-cmd <cfg> stop hard

Immediately and unconditionally powers off the selected VM

vmware-cmd <cfg> removesnapshots

Removes all snapshots from the selected VM

vim-cmd /vmsvc/getallvms

Lists all VMs registered on a host and outputs VM IDs, which are used with other vim-cmd VM operations <vmid>

vim-cmd /vmsvc/get.guest <vmid>

Lists detailed VM information, including power state, heartbeat status and IP address

vim-cmd /vmsvc/power.off <vmid>

Powers off the selected VM

vim-cmd /vmsvc/snapshot.removeall <vmid>

Removes all snapshots from the selected VM

vm-support -x

Lists all VMs running on a host and outputs VM IDs, which is used with other vm-support VM operations <vmid>

vm-support -X <vmid>

Forcibly shuts down the selected VM and generates core dumps and log files

Table 1 – Common commands when managing virtual machines in vSphere

Because there are many software development kits available for vSphere, a wide variety of programming languages are used for writing scripts to help automate operations that are normally tedious to perform. The most common and easiest to use is Microsoft's Windows PowerShell, which is a command-line shell and associated scripting language that helps automate common administration tasks and also provides information about the VMware environment. For example, if you had to create 100 VMs for a customer, rather than do them one by one using the vSphere Client, which could take hours, you could use a script and have it done in minutes. PowerShell is fairly easy to install and use, and there are many great scripts that work with vSphere environments.

About the expert
Eric Siebert is a 25-year IT veteran whose primary focus is VMware virtualization and Windows server administration. He is one of the 300 vExperts named by VMware for 2009. He is the author of the book VI3 Implementation and Administration and a frequent TechTarget contributor. In addition, he maintains vSphere-land.com, a VMware information site.


Dig Deeper on Server virtualization technology and services