Error cloning VDI for VM

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Error cloning VDI for VM

Post 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");
box.RegisterMachine(machine);

//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);
hddprogress.WaitForCompletion(-1);

//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

machsettings.SaveSettings();
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

thanks
Steve
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Error cloning VDI for VM

Post 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
hddprogress.resultCode
hddprogress.errorInfo?

See SDK reference of IProgress
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Error cloning VDI for VM

Post 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.

thx
Steve
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Error cloning VDI for VM

Post 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.
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Error cloning VDI for VM

Post 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.
Post Reply