Scripting creation of virtual machines in ESX shell

Scripting the creation of virtual machines using VMware power tools is simpler that you might think. This book excerpt tells you how.

Scripting Creation of Virtual Machines in ESX Shell

Scripting the creation of virtual machines is simpler than one might think.We will leverage your newly gained experience from previous sections on utilizing the VMware tools to automate the VM creation process. In the previous section, you manually created a virtual machine configuration file and virtual disk.You then registered the virtual machine with ESX Server.We're going to now essentially take all those commands and steps and automate them in a script that you can run repeatedly and customize to build various types of virtual machines.

The VMware ESX shell is simply the service console operating system. This operating system is a custom version of Linux that VMware created. In Linux, you can create a simple text file to automate commands and then execute it. If you are familiar with DOS batch files, then this will be easy for you. Code Listing 4.6 shows an example of scripted VM creation.

Code Listing 4.6 Scripted VM Creation

##### VM Creation Script #####################################
#Script Version 1.1
#Author David E. Hart
#Date 10-05-06
#
#--------+
# Purpose|
#--------+-----------------------------------------------------
# This script will create a VM with the following attributes;
# Virtual Machine Name = ScriptedVM
# 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
#NVMSIZE is the size of the virtual disk to be created
#--------------------------------------------------------------
###############################################################
### Default Variable settings - change this to your preferences
NVM="ScriptedVM" # Name of Virtual Machine
NVMDIR="ScriptedVM" # Specify only the folder name to be created; NOT the
complete path
NVMOS="winnetstandard" # Type of OS for Virtual Machine
NVMSIZE="4g" # Size of Virtual Machine Disk
VMMEMSIZE="256" # Default Memory Size
### End Variable Declaration
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'"'
www.syngress.com
Building a VM • Chapter 4 151
370_VMware_Tools_04_dummy.qxd 10/12/06 7:28 PM Page 151
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
#Creates 4gb Virtual disk
cd /vmfs/volumes/storage1/$NVMDIR #change to the VM dir
vmkfstools -c $NVMSIZE $NVM.vmdk -a lsilogic
#Register VM
vmware-cmd -s register /vmfs/volumes/storage1/$NVMDIR/$NVM.vmx

The script in Code Listing 4.6 will create a virtual machine that has the following characteristics:

  • A VM called ScriptedVM in a directory named ScriptedVM on storage1
  • A VM that will be assigned 256MB of memory
  • A VM that will have a 4GB SCSI hard drive (lsilogic controller)
  • A VM configured for a Windows 2003 standard operating system
  • A floppy drive assigned, not connected at startup
  • A CD-ROM attached to the ESX server's CD-ROM drive, not connected at startup
  • An Ethernet adapter connected to the VM Network, enabled at startup

The exec commands in the script are system-level commands in Linux to set up the writing to, and saving of, the script file. It redirects the console screen's output to the script file.The use of the echo commands in the script sends the commands to the screen which are redirected to the file for writing.The file is then closed and the virtual configuration file,VMX, is saved.The permissions are changed on the configuration file so any user on ESX can access the virtual machine.Then the script creates the virtual disk and registers the VM with the ESX server.

Use the following process to set up your script on the ESX server:

  • Log in locally or connect to your ESX server remotely.
  • Log in with an ID that has root privileges (see Figure 4.9).

Figure 4.9 Gaining Root Level Access on ESX Server

  • Change to the location or create a location of where you would like to store your scripts (see Figure 4.10).

Figure 4.10 Script Storage Location

  • Type VI newvm.script and press Enter.
  • Press I for insert and type in the script as shown in Code Listing 4.6 (see Figure 4.11).

Figure 4.11Using VI to Create Shell Script

  • Press Esc and then press and hold Shift while pressing ZZ to exit and save.
  • You should now have a file called newvm.script listed. Before you run the script, you must set permissions on it.To do this, type chmod 755 newvm.script (see Figure 4.12).

Figure 4.12 Setting Permissions on Script File

  • Run the script by typing ./newvm.script (see Figure 4.13).

Figure 4.13 The Execution of Shell Script

The virtual machine has now been created and registered with ESX.The next steps are for you to power it on and install the guest operating system. Creating scripts in ESX shell will save you time and effort in creating new virtual machines in your environment.

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.

Dig Deeper on MSP technology services

MicroScope
Security
Storage
Networking
Cloud Computing
Data Management
Business Analytics
Close