Page 1 of 1

Error cloning VDI for VM

PostPosted: 8. Apr 2013, 09:18
by rousseauhk
I'm trying the following code (C#) to create a cloned VM, but it fails:

VirtualBox.VirtualBox box = new VirtualBox.VirtualBox();

//create a new machine
IMachine machine = box.CreateMachine(null, "MyClone", null, null, "forceOverwrite=1");

//Lock machine for editing
machine.LockMachine(session, LockType.LockType_Write);
IMachine machsettings = session.Machine;

//clone the disk image from an existing vdi (source VDI is not being locked/used)
IMedium hddorig = box.OpenMedium("c:\\tmp\\VDI\\dsl-4.4.10-x86.vdi", DeviceType.DeviceType_HardDisk, AccessMode.AccessMode_ReadOnly, 0);
IMedium hddclone = box.CreateHardDisk("VDI", "c:\\tmp\\VDI\\clone.vdi");
IProgress hddprogress = hddorig.CloneTo(hddclone, 0, hddorig);

//attach disk image to machine
machsettings.AddStorageController("IDE", StorageBus.StorageBus_IDE);
machsettings.AttachDevice("IDE", 0, 0, DeviceType.DeviceType_HardDisk, hddclone); //fails - Storage for the medium 'c:\tmp\VDI\clone.vdi' is not created


AttachDevice always fails saying "Storage for the medium is not created". What step am I missing here?

If I just AttachDevice(... hddorig), it works fine - so it looks like there is some issue creating hddclone


Re: Error cloning VDI for VM

PostPosted: 8. Apr 2013, 10:01
by noteirak
You do not check for hddprogress.resultCode and hddprogress.completed, so you assume everything went fine, but clearly it didn't.
What is the value of

See SDK reference of IProgress

Re: Error cloning VDI for VM

PostPosted: 8. Apr 2013, 11:29
by rousseauhk
Thanks for the pointer.. ErrorInfo gives me:

Cannot register the hard disk 'c:\tmp\VDI\clone.vdi' {53a94db5-a224-4a5d-9ca7-335bd1f68892} because a hard disk 'c:\tmp\VDI\clone.vdi' with UUID {89f8999e-f022-4a02-97e7-a7e2fff9b692} already exists

I deleted the previous clone.vdi before I ran the code, and the file doesnt get created until the CloneTo() method runs - so no idea why it thinks the file already exists.
FWIW the original file is a ~256mb linux VDI, but the clone.vdi that is created is only ~45k.


Re: Error cloning VDI for VM

PostPosted: 8. Apr 2013, 12:04
by mpack
This matches the known behaviour of VirtualBox, which is that the VBoxSVC background process retains a memory of media even after it is deleted, and will refuse to allow "conflicting" registrations during that time. VBoxSVC must be shut down and restarted to get around this.

See BugTracker ticket 10311.

Re: Error cloning VDI for VM

PostPosted: 8. Apr 2013, 12:17
by rousseauhk
Great thanks. I tried with "clone2.vdi" and the code works fine now.

I think a longer-term workaround would be to add a date/timestamp to the filename to make sure it is always unique.