Install VMWare vSphere SDK for Perl to Debian 9

As you can see in my previous post, installing VMware vSphere SDK for Perl 6.5 or 6.7 to a CentOS (or any RHEL) 7 system is a pain in your ass. If you would like to install it any Debian (like Ubuntu) system it would be more difficult for you. This is the reason why I wrote this post, I will guide you through this operation.

First of all, you have no chance to use precompiled modules at all, but in this scenario, you don’t even need to use CPAN and compile modules. All modules can be installed via apt, but you will need to modify some Perl scripts (even the installer). After you did it, your SDK scripts will work without issues.

I have tested this in the latest Debian 9 Stretch (64bit) VM with vSphere SDK for Perl 6.5 and vSphere SDK for Perl 6.7, fresh and clean installs, only the necessary packages was installed, but I’m pretty sure that it will work with the lower Debian 8.x (Jessie) versions or the upper 10.x (sid) versions as well as the Ubuntu latest 16.x (Xenial)

Optional – If you plan to install the SDK for Perl to a VM, then you should install open-vm-tools:

apt install open-vm-tools

First of all, upload the 64bit SDK for Perl to your server and unpack it. Assumed you have uploaded the packed content to your root directory, you can unpack it:

tar -xvzf VMware-vSphere-Perl-SDK-6.x.0-yyyyyyy.x86_64.tar

and enter to the unpacked directory:

cd /root/vmware-vsphere-cli-distrib/bin/

Before you execute the installer, you will need to install the perl modules. This is the complete list, it will install all the dependencies you will need:

apt install perl perl-doc perl-modules-5.24 libssl-dev libxml-libxml-perl libcrypt-ssleay-perl libmodule-build-perl libarchive-zip-perl libpath-class-perl libdata-uuid-perl libsocket6-perl libio-socket-inet6-perl libnet-inet6glue-perl libdevel-checklib-perl libio-captureoutput-perl liblwp-protocol-https-perl

Before you run the installer, first modify it:

/root/vmware-vsphere-cli-distrib/bin/vmware-uninstall-vSphere-CLI.pl

find line 2382 and 2410 (which are exactly the same) and replace this:

{‘module’ => ‘UUID’, ‘version’ => ‘0.27’, ‘path’ => ‘UUID-0.27’},

to that:

{‘module’ => ‘Data::UUID’, ‘version’ => ‘0.27’, ‘path’ => ‘UUID-0.27’},

if you are using Ubuntu, skip this step. Otherwise, find line 2283 and replace this:

