public void SetMacAddressType(MacAddressType macAddressType) { bool flag = false; switch (macAddressType) { case MacAddressType.Static: if (this._AdapterSettingData.Address != "000000000000") { flag = true; this._AdapterSettingData.StaticMacAddress = true; break; } break; case MacAddressType.Dynamic: flag = true; this._AdapterSettingData.StaticMacAddress = false; this._AdapterSettingData.Address = "000000000000"; break; } if (!flag) { return; } using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { IResourceAllocationSettingData rasd = managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)this._AdapterSettingData).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>(); this._AdapterSettingData.TryDispose(); this._AdapterSettingData = CommonEthernetPortSettingData.CreateFromResourceAllocationSettingData(rasd); } }
public void DeleteVm(IVm vm, DeleteVmOption deleteVmOption) { List <string> list = vm.GetVirtualDiskPaths().ToList <string>(); vm.PowerOff(); using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (vm) managmentService.DestroySystem(((IVmInternals)vm).ComputerSystem); } if (deleteVmOption != DeleteVmOption.DeleteVhds) { return; } foreach (string path in list) { try { if (File.Exists(path)) { File.Delete(path); } } catch (Exception ex) { this._Logger.FormatWarningWithException(ex, "Unable to delete the file \"{0}\" due to: ", (object)path); } } }
public void Disable() { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData == null) { this._Logger.Information("Adapter disconnected."); } else { allocationSettingData.EnabledState = (ushort)3; string[] resourceSettings = new string[1] { ((IWMICommon)allocationSettingData).Object.GetText(TextFormat.WmiDtd20) }; managmentService.ModifyResourceSettings(resourceSettings).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(rasd => rasd.Dispose())); } } } } }
public void Disconnect() { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData == null) { NIC.tracer.TraceInformation("Did not find connection to remove."); } else { using (IResourceAllocationSettingData fromObject = ResourceAllocationSettingData.CreateFromObject(((IWMICommon)allocationSettingData).Object)) { NIC.tracer.TraceInformation("Found connection to remove."); managmentService.RemoveResourceSettings(new IResourceAllocationSettingData[1] { fromObject }); } } } } } }
public HyperVService(string address, NetworkCredential cred, ILogger logger, IHvServiceCallContext callContext) { this._Address = address; this._Cred = cred; this._Logger = logger; this._CallContext = callContext; this._Scope = WMIUtils.ConnectToServer(this._Address, CUtils.CombinUsernameAndDomain(this._Cred.UserName, this._Cred.Domain), this._Cred.Password, "\\root\\virtualization\\v2"); this._VirtSysMgmt = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Scope); this._SystemName = this._VirtSysMgmt.SystemName; }
public void ConnectUsingID(string virtualSwitchID) { NIC.tracer.TraceInformation(string.Format("Connecting using ID {0}", (object)virtualSwitchID)); using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host)) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.Name == virtualSwitchID)).FirstOrDefault <IVirtualEthernetSwitch>()) { using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData1 == null) { NIC.tracer.TraceInformation(string.Format("Did not find existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID)); using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host)) { Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found"); Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchID + "' not found"); allocationSettingData2.Parent = ((IWMICommon)this._AdapterSettingData).Object.Path.Path; NIC.tracer.TraceInformation(string.Format("Switch port parent {0}", (object)allocationSettingData2.Parent)); allocationSettingData2.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; NIC.tracer.TraceInformation(string.Format("Switch port host resource {0}", (object[])allocationSettingData2.HostResource)); NIC.tracer.TraceInformation(string.Format("Adding resource settings to VM: {0}", (object)((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20))); managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose(); } } else { NIC.tracer.TraceInformation(string.Format("Found existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID)); allocationSettingData1.EnabledState = (ushort)2; allocationSettingData1.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; NIC.tracer.TraceInformation(string.Format("Ethernet port allocation setting data for {0} set to {1}", (object)virtualSwitchID, (object)((IEnumerable <string>)allocationSettingData1.HostResource).FirstOrDefault <string>())); NIC.tracer.TraceInformation(string.Format("Modifying resource settings: {0}", (object)((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20))); managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose())); } } } } } } }
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 static INIC Create(IComputerSystem host, IComputerSystem vm, VirtualNicType nicType, string nicGuid, ILogger logger) { ICommonEthernetPortSettingData ethernetPortSettingData; switch (nicType) { case VirtualNicType.Default: case VirtualNicType.Legacy: ethernetPortSettingData = (ICommonEthernetPortSettingData)EmulatedEthernetPortSettingData.GetDefaultLegacyAdapter(host); ethernetPortSettingData.ElementName = "Legacy Network Adapter"; break; case VirtualNicType.Synthetic: ethernetPortSettingData = (ICommonEthernetPortSettingData)SyntheticEthernetPortSettingData.GetDefaultSyntheticAdapter(host); ((ISyntheticEthernetPortSettingData)ethernetPortSettingData).VirtualSystemIdentifiers = new string[1] { nicGuid }; ethernetPortSettingData.ElementName = "Synthetic Network Adapter"; break; default: throw new HyperVException("I don't know about the nic type " + (object)nicType); } ethernetPortSettingData.StaticMacAddress = false; using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(host)) { using (IVirtualSystemSettingData systemSettingData = vm.GetVirtualSystemSettingData()) { IResourceAllocationSettingData adapter = managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)ethernetPortSettingData).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>(); try { ICommonEthernetPortSettingData adapterSettingData = CommonEthernetPortSettingData.GetRelated(systemSettingData).Where <ICommonEthernetPortSettingData>((Func <ICommonEthernetPortSettingData, bool>)(cepsd => adapter.InstanceID == cepsd.InstanceID)).FirstOrDefault <ICommonEthernetPortSettingData>(); if (adapterSettingData == null) { throw new HyperVException("Couldn't find the network adapter we just created"); } return((INIC) new NIC(host, vm, adapterSettingData, logger)); } finally { if (adapter != null) { adapter.Dispose(); } } } } }
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); } }
public void Connect(string virtualSwitchName) { using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host)) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.ElementName == virtualSwitchName)).FirstOrDefault <IVirtualEthernetSwitch>()) { using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData1 == null) { using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host)) { Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found"); Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found"); allocationSettingData2.Parent = ((IWMICommon)this._AdapterSettingData).Object.Path.Path; allocationSettingData2.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose(); } } else { Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found"); allocationSettingData1.EnabledState = (ushort)2; allocationSettingData1.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose())); } } } } } } }
public void SetVLan(ushort vLanId) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { using (IEthernetSwitchPortVlanSettingData portVlanSettingData1 = EthernetSwitchPortVlanSettingData.CreateEthernetSwitchPortVlanSettingData(this._Host)) { if (allocationSettingData == null) { this._Logger.Information("Nic not connected - no changes have been made to Vlan configuration."); } else if ((int)allocationSettingData.EnabledState == 3) { this._Logger.Information("Nic disabled - no changes have been made to Vlan configuration."); } else { using (IEthernetSwitchPortVlanSettingData portVlanSettingData2 = EthernetSwitchPortVlanSettingData.GetRelated(allocationSettingData).FirstOrDefault <IEthernetSwitchPortVlanSettingData>()) { if (portVlanSettingData2 == null) { portVlanSettingData1.AccessVlanId = vLanId; portVlanSettingData1.OperationMode = 1U; managmentService.AddFeatureSettings(allocationSettingData, new string[1] { ((IWMICommon)portVlanSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose())); } else { portVlanSettingData2.AccessVlanId = vLanId; portVlanSettingData2.OperationMode = 1U; managmentService.ModifyFeatureSettings(new string[1] { ((IWMICommon)portVlanSettingData2).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose())); } } } } } } } }
public HostCfgInfo GetCfg(string address, string username, string password) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemManagementServiceSettingData serviceSettingData = VirtualSystemManagementServiceSettingData.GetRelated(managmentService).FirstOrDefault <IVirtualSystemManagementServiceSettingData>()) { ManagementScope server = WMIUtils.ConnectToServer(address, username, password); ManagementObject managementObject = WMIUtils.QueryFirst(server, "Select * From Win32_ComputerSystem"); return(new HostCfgInfo() { Name = this._Host.ElementName, DefaultExternalDataRoot = serviceSettingData.DefaultExternalDataRoot, DefaultVirtualHardDiskPath = serviceSettingData.DefaultVirtualHardDiskPath, CpuNum = (long)(uint)WMIUtils.GetPropertyValue((ManagementBaseObject)managementObject, "NumberOfLogicalProcessors"), Ram = (long)(ulong)WMIUtils.GetPropertyValue((ManagementBaseObject)managementObject, "TotalPhysicalMemory"), Volumes = SystemStateReader.GetVolumes(server), VirtualSwitches = this.GetAllVirtualSwitchesWithUuid().Values.Select <IVirtualSwitch, VirtualSwitchInfo>((Func <IVirtualSwitch, VirtualSwitchInfo>)(vs => vs.GetCfg())).ToList <VirtualSwitchInfo>(), OsInfo = WMIUtils.GetOperatingSystemInfo(server) }); } } }
public void Dispose() { this._VirtSysMgmt.TryDispose(); this._VirtSysMgmt = (IVirtualSystemManagementService)null; GC.SuppressFinalize((object)this); }
public static IEnumerable <IVirtualSystemManagementServiceSettingData> GetRelated(IVirtualSystemManagementService vsms) { ManagementObjectCollection settings = ((IWMICommon)vsms).Object.GetRelated("Msvm_VirtualSystemManagementServiceSettingData"); try { foreach (ManagementObject instance in settings) { yield return((IVirtualSystemManagementServiceSettingData) new VirtualSystemManagementServiceSettingData(instance)); } } finally { if (settings != null) { settings.Dispose(); } } settings = (ManagementObjectCollection)null; }
protected void AddNicToVm(IVm vm, VirtualNicType nicType, string vswitchName, NicConnectionStatus nicStatus, string nicGuid, int VLan) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.Query(this._Host, "ElementName='{0}'", (object)vswitchName).FirstOrDefault <IVirtualEthernetSwitch>()) { if (virtualEthernetSwitch == null) { throw new HyperVException("Virtual Ethernet switch '" + vswitchName + "' not found"); } using (IVirtualSystemSettingData systemSettingData = ((IVmInternals)vm).ComputerSystem.GetVirtualSystemSettingData()) { ICommonEthernetPortSettingData ethernetPortSettingData; if (nicType != VirtualNicType.Legacy) { if (nicType != VirtualNicType.Synthetic) { throw new HyperVException("I don't know about the nic type " + nicType.ToString()); } ethernetPortSettingData = (ICommonEthernetPortSettingData)SyntheticEthernetPortSettingData.GetDefaultSyntheticAdapter(this._Host); ((ISyntheticEthernetPortSettingData)ethernetPortSettingData).VirtualSystemIdentifiers = new string[1] { nicGuid }; ethernetPortSettingData.ElementName = "Synthetic Network Adapter"; } else { ethernetPortSettingData = (ICommonEthernetPortSettingData)EmulatedEthernetPortSettingData.GetDefaultLegacyAdapter(this._Host); ethernetPortSettingData.ElementName = "Legacy Network Adapter"; } using (ethernetPortSettingData) { ethernetPortSettingData.StaticMacAddress = false; using (IResourceAllocationSettingData allocationSettingData1 = managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)ethernetPortSettingData).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>()) { if (nicStatus != NicConnectionStatus.Connected) { return; } using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host)) { using (IVirtualSwitch virtualSwitchByName = this.GetVirtualSwitchByName(vswitchName)) { Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found"); Invariant.ArgumentNotNull((object)virtualSwitchByName, "Virtual switch '" + vswitchName + "' not found"); allocationSettingData2.Parent = ((IWMICommon)allocationSettingData1).Object.Path.Path; allocationSettingData2.HostResource = new string[1] { virtualSwitchByName.Object.Path.Path }; managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose(); } } } } } } } }