Still Life

A Series of Mental Snapshots

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

Advertisements

10 Responses to “Basic Scripting of VM operations on ESX Server Using vmware-cmd”

  1. Lara said

    Hi – nice page. Would help if you could show us some of your scripts – samples. Thanks

  2. Steve said

    Hi Lara,

    Thanks for the feedback, I really appreciate it! I will have to search around for the script that I implemented, if I can find I’ll post it up. In the future I will also always try to include at least one example!

  3. Abhinay said

    Hi,
    Good to see all options for vmware-cmd on one page.

    Thanks

  4. AB said

    Excellent!!!!!!!!!!!!!!!

    You are making life simple and happy.I love to work & share ……

    You are awesome

  5. Steve said

    It would be really nice to know what goes in tag. The rest of the information that’s on this page can be seem in the Service Console anyway.

    Could anyone please give an example what should one put in the tag? Path to where?

    vmware-cmd removesnapshots

  6. Shane said

    Steve…thanks for the page…good stuff! 🙂
    Question – do you have a script or know of a way to not just check if there’s a snapshot on one VM, but a group of VMs..like on a datastore…or better yet, in a cluster?

    Thanks!

    • Steve said

      Hi Shane,

      Thanks for the comment, but I unfortunately don’t know of a way to check if their is snapshot on a group/cluster I have been out of the VMWare ESX game for a little too long!

      I hope you are able to find a good solution

      –Steve

      • Shane said

        Hi Steve,
        Thanks for the reply. I have to be honest, since I submitted the above post, with the projects I’ve been on, I haven’t had time to look further myself! 🙂

        Regards.

  7. Gany said

    Nice Work Dude. Expecting much more like this with examples. . .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: