VirtualBox COM API - 创建克隆VM时出错

时间:2013-04-08 04:45:25

标签: com virtual-machine virtualbox

尝试使用以下代码(C#)创建克隆的VM,但它失败了:

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始终无法说“未创建存储介质”。我在这里错过了什么步骤?

以下代码工作正常,因此看起来创建hddclone存在一些问题。     machsettings.AttachDevice(“IDE”,0,0,DeviceType.DeviceType_HardDisk,hddorig); // OK

感谢 史蒂夫

1 个答案:

答案 0 :(得分:1)

我发现问题是由VirtualBox的已知问题引起的,该问题会保留旧文件的内存。

解决方法是确保克隆的vdi始终具有唯一名称,例如通过在文件名中附加时间戳。