Still Life

A Series of Mental Snapshots

Archive for the ‘ESX Scripting’ Category

Basic Scripting of VM operations on ESX Server Using vmware-cmd

Posted by Steve on June 12, 2008

I’m writing a little more about ESX scripting because I had a lot of trouble tracking down some of this information, and if it had been all in one spot I would have saved a lot of time and trouble! I will be going over a few basic commands, as well as a few errors that I found. All of this was done on a ESX 3.5 machine.

This all started recently when I needed to reproduce a bug to show that it actually was an ESX specific issue. I learned quite a bit about ESX scripting from this experience, and hopefully some of the knowledge I am about to impart upon the vast interwebs helps someone out!

Now in starting the main command, that can be entered from anywhere in the command, that you will be using is vmware-cmd. Entering ‘vmware-cmd‘ will provide the following output:

Usage: /usr/bin/vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>
/usr/bin/vmware-cmd -s <options> <server-action> <arguments>

Options:
Connection Options:
-H <host> specifies an alternative host (if set, -U and -P must also be set)
-O <port> specifies an alternative port
-U <username> specifies a user
-P <password> specifies a password
General Options:
-h More detailed help.
-q Quiet. Minimal output
-v Verbose.

Server Operations:
/usr/bin/vmware-cmd -l
/usr/bin/vmware-cmd -s register <config_file_path>
/usr/bin/vmware-cmd -s unregister <config_file_path>
/usr/bin/vmware-cmd -s getresource <variable>
/usr/bin/vmware-cmd -s setresource <variable> <value>

VM Operations:
/usr/bin/vmware-cmd <cfg> getconnectedusers
/usr/bin/vmware-cmd <cfg> getstate
/usr/bin/vmware-cmd <cfg> start <powerop_mode>
/usr/bin/vmware-cmd <cfg> stop <powerop_mode>
/usr/bin/vmware-cmd <cfg> reset <powerop_mode>
/usr/bin/vmware-cmd <cfg> suspend <powerop_mode>
/usr/bin/vmware-cmd <cfg> setconfig <variable> <value>
/usr/bin/vmware-cmd <cfg> getconfig <variable>
/usr/bin/vmware-cmd <cfg> setguestinfo <variable> <value>
/usr/bin/vmware-cmd <cfg> getguestinfo <variable>
/usr/bin/vmware-cmd <cfg> getproductinfo <prodinfo>
/usr/bin/vmware-cmd <cfg> connectdevice <device_name>
/usr/bin/vmware-cmd <cfg> disconnectdevice <device_name>
/usr/bin/vmware-cmd <cfg> getconfigfile
/usr/bin/vmware-cmd <cfg> getheartbeat
/usr/bin/vmware-cmd <cfg> gettoolslastactive
/usr/bin/vmware-cmd <cfg> getresource <variable>
/usr/bin/vmware-cmd <cfg> setresource <variable> <value>
/usr/bin/vmware-cmd <cfg> hassnapshot
/usr/bin/vmware-cmd <cfg> createsnapshot <name> <description> <quiesce> <memory>
/usr/bin/vmware-cmd <cfg> revertsnapshot
/usr/bin/vmware-cmd <cfg> removesnapshots
/usr/bin/vmware-cmd <cfg> answer

These are all the different commands you can run with vmware-cmd. This in itself is only somewhat useful because for the more detailed commands, such as getconfig and setconfig, it does not tell you what variables to enter. I will go over a few of the ones that I have played with next.

The first useful command is:
vmware-cmd -l

This lists out the location of all of the VMs’ .vmx files on the ESX server. This is very useful as you will need this when you run any of the vmware-cmd commands. As stated above the syntax for the command is:
vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>

Now onto a few of the commands, there are the ones that are pretty self explanatory,
Remove All VM Snapshots: vmware-cmd <cfg> removesnapshots
Check if VM has any Snapshots: vmware-cmd <cfg> hassnapshot
Create A Snapshot[note everything but the name is optional]: vmware-cmd <cfg> createsnapshot <nameofsnapshot> <descriptionofsnapshot> <quiesce> <memory>
Revert to the previous snapshot: vmware-cmd <cfg> revertsnapshot
Start a VM: vmware-cmd <cfg> start <powerop_mode>
Get the state of the VM: vmware-cmd <cfg> getstate

Those are some of the simple useful ones, now here are a few that are a little finicky.
Stopping a VM: vmware-cmd <cfg> stop <powerop_mode>
Resetting a VM: vmware-cmd <cfg> reset <powerop_mode>

Here the <powerop_mode> is the tricky option. There are three options:
Soft: VMware tools need to be installed and running, therefore there are many cases when this will not work!
Hard: Performs the operation no matter what
Trysoft: Trys a soft and if that fails proceeds to a Hard. This is the best one in my opinion

get/setconfig:
vmware-cmd <cfg> setconfig <variable> <value>
vmware-cmd <cfg> getconfig <variable>

Setting or getting a configuration is a bit confusing at first because it asks for a variable, where is this variable coming from!?!? Luckily its actually pretty simple, you can set/getconfig on any of the settings in the .vmx file. There unfortunately is an issue with ESX 3.x ; some of the parameters that you set, only get set if you reset the hostD service by entering: service mgmt-vmware restart . This really is only a workaround as running this command will reset all of the connections that the ESX server has with the VMs, and it does take some time for it to come back up.

There you have it, this should be enough info to get you started on some ESX scripting. If you have any questions on how some of these commands work feel free to drop me a comment, and I’ll do my best to help you out. Below are also a few resources that I found helpful, but keep in mind if you are running ESX 3.x some things may work differently!

Useful References
Guide for driving the ESX server via Command line – http://www.rtfm-ed.eu/docs/vmwdocs/esx3.x-vc2.x-serviceconsole-guide.pdf
VMware Scripting API – http://wiki.eclab.byu.edu/plugin/attachments/ScriptingAPI/Scripting_API_215.pdf

Posted in ESX Scripting | Tagged: , , , , , , , , , | 10 Comments »

Taking Screenshots From VMWare ESX Command line using vmdumper

Posted by Steve on June 11, 2008

VMdumper is something that apparently does not exist, well according to google at least. But fortunately,  I have learned of, and now am sharing some of the usefulness of this little tool in VMware.

The main thing that I have used it for is to take a .png screenshot of a virtual machine on that ESX server. This has a whole bevy of usefulness. Specifically for me, I was trying to catch a specific error that was occurring when my VMs were booting in a specific way and that left no trace if itself in any log files, the only way to know it happens is to look at the screen, thus a screenshot was in order!

The vmdumper command can be accessed on your ESX server by connecting via SSH and navigating to the following directory:
/usr/lib/vmware/bin/

From here you can run ./vmdumper which will provide the following output:
./vmdumper: [options] <world id|–listVM> <unsync|sync|vmx|vmx_force|samples_on|samples_off|nmi|screenshot|backtrace>
-f: ignore vsi version check
-h: print friendly help message
-l: print information about running VMs

These are the options available to you using this VMdumper tool! So moving on, you know where the tool is, and you want to take a screenshot! To take a screen shot, you need to know the VMID. The VMID is unfortunately dynamic, but have no fear another command is here. The vmdumper will list all of the information you need with the following command:
/usr/lib/vmware/bin/vmdumper –listVM

This will list out all of the VMs that are on your ESX server in an uglyish format, but it is pretty simple to extract the desired information, that is the VMID with the following command:
VMID=`/usr/lib/vmware/bin/vmdumper –listVM | grep VMsDisplayName | awk ‘/vmid/ {print $1}’ | awk -F= ‘ {print $2}’`

Basically what is happening is that this command is that the list of VMS is piped out and the grep command finds the line that has the desired VMID with the use of the VM’s display name. The first awk command pulls out the whole vmid filed, and the second awk pulls just the vmid number, and finally it is assigned to the variable VMID!

Now the screenshot command can be done from any directory, you just need to execute the following command:
/usr/lib/vmware/bin/vmdumper VMID screenshot

The screenshot will be deposited where that VMs .vmx file is located and will be labeled vmdumper.png!

There you have it, a screenshot of a running VM from the command line that you can run from a bash shell script! One final time here are the commands you’ll need to run.
VMID=`/usr/lib/vmware/bin/vmdumper –listVM | grep ${VMS[counter]} | awk ‘/vmid/ {print $1}’ | awk -F= ‘ {print $2}’` #Gets the VMID
/usr/lib/vmware/bin/vmdumper $VMID screenshot #take the screenshot

Hopefully this helps someone out, I know it made my day a lot easier!

–Steve

Post Script:
This is the first time I have posted a sort of How To, or purely information related post, feel free to drop me a line on the formatting or if there is any info that should be included that is not!

Posted in ESX Scripting | Tagged: , , , , , , , , , | 2 Comments »