CyberGround

VMWare vSphere Customize linux guest with a shell script

Home Blog Categories Links

VMWare vSphere Customize linux guest with a shell script

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

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. 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 which will make your changes, then remove itself and reboot the guest: bash /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'"

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.