CyberGround

VMWare vSphere 6.x Customize linux guest IP address via Perl SDK

Home Blog Categories Links

VMWare vSphere 6.x Customize linux guest IP address via Perl SDK

Darwick 2018-01-02 Categories: #automation #perl-sdk #vmware

I must confess that I like the VSphere’s Perl SDK while I set some automation job, but one of its bottleneck is the purely wrote example scripts. If you would like to deploy a template, you should use vmclone.pl with its functions and you will be done. It is great but what about the VM’s network settings? If you deploy a Windows guest, you can set up the network via vmclone.pl of course, but you need to enter it into a domain. What about deploying a Linux server? you are out of luck, because you cannot set up the networking.

After digging into the web, I found vmclone2.pl which seems to be useful, but it was wrote at 2010, and its biggest problem is that will not work on VSphere 6.x environment and it is only useful for Linux VM deploying. As far as I know, you are out of options, so if you would like to customize your Windows (without entering into domain) and Linux guest in the same script, you need to write your own Perl code…

… Until now, because I wrote it already. 🙂 Let me explain how it works. My concept was the same as in vmclone2.pl The Perl script itself (vmclone.pl) only need some little modifications, because it just calls some functions. Hence most of VMUtil.pm functions need to be rewritten. In the original VMUtil.pm the Windows servers has been entered into a domain. I just disabled this function, because in most cases, you will use as a standalone server and you can enter it into domain later. Most of the original values in the schema was also untouched, and I also added some new. Here is the description for all values:

  • Cust-Type (new value): It should be win or lin. It determines the VM type, it is quite obvious. lin for Linux guests, win for Windows guests.
  • Machine-Name (old value): The virtual machine’s hostname.
  • Timezone (old value): Timezone for Windows guests. It must be a timezone code. You can find yours here.aspx).
  • Linux-Timezone (new value): Timezone for Linux guests. It must be a valid tz value. You can find yours here.
  • UTC-Clock (new value): It determines that your clock is set to UTC or not. This value should be 0 or 1.
  • Password (old value): The Administrator user’s password. It will be applied in Windows guests, I didn’t find an option to set it as a root password for Linux. (Anyway, is that possible?)
  • Full-Name (old value): The Windows guest owner’s full name.
  • Orgnization-Name (old value): The Windows guest owner’s company.
  • ProductId (old value): The Windows guest’s serial number. Beware! It MUST be a valid product key. If you type something wrong, your VM will not boot up.
  • IP0 (old value): The IP address for the VM.
  • IP0Gateway (old value): The default gateway for the VM.
  • IP0dnsServers (old value): The DNS server (resolver) address(es) for the VM. Separate them with colons, you can set up 1-3 in Linux guests, and 2 in Windows guests.
  • IP0Subnet (old value): The netmask for the VM. CIDR mask cannot be used here!
  • IP0dnsDomain (old value): The DNS suffix for the VM.

Assumed that you installed VSphere Perl SDK in the default place, you should put my VMUtil.pm into /usr/lib/vmware-vcli/apps/AppUtil and vmclone.pl into /usr/lib/vmware-vcli/apps/vm I have uploaded the Perl scripts as TXT files because of security reasons, you should rename them back as it was originally. Make backup for your files first, then overwrite them with my versions.

The last two things you need to set up is the schema (vmclone.xsd) file and the data (vmclone.xml) file. They are pretty simple, almost looks like the originals.

The schema file (vmclone.xsd)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="Cust-Type" type="xs:string"/>
<xs:element name="UTC-Clock">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="0"/>
<xs:enumeration value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Machine-Name" type="xs:string"/>
<xs:element name="Timezone" type="xs:integer"/>
<xs:element name="Linux-Timezone" type="xs:string"/>
<xs:element name="Password" type="xs:string"/>
<xs:element name="Full-Name" type="xs:string"/>
<xs:element name="Orgnization-Name" type="xs:string"/>
<xs:element name="ProductId" type="xs:string"/>
<xs:element name="IP0" type="xs:string"/>
<xs:element name="IP0Gateway" type="xs:string"/>
<xs:element name="IP0dnsServers" type="xs:string"/>
<xs:element name="IP0Subnet" type="xs:string"/>
<xs:element name="IP0dnsDomain" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="Customization-Spec">
<xs:complexType>
<xs:sequence>
<xs:element ref="Cust-Type"/>
<xs:element ref="Machine-Name"/>
<xs:element ref="Timezone"/>
<xs:element ref="Linux-Timezone"/>
<xs:element ref="UTC-Clock"/>
<xs:element ref="Password"/>
<xs:element ref="Full-Name"/>
<xs:element ref="Orgnization-Name"/>
<xs:element ref="ProductId"/>
<xs:element ref="IP0"/>
<xs:element ref="IP0Gateway"/>
<xs:element ref="IP0dnsServers"/>
<xs:element ref="IP0Subnet"/>
<xs:element ref="IP0dnsDomain"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Specification">
<xs:complexType>
<xs:sequence>
<xs:element ref="Customization-Spec" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

The data file when we want to deploy a windows guest (vmclone.xml)

<?xml version="1.0"?>
<Specification>
<Customization-Spec>
<Cust-Type>win</Cust-Type>
<Machine-Name>testwindows</Machine-Name>
<Timezone>095</Timezone>
<Linux-Timezone>Europe/Budapest</Linux-Timezone>
<UTC-Clock>0</UTC-Clock>
<Password>Securepassword</Password>
<Full-Name>Owner name</Full-Name>
<Orgnization-Name>Owner company</Orgnization-Name>
<ProductId>XXXXX-XXXXX-XXXXX-XXXXX-XXXXX</ProductId>
<IP0>1.2.3.4</IP0>
<IP0Gateway>1.2.3.254</IP0Gateway>
<IP0dnsServers>8.8.8.8:8.8.4.4</IP0dnsServers>
<IP0Subnet>255.255.255.0</IP0Subnet>
<IP0dnsDomain>example.com</IP0dnsDomain>
</Customization-Spec>
</Specification>

The data file when we want to deploy a linux guest (vmclone.xml)

<?xml version="1.0"?>
<Specification>
<Customization-Spec>
<Cust-Type>lin</Cust-Type>
<Machine-Name>testlinux</Machine-Name>
<Timezone>095</Timezone>
<Linux-Timezone>Europe/Budapest</Linux-Timezone>
<UTC-Clock>0</UTC-Clock>
<Password>Securepassword</Password>
<Full-Name>Owner name</Full-Name>
<Orgnization-Name>Owner company</Orgnization-Name>
<ProductId>XXXXX-XXXXX-XXXXX-XXXXX-XXXXX</ProductId>
<IP0>1.2.3.4</IP0>
<IP0Gateway>1.2.3.254</IP0Gateway>
<IP0dnsServers>8.8.8.8:8.8.4.4</IP0dnsServers>
<IP0Subnet>255.255.255.0</IP0Subnet>
<IP0dnsDomain>example.com</IP0dnsDomain>
</Customization-Spec>
</Specification>

Put these files somewhere, and try to clone a template into VM. The command is the same as that you should use in vmclone:

/usr/lib/vmware-vcli/apps/vm/vmclone.pl --username "vcuser" --password "vcpass" --server "vcenterip" --datastore "clonefromandtohere" --vmhost "clonefromhost" --vmname "clonefromvm" --vmname_destination "clonetovm" --customize_guest yes --filename "vmclone.xml" --schema "vmclone.xsd"

I have tried this in VSphere 6.0 and 6.5 environment and works great as I expected.