Thursday, September 24, 2009

How to clone VirtualBox virtual machine

VirtualBox is great piece of software for virtualization. I like it more than VMWare or VirtualPC. The downside of it is its lack of easy cloning or copying capabilities. There are none. And because everything in VirtualBox has UUID (guid), pure file copy does not work well.

All advices I have seen were about: create the new virtual machine manually, clone HDD .vdi file with  VBoxManage clonevdi command and attach it to the newly created virtual machine. Manually!?

That's why I've created a simple Python script that cares about exporting and importing virtual machines (Thanks for the VirtualBox 3 SDK!). When exporting, you can select the virtual machine and snapshot you want to export:

The exported machines are stored in exp folder at the script file. The time stamp is added to the machine name for unique identification. When importing, you are offered with the saved virtual machines and you simply choose one. Of course you can copy the exported machine to different computer (having the same VirtualBox versions is recommended).

The script is very simple and targeted to my needs. That means it exports only primary HDD and network adapter 0. It does not export CD/DVD-ROM, Floppy nor Serial ports.

Try it and let me know whether it's helpful: download script (it requires ConfigObj 4).

Update 1. 10. 2009: I struggled running the script on my new Win 7 64-bit system. It complained class is not registered for VirtualBox.Session. The reason was mixing 32-bit and 64-bit programs - see the explanation. If you use 64-bit OS, you have to have installed 64-bit version of Python and pywin32!

Update 11. 11. 2009: I have updated the script to better serve my needs. For example, when restoring virtual machine, you can enter a new name for it. Be carefull when restoring one saved machine to two new ones as you have to chagne MAC address manually.

Unfortunately, the old config file is not compactible with the new version. But I think you can guess from the Python exception what is wrong with the config file and fix it. The old version is still available.

Update 29. 12. 2009: When I upgraded to VirtualBox 3.1, I received KeyError when I run my script ({2158464A-F706-414B-A8C4-FB589DFC6B62}). To get rid of it, delete all from from \Python26\Lib\site-packages\win32com\gen_py\ except