VM creation from CLI

For testing purposes, whilst developing a zeroconf daemon I needed to be able to quickly create some VMs and register them on vSphere. The process of opening infrastructure client and running through the wizard etc. or other methods of creating virtual machines was just too slow. After a bit of searching around and looking at kb.vmware.com, it became apparent that VMware don’t support creating virtual machines from their CLI toolset.

I found script from 2006 that uses commands that don’t even exist anymore, but with a bit of of tweaking will happily create test virtual machines.


# Thanks to http://searchitchannel.techtarget.com/feature/Scripting-creation-of-virtual-machines-in-ESX-shell
# and the Author David E. Hart

echo "VMware Creation Script"
echo "------------------------"

# Check that correct variables are passed in

if [[ "$1" && "$2" && "$3" && "$4" && "$5" ]]; then 

# Create the VM directory and begin creation of configuration
        mkdir /vmfs/volumes/$2/$1 # Creates directory
        exec 6>&1 # Sets up write to file
        exec 1>/vmfs/volumes/datastore1/$1/$1.vmx # Open file

# write the configuration
        echo config.version = '"'8'"' # For ESX 3.x the value is 8
        echo virtualHW.version = '"'9'"' # For ESX 3.x the value is 4
        echo memsize = '"'$5'"'
        echo floppy0.present = '"'TRUE'"' # setup VM with floppy
        echo displayName = '"'$1'"' # name of virtual machine
        echo guestOS = '"'$3'"'
        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 scsi0.present = '"'true'"'
        echo scsi0.sharedBus = '"'none'"'
        echo scsi0.virtualDev = '"'lsilogic'"'
        echo scsi0:0.present = '"'true'"' # Virtual Disk Settings
        echo scsi0:0.fileName = '"'$1.vmdk'"'
        echo scsi0:0.deviceType = '"'scsi-hardDisk'"'
# 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/$2/$1/$1.vmx
#Creates Virtual disk
        cd /vmfs/volumes/$2/$1 #change to the VM dir
        vmkfstools -c $4 $1.vmdk -a lsilogic

#Register VM
        vim-cmd solo/registervm /vmfs/volumes/$2/$1/$1.vmx

echo ""
echo "Usage:"
echo "Due to the lack of shell scripting functionality the following rules have to be followed"
echo "Names cant have spaces in them so use an _"
echo "OS types: centos64Guest rhel6_64Guest other26xLinuxGuest windows8Server64Guest"
echo "create_vm.sh     "
echo ""
echo "e.g."
echo "create_vm.sh test_vm winnetstandard 4g 1024"
echo ""
echo ""
exit 1

One thought on “VM creation from CLI”

Leave a Reply

Your email address will not be published. Required fields are marked *