1 Reply Latest reply on Jul 7, 2006 8:40 PM by kingneutron

    QnD VM Clone Bash Script

    djflux Enthusiast



      Below is my quick and dirty script to clone virtual machines that I use with VMware Server Beta/RC running on a Linux host.  I KNOW it can be improved (by leaps and bounds), but "it works for me" as something quick when I need to clone a machine.


      The way I use it is to create a core load of a machine/OS the way I want it and then (for Windows VMs) I put newsid (http://www.sysinternals.com/Utilities/NewSid.html) in the image.  Then I use my script to clone it.


      IMPORTANT:  Your source machine SHOULD BE POWERED DOWN for the script to work properly. 


      After you have made a clone of a Windows machine you'll have to use newsid to change the Windows name of the machine so that you won't have duplicate names on your network.


      To use the script change the VMROOT variable to the path in which your VMs reside and then run as follows


      clonevm mygoldimage mynewclone


      The script will look for the first argument to the as the source VM, copy all the files to the new destination VM, and then edit the appropriate files so that the machine will start.  In the above example you will end up with a cloned VM in your $VMROOT called mynewclone.  You can then import the machine into the VMware Server console and start it. 


      VMware Server will ask you about the machine GUID.  I usually create a new one because I'm going to create a whole new machine.  Answer the question as is appropriate for your situation.


      Feel free to use and improve it.  Please post any improvements so that we can all benefit












      cd $VMROOT


      cp -av $ROOT $CLONE


      cd $CLONE


      \# Loop through all named VM files and rename them

      \# to the new clone name


      ls -1 | grep $ROOT | while read VMFILE;


          NEWFILE=`echo $VMFILE | sed -e s/$ROOT/$CLONE/`

          mv $VMFILE $NEWFILE



      \# Fix the vmx and vmdk files with the new clone name


      cp $CLONE.vmx $CLONE.vmx.bak

      cp $CLONE.vmdk $CLONE.vmdk.bak

      sed -e s/$ROOT/$CLONE/g $CLONE.vmx > cloned.vmx

      sed -e s/$ROOT/$CLONE/g $CLONE.vmdk > cloned.vmdk

      cp -f cloned.vmx $CLONE.vmx

      cp -f cloned.vmdk $CLONE.vmdk

        • 1. Re: QnD VM Clone Bash Script
          kingneutron Master

          --Not tested, but $suggestion:




          echo 'o Example usage:'

          echo $0' vm-source-dir destination-dir-for-clone'


          \# Where the destination VM should go under



          \# Can be ' . ' for current-dir; remember, VMs can be

          \# anywhere - not just under one directory


          \[ "$ROOT" = '.' ] && ROOT=$PWD


          \# With the kind help of O'Reilly Bash 2nd edition, p.100

          shortroot=`echo $\{ROOT##/*/}`

          \# We need just the end-directory (short form)

          \# Example: ROOT="/mnt/vmware/virtmachines/win2kpro"

          \# o shortroot Returns: "win2kpro"


          \# Name of the new clone VM




          mkdir -pv $destdir


          cd $ROOT


          #cp -av $ROOT $CLONE

          ##cp -apRv $ROOT $CLONE

          time tar cpf - *|(cd $destdir; tar xpvf -)


          cd $destdir


          \# Loop through all named VM files and rename them

          \# to the new clone name

          \# # Adding /bin gets around any aliases == KN

          \# # This part may need some $work, PLEASE TEST == KN

          /bin/ls -1 | grep $shortroot | while read VMFILE;


          NEWFILE=`echo $VMFILE | sed -e s/$shortroot/$CLONE/`

          /bin/mv -f -v $VMFILE $NEWFILE



          \# Fix the vmx and vmdk files with the new clone name


          /bin/cp -pv $CLONE.vmx $CLONE.vmx.bak

          /bin/cp -pv $CLONE.vmdk $CLONE.vmdk.bak

          sed -e s/$shortroot/$CLONE/g $CLONE.vmx > cloned.vmx

          sed -e s/$shortroot/$CLONE/g $CLONE.vmdk > cloned.vmdk

          /bin/cp -fv cloned.vmx $CLONE.vmx

          /bin/cp -fv cloned.vmdk $CLONE.vmdk


          /bin/ls -al



          Message was edited by: