Cloning virtual machines utilizing VmPerl Scripts

VmPerl scripting language allows for more flexibility and functionality when creating a virtual machine with VMware power tools. Learn how to use it here.

Cloning Virtual Machines Utilizing VmPerl Scripts

You already know the benefits of cloning, but by utilizing the VmPerl scripting language you can build scripted procurement systems.VmPerl provides you more flexibility and more functionality than shell scripting.This allows you to be more creative in your approach to VM creation.We will add the cloning functionality to the example script in Code Listing 4.7.We will also add a new menu option for cloning and a new subroutine. In addition, we will use the VMware command tool vmkfstools with the -i option for cloning as we did in the previous chapter. Code Listing 4.11 shows the new Perl script with cloning.

Code Listing 4.11 Scripted VM Creation with Perl Utilizing Cloning

#!/usr/bin/perl -w
use VMware::VmPerl;
use VMware::VmPerl::Server;
use VMware::VmPerl::ConnectParams;
#use strict;
##### VM Menu Driven Creation Script with Cloning ############
#Script Version 1.3
#Author David E. Hart
#Date 10-05-06
#
#----------+
#Purpose |
#-----------
# This script presents a menu for automatically building
# virtual machine config files (VMX) and disk files (VMDK)
# This script demonstrates how to automate the setup
# of virtual environments and includes cloning of VMs
#---------------------------+
#Custom Variables Section |
#---------------------------+
#vmname = virtual machine name, will be used for disk as well
#vmmem = amount of memory assigned to VM
#vmos = OS that VM is configured for
#vmdisk = size of VM disk
###################################################
main: # main menu
system("clear");
print " MAIN MENU n";
print "------------------- Virtual Machine Creation --------- n";
print "n";
print "n";
print "n";
print " 1) Create a Custom VM n";
print "n";
print " 2) Create VM's from Defined Templates n";
print "n";
print " 3) View ESX's registered VM's n";
print "n";
print " 4) Clone an Existing VM n";
print "n";
print " 5) Exit n";
print "n";
print " Your Selection - ";
$menuopt = <>; chomp $menuopt; # Get user selection
if ($menuopt == 1) { # Get input for custom VM
system("clear");
print "What do you Want to Name your VM? ";
$vmname = <>; chomp $vmname; # use chomp to remove carriage return
print "How much memory do you want to assign? ";
$vmmem = <>;chomp $vmmem;
print "Do you want to run Windows 2003STD as the OS? (y/n) ";
$vmos = <>;chomp $vmos;
if ($vmos eq "y") {
$vmos = "winNetStandard";
} # Only 2 options for this example
else {
print "Do you want to run Windows 2003Ent as the OS? (y/n) ";
$vmos2 = <>;chomp $vmos2;
if ($vmos2 eq "y") {
$vmos = "winnetenterprise";
}
}
print "What size hard disk do you want to set up (gb)? ";
$vmdisk = <>;chomp $vmdisk;
print "n";
$x = writevmx(); # Subrouting for creating VMX file
if ($x == 1) {
print "VMX file written successfully n";
}
$w = setper(); # Subroutine to set permissions so anyone can
use VM
if ($w == 1) {
print "Permissions set successfully n";
}
$y = createdisk(); # subrouting to create VMDK disk file
if ($y == 1) {
print "Virtual disk created successfully n";
}
$z = registervm(); # subroutine to register VM with ESX
if ($z == 1) {
print "VM registered successfully n";
}
print "Press the ENTER key to continue ...";
$pause = 
  
   ;
goto main
}
if ($menuopt == 2) { # option to display the templates
menu1:
system("clear");
print " Defined Templates n";
print " ----------------- n";
print "n";
print "n";
print " 1) Windows 2003std VM with 256m, 4gb drive n";
print "n";
print " 2) Windows 2003ent VM with 1gig, 8gb drive n";
print "n";
print "n";
print "n";
print "n";
print " Your Selection - ";
$menu1opt = <>; chomp $menu1opt;
if ($menu1opt == 1) {
$vmname = "2003std25m4gb";
$vmmem = "256"; # change and add on similar sections
$vmdisk = "4"; # to create templates for your environment
$vmos = "winnetstandard";
$x = writevmx();
if ($x == 1) {
print "VMX file written successfully na";
}
$w = setper();
if ($w == 1) {
print "Permissions set successfully na";
}
$y = createdisk(); # Call subroutines to create VM's
if ($y == 1) {
print "Virtual disk created successfully na";
}
$z = registervm();
if ($z == 1) {
print "VM registered successfully na";
}
print "Press the ENTER key to continue ...";
$pause = 
   
    ;
goto main
}
if ($menu1opt == 2) {
$vmname = "2003Ent1gb8gb";
$vmmem = "1024";
$vmdisk = "8";
$vmos = "winnetenterprise";
$x = writevmx();
if ($x == 1) {
print "VMX file written successfully na";
}
$w = setper();
if ($w == 1) {
print "Permissions set successfully na";
}
$y = createdisk();
if ($y == 1) {
print "Virtual disk created successfully na";
}
$z = registervm();
if ($z == 1) {
print "VM registered successfully na";
}
print "Press the ENTER key to continue ...";
$pause = 
    
     ;
goto main
}
else {
goto menu1;
}
}
if ($menuopt == 3) { # Use a function of VmPerl to display registered VMs
system("clear");
my ($server_name, $user, $passwd) = @ARGV; # Assume running in ESX
server
my $port = 902; # with appropriate
rights
VMware::VmPerl::ConnectParams::new($server_name,$port,$user,$passwd);
VMware::VmPerl::ConnectParams::new(undef,$port,$user,$passwd);
my $connect_params = VMware::VmPerl::ConnectParams::new();
# Establish a persistent connection with server
my $server = VMware::VmPerl::Server::new();
if (!$server->connect($connect_params)) {
my ($error_number, $error_string) = $server->get_last_error();
die "Could not connect to server: Error $error_number:
$error_stringn";
}
print "nThe following virtual machines are registered:n";
# Obtain a list containing every config file path registered with the
server.
my @list = $server->registered_vm_names();
if (!defined($list[0])) {
my ($error_number, $error_string) = $server->get_last_error();
die "Could not get list of VMs from server: Error $error_number:
".
"$error_stringn";
}
print "$_n" foreach (@list);
# Destroys the server object, thus disconnecting from the server.
undef $server;
print "Press the ENTER key to continue ...";
$pause = 
     
      ;
goto main
}
if ($menuopt == 4) {
system("clear");
print " Clone Existing VM.s n";
print " ------------------- n";
print "n";
print "n";
print " 1) Clone ScriptedVM n";
print "n";
print " 2) Clone ScriptedPerlVM n";
print "n";
print "n";
print "n";
print "n";
print " Your Selection - ";
$menu4opt = <>; chomp $menu4opt;
if ($menu4opt == 1) {
$vmname = "ScriptedPerlCloneVM";
$vmmem = "256"; # change and add on similar sections
$vmdisk = "4"; # to create templates for your environment
$vmos = "winnetstandard";
$vmpath ="/vmfs/volumes/storage1/ScriptedVM/ScriptedVM.vmdk";
$x = writevmx();
if ($x == 1) {
print "VMX file written successfully na";
}
$w = setper();
if ($w == 1) {
print "Permissions set successfully na";
}
$y = clonedisk(); # Call subroutines to create VM's
if ($y == 1) {
print "Virtual disk cloned successfully na";
}
$z = registervm();
if ($z == 1) {
print "VM registered successfully na";
}
print "Press the ENTER key to continue ...";
$pause = 
      
       ;
goto main
}
if ($menu4opt == 2) {
$vmname = "ScriptedPerlVMClone";
$vmmem = "1024";
$vmdisk = "8";
$vmos = "winnetenterprise";
$vmpath ="/vmfs/volumes/storage1/perlvm/ScriptedPerlVM";
$x = writevmx();
if ($x == 1) {
print "VMX file written successfully na";
}
$w = setper();
if ($w == 1) {
print "Permifsions set successfully na";
}
$y = clonedisk();
if ($y == 1) {
print "Virtual disk cloned successfully na";
}
$z = registervm();
if ($z == 1) {
print "VM registered successfully na";
}
print "Press the ENTER key to continue ...";
$pause = 
       
        ;
goto main
}
else {
goto menu1;
}
}
if ($menuopt == 5) {
goto end1
}
sub writevmx { # Subroutine to create VM's VMX config file
# $file = '/vmfs/volumes/storage1/perlvm/perlvm.vmx'; #
Name the file
$file = "/vmfs/volumes/storage1/perlvm/" . $vmname . ".vmx";
open(INFO, ">$file"); # Open for output
print INFO 'config.version = "6" ' . "n";
print INFO 'virtualHW.version = "3" ' . "n";
print INFO 'memsize = "' . $vmmem . '" ' . "n";
print INFO 'floppy0.present = "TRUE" ' . "n";
print INFO 'displayName = "' . $vmname . '" ' . "n";
print INFO 'guestOS = "' . $vmos . '" ' . "n";
print INFO 'ide0:0.present = "TRUE" ' . "n";
print INFO 'ide0:0.deviceType = "cdrom-raw" ' . "n";
print INFO 'ide:0.startConnected = "false" ' . "n";
print INFO 'floppy0.startConnected = "FALSE" ' . "n";
print INFO 'floppy0.fileName = "/dev/fd0" ' . "n";
print INFO 'Ethernet0.present = "TRUE" ' . "n";
print INFO 'Ethernet0.connectionType = "monitor_dev" ' . "n";
print INFO 'Ethernet0.networkName = "VM Network" ' . "n";
print INFO 'Ethernet0.addressType = "vpx" ' . "n";
print INFO 'scsi0.present = "true" ' . "n";
print INFO 'scsi0.sharedBus = "none" ' . "n";
print INFO 'scsi0.virtualDev = "lsilogic" ' . "n";
print INFO 'scsi0:0.present = "true" ' . "n";
print INFO 'scsi0:0.fileName = "' . $vmname . '.vmdk" ' . "n";
print INFO 'scsi0:0.deviceType = "scsi-hardDisk" ' . "n";
close(INFO); # Close the file
}
sub createdisk { # Subroutine to create virtual disk
$cr = "vmkfstools -c " . $vmdisk . "g " . "
/vmfs/volumes/storage1/perlvm/". $vmname . ".vmdk -a lsilogic";
system("$cr");
};
sub clonedisk { # Subroutine to create virtual disk
$cr = "vmkfstools -i " . $vmpath . " " . "
/vmfs/volumes/storage1/perlvm/" . $vmname . "vmdk";
system("$cr");
};
sub registervm { # Subroutine to register VM with ESX server
$rg = "vmware-cmd -s register /vmfs/volumes/storage1/perlvm/" .
$vmname . ".vmx";
system("$rg");
}
sub setper{ # Subroutine to set permission on VMX file
$pm = "chmod 755 /vmfs/volumes/storage1/perlvm/" . $vmname .
".vmx";
system("$pm");
}
end1:

       
      
     
    
   
  

The preceding code is highlighted with the changes necessary to support cloning. When the code is executed, you now have a new menu option #4, for cloning of virtual machines.The code currently clones ScriptedVM.vmdk and ScriptedPerlVM.vmdk, created from previous sections.You can easily modify the code to request the name of the VMs to clone.You could even have the code generate a list of VMs registered with the ESX server and then you would select from this list.The script is provided as an example of how you would go about setting up your own VMs to use as templates and how to automate creating clones of these. Go ahead and expand the sample script to include other options such as "lab setup" where the option clones a series of virtual templates to set up a virtual test environment.

Summary

In this chapter, you learned how to use the built-in command-line tools from VMware—namely, vmkfstools and vmware-cmd—to build and clone virtual machines.You also learned how to use ESX shell scripting to incorporate these tools and automate the VM and cloning process.We showed you how to employ VmPerl for advanced scripting of VM creation and cloning.We then showed you how to use the code examples to build a rough VM creation and cloning architecture for you to expand on.You should now have a good understanding of what you can script on the ESX server as it relates to virtual machine creation.

Use the following table of contents to navigate to chapter excerpts, or click here to view Chapter 4 in its entirety.



Building a VM
  Home: Introduction
 Part 1: Creation of virtual machines utilizing command line-tools
 Part 2: Scripting creation of virtual machines in ESX shell
 Part 3: Scripting creation of virtual machines in Perl Scripts
 Part 4: Cloning virtual machines utilizing ESX Shell scripts
 Part 5: Cloning virtual machines utilizing VmPerl Scripts
ABOUT THE BOOK:   
Scripting VMware Power Tools shows readers scripting techniques using both ESX and Linux commands to automate administrative tasks of ESX Server. It covers VMware ESX Server native tools and discusses in detail the different scripting APIs and how they can be used to provide some very useful, practical, and time-saving tools to manage a virtual infrastructure. From virtual server provisioning to backups, and everything in between, this book is a one-stop shop for virtual tools!Purchase the book from Syngress Publishing.
ABOUT THE AUTHOR:   
Lead author Al Muller is a consultant for Callisma, a wholly owned subsidiary of AT&T. He has been in the IT field since 1995, getting his start as a database administrator in the Navy. In 2002 he began using VMware's GSX Server and within a year was involved in his first virtualization project. Since then, he has been an eager proponent of virtualization technology and has worked on a number of different server consolidation and virtualization projects.

This was first published in February 2007

Dig deeper on Virtualization Technology and Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

MicroscopeUK

SearchCloudProvider

SearchSecurity

SearchStorage

SearchNetworking

SearchCloudComputing

SearchConsumerization

SearchDataManagement

SearchBusinessAnalytics

Close