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:
- Select your template which you want to be customizable and convert into VM. I assume you know your login information.
- Write the shell script and put it into your root folder. Name it customization.sh You can download my example for Debian at the end of this post. Make it executable. (chmod +x /root/customization.sh)
- 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.
- 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.
- Convert back your VM into template.
- 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.
- 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)
- Deploy your template as you did usually, then power it on. Wait few minutes to make sure it is booted up.
- Call the guestOpsManagement.pl script which will make your changes, then remove itself and reboot the guest:
/usr/lib/vmware-vcli/apps/vm/guestOpsManagement.pl --username "email@example.com" --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'"
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.
The customization.sh script example for Debian (and Ubuntu) distributions:
#!/bin/bash # # Debian based script after the VM deployment # (C) by Mr.Zsolt # Input variables HOSTNAME=$1 DOMAINSUFFIX=$2 IP=$3 GATEWAY=$4 NEWPASS=$5 # Change hostname echo $HOSTNAME > /etc/hostname # Change domain sed -i '/Debian-template/c\'"$IP"'\t'"$DOMAINSUFFIX"'\t'"$HOSTNAME"'' /etc/hosts # Make the interfaces file echo "# This file describes the network interfaces available on your system" > /etc/network/interfaces echo "# and how to activate them. For more information, see interfaces(5)." >> /etc/network/interfaces echo "" >> /etc/network/interfaces echo "# The loopback network interface" >> /etc/network/interfaces echo "auto lo" >> /etc/network/interfaces echo "iface lo inet loopback" >> /etc/network/interfaces echo "" >> /etc/network/interfaces echo "allow-hotplug eth0" >> /etc/network/interfaces echo "iface eth0 inet static" >> /etc/network/interfaces echo " address $IP" >> /etc/network/interfaces echo " netmask 255.255.255.0" >> /etc/network/interfaces echo " gateway $GATEWAY" >> /etc/network/interfaces # Change root password usermod --password $(echo $NEWPASS | openssl passwd -1 -stdin) root # Remove myself rm -rf /root/customization.sh # Reboot when done reboot