Howto Create Dynamic Creation of Virtual Machines on VMware ESX 3.5

Posted on 2:28 PM by Bharathvn

This illustrates how to create dynamic VM images:

Now that we have looked at what makes up the vmx file, let’s generate some
scripts to dynamically create virtual machines. First, we’ll take a script and
modify it so we can create a virtual machine that will use a golden image as
its base.We’ll then make a couple of changes so we can take advantage of
Altiris in the VM creation.We will then modify the script so that a virtual
machine will be created and then start the VM with the installation CD
mounted to begin the installation.
Code Listing 5.9 shows script that uses a golden image disk file. A golden
image disk file is a fully loaded and patched virtual machine vmx file that has
had sysprep run on it so it can be cloned.

++++++++++++++++++++++++++++++++++++++++++++++++++
WARNING
Please make sure you look through these scripts and make any changes
needed to match your environment. Pay attention to the vmhba path
and double-check these values with the values in your own environment.
Code Listing 5.9 Using a Golden Image Disk File to Dynamically Create a
Virtual Machine
++++++++++++++++++++++++++++++++++++++++++++++++++

#!/bin/bash
#Scripting VMware Power Tools: Automating Virtual Infrastructure
Administration
#Dynamic Creation of a new Virtual Machine using a Golden Image
#Stephen Beaver
#####USER MODIFICATION################
#VMNAME is the name of the new virtual machine
#VMOS specifies which Operating System the virtual machine will have
#GLDIMAGE is the path to the "Golden Image" VMDK file
#DESTVMFS is the path to VMFS partition that the VMDK file
#####################################
VMOS="winNetStandard"
VMMEMSIZE="256"
GLDIMAGE="/vmfs/FHVMFS1/Windows_2003_Standard.vmdk"
DESTVMFS="vmhba0:0:0:10"
#####END MODIFICATION#####
LOG="/var/log/$1.log"
echo "Start of Logging" > $LOG
echo "Importing Golden Image Disk File VMDK" >> $LOG
vmkfstools -i $GLDIMAGE $DESTVMFS:$1.vmdk
echo "Creating VMX Configuration File" >> $LOG
mkdir /home/vmware/$1
exec 6>&1
exec 1>/home/vmware/$1/$1.vmx
# write the configuration file
214 Chapter 5 • Modifying VMs
echo #!/usr/bin/vmware
echo config.version = '"'6'"'
echo virtualHW.version = '"'3'"'
echo memsize = '"'$VMMEMSIZE'"'
echo floppy0.present = '"'TRUE'"'
echo usb.present = '"'FALSE'"'
echo displayName = '"'$1'"'
echo guestOS = '"'$VMOS'"'
echo suspend.Directory = '"'/vmfs/vmhba0:0:0:10/'"'
echo checkpoint.cptConfigName = '"'$1'"'
echo priority.grabbed = '"'normal'"'
echo priority.ungrabbed = '"'normal'"'
echo ide1:0.present = '"'TRUE'"'
echo ide1:0.fileName = '"'auto detect'"'
echo ide1:0.deviceType = '"'cdrom-raw'"'
echo ide1:0.startConnected = '"'FALSE'"'
echo floppy0.startConnected = '"'FALSE'"'
echo floppy0.fileName = '"'/dev/fd0'"'
echo Ethernet0.present = '"'TRUE'"'
echo Ethernet0.connectionType = '"'monitor_dev'"'
echo Ethernet0.networkName = '"'Network0'"'
echo draw = '"'gdi'"'
echo
echo scsi0.present = '"'TRUE'"'
echo scsi0:1.present = '"'TRUE'"'
echo scsi0:1.name = '"'$DESTVMFS:$1.vmdk'"'
echo scsi0:1.writeThrough = '"'TRUE'"'
echo scsi0.virtualDev = '"'vmxlsilogic'"'
echo
# close file
exec 1>&-
# make stdout a copy of FD 6 (reset stdout), and close FD6
exec 1>&6
exec 6>&-
Modifying VMs • Chapter 5 215
echo "VMX Configuration File Created Successfully" >> $LOG
#Change the file permissions
chmod 755 /home/vmware/$1/$1.vmx
#Register the new VM
echo "Registering .vmx Configuration" >> $LOG
vmware-cmd -s register /home/vmware/$1/$1.vmx
echo "VMX Initialization Completed Successfully" >> $LOG

++++++++++++++++++++++++++++++++++++++++++++++++++
NOTE
Notice that the preceding script uses a golden image file that is local to
that machine. If your golden image is located on a network share, you
can easily mount that share and import the file from there. To mount a
network share you can use the following command:
mount -t smbfs //server/share /mnt/smb -o
++++++++++++++++++++++++++++++++++++++++++++++++++
username=username/domain,password=password