2 Replies Latest reply on Sep 17, 2008 5:01 PM by phxrider

    Cloning VMs on Linux Hosts

    phxrider Novice

      This script is written by me and contributed for all to use and improve or tell me someone already did it


      It's little shell script for one-step cloning of VM's in VMWare Server running under Linux.  So far it works for me*.  I think it accounts for everything necessary to copy a VM to another name and it will not allow you to overwrite an existing VM.  It copies the directory from the source to the destination, then renames all the files with the old VM name to the new one, then uses sed to replace occurrences of the old name with the new one in all the text config files.


      After running, you can just add the new VM to the console, make sure you choose "I copied it" when asked so it will fix all the little things that need to be unique.  The VM should work from there.


      Save the attachment to your "Virtual Machines" directory (default =is '/var/lib/vmware/Virtual Machines') and make it executable (chmod +x <filename>).  Run it like this as root or sudo:

      ./clone_vm.sh <source vm> <new vm>


      Example - to copy a vm called "WindowsXP_Template" to a new machine called WinXP:

      ./clone_vm.sh WindowsXP_Template WinXP

      or use quotation marks around names with spaces:

      ./clone_vm.sh "Windows XP Template" "My New XP VM"


      I went to great lengths and many, many four letter words to make this work with spaces in the file names.


      If you use tabs to complete the names when you type, make sure you get rid of the "/" character at the end of the name.


      *Caveats:  I have not tried this on any VM's with a snapshot yet - I don't know what will happen or if I left anything out that needs to be handled.  The script is written to require the VM to be shut down first, if there are .lck's it will quit and yell at you.  If you use tabs to complete the directory names when you type, make sure you get rid of the "/" character at the end of the name and don't use the escape character format with spaces (like Windows\ XP\ VM) use quoting (like "Windows XP VM").  Actually I haven't tested that so it MIGHT work, but no promises.


      This is my first shell script so there might be a better way to do it, anyone feel free to suggest if you know a better way to do anything in it.

        • 1. Re: Cloning VMs on Linux Hosts
          phxrider Novice

          I removed the script text from the previous post since the board totally hosed it....  just use the attachment, save and make executable or edit as you please.

          • 2. Re: Cloning VMs on Linux Hosts
            phxrider Novice

            Slight updates...


            - Changed the "Usage" message when incorrect number of arguments entered (just for appearance).

            - Changed the copy command to not preserve attributes and the permissions settings to set EVERYTHING (owner and all permissions) at the end of the script.


            Added a 2nd script (change_name.sh) that will do the same thing on a VM that was previously copied.  It pretty much does exactly the same stuff as the clone script except copy the directory - instead it assumes the destination directory already exists.  You just feed it the original name and the new one, and it renames files and replaces all the text strings in the config files (so you need to have already renamed the new VM directory to whatever you supply to the script as the new name or it won't work).  This could be especially useful when you've copied a VM from elsewhere to your server, and it can also fix VM's if you attempted to clone one manually and missed a string or several somewhere (actually this is why this version exists).