javascript:void(0)

Tuesday, July 26, 2011

Automating the New Integrated VIX/Guest Operations API in vSphere 5

VMware hinted earlier this year that the future of the VIX API would eventually be integrated into the core vSphere API, it looks like the wait is finally over. With the latest release of vSphere 5, the VIX API is no longer a separate API but consolidated into the core vSphere API which allows users to perform guest level operations directly to a virtual machine that is running VMware Tools.

There is now a new managed object called GuestOperationsManager which provides functionality for Authentication (authManager), File management (fileManager) and process management (processManager). The API is actually quite easy to use as you need to first acquire a guest auth credential within the guest and then you can perform any of the various guest operations using this credential cache.
I wanted to show the power of the VIX API by creating a vSphere SDK for Perl script called guestOpsManagement.pl that implements majority of the VIX/Guest Operations for users to manage from a centralized script. You will need to have a system that has the vCLI installed or use VMware vMA and to utilize the new VIX operations, your virtual machines must have the latest VMware Tools installed.

You can download guestOpsManagement.pl script here

The script supports 12 different VIX Operations which are described below:

OperationDescription
validateAllows a user to validate guest credentials without performing any VIX Operation
psPerforms a process listing within the guestOS
startprogStarts a program within the guestOS
killKills a particular process within the guestOS
lsPerorms a listing of a particular directory within the guesOS
mkdirCreates a directory within the guestOS
rmdirRemoves a directory within the guestOS
rmRemoves a file within the guestOS
mvMove/Rename a file within the guestOS
mvdirMove a directory within the guestOS
copyfromguestDownload a file from within the guestOS to local system
copytoguestUpload a file to the guestOS from a local system

Here is an example of performing the "validate" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation validate --guestusername root

Here is an example of performing the "ps" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation ps --guestusername root

In the next example, we will kill off the "tail" process as listed above.

Here is an example of performing the "kill" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation kill --guestusername root --pid 13198

Here is an example of performing the "startprog" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation startprog --guestusername root --working_dir /root --program_path /usr/bin/touch --program_args /root/virtuallyghetto

Note: There is known vSphere SDK for Perl bug for the return value of a "long" which will be resolved in GA release of vSphere SDK for Perl 5.0. You should not see the "error" message when running this operation

Here is an example of performing the "ls" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation ls --guestusername root --filepath_src /var/log

Here is an example of performing the "mkdir" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mkdir --guestusername root --filepath_src /tmp/virtuallyghetto

Here is an example of performing the "rmdir" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation rmdir --guestusername root --filepath_src /tmp/virtuallyghetto

Note: If you would like to perform a recursive directory delete, you will need to specify the --recursive, please use with caution

Here is an example of performing the "rm" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation rm --guestusername root --filepath_src /root/virtuallyghetto

Here is an example of performing the "mv" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mv --guestusername root --filepath_src /root/steve_jablonsky --filepath_dst /root/hans_zimmer

Here is an example of performing the "mvdir" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mvdir --guestusername root --filepath_src /tmp/foo --filepath_dst /tmp/bar

Here is an example of performing the "copyfromguest" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation copyfromguest --guestusername root --filepath_src /var/log/messages

Here is an example of performing the "copytoguest" operation:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation copytoguest --guestusername root --filepath_src /home/vi-admin/test.pl --filepath_dst /root/test.pl

There is also complete perl docs for this script which can be called using the following command:

perldoc guestOpsManagement.pl

8 comments:

  1. Calling these methods in the MOB always returns "Not initialized: boolean interactiveSession"

    Here is the auth param (ListProcessesInGuest):

    <auth>
    <username>MyUsername</username>
    <password>MyPassword</password>
    <interactiveSession>false</interactiveSession>
    </auth>

    ReplyDelete
  2. Are you creating a new NamePasswordAuthentication? which is then passed to any of the VIX functions? Curious on why you're going directly through the MOB versus one of the vSphere SDKs?

    ReplyDelete
  3. Just wanting to test it out before I put in the effort. NamePasswordAuthentication inherits from GuestAuthentication. Have you tried this from the MOB?

    ReplyDelete
  4. @Bob,

    I've already created a Perl script that implements majority of the VIX operations, I would say the hard work is pretty much done :) It works very well, but no I've not used the MOB test VIX operations. I only use the MOB if I have to, else I use the SDKs which is what they're there for.

    ReplyDelete
  5. This is a really useful script for me, now that VMware have dropped guest customisation for Ubuntu and Debian - I can use this to write a script to perform my customisations again (and do a lot more besides). However, I'm having trouble getting it to work on my vSphere 5 setup. It dies with this error:

    Can't locate object method "guestOperationsManager" via package "ServiceContent" at /usr/local/vmware/bin/guestOpsManagement.pl line 370.

    When I use a web browser to look at ServiceContent, I can see GuestOperationsManager, so it is there, but for some reason the script can't see it.

    Any ideas?

    Tim

    ReplyDelete
  6. @Tim, Are you sure you're using the latest vSphere SDK for Perl 5.0? It sounds like that might be the problem and not so much for the actual host itself.

    ReplyDelete
  7. Hi William, thanks for the script!

    I'm trying to iterate over all the *nix hosts to run a mkdir /tmp/vmware-root in each Guest OS - but at each iteration it is asking for the vcenter password (see community thread below) - is there a way to cache the vcenter password btw iterations or hard code it in the perl script as a workaround?

    http://communities.vmware.com/message/2003225#2003225

    thanks!

    ReplyDelete
  8. @vExpert2011 - You can easily do so by setting --password option and then use a simple "for" loop to iterate through all your VMs

    ReplyDelete

 
/*http://blog.cartercole.com/2009/10/awesome-syntax-highlighting-made-easy.html*/