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:
Operation | Description |
---|---|
validate | Allows a user to validate guest credentials without performing any VIX Operation |
ps | Performs a process listing within the guestOS |
startprog | Starts a program within the guestOS |
kill | Kills a particular process within the guestOS |
ls | Perorms a listing of a particular directory within the guesOS |
mkdir | Creates a directory within the guestOS |
rmdir | Removes a directory within the guestOS |
rm | Removes a file within the guestOS |
mv | Move/Rename a file within the guestOS |
mvdir | Move a directory within the guestOS |
copyfromguest | Download a file from within the guestOS to local system |
copytoguest | Upload a file to the guestOS from a local system |
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation validate --guestusername root
./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.
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation kill --guestusername root --pid 13198
./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
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation ls --guestusername root --filepath_src /var/log
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mkdir --guestusername root --filepath_src /tmp/virtuallyghetto
./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
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation rm --guestusername root --filepath_src /root/virtuallyghetto
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mv --guestusername root --filepath_src /root/steve_jablonsky --filepath_dst /root/hans_zimmer
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation mvdir --guestusername root --filepath_src /tmp/foo --filepath_dst /tmp/bar
./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5 --operation copyfromguest --guestusername root --filepath_src /var/log/messages
./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