I recently had the opportunity to spend some time automating VMware for a client. This post focuses on the vSphere SDK for Perl and modern Linux guests (e.g. RHEL6). VMware’s own documentation is pretty good, documenting every method of the SDK, but overall, I found it clunky to use. Also, with the SDK being distributed as a monolithic tarball, you’re left to install this yourself, using CPAN manually to resolve a number of dependencies.
Once you have the VMware SDK installed following the instructions included in the tarball, you have a few options.
You can write your own scripts quite easily using the Perl SDK’s hundreds of available methods, but this is out of scope for this post. VMware helpfully includes several example tools already that (mostly) work just fine in /usr/lib/vmware-cli/apps/.
All of the VMware Perl utilities can use either –username, –password, and –server options, but you can also store the credentials in your home directory, in $HOME/.visdkrc:
For the rest of this post, I’ll omit the username, password, and server settings. Assume them if you’re not using stored credentials.
Creating a VM
vmcreate.pl does exactly what you’d imagine – it creates a VM. The Syntax is simple:
yourvm.xml should look like the XML below, with appropriate values for each item. VMware provides examples here.
However, there’s one key caveat to all this: the default settings for vmcreate.pl are in the case of the network adapter not optimized, and in the case of the default SCSI bus incompatible with RHEL6 (and CentOS6, and probably any verison of Fedora).
I have created a patch file that fixes these things:
- Storage controller is updated to Paravirtual
- Network adapter is updated to VMXNET3
- Default provisioning is added to be "thin"
Where's the MAC Address?
Now at this point, you might be asking yourself, where’s the MAC address? I left vmcreate.pl to it’s default action, which is let VMware auto-generate it. However, I still needed to fetch it back after the VM was created, so that I could create the profile in Cobbler for automatic installation.
There’s no real easy way with the utilities, the only way I found was file scraping.
fileaccess.pl is a utility that allows you to download files from VMware, including the .vmx file which contains the auto-generated MAC address. Replace DATACENTEr, DATASTORE, and VMNAME with the appropriate fields.
You’ll need the “ethernet0.generatedAddress” field from this file.
Booting Your New VM
You can control the state of your VM’s with vmcontrol.pl:
You can also change the operation to ‘poweroff.’
Update: A colleague pointed me to the idea that working with Jython and the Java VI SDK might be a good idea for more complicated use cases. I tested it a little, and it works pretty well.