public IVm CreateMinimalVm(string name, string location) { IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(this._Host); systemSettingData.ElementName = name; systemSettingData.ConfigurationDataRoot = location; systemSettingData.SnapshotDataRoot = location; List <string> stringList = new List <string>(); using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) return((IVm) new Vm(this._Host, managmentService.DefineSystem(systemSettingData, stringList.ToArray(), (IVirtualSystemSettingData)null), this._Logger)); }
public IVm CreateVm(VmCreationInfo info) { IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(this._Host); systemSettingData.ElementName = info.Name; systemSettingData.ConfigurationDataRoot = info.Location; systemSettingData.SnapshotDataRoot = info.Location; systemSettingData.BootOrder = new ushort[4] { (ushort)2, (ushort)1, (ushort)0, (ushort)3 }; if (!string.IsNullOrEmpty(info.VirtualSystemSubType) && info.VirtualSystemSubType != "Microsoft:Hyper-V:SubType:1") { systemSettingData.VirtualSystemSubType = info.VirtualSystemSubType; } List <string> stringList = new List <string>(); using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { IVm vm = (IVm) new Vm(this._Host, managmentService.DefineSystem(systemSettingData, stringList.ToArray(), (IVirtualSystemSettingData)null), this._Logger); vm.CPUCount = (ulong)info.NumCPU; vm.CPUCompatibility = info.CPUCompatibility; if (info.MemoryMB % 2L == 1L) { ++info.MemoryMB; } vm.MemorySize = (ulong)(info.MemoryMB * 1024L) * 1024UL; try { foreach (HVNicInfo nic in info.Nics) { if (string.Compare(nic.VirtualNetwork, "---Discard---", true, CultureInfo.InvariantCulture) != 0) { NicConnectionStatus nicStatus = string.IsNullOrEmpty(nic.VirtualNetwork) ? NicConnectionStatus.Disconnected : NicConnectionStatus.Connected; vm.AddNic(nic.NicType, nic.VirtualNetwork, nicStatus, nic.NicGuid, nic.vLan); } } } catch (Exception ex) { this._Logger.FormatErrorWithException(ex, "Failed to provision the NICs for the vm. Removing the vm"); using (vm) managmentService.DestroySystem(((IVmInternals)vm).ComputerSystem); throw; } return(vm); } }