1

尝试使用以下代码 (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); //好的

谢谢史蒂夫

4

1 回答 1

1

我发现这个问题是由 VirtualBox 保留旧文件的内存的一个已知问题引起的。

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

于 2013-04-08T10:18:52.743 回答