/// <summary> /// Change the state of the virtual machine specified by the /// virtual machine reference (ResourceLocator) and the specified state (start/stop/etc.) /// </summary> public string ChangeVirtualMachineState(Xen_VirtualMachine_Reference_Type virtualSystem, UInt16 state) { string resourceURIInUse = null; string response = null; resourceURIInUse = string.Format("{0}/{1}", m_cim_resourceURIBase, "Xen_ComputerSystem"); IWSManResourceLocator virtualMachineLoc = (IWSManResourceLocator)ConvertToResourceLocator(resourceURIInUse, virtualSystem); try { string vmParams = GenerateStateChangeInputXML(state); response = m_wsmanSession.Invoke("RequestStateChange", virtualMachineLoc, vmParams, 0); // The response contains reference to the resulting VM. Deserialize it into a class. using (StringReader stream = new StringReader(response)) { XmlSerializer serializer = new XmlSerializer(typeof(Xen_RequestStateChange_OUTPUT_Type)); Xen_RequestStateChange_OUTPUT_Type responseSer = (Xen_RequestStateChange_OUTPUT_Type)serializer.Deserialize(stream); WaitForJobCompletion(responseSer.Job, "Xen_SystemStateChangeJob", typeof(Xen_SystemStateChangeJob_Type)); // wait until the state change job is complete } } catch (Exception ex) { throw ex; } return(response); }
/// <summary> /// Add a virtual Disk and a virtual NIC to the virtual machine /// specified by the virtual machine reference (resource locator) /// </summary> public void AddDiskAndNicToVirtualMachine(Xen_VirtualMachine_Reference_Type definedSystem) { IWSManResourceLocator vsmsService = GetVirtualSystemManagementService(); string responseAddResourceSetting = null; string diskPoolId = GetStoragePool(); string netPoolId = GetNetworkPool(); StringCollection rasds = GetRasdsToBeAdded(diskPoolId, netPoolId); if (rasds != null) { string vmuuid = null; // add the RASDs one by one foreach (String rasd in rasds) { vmuuid = FindVirtualMachineID(definedSystem); String vmParams = GenerateAddResourceInputXML(vmuuid, rasd); if (vmParams != null) { responseAddResourceSetting = m_wsmanSession.Invoke("AddResourceSetting", vsmsService, vmParams, 0); } } } else { throw new Exception("No RASDs defined"); } }
/// <summary> /// Create a Basic HVM (Hardware Virtualized) virtual machine with the /// specified name, description, number of processors and memory. /// </summary> public Xen_VirtualMachine_Reference_Type CreateVirtualMachine( string vmName, string vmDescription, int numProcs, int memMB ) { IWSManResourceLocator vsmsService = GetVirtualSystemManagementService(); // Create an empty VM with default processor and memory settings and no NIC or virtual disk string vmParams = GenerateDefineSystemInputXML(vmName, vmDescription, numProcs, memMB); string responseDefineSystem = m_wsmanSession.Invoke("DefineSystem", vsmsService, vmParams, 0); Xen_VirtualMachine_Reference_Type definedSystem = null; // The response contains a reference to the resulting VM. Deserialize it into a class. using (StringReader stream = new StringReader(responseDefineSystem)) { XmlSerializer serializer = new XmlSerializer(typeof(Xen_DefineSystem_OUTPUT_Type)); Xen_DefineSystem_OUTPUT_Type responseDS = (Xen_DefineSystem_OUTPUT_Type)serializer.Deserialize(stream); definedSystem = responseDS.ResultingSystem; if (responseDS.ReturnValue == 4096) // a job will be returned { WaitForJobCompletion(responseDS.Job, "Xen_VirtualSystemModifyResourcesJob", typeof(Xen_VirtualSystemModifyResourcesJob_Type)); } } return(definedSystem); }
/// <summary> /// Main entry point /// </summary> static void Main(string[] args) { MyWSManSession mysession = new MyWSManSession(); mysession.SetupCIMConnection("192.168.1.100", "root", "mypass"); // setup the CIM connection to the server Xen_VirtualMachine_Reference_Type newSystem = mysession.CreateVirtualMachine("WSManTestVM", "My VM Description", 2, 1024); // create a new VM mysession.AddDiskAndNicToVirtualMachine(newSystem); //Add a few more resources to the VM mysession.ChangeVirtualMachineState(newSystem, 2); //start the VM mysession.ChangeVirtualMachineState(newSystem, 32768); //Force Shutdown of the VM mysession.DeleteVirtualMachine(newSystem); //Delete the VM }
/// <summary> /// Get the Virtual Machine ID from a given Virtual Machine CIM reference /// </summary> private string FindVirtualMachineID(Xen_VirtualMachine_Reference_Type vmRef) { string vmuuid = null; foreach (CTX_Selector_Type selector in vmRef.ReferenceParameters.SelectorSet.Selector) { if (selector.Name.Equals("Name")) { vmuuid = selector.Value; break; } } return(vmuuid); }
/// <summary> /// Delete the Virtual Machine specified by the /// Virtual Machine reference (ResourceLocator instance) /// </summary> public string DeleteVirtualMachine(Xen_VirtualMachine_Reference_Type affectedVM) { string responseDestroy = null; string vmid = null; try { vmid = FindVirtualMachineID(affectedVM); IWSManResourceLocator vsmsService = GetVirtualSystemManagementService(); if (vmid != null) { string vmParams = GenerateDeleteVMInputXML(vmid); responseDestroy = m_wsmanSession.Invoke("DestroySystem", vsmsService, vmParams, 0); } } catch (Exception ex) { throw ex; } return(responseDestroy); }
/// <summary> /// Get the Virtual Machine ID from a given Virtual Machine CIM reference /// </summary> private string FindVirtualMachineID(Xen_VirtualMachine_Reference_Type vmRef) { string vmuuid = null; foreach (CTX_Selector_Type selector in vmRef.ReferenceParameters.SelectorSet.Selector) { if (selector.Name.Equals("Name")) { vmuuid = selector.Value; break; } } return vmuuid; }
/// <summary> /// Delete the Virtual Machine specified by the /// Virtual Machine reference (ResourceLocator instance) /// </summary> public string DeleteVirtualMachine(Xen_VirtualMachine_Reference_Type affectedVM) { string responseDestroy = null; string vmid = null; try { vmid = FindVirtualMachineID(affectedVM); IWSManResourceLocator vsmsService = GetVirtualSystemManagementService(); if (vmid != null) { string vmParams = GenerateDeleteVMInputXML(vmid); responseDestroy = m_wsmanSession.Invoke("DestroySystem", vsmsService, vmParams, 0); } } catch (Exception ex) { throw ex; } return responseDestroy; }
/// <summary> /// Change the state of the virtual machine specified by the /// virtual machine reference (ResourceLocator) and the specified state (start/stop/etc.) /// </summary> public string ChangeVirtualMachineState(Xen_VirtualMachine_Reference_Type virtualSystem, UInt16 state) { string resourceURIInUse = null; string response = null; resourceURIInUse = string.Format("{0}/{1}", m_cim_resourceURIBase, "Xen_ComputerSystem"); IWSManResourceLocator virtualMachineLoc = (IWSManResourceLocator)ConvertToResourceLocator(resourceURIInUse, virtualSystem); try { string vmParams = GenerateStateChangeInputXML(state); response = m_wsmanSession.Invoke("RequestStateChange", virtualMachineLoc, vmParams, 0); // The response contains reference to the resulting VM. Deserialize it into a class. using (StringReader stream = new StringReader(response)) { XmlSerializer serializer = new XmlSerializer(typeof(Xen_RequestStateChange_OUTPUT_Type)); Xen_RequestStateChange_OUTPUT_Type responseSer = (Xen_RequestStateChange_OUTPUT_Type)serializer.Deserialize(stream); WaitForJobCompletion(responseSer.Job, "Xen_SystemStateChangeJob", typeof(Xen_SystemStateChangeJob_Type)); // wait until the state change job is complete } } catch (Exception ex) { throw ex; } return response; }
/// <summary> /// Add a virtual Disk and a virtual NIC to the virtual machine /// specified by the virtual machine reference (resource locator) /// </summary> public void AddDiskAndNicToVirtualMachine(Xen_VirtualMachine_Reference_Type definedSystem) { IWSManResourceLocator vsmsService = GetVirtualSystemManagementService(); string responseAddResourceSetting = null; string diskPoolId = GetStoragePool(); string netPoolId = GetNetworkPool(); StringCollection rasds = GetRasdsToBeAdded(diskPoolId, netPoolId); if (rasds != null) { string vmuuid = null; // add the RASDs one by one foreach (String rasd in rasds) { vmuuid = FindVirtualMachineID(definedSystem); String vmParams = GenerateAddResourceInputXML(vmuuid, rasd); if (vmParams != null) { responseAddResourceSetting = m_wsmanSession.Invoke("AddResourceSetting", vsmsService, vmParams, 0); } } } else { throw new Exception("No RASDs defined"); } }