When I need to create new virtual machines (for a test lab for instance), I do not want to spend a lot of time on deployment. For customers who use VMWare GSX without VirtualCenter, I use a simple batch file to roll out virtual machines automatically.
Before you can use the batch file, you need a sysprepped virtual machine. This article presumes that you know how sysprep works. On the virtual machine's disk, c:\sysprep\sysprep.inf will be the answer file that provides all answers to the mini-setup wizard. The mini-setup is run when you deploy the virtual machine and start it for the first time. You should make mini-setup fully unattended.
Of course, I want to provide the computername automatically during deployment. In the sysprep.inf file, do not fill in a computername behind Computername=. So the [UserData] section should be like:
[Userdata]
...
Computername=
...
When the machine has been sysprepped and is shutdown, you have a template to create other machines from. In the template directory, you should modify the vmx file as required. You should remove the displayName entry because we will set the display name during deployment. You should also remove uuid settings so VMWare can create a new uuid for your new virtual machines automatically without asking questions.
So suppose you have a template directory e:\vm\W2003Template. The directory has two files: w2003.vmdk and w2003.vmx.
To create a new virtual machine based on this template, I use the batch file with one parameter: the name of the new virtual machine. For instance:
deploy.cmd w2003srv
The batch file contains some variables that point to the template directory, the directory where new virtual machines should be created and so on...
The batch file then does the following things:
- A new folder called w2003srv is created under a directory you specify in the batch file.
- The w2003.vmx file from the template is copied to the new folder (e:\vm\w2003srv) as w2003srv.vmx.
- The displayName is set inside w2003srv.vmx to w2003srv.
- The w2003.vmdk file is copied from the template directory to the new folder.
- w2003.vmdk is mounted on V: and v:\sysprep\sysprep.inf is modified. Computername= is replaced with Computername=w2003srv.
To replace Computername= with something else, I used munge.exe from one of the Windows resource kits. Munge requires a munge script. That is just a file with one line that contains the value to search and replace. The munge script is created by the batch file.
To mount a vmdk on a drive letter, I used vmware-mount.exe. This is a download from the VMWare site.
That's it. You now have a new virtual machine based on a specific template. The computername of this virtual machine is automatically set in sysprep.inf. When you start the new vm and mini-setup is fully automated, the new machine will be up and running without having to provide answers to the setup process.
This is a very simple solution and was created out of need. It could be further enhanced by using VMWare APIs to automatically add this virtual machine to the inventory, maybe start the virtual machine automatically after creation, and so on. Maybe next time! ![]()
UPDATE: well, what about providing the information to register and start the virtual machine now? It is really easy, thanks to the sample vmware-cmd script that VMWare provides. That script can be found in c:\Program Files\VMware\VMware VmPerl Scripting API. Check the attached reg-start.cmd for a sample script that registers and starts a vm automagically.



