As we've seen in previous sections,VMware provides you with built-in tools
to accomplish most virtualization tasks. Cloning is the process of copying an
existing virtual machine's virtual disk to a new file for a new virtual machine
to use.The source virtual machine is considered to be the template VM.
Understand that cloning creates an exact replica of the VM's disk contents. It
is very similar to disk imaging. So when the is clone is configured and turned
on in ESX Server it will come up with all the same attributes as the template
VM.To address this, the template VM should be prepared in advanced for
cloning. For Microsoft Windows–based servers, you would use Sysprep to prepare
the template image for cloning. Using template images saves an enormous
amount of time when procuring servers for all types of situations.
Because the template VM has the operating system preinstalled and configured,
setup time for new systems is drastically reduced.
To clone an existing template virtual machine, we will use the VMware
utility vmkfstools.We will use the -i option which instructs vmkfstools to
import an existing template VM's disk file (VMDK) and copy it.The command
syntax is as follows:
Vmkfstools –I /pathtoTemplateVM/template.vmdk
/pathtoDestinationVM/newvm.vmdk
The cloning process does not create a virtual machine configuration file
or register the new virtual machine with ESX Server.We will leverage what
you've learned in the previous sections to modify the ESX shell script from
Code Listing 4.6.We need to modify the part of the script that calls vmkfstools to create
the 4GB virtual disk.We are instead going to use the -i command and clone
an existing virtual disk. If you've been implementing the scripts in the previous
sections, then you will have an example virtual machine disk that we
can use for this section. If not, create a quick empty VM via any of the ESX
GUI methods: Virtual Client, Virtual Infrastructure Client (ESX 3.0),Web,
and so on.
Once you have your source template virtual disk ready, go ahead and edit
the code to support cloning (see Code Listing 4.10).
Code Listing 4.10 ESX Shell Script VM Creation Utilizing Cloning
##### VM Creation Script Utilizing Cloning ####################
#Script Version 1.2
#Author David E. Hart
#Date 10-05-06
#
#--------+
# Purpose|
#--------+-----------------------------------------------------
# This script will create a VM utilizing the cloning option of # the
vmkfstools command tool;
# The New Virtual Machine Configuration will be set as follows
# Virtual Machine Name = ScriptedCloneVM
# Location of Virtual Machine = /VMFS/volumes/storage1/ScriptedVM
# Virtual Machine Type = "Microsoft Windows 2003 Standard"
# Virtual Machine Memory Allocation = 256 meg
#
#----------------------------------------+
#Custom Variable Section for Modification|
#----------------------------------------+---------------------
#NVM is name of virtual machine(NVM). No Spaces allowed in name
#NVMDIR is the directory which holds all the VM files
#NVMOS specifies VM Operating System
#--------------------------------------------------------------
###############################################################
### Default Variable settings - change this to your preferences
NVM="ScriptedCloneVM" # Name of Virtual Machine
NVMDIR="ScriptedCloneVM" # Specify only the folder name to be created;
NOT the complete path
NVMOS="winnetstandard" # Type of OS for Virtual Machine
VMMEMSIZE="256" # Default Memory Size
### End Variable Declaration
www.syngress.com
mkdir /vmfs/volumes/storage1/$NVMDIR # Creates directory
exec 6>&1 # Sets up write to file
exec 1>/vmfs/volumes/storage1/$NVMDIR/$NVM.vmx # Open file
# write the configuration
echo config.version = '"'6'"' # For ESX 3.x the value is 8
echo virtualHW.version = '"'3'"' # For ESX 3.x the value is 4
echo memsize = '"'$VMMEMSIZE'"'
echo floppy0.present = '"'TRUE'"' # setup VM with floppy
echo displayName = '"'$NVM'"' # name of virtual machine
echo guestOS = '"'$NVMOS'"'
echo
echo ide0:0.present = '"'TRUE'"'
echo ide0:0.deviceType = '"'cdrom-raw'"'
echo ide:0.startConnected = '"'false'"' # CDROM enabled
echo floppy0.startConnected = '"'FALSE'"'
echo floppy0.fileName = '"'/dev/fd0'"'
echo Ethernet0.present = '"'TRUE'"'
echo Ethernet0.networkName = '"'VM Network'"' # Default network
echo Ethernet0.addressType = '"'vpx'"'
echo
echo scsi0.present = '"'true'"'
echo scsi0.sharedBus = '"'none'"'
echo scsi0.virtualDev = '"'lsilogic'"'
echo scsi0:0.present = '"'true'"' # Virtual Disk Settings
echo scsi0:0.fileName = '"'$NVM.vmdk'"'
echo scsi0:0.deviceType = '"'scsi-hardDisk'"'
echo
# close file
exec 1>&-
# make stdout a copy of FD 6 (reset stdout), and close FD6
exec 1>&6
exec 6>&-
# Change permissions on the file so it can be executed by anyone
chmod 755 /vmfs/volumes/storage1/$NVMDIR/$NVM.vmx
#Clone existing Template VM's VMDK into current directory
cd /vmfs/volumes/storage1/$NVMDIR #change to the VM dir
vmkfstools -i /vmfs/volumes/storage1/ScriptedVM/ScriptedVM.vmdk $NVM.vmdk
#Register VM
vmware-cmd -s register /vmfs/volumes/storage1/$NVMDIR/$NVM.vmx
When you execute the script, the status of the cloning process will be displayed
(see Figure 4.17).
Figure 4.17 Cloning Process
When the script finishes, you will have a new cloned copy of the template
VM ready for use. Log on to the ESX GUI and validate that the new VM is
registered and available.
The ability to script the cloning of existing template VMs allows you to
pre-stage your virtual environments for your particular needs. For instance,
you could have a Windows Lab of four servers pre-staged. Just run the
WindowsLab script and all four VMs are created and ready to go. In the next
chapter, you will learn how to perform operations on VMs such as starting
and stopping VMs via scripting.
Use the following table of contents to navigate to chapter excerpts, or click here to view Chapter 4 in its entirety.