if ( direct_command(“cat /etc/*-release | grep -I ubuntu”) || direct_command(“cat /proc/version | grep -I ubuntu”) ) {

to that:

if ( direct_command(“cat /etc/*-release | grep -I debian”) || direct_command(“cat /proc/version | grep -i debian”) ) {

Now you can execute the installer:

perl /root/vmware-vsphere-cli-distrib/bin/vmware-uninstall-vSphere-CLI.pl

and follow the steps. If everything goes well, the SDK for Perl will be installed as desired.

Now you need to modify a perl script, which is located in /usr/share/perl/5.24.1/VMware/SSOConnection.pm

find the line 27 and replace this:

use UUID;

to that:

use Data::UUID;

The installation is completed, but sadly, you will find that this will not work at all. If you try to run any script, you will see an error: Server version unavailable at https://your.vcenter.host/sdk/vimService.wsdl

I try to find the answer, most of them suggested me to disable the ssl hostname verification, or downgrade the libwww-perl, but I did not wanted to do them. After a lot of googleing, I found the answer in VMWare community forum: https://communities.vmware.com/thread/428588 There is a lot of answers and suggestions, but I accepted which suggested me to switch from IO::Socket::SSL to Net::SSL To make this happen, modify /usr/share/perl/5.24.1/VMware/VICommon.pm and after module loads (like in line 26) add this:

BEGIN {
$ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = “Net::SSL”;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
}

Save your changes. This script will be included by all of your other SDK scripts, so this solution will work for you. If you found any better solution to bypass the issue, please write it to me in comments.

That is all, hope this helps to you. 🙂

Install VMWare vSphere SDK for Perl to CentOS 7

Did you ever tried to install the VMware vSphere SDK for Perl 6.5 or 6.7 to a CentOS (or any RHEL) 7 system? Yes, it will be a pain in your ass. In CentOS 6 systems, the installer has precompiled Perl modules and will install it. But in CentOS (or any RHEL) 7 systems, you will need to get and compile them from CPAN. If you try to make this way, you will have to face many issues, but it could be another entire blog post.

Hopefully, I have find a way how can you install all of the Perl modules via yum and you don’t need to use (or even install) CPAN. Then your application which calls the SDK for Perl scripts will work correctly without issues.

I have tested this in the latest CentOS 7 (64bit) VM with vSphere SDK for Perl 6.5 and vSphere SDK for Perl 6.7, fresh and clean installs, only the necessary packages was installed, but I’m pretty sure that it will work with the lower 6.x versions as well and into physical environment. However, I’m sure  this guide will not work exactly with SUSE or Debian distributions, because they have quite different package managers and package versions.

First of all, you will need to install EPEL Release if you did not installed it already:

yum install epel-release

Optional – If you plan to install the SDK for Perl to a VM, then you should install open-vm-tools:

yum install open-vm-tools

There are some pre-requirements which are also needed:

yum install perl openssl-devel

All of the preparations are complete, so you can upload the 64bit SDK for Perl to your server and unpack it. Assumed you have uploaded the packed content to your root directory, you can unpack it:

tar -xvzf VMware-vSphere-Perl-SDK-6.x.0-yyyyyyy.x86_64.tar

and enter to the unpacked directory:

cd /root/vmware-vsphere-cli-distrib/bin/

Before you execute the installer, you will need to install the perl modules. This is the complete list, it will install all the dependencies you will need:

yum install perl-Time-Piece perl-Archive-Zip perl-ExtUtils-Install perl-Path-Class perl-Try-Tiny perl-Crypt-SSLeay perl-version perl-Data-Dumper perl-HTML-Parser uuid-perl perl-XML-SAX perl-XML-LibXML perl-LWP-Protocol-https perl-Socket6 perl-Text-Template perl-IO-Socket-INET6 perl-Net-INET6Glue perl-Module-Build

Before you run the installer, first modify it:

/root/vmware-vsphere-cli-distrib/bin/vmware-uninstall-vSphere-CLI.pl

find line 2382 and 2410 (which are exactly the same) and replace this:

{‘module’ => ‘UUID’, ‘version’ => ‘0.27’, ‘path’ => ‘UUID-0.27’},

to that:

{‘module’ => ‘Data::UUID’, ‘version’ => ‘0.27’, ‘path’ => ‘UUID-0.27’},

Now you can execute the installer:

perl /root/vmware-vsphere-cli-distrib/bin/vmware-uninstall-vSphere-CLI.pl

and follow the steps. If everything goes well, the SDK for Perl will be installed as desired. Only one little modification is required before you execute the (sample) perl scripts. In this file:

/usr/share/perl5/VMware/SSOConnection.pm

find the line 27 and replace this:

use UUID;

to that:

use Data::UUID;

That is all, everything is set, you can use the SDK for Perl.

VMWare vSphere Customize linux guest with a shell script

If you read my previous post about guest optimization, you should see how can you customize your Windows or Linux guest with a modified vmclone.pl script. However, it can’t be used for all cases. It is because vmclone.pl will call the guest customization function in the vSphere environment which is not available for all distributions (Debian, for example) and it has some limitations. (like you cannot set the root password for Linux guests)

Fortunately, there is a solution for this case. The point is that you can use a shell script inside your guest template and after the deployment and boot up, you can call that shell script via Perl SDK. For that, you can use a Perl script written by lamw (as usually, he is one of the best in vSphere automatization :)) and it will fire up the shell script which make your desired settings. Let see how it works step by step:

  1. Select your template which you want to be customizable and convert into VM. I assume you know your login information.
  2. Write the shell script and put it into your root folder. Name it customization.sh You can download my example for Debian from here. Make it executable. (chmod +x /root/customization.sh)
  3. There are five parameters that you need to pass: The guest’s hostname, domain name, IP address, default gateway and the root password. You will need to pass this parameters to the shell script at this order.
  4. Optional, but strongly suggested step: Make a second copy from this VM and try this shell script on there. (Point 9) If it changes these five parameters successfully and removes itself, then you can use it in your live template VM.
  5. Convert back your VM into template.
  6. Download guestOpsManagement.pl from here. This script has written by lamw, so all credits belongs to him. You should read his full documentation about this Perl script’s functions and parameters in his post here.
  7. Put the file into your Perl SDK vm app directory, which is /usr/lib/vmware-vcli/apps/vm for default. Make it executable. (chmod +x /usr/lib/vmware-vcli/apps/vm/guestOpsManagement.pl)
  8. Deploy your template as you did usually, then power it on. Wait few minutes to make sure it is booted up.
  9. Call the guestOpsManagement.pl script:

    /usr/lib/vmware-vcli/apps/vm/guestOpsManagement.pl –username “username@vcenter.server.domain” –password “vCenter password” –server “vcenter.host.domain” –vmname “your-vm-name” –operation startprog –guestusername “root” –guestpassword “CurrentRootPass” –working_dir “/root” –program_path “/root/customization.sh” –program_args “‘newhostname’ ‘newdomain.tld’ ‘newip’ “newdefgw’ ‘newrootpass'”

    and it will make your changes, then remove itself and reboot the guest.

That is all. If everything went well, then your new IP settings and root password has been set into the Linux guest. Anyway, here is some tip and trick about the shell script:

  • In line 17, it will change the existing hostname to the new one, so you need to rewrite Debian-template to the hostname that you provided via the template installation. Otherwise your /etc/hosts file will be broken.
  • The guestOpsManagement.pl script will log in to the guest VM as root, hence you need to know your existing root password and provide it to the script. (as you seen in the example command) Root login must also allowed.
  • Beware! In the (quite) new distribution (like Debian 9 and RHEL 7) udevd and systemd assign predictable interface names (like ens160 and eno 192) instead of ethx. You have two options: Disable it somehow in your template or simply modify my shell script. Of course, the second option is easier, just change the interface names in line 27 and 28.
  • The network settings will not work in RHEL distributions, so my script will need a modification. I might update this post with another examples. 🙂
  • Make sure that all of the necessary commands are installed into your guest template. If sed, or anything else is missing, the script will fail.
  • You can make some additional changes in my script, of course. More options, more customizations are available. 🙂

The shell script will work fine in every distribution (read my notices above) and I tested guestOpsManagement.pl in vSphere 6.5 environtment, but I’m pretty sure that this will work from 5.0 and above versions as well.

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

I must confess that I like the VSphere’s Perl SDK while I set some automatization 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.
  • 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.

 

VMWare vSphere 6.x How to get remote console for your customers without vcenter login

As a hosting provider’s systems engineer, I had the task to give console access to our customer’s virtual machines. Because we built our vSphere network infrastructure to a private network of course, therefore we did not give vCenter access to our customers directly, they have a separated public management server and web interface which connects to this private network. Between the public user interface and private network, we are using the vSphere Perl SDK to manage customer commands. (like deploy vm, poweron, poweroff, …)

I’m pretty sure this scenario is familiar most of you who works in hosting environment, so the only one question is, how to set up the console and give it to the customers? You have plenty of options available, but most of them did not work by the way. 🙂

First and imho the best option was the pre-authenticated console. Lamw has a very good how to about this. I must confess, I have used this solution. The only problem is that this does not work in vCenter 6.0 and above. You can see in the post’s comments, the authentication has been removed by VMWare. and when you try to use pre-authenticated console, it will redirect you back to vCenter login page. I spent many days to find a way to bypass this block, there are some useful information in the VMWare community portal how to get the Jsession ID and use it, but after a lot of trying it leads me to a dead route.

Second option is to use vSphere’s HTML5 console, which can be download via my vmware portal. It is the officially developed console. You can customize it as much as you want, and you can also integrate it into your customer portal… But only in theory… Vittoriop has wrote a how to use this thing, but it did not work for me either. I had plenty of problems with it. First, the console could not have made the connection to my ESXi host, no matter which way and how I try. There was also a lot of javascript errors too which can’t be handled at all, so I realized it is a dead route too.

The third and finally working option for me is the VNC console. The concept to make this work looks like this:

  1. Open some ports for VNC in your ESXi machines
  2. Set up the VNC port, password in your virtual machine
  3. Integrate a web VNC and a proxy (noVNC in my case) into the customer interface code

Okey, let me show you how it works in practice:

First of all, log in to your vCenter server or ESXi hypervisor. Select your ESXi host (hosts and clusters) and under Configure tab, under System box, select Security profile. In the firewall section, select edit, then look for gdbserver and tick the checkbox, then click ok.

This will open the firewall ports from 1000 – 9999 and from 50000 to 50999 which can be used to configure VNC ports in the VM configurations. Optionally, you can set the allowed IP addresses for extra security. Only your customer manager server’s internal IP address need to be allowed.

After you finished it, the firewall ports will opening. Now the virtual machines must be set, to listen for VNC requests. To do this, select one of your VM, be sure it has turned off state.  Select Configure tab, under Settings box, VM Options. Click to edit, drop down to Advanced, then click Edit configuration. You will see the Name and Value boxes in the bottom. Add each of this three name and value pair:

Name: RemoteDisplay.vnc.enabled Value: true
Name: RemoteDisplay.vnc.port Value: 50011
Name: RemoteDisplay.vnc.password Value: yoursecuretpassword

Then click ok to save the settings. Now you can power on your virtual machine.

At this point, you can fire up you VNC client (Tight VNC or anything that you like) and test these settings. Connect to your ESXi server’s IP address and the dedicated port which you set up above in the VM. The VNC client should ask your password, give it. If everything went fine, you will see your VM up and running.

Of course, you can automate this procedure using lamw’s vmAdvSettings.pl script. This script written to vSphere Perl SDK (which I use in unix environtment) but I’m pretty sure you can find example scripts for this task to Java or C# SDK. The script works as any other vSphere Perl SDK script, but I give you some examples for this case:

./vmAdvSettings.pl --username "username@vcenter.server.domain" --password "vCenter password" --server "vcenter.host.domain" --vmname "your-vm-name" --operation "update" --key "RemoteDisplay.vnc.enabled" --value "true"
./vmAdvSettings.pl --username "username@vcenter.server.domain" --password "vCenter password" --server "vcenter.host.domain" --vmname "your-vm-name" --operation "update" --key "RemoteDisplay.vnc.port" --value "50011"
./vmAdvSettings.pl --username "username@vcenter.server.domain" --password "vCenter password" --server "vcenter.host.domain" --vmname "your-vm-name" --operation "update" --key "RemoteDisplay.vnc.password" --value "yoursecretpassword"

Almost done, you just need to download a VNC proxy, which will connect to the VM’s VNC port as you tested it below. Because you need to give something to your customers too, it should be a nice web access VNC coded in HTML5. Good news, noVNC have both in one. Of course, you can use anything else you want, but I made my decision to use noVNC.

In the example, I assume you are using one of the Linux systems (RHEL or Debian based are both good) for your customer manager portal, which has a network access to the public internet where your customers can reach it, and a private network where it can reach you vSphere system. I also assume that your webapp which your customers see via your public domain installed to /var/www/html

First of all, download noVNC from https://github.com/novnc/noVNC/archive/master.zip and unzip it into your webapp dir into a separated folder. (In my example, to: /var/www/html/novnc) noVNC has a websockify proxy which will handle the requests between the HTML5 VNC console and the VM. Start it first:

/var/www/html/novnc/utils/websockify/run -D --web /var/www/html/novnc/ 6011 esxi.host.domain:50011

The last part, connect to the websockify process with noVNC. You need to call the right URL, nothing more: http://customer.manager.public.domain:6011/vnc.html?autoconnect=true&host=customer.manager.public.domain&port=6011&password=yoursecretpassword

That is all, if you see your VM up and running, then you made everything fine.

This solution is tested in 6.0 and 6.5 environments, everything works perfectly. No ticket or jsession id hacks, so I think it is the clearest way to make it.