Keeping with my recent trend of ESXi howto’s I thought i would through something up on cloning VM’s. Despite being one of the most useful features of using virtual hardware for labs it at first seems completely absent from ESXi.
But though not quite as easy as a right click it is possible.
Note: Though this can be done from the console by pressing ALT-F1 and typing “unsupported ” you may want to consider enabling SSH with this hack first.
- First thing you need to do once you have console access is find the root of your datastore for most this will be “/vmfs/volumes/<DATASTORE NAME>”.
- Once found make a new folder to house the newly cloned VM using “mkdir <DIRECTORY NAME>”.
- Next jump in to the folder for the VM you are looking to clone, you are looking for the name of the .vmdk file it will usually be <VM NAME>-000001.vmdk.
- Now that we have everything in place we can clone the VM with “vmkfstools -i <PATH TO SOURCE vmdk FILE> <PATH TO DESTINATION vmdk>” you will see a percent counter work up to 100%.
- Once the disk cloning is complete head back to the infrastructure client to create a new VM.
- You must choose Custom this will allow you to use the existing disk you just cloned.
- Set every thing else as necessary until you get to “Select A Disk” here use “Use An Existing Disk” and Next.
- On the next page you will be able to browse for the disk you cloned in the datastore select it and wrap up the VM creation.
Tidbit: placing “-d thin” between the source and destination disk paths when cloning will create a thin disk.
If you want to learn more about vmkfstools take a look here for some detailed usage
Also comments and questions are welcome.
Here’s shell script for cloning in ESXi 4.0
———————————————————————-
SOURCEVM=$1
TARGETVM=$2
DATASTORE=/vmfs/volumes/VMs
#create new VM directory
mkdir $DATASTORE/”$2″
#migrate disk image using built in tool
vmkfstools -i $DATASTORE/”$1″/”$1″.vmdk $DATASTORE/”$2″/”$2″.vmdk
#copy ancillary files to create a complete VM
cp $DATASTORE/”$1″/”$1″.vmx $DATASTORE/”$2″/”$2″.vmx
cp $DATASTORE/”$1″/”$1″.vmxf $DATASTORE/”$2″/”$2″.vmxf
cp $DATASTORE/”$1″/”$1″.vmsd $DATASTORE/”$2″/”$2″.vmsd
cp $DATASTORE/”$1″/”$1″.nvram $DATASTORE/”$2″/”$2″.nvram
#change VM parameters to have new VM name
sed ‘s/’”$1″‘/’”$2″‘/g’ $DATASTORE/”$2″/”$2″.vmx > TMPFILE && mv TMPFILE $DATASTORE/”$2″/”$2″.vmx
sed ‘s/’”$1″‘/’”$2″‘/g’ $DATASTORE/”$2″/”$2″.vmxf > TMPFILE && mv TMPFILE $DATASTORE/”$2″/”$2″.vmxf
#remove uuid and MAC to ensure unique ones are created when the VM is started
sed ‘/uuid.location/d’ $DATASTORE/”$2″/”$2″.vmx > TMPFILE && mv TMPFILE $DATASTORE/”$2″/”$2″.vmx
sed ‘/uuid.bios/d’ $DATASTORE/”$2″/”$2″.vmx > TMPFILE && mv TMPFILE $DATASTORE/”$2″/”$2″.vmx
sed ‘/ethernet0.generatedAddress/d’ $DATASTORE/”$2″/”$2″.vmx > TMPFILE && mv TMPFILE $DATASTORE/”$2″/”$2″.vmx