Virtualbox SDK - CloneTo function

This is for discussing general topics about how to use VirtualBox.
Post Reply
cendalc
Posts: 5
Joined: 16. Sep 2009, 08:42
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win7, Debian

Virtualbox SDK - CloneTo function

Post by cendalc »

Hi,
I'd like to create a small python script that will clone an existing virtual machine (on Windows). I can easily clone existing disk (or snapshot) to a new base image with the following code:

Code: Select all

from vboxapi import VirtualBoxManager
vbm = VirtualBoxManager(None, None)
from vboxapi.VirtualBox_constants import VirtualBoxReflectionInfo
vbc = VirtualBoxReflectionInfo()
clone_hd = vbm.vbox.createHardDisk('', r'm:\virtualbox\clon')
source_hd = vbm.vbox.machines[6].hardDiskAttachments[0].hardDisk
progress = source_hd.cloneTo(clone_hd, vbc.HardDiskVariant_Standard, None)
progress.waitForCompletion(-1)
This creates a new base image from the current snapshot of machine[6] - I can correctly see it in Virtual Media Manager.

However, I'd like to preserve the differencing images of the machine [6]. It looks like cloneTo function should be able to do it but I have not been successful in it. I tried the following code:

Code: Select all

from vboxapi import VirtualBoxManager
vbm = VirtualBoxManager(None, None)
from vboxapi.VirtualBox_constants import VirtualBoxReflectionInfo
vbc = VirtualBoxReflectionInfo()
clone_roothd = vbm.vbox.createHardDisk('', r'm:\virtualbox\root')
source_roothd = vbm.vbox.machines[6].hardDiskAttachments[0].hardDisk.parent
progress = source_roothd.cloneTo(clone_roothd, vbc.HardDiskVariant_Standard, None)
progress.waitForCompletion(-1)
clone_childhd = vbm.vbox.createHardDisk('', r'm:\virtualbox\child')
source_childhd = vbm.vbox.machines[6].hardDiskAttachments[0].hardDisk
progress = source_childhd.cloneTo(clone_childhd, vbc.HardDiskVariant_Diff, clone_roothd)
progress.waitForCompletion(-1)
This creates a base image root with its child image child but the child image has the same size as the root and there is warning in Virtual Media Manager:
Parent UUID of the hard disk child does not match UUID of its parent hard disk stored in the media registry

Image

I set clone_roothd as the root for clone hard disk - UUID {...379} - but the {...880b} UUID is UUID of the source_roothd and not of the clone_roothd.

Is this a bug? Or do I do something wrong? Does exist an example of using cloneTo function somewhere? Thanks.
cendalc
Posts: 5
Joined: 16. Sep 2009, 08:42
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win7, Debian

Re: Virtualbox SDK - CloneTo function

Post by cendalc »

I have not found answer to my question yet...

But I have created a simple script to export/import virtual machines:
http://gui-at.blogspot.com/2009/09/how- ... chine.html
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: Virtualbox SDK - CloneTo function

Post by mpack »

As far as I know, VBox always generates a new UUID when it clones a virtual disk, which will destroy the linkage between snapshots. This is not really fixable, since even if you could keep the UUID you would then have a different problem: conflicting UUIDs. Basically your tool needs to refuse to clone a VM that has snapshots attached.
cendalc
Posts: 5
Joined: 16. Sep 2009, 08:42
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win7, Debian

Re: Virtualbox SDK - CloneTo function

Post by cendalc »

mpack wrote:Basically your tool needs to refuse to clone a VM that has snapshots attached.
The tool does not try to preserve snapshots - the CloneTo function is used to create base image based on the snapshot state. Which is sufficient for me because I don't need to get rid of all snapshots before I can clone the virtual machine.
Post Reply