VMware Communities > VMTN > VMware Serverâ„¢ > VMware Server 2 > Documents

Cloning VMs on Linux Hosts with VMWare Server 2.0

VERSION 9 Published

Created on: Oct 3, 2008 2:16 PM by phxrider - Last Modified:  Apr 20, 2009 5:16 PM by phxrider

I think it's a MAJOR pain not having a "clone" feature in VMWare Server without using Virtual Infrastructure, so I made my own Linux shell scripts to do it.

The clone script does a straight clone of a VM. It copies the directory, renames the files and tweaks all the text inside the files by using sed to replace the old name with the new one, then sets all the permissions to what they would be if you created a new VM.

The rename script basically does all but copy it. This is handy for if you tried to rename a directory manually or attempted a manual copy and then found it still shows up under the old name in the web console. Remember this one assumes you have already renamed the directory to the new name and it MUST be the same as what you intend for the new name to be.

Both work the same way... scriptname <old name> <new name>

You can use quotes around the names if there are spaces, I went to great lengths to make sure it would still work with spaces in names (although I personally hate spaces).

Save the scripts to the base directory where your VM's are, "standard" location is "/var/lib/vmware/Virtual Machines". Make executable with chmod +x <scriptname>.

Once ran, add the new/renamed VM to the web console and choose "I copied it" or "I moved it" as appropriate.

These scripts are offered free to everyone to use, modify, or do anything else you want with except take credit for the original version. Absolutely no warranty or guarantee of any kind on the part of anyone that they won't hose your VMs or even your whole system.
(I reasonably sure they won't though http://communities.vmware.com/images/emoticons/wink.gif )


Edit on 2/23/09: If I understand correctly how the split disks work, the 3rd file attached (clone-split-disk.sh) should work for those VM's. I have not tested it myself and will likely not get a chance anytime soon, so I will rely on feedback from the community for any near-future tweaking. This is really just a change to one line (line 34) in the file - in the original we exclude the binary .vmdk file from parsing to find-and-replace since this is how single-disk-file versions are saved; in this version we exclude -f???.vmdk where "?" is a number between 0-9 (i.e. files like -f001.vmdk, -f002.vmdk, etc) while we parse the .vmdk file since with split disks, the .vmdk is a text file describing the -f???.vmdk binary disk files. IF you use a combination of split disks and single-file disks, probably neither one of these will work - maybe in a future version I will be able to merge the two scripts and add logic to tell the disk types apart.

Attachments:
Average User Rating
(1 rating)




Oct 19, 2008 1:37 PM curriertech  says:

Thanks for this! I too think it's silly that there's no clone feature in Server. When I run this it completes without errors but after I add the clone to inventory it shows 'hard disk not valid'. Any ideas? I'm running Ubuntu 8.04 with the latest rev. of Server 2.0.


-Josh.

Oct 20, 2008 9:54 AM phxrider  says: in response to: curriertech

Do you have your output from the script still? It's possible that I didn't account for every possible variation in naming... It checks to make sure the VM is shut down, but is it possible it was running? Also, I don't know if it works on VM's with snapshots, so I would for now recommend deleting any snapshots before attempting a clone.

Things I would check are to make sure all the filenames look right, then:
grep "oldname" .v
grep "newname" .v

You should not get any results from "oldname" except maybe in the binary .vmdk file, which the script leaves alone.

You can also dump the hard disk from the VM settings and re-add it and see if that fixes it.

I also use 2.0 final + Ubuntu 8.04 and haven't seen this problem. One possibility is the renaming of the vmdk could be upsetting it, although that seems unlikely since I've done i lots of times this way now without issue. I can try changing it to rename the .vmdk files using vmware-vdiskmanager -n instead of just renaming with a mv command and see if that does it, I'll have to play with it a little to get that working.

Oct 20, 2008 10:02 AM curriertech  says: in response to: phxrider

I don't have the output, sorry. I was bored since I first tried it and put ESXi on that host to do some testing. I do recall looking for references to the old name and I didn't find any. I will do more testing with this when I can get Server 2.0 on Ubuntu installed on another host.

-Josh.

Oct 20, 2008 10:02 AM phxrider  says: in response to: phxrider

NOTE ON ABOVE: There is supposed to be an asterisk after the .v in the grep lines... the board was kind enough to interpret that as formatting instead :(

I also neglected to mention that you SHOUD get lots of results grepping "newname" since that should have replaced "oldname" in all the files.

Oct 25, 2008 5:57 AM vmetc_rich  says:

Thanks for these scripts! clone_vm.sh worked great on my Ubuntu Server with VMware Server 2.0. Check out my post about creating a W2K3 template for future clones using your script at http://vmetc.com/2008/10/25/linux-scripts-to-clone-and-rename-vmware-server-vms/#more-1176

Nov 18, 2008 10:58 PM asta2762  says:

Hi.

Great scripts!!

One question though.

I have a win2k guest i want to clone on my vmware server 2.0 on Ubuntu 8.04 server.

After i ran the script i get this error: "Hard disk not valid" after the vm-copy was added in the "VMware Infrastructure Web Access".

What can be the problem???

Best regards,

Nicolai

Feb 23, 2009 8:43 AM Rethink  says: in response to: asta2762

On the cloned PC:
If you created the hard drives to be split into 2Gb files, the main .vmdk file has to be edited so that the
#Extent description refers to the 2Gb .vmdk files.
The script does not seem to rename it.

Feb 23, 2009 8:52 AM phxrider  says: in response to: Rethink

Can someone post a description of what this looks like so I can modify the script to include it?

Thanks, Rob

Feb 25, 2009 7:57 AM Rethink  says: in response to: phxrider

Split script works!

Thanks
Troy

Mar 22, 2009 1:11 PM Atolf19  says:

Nice work phxRider.
When your script ends, you have to go to Vim to 'register' the new VM. I made some modifications to your script for shutting down VM and registering in the server after copied using vmrun. Those are the commands I've used:
1.vmrun -T server -h http://10.10.10.1:8222/sdk -u user -p password suspend "[standard] vm/vm.vmx"
2.your script
3.vmrun -T server -h http://10.10.10.1:8222/sdk -u user -p password register "[standard] vm/vm1.vmx"

Now I'd like not to go to the Console web access to answer the question 'Do you copied or Do you move?'. Do you know how to automate this step? Any ideas about grant permission on vm to users (command way)?

Regards

David

Apr 30, 2009 1:17 AM Luca Gallicani  says:

Hi,

I've got an error on cloned virtual disk using the clone-split-disk.sh. I find that there is a vmware tool to rename vdisk (vmware-vdiskmanager)
and i ask you why you do not use it to rename vdisk. Can you help me to modify the script?

Oct 7, 2009 7:16 AM CSS31  says:

Good day,

Thanks to phxrider for this good work.

I have made some improvements of his scripts. You could find clone_vm2.sh and rename_vm.sh on my profile. (I hope the publication of my scripts will work.)

greeting,
--
CSS