This is a follow-up post to VMware/CentOS build where I showed how I built my CentOS/VMWare server step by step. You’ll find that you will want to copy virtual machines to duplicate them. However, the VMware Server Console does not have a way for you to copy them. You have to do it with the CLI and root access to the server. In order to do this quickly, without having to remember what to do, I wrote a script.

This script will copy the VM and change the names in the config file and the harddisk images. I think there are probably more things that this could do and maybe more efficiently. However, I find this script to work for me. Maybe it will for you too.

++++

Begin Script

++++

#/bin/sh

#

# Script used to copy VMs for use

# with VMWare.

#

# Usage:

# copyvm VMTOCLONE NEWVMNAME

#

# Created 20090309

#

# Setup Variables

# Set “VMROOT” to VM directory where VMs reside

#

VMROOT=”/var/lib/vmware/Virtual Machines”

OLDVM=$1

NEWVM=$2

#

# Change to VM Root directory

#

cd “$VMROOT”

clear

echo

echo

echo Copying the VM called $OLDVM to new VM named $NEWVM.

echo This could take awhile…go get some coffee!

echo

echo

#

# Copy Contents of Original VM to New Directory

#

cp -ax $OLDVM $NEWVM

echo Done copying…

echo Fixing Image and file names

echo

echo

#

# Change to new VM’s Directory

#

cd $NEWVM

#

# Use VMWare diskmanager to rename VM’s Disk Image and fix file names

#

/usr/bin/vmware-vdiskmanager -n $OLDVM.vmdk $NEWVM.vmdk

#

# Change vmx file then edit the config

#

mv $OLDVM.vmx $NEWVM.vmx

sed -i “s/$OLDVM.vmdk/$NEWVM.vmdk/” $NEWVM.vmx

# Set Display name if old display name matched Old vmname

sed -i “s/$OLDVM/$NEWVM/” $NEWVM.vmx

echo All done, Enjoy!

echo

echo

++++

End Script

++++