Just Enough Developed Infrastructure

Automated creation of vmware server virtual machines

If you frequently want to rebuild a virtual machine, chances are that you grow tired of going through the web interface of vmware server: after your first installation , you have to enter the bios to select booting from PXE to have higher priority then HDD boot. But if you do a PXE install then, you have to make sure that you catch the installation reboot because otherwise it will loop doing the installation.
In the following scenario we will create a vmx and vmdk file usable to start a PXE-boot.
Let's see how we can make this work via the commandline.
Create a VMX template
Just as a side note: there exist alternatives to the vmware workstation and vmware server way of creating vmx files.

  1. Easyvmx : http://www.easyvmx.com
  2. Vmxbuilder : http://www.vmxbuilder.com
  3. VMBuilder : http://dcgrendel.thewaffleiron.net
It turns out that none of the alternatives had the option for Redhat Enterprise Linux 5 - 64 Bit available in it's configuration.
So we used the webgui of vmware server to fit our Centos 5.2 client distribution.The most important settings in the vmx file are:

  • scsi0.virtualDev = "lsilogic"
  • scsi0:0.fileName = "<our filestore>/$VM_NAME/$VM_NAME.vmdk"
  • ethernet0.virtualDev = "e1000"
Check if registered
To verify is there isn't a virtual machine already listed with our name, you can check the registered VM's

vmrun -T server -h https://localhost:8333/sdk -u $VMWARE_USER -p $VMWARE_PASSWORD listRegisteredVM


Stop the virtual Machine
If there is one registered, stop it before we will scratch it.

vmrun -T server -h https://localhost:8333/sdk -u $VMWARE_USER -p $VMWARE_PASSWORD stop "[standard] $VM_NAME/$VM_NAME.vmx"


Unregister the Virtual Machine
Remove it from the inventory

vmrun -T server -h https://localhost:8333/sdk -u $VMWARE_USER -p $VMWARE_PASSWORD stop "[standard] $VM_NAME/$VM_NAME.vmx"


Remove the previous files
remove and recreate the files in the Virtual Machine location = VMWARE_PATH

rm -rf "$VMWARE_PATH/$VM_NAME"

mkdir -p "$VMWARE_PATH/$VM_NAME"


Re-create the virtual disk
First thing to recreate is the disk with disksize $DISK_SIZE and with the controller type that matches the VMX file we created.

vmware-vdiskmanager -c -s $DISK_SIZE -a $DISK_CONTROLLER -t 0 "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmdk"


$DISK_Controller has to correspond to the controller in the VMX. lsilogic for centos
"qemu-img" can be used as alternative, but it does not have an option to specify the controller type
Fix the Mac Addres
When you make a standard VMX, vmware assigns a self generated mac-address. In order to have the machine respond to the PXE-boot, we need to fix the mac address by removing the ethernet0.autogeneratedAddress and setting the addressType to 'static'.
The valid range for Mac-Addresses is : 00:50:56:00:00:00 - 00:50:56:3f:ff:ff
See http://sanbarrow.com/vmx/vmx-network-advanced.html for more detail.
We will get the mac-address from another source where the names and mac-addresses are managed. In our case we get it from the cobbler inventory

MAC_ADDRESS=$(cobbler report --what=systems --name=$VM_NAME | grep "mac address" | cut -d ':' -f 2-| sed -e 's/^ //'g)


cat $VM_TEMPLATE | grep -v "ethernet0.addressType"| grep -v "ethernet0.generatedAddress" > "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"

echo 'ethernet0.addressType = "static"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"

echo 'ethernet0.address = "'$MAC_ADDRESS'"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"


Add remote management
To enable the remote display of a virtual machine we set the correct VNC port , so when the machine boots we can access it on VNC_PORT

echo 'RemoteDisplay.vnc.enabled = "True"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"

echo 'RemoteDisplay.vnc.password = "$VNC_PASSWORD" >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"

echo 'RemoteDisplay.vnc.port = "'$VNC_PORT'"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"


Ignore altered UID
Vmware has a way of detecting changes to virtual machines by using uuid's. For more details see http://communities.vmware.com/thread/189294
In the VM_TEMPLATE we had:
uuid.location = "56 4d 71 ec 89 ef d7 7a-c5 6a d5 e2 8d ec 91 dd" uuid.bios = "56 4d 71 ec 89 ef d7 7a-c5 6a d5 e2 8d ec 91 dd" vc.uuid = "52 ae 03 1b 89 41 c3 06-9f 1a 7c 35 f8 fc 3b 30"
If we would boot our machine, the startup would hang, asking you to confirm the following:
msg.uuid.altered:This virtual machine may have been moved or copied. In order to configure certain management and networking features VMware Server needs to know which. Did you move this virtual machine, or did you copy it? If you don't know, answer "I copied it".

echo 'uuid.action = "create"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"

echo 'msg.autoAnswer = "TRUE"' >> "$VMWARE_PATH/$VM_NAME/$VM_NAME.vmx"


Register the new virtual machine

vmrun -T server -h https://localhost:8333/sdk -u $VMWARE_USER -p $VMWARE_PASSWORD register "[standard] $VM_NAME/$VM_NAME.vmx"


Start the new virtual machine

vmrun -T server -h https://localhost:8333/sdk -u $VMWARE_USER -p $VMWARE_PASSWORD start "[standard] $VM_NAME/$VM_NAME.vmx"


Installation starts over PXE
If the Cdrom and Harddisk do not contain any operating system, the default for a vmware machine is to try to do a PXE boot over the network, which will start the installation and after a reboot you will get your system booting from the HDD install.
You can follow the installation using a VNC client to connect to the virtual machine booting on VNC_PORT