DeleteSan( string poolId) { Console.WriteLine("Deleting Virtual SAN - {0} ...", poolId); ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject rpConfigurationService = FibreChannelUtilities.GetResourcePoolConfigurationService(scope)) using (ManagementBaseObject inParams = rpConfigurationService.GetMethodParameters("DeletePool")) { inParams["Pool"] = FibreChannelUtilities.GetResourcePoolPath(scope, poolId); using (ManagementBaseObject outParams = rpConfigurationService.InvokeMethod( "DeletePool", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope, true, true); } } Console.WriteLine("Successfully deleted Virtual SAN - {0}", poolId); }
ModifySanResources( string poolId, string[] newHostResources) { if (newHostResources == null) { Console.WriteLine("Deleting all resources assigned to Virtual SAN {0} ...", poolId); } else { Console.WriteLine("Modifying resources assigned to Virtual SAN {0} ...", poolId); } ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject rpConfigurationService = FibreChannelUtilities.GetResourcePoolConfigurationService(scope)) { string poolPath = FibreChannelUtilities.GetResourcePoolPath(scope, poolId); string[] parentPoolPathArray = new string[1]; parentPoolPathArray[0] = FibreChannelUtilities.GetResourcePoolPath(scope, null); string[] newPoolAllocationSettingsArray = new string[1]; newPoolAllocationSettingsArray[0] = FibreChannelUtilities.GetNewPoolAllocationSettings(scope, poolId, newHostResources); using (ManagementBaseObject inParams = rpConfigurationService.GetMethodParameters("ModifyPoolResources")) { inParams["ChildPool"] = poolPath; inParams["ParentPools"] = parentPoolPathArray; inParams["AllocationSettings"] = newPoolAllocationSettingsArray; using (ManagementBaseObject outParams = rpConfigurationService.InvokeMethod( "ModifyPoolResources", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope, true, true); } } } if (newHostResources == null) { Console.WriteLine("Successfully deleted all resources assigned to Virtual SAN {0}", poolId); } else { Console.WriteLine("Successfully modified resources assigned to Virtual SAN {0}", poolId); } }
GetDefaultSyntheticFcAdapter( ManagementScope scope) { ObjectQuery query = new ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE " + "ResourceSubType = 'Microsoft:Hyper-V:Synthetic FibreChannel Port' " + "AND Primordial = True"); using (ManagementObjectSearcher queryExecute = new ManagementObjectSearcher(scope, query)) using (ManagementObject resourcePool = WmiUtilities.GetFirstObjectFromCollection(queryExecute.Get())) { return(FibreChannelUtilities.GetDefaultObjectFromResourcePool(resourcePool, scope)); } }
DeleteVirtualFcPort( string virtualMachineName, WorldWideName wwnA, WorldWideName wwnB) { Console.WriteLine("Removing virtual FC port(s) with following WWNs from VM {0}:", virtualMachineName); Console.WriteLine("\tVirtualPortWWPN {0}, VirtualPortWWNN {1}", wwnA.PortName, wwnA.NodeName); Console.WriteLine("\tSecondaryWWPN {0}, SecondaryWWNN {1}", wwnB.PortName, wwnB.NodeName); ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope)) // // Find the virtual machine we want to connect. // using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(virtualMachineName, scope)) { string[] portsToRemove = GetResourcesToRemove(managementService, virtualMachine, wwnA, wwnB); if (portsToRemove.Length == 0) { Console.WriteLine("The specified world wide names were not found in VM {0}", virtualMachineName); return; } using (ManagementBaseObject inParams = managementService.GetMethodParameters("RemoveResourceSettings")) { inParams["ResourceSettings"] = portsToRemove; using (ManagementBaseObject addAdapterOutParams = managementService.InvokeMethod("RemoveResourceSettings", inParams, null)) { WmiUtilities.ValidateOutput(addAdapterOutParams, scope, true, true); } } } Console.WriteLine("Successfully deleted the virtual FC port on VM {0}.", virtualMachineName); }
ConfigWwnGenerator( string minWwpn, string maxWwpn, string newWwnn) { Console.WriteLine("Trying to configure the WWN generator with:"); Console.WriteLine("\tMinimumWWPNAddress = {0}", minWwpn); Console.WriteLine("\tMaximumWWPNAddress = {0}", maxWwpn); if (newWwnn == null) { Console.WriteLine("\tThe CurrentWWNNAddress will not be modified."); } else { Console.WriteLine("\tCurrentWWNNAddress = {0}", newWwnn); } ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject service = WmiUtilities.GetVirtualMachineManagementService(scope)) using (ManagementObject settings = WmiUtilities.GetVirtualMachineManagementServiceSettings(scope)) using (ManagementBaseObject inParams = service.GetMethodParameters("ModifyServiceSettings")) { settings["MinimumWWPNAddress"] = minWwpn; settings["MaximumWWPNAddress"] = maxWwpn; if (newWwnn != null) { settings["CurrentWWNNAddress"] = newWwnn; } inParams["SettingData"] = settings.GetText(TextFormat.WmiDtd20); using (ManagementBaseObject outParams = service.InvokeMethod("ModifyServiceSettings", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope, true, true); } } Console.WriteLine("Successfully configured the WWN Generator."); }
CreateSan( string poolId, string notes, string[] hostResources) { Console.WriteLine("Creating Virtual SAN - {0} ...", poolId); ManagementScope scope = FibreChannelUtilities.GetFcScope(); string resourcePoolSettingData = FibreChannelUtilities.GetSettingsForPool(scope, poolId, notes); // // Fibre Channel Connection Resource Pools have only 1 parent, the primordial pool. // string[] parentPoolPathArray = new string[1]; parentPoolPathArray[0] = FibreChannelUtilities.GetResourcePoolPath(scope, null); string[] resourceAllocationSettingDataArray = new string[1]; resourceAllocationSettingDataArray[0] = FibreChannelUtilities.GetNewPoolAllocationSettings(scope, poolId, hostResources); using (ManagementObject rpConfigurationService = FibreChannelUtilities.GetResourcePoolConfigurationService(scope)) using (ManagementBaseObject inParams = rpConfigurationService.GetMethodParameters("CreatePool")) { inParams["PoolSettings"] = resourcePoolSettingData; inParams["ParentPools"] = parentPoolPathArray; inParams["AllocationSettings"] = resourceAllocationSettingDataArray; using (ManagementBaseObject outParams = rpConfigurationService.InvokeMethod( "CreatePool", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope, true, true); } } Console.WriteLine("Successfully Created Virtual SAN: {0}", poolId); }
ExecuteSample( string[] args) { if (args.Length == 0 || args[0] == "/?") { Console.WriteLine("Usage: ModifySanPorts <SanName> [WWPN WWNN]*"); return; } try { string sanName = args[0]; List <string> switchPaths = new List <string>(); if (args.Length >= 3 && args.Length % 2 == 1) { for (int index = 2; index < args.Length; index += 2) { WorldWideName wwn = new WorldWideName(); wwn.PortName = args[index - 1]; wwn.NodeName = args[index]; // // Convert the WWN to the path of the corresponding Virtual FC Switch to be used // as HostResources for the ResourcePool. // switchPaths.Add(FibreChannelUtilities.GetHostResourceFromWwn(wwn)); } } else if (args.Length != 1) { Console.WriteLine("Usage: ModifySanPorts sanName [WWPN WWNN]*"); return; } ModifySanResources(sanName, switchPaths.ToArray()); } catch (Exception ex) { Console.WriteLine("Failed to modify san ports. Error message details:\n"); Console.WriteLine(ex.Message); } }
BuildSanInfo() { ManagementObjectCollection fcPools = WmiUtilities.GetResourcePools(FibreChannelUtilities.FcConnectionResourceType, FibreChannelUtilities.FcConnectionResourceSubType, FibreChannelUtilities.GetFcScope()); List <SanInfo> sanInfoList = new List <SanInfo>(); foreach (ManagementObject san in fcPools) { // // Virtual SANs are resource pools that are children of the primordial pool. // if (!(bool)san["Primordial"]) { SanInfo sanInfo = new SanInfo(); sanInfo.SanName = (string)san["PoolId"]; // // The resources assigned to the Virtual SAN are present in the HostResource // property of the associated Resource Allocation Setting Data. // using (ManagementObjectCollection poolRasds = san.GetRelated("Msvm_FcPortAllocationSettingData")) using (ManagementObject allocationSettingData = WmiUtilities.GetFirstObjectFromCollection(poolRasds)) { sanInfo.HostResources = (string[])allocationSettingData["HostResource"]; } sanInfoList.Add(sanInfo); } } if (sanInfoList.Count == 0) { Console.WriteLine("No Virtual SANs detected in the system."); } return(sanInfoList); }
ExecuteSample( string[] args) { if (args.Length < 2 || (args.Length > 0 && args[0] == "/?")) { Console.WriteLine("Usage: CreateVirtualFcPort <VM Name> <SAN Name> [WWPN-A WWNN-A WWPN-B WWNN-B]"); Console.WriteLine(); Console.WriteLine("Note: The 2 sets of WWNs for the virtual port are optional and if omitted"); Console.WriteLine(" will be auto generated using the WWPN Generator and the CurrentWWNNAddress."); return; } try { WorldWideName wwnA = new WorldWideName(); WorldWideName wwnB = new WorldWideName(); if (args.Length == 6) { wwnA.PortName = args[2]; wwnA.NodeName = args[3]; wwnB.PortName = args[4]; wwnB.NodeName = args[5]; } else { Console.WriteLine("Auto Generating WWNs for Virtual FC Port to be added..."); FibreChannelUtilities.GenerateWorldWideNames( FibreChannelUtilities.GetFcScope(), ref wwnA, ref wwnB); } CreateVirtualFcPort(args[0], args[1], wwnA, wwnB); } catch (Exception ex) { Console.WriteLine("Failed to create a virtual FC port. Error message details:\n"); Console.WriteLine(ex.Message); } }
ExecuteSample( string[] args) { if (args.Length < 3 || (args.Length > 0 && args[0] == "/?")) { Console.WriteLine("Usage: CreateSan <SanName> <[WWPN WWNN]+> [SanNotes]"); return; } try { string sanName = args[0]; string sanNotes = @"Notes for virtual SAN - " + sanName; List <string> hostResources = new List <string>(); if (args.Length % 2 == 0) { sanNotes = args[args.Length - 1]; } for (int index = 2; index < args.Length; index += 2) { WorldWideName wwn = new WorldWideName(); wwn.PortName = args[index - 1]; wwn.NodeName = args[index]; // // Convert the WWN to the path of the corresponding Virtual FC Switch to be used // as HostResources for the ResourcePool. // hostResources.Add(FibreChannelUtilities.GetHostResourceFromWwn(wwn)); } CreateSan(sanName, sanNotes, hostResources.ToArray()); } catch (Exception ex) { Console.WriteLine("Failed to create san. Error message details:\n"); Console.WriteLine(ex.Message); } }
BuildPortInfo() { List <FcPortInfo> portInfoList = new List <FcPortInfo>(); ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementClass portClass = new ManagementClass("Msvm_ExternalFcPort")) { portClass.Scope = scope; using (ManagementObjectCollection fcPorts = portClass.GetInstances()) { foreach (ManagementObject port in fcPorts) { WorldWideName wwn = new WorldWideName(); wwn.NodeName = (string)port["WWNN"]; wwn.PortName = (string)port["WWPN"]; FcPortInfo portInfo = new FcPortInfo(); portInfo.PortWwn = wwn; // // Convert the WWN to the path of the corresponding Virtual FC Switch to be used // as HostResources for the ResourcePool. // portInfo.HostResource = FibreChannelUtilities.GetHostResourceFromWwn(wwn); portInfo.Status = GetPortStatus(port); portInfoList.Add(portInfo); } } } if (portInfoList.Count == 0) { Console.WriteLine("No FibreChannel Ports found in the system."); } return(portInfoList); }
ModifySanSettings( string poolId, string newPoolId ) { Console.WriteLine("Modifying a Virtual SAN's settings:"); Console.WriteLine("\tSAN Name: {0} (change to {1})", poolId, newPoolId); ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject rpConfigurationService = FibreChannelUtilities.GetResourcePoolConfigurationService(scope)) { string resourcePoolSettingData = FibreChannelUtilities.GetSettingsForPool(scope, newPoolId, null); string poolPath = FibreChannelUtilities.GetResourcePoolPath(scope, poolId); using (ManagementBaseObject inParams = rpConfigurationService.GetMethodParameters("ModifyPoolSettings")) { inParams["ChildPool"] = poolPath; inParams["PoolSettings"] = resourcePoolSettingData; using (ManagementBaseObject outParams = rpConfigurationService.InvokeMethod( "ModifyPoolSettings", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope, true, true); } } } Console.WriteLine("Successfully renamed Virtual SAN: from {0} to {1}", poolId, newPoolId); }
CreateVirtualFcPort( string virtualMachineName, string sanName, WorldWideName wwnA, WorldWideName wwnB) { Console.WriteLine("Adding Virtual FC Port to VM {0} :", virtualMachineName); Console.WriteLine("\tVirtualWWPN: {0}, VirtualWWNN {1}", wwnA.PortName, wwnA.NodeName); Console.WriteLine("\tSecondaryWWPN: {0}, SecondaryWWNN {1}", wwnB.PortName, wwnB.NodeName); Console.WriteLine("\tProviding connectivity to Virtual SAN {0}", sanName); ManagementScope scope = FibreChannelUtilities.GetFcScope(); using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope)) // // Find the virtual machine we want to connect. // using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(virtualMachineName, scope)) // // Get the virtual machine's settings object which is used // to make configuration changes. // using (ManagementObject virtualMachineSettings = WmiUtilities.GetVirtualMachineSettings(virtualMachine)) // // Add a new synthetic FC Adapter device to the virtual machine. // using (ManagementObject syntheticAdapter = AddSyntheticFcAdapter(virtualMachine, scope, wwnA, wwnB)) // // Now that we have added a FC adapter to the virtual machine we can configure its // connection settings. // using (ManagementObject connectionSettingsToAdd = FibreChannelUtilities.GetDefaultFcPortAllocationSettingData(scope, sanName)) { connectionSettingsToAdd["PoolID"] = sanName; connectionSettingsToAdd["Parent"] = syntheticAdapter.Path.Path; // // Now add the connection settings. // using (ManagementBaseObject addConnectionInParams = managementService.GetMethodParameters("AddResourceSettings")) { addConnectionInParams["AffectedConfiguration"] = virtualMachineSettings.Path.Path; addConnectionInParams["ResourceSettings"] = new string[] { connectionSettingsToAdd.GetText(TextFormat.WmiDtd20) }; using (ManagementBaseObject addConnectionOutParams = managementService.InvokeMethod("AddResourceSettings", addConnectionInParams, null)) { WmiUtilities.ValidateOutput(addConnectionOutParams, scope, true, true); } } } Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Successfully created virtual FC port on VM '{0}' and connected to san '{1}'.", virtualMachineName, sanName)); }