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 __init__.py.

6 comments:

Unknown said...

Hi Lukaš,
I read you blog about VirtualBox. Please, do you have any idea, how quickly synchronize between two machines only diferences? I need it to move between my work and home PC.
Thank you
Rosťa

Lukáš Čenovský said...

@R: No idea except synchronize them as two normal computers.

Marinus said...

There seems to be a problem with your script.

Python 2.6.3
VirtualBox 3.1.2r56127


Exporting machine 'vap' (current state)...Traceback (most recent call last):
File "C:\live\scripts\VBoxUtil.py", line 336, in
VBoxUtil()
File "C:\live\scripts\VBoxUtil.py", line 126, in __init__
self.main_menu()
File "C:\live\scripts\VBoxUtil.py", line 331, in main_menu
self.export_machine_menu()
File "C:\live\scripts\VBoxUtil.py", line 239, in export_machine_menu
self.export_machine_snapshot_menu()
File "C:\live\scripts\VBoxUtil.py", line 226, in export_machine_snapshot_menu
self.export_machine()
File "C:\live\scripts\VBoxUtil.py", line 183, in export_machine
'self.currentSnapshotMachine.%s' % attr)
File "", line 1, in
File "C:\pf\Python26\lib\site-packages\vboxapi\__init__.py", line 128, in Cust
omGetAttr
return _COMForward['getattr'](self,attr)
File "C:\pf\Python26\lib\site-packages\win32com\client\__init__.py", line 462,
in __getattr__
raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr
))
AttributeError: '' object has no attribute 'port'

Lukáš Čenovský said...

You probably don't have the port filled.

I did a blind fix (just wrapped the problematic part with try-except block) so you can try it.

Anonymous said...

This looks like it might be a great utility, but could you offer some more installation instructions? I'm a win64 user and not having much success. I'm guessing that the VBoxUtil.py and configobj.py files need to reside in VirtualBox\sdk\install directory (best guess based on previous errors indicating they can't find vboxapi which I see in this dir). I have Python 64 bit 2.7.2 installed and I still get errors I don't understand:

C:\Program Files\Oracle\VirtualBox\sdk\install>VBoxUtil.py
Traceback (most recent call last):
File "C:\Program Files\Oracle\VirtualBox\sdk\install\VBoxUtil.py", line 342, i
n
VBoxUtil()
File "C:\Program Files\Oracle\VirtualBox\sdk\install\VBoxUtil.py", line 121, i
n __init__
self.vbm = VirtualBoxManager(None, None)
File "C:\Program Files\Oracle\VirtualBox\sdk\install\vboxapi\__init__.py", lin
e 513, in __init__
exec "self.platform = Platform"+style+"(platparams)"
File "", line 1, in
File "C:\Program Files\Oracle\VirtualBox\sdk\install\vboxapi\__init__.py", lin
e 200, in __init__
from win32com import universal
ImportError: No module named win32com

Lukáš Čenovský said...

@Anonymous You need to install 64-bit pywin32 module and you must have installed VirtualBox SDK. My script can be in any directory then.