Esempio n. 1
0
        public static IVirtualSystemSettingData GetVirtualSystemSettingDataByName(ManagementScope scope, string name)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.Query(scope, "ElementName='{0}'", (object)name).Where <IVirtualSystemSettingData>((Func <IVirtualSystemSettingData, bool>)(vssd => vssd.VirtualSystemType == "Microsoft:Hyper-V:System:Realized")).FirstOrDefault <IVirtualSystemSettingData>();

            if (systemSettingData != null)
            {
                return(systemSettingData);
            }
            throw new HyperVException("VirtualSystemSettingData not found for VM name " + name);
        }
Esempio n. 2
0
        public static IVirtualSystemSettingData GetVirtualSystemSettingDataByVmUuid(ManagementScope scope, string vmUuid)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.Query(scope, "VirtualSystemIdentifier='{0}'", (object)vmUuid).Where <IVirtualSystemSettingData>((Func <IVirtualSystemSettingData, bool>)(vssd => vssd.VirtualSystemType == "Microsoft:Hyper-V:System:Realized")).FirstOrDefault <IVirtualSystemSettingData>();

            if (systemSettingData != null)
            {
                return(systemSettingData);
            }
            throw new HyperVException("VirtualSystemSettingData not found for VM uuid " + vmUuid);
        }
Esempio n. 3
0
 public IVirtualSystemSettingData CreateSnapshot(IComputerSystem affectedSystem, string snapshotSettings, ushort snapshotType)
 {
     using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(CreateSnapshot)))
     {
         methodParameters["AffectedSystem"]   = (object)((IWMICommon)affectedSystem).Object;
         methodParameters["SnapshotSettings"] = (object)snapshotSettings;
         methodParameters["SnapshotType"]     = (object)snapshotType;
         using (ManagementBaseObject managementBaseObject = this._Instance.InvokeAndWait(nameof(CreateSnapshot), methodParameters))
             return(VirtualSystemSettingData.CreateFromObject(new ManagementObject((string)managementBaseObject["ResultingSnapshot"])));
     }
 }
Esempio n. 4
0
        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));
        }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
 public ISummaryInformation GetSummaryInformation(string vmName)
 {
     using (IVirtualSystemSettingData settingDataByName = VirtualSystemSettingData.GetVirtualSystemSettingDataByName(this._Instance.Scope, vmName))
     {
         if (settingDataByName == null)
         {
             throw new HyperVmNotFoundException(vmName);
         }
         ManagementObject[] managementObjectArray = new ManagementObject[1]
         {
             settingDataByName.ManagementObject
         };
         using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(GetSummaryInformation)))
         {
             uint[] numArray = new uint[17]
             {
                 1U,
                 2U,
                 3U,
                 4U,
                 8U,
                 100U,
                 103U,
                 104U,
                 105U,
                 106U,
                 107U,
                 109U,
                 110U,
                 112U,
                 113U,
                 123U,
                 132U
             };
             methodParameters["SettingData"]          = (object)managementObjectArray;
             methodParameters["RequestedInformation"] = (object)numArray;
             using (ManagementBaseObject managementBaseObject = this._Instance.InvokeAndWait(nameof(GetSummaryInformation), methodParameters))
             {
                 if ((int)(uint)managementBaseObject["ReturnValue"] == 0)
                 {
                     return((ISummaryInformation) new SummaryInformation(((ManagementBaseObject[])managementBaseObject["SummaryInformation"])[0]));
                 }
             }
             throw new HyperVException("Failed to retrieve SummaryInformation for VM " + vmName);
         }
     }
 }
Esempio n. 7
0
        public static IVirtualSystemSettingData CreateFromExisting(IComputerSystem host, IVirtualSystemSettingData vssd, string location)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(host);
            List <string>             stringList        = new List <string>()
            {
                "ElementName",
                "ConfigurationDataRoot",
                "SnapshotDataRoot",
                "AdditionalRecoveryInformation",
                "AllowFullSCSICommandSet",
                "AllowReducedFcRedundancy",
                "AutomaticRecoveryAction",
                "AutomaticShutdownAction",
                "AutomaticStartupAction",
                "AutomaticStartupActionDelay",
                "AutomaticStartupActionSequenceNumber",
                "BaseBoardSerialNumber",
                "BIOSGUID",
                "BIOSNumLock",
                "BIOSSerialNumber",
                "BootOrder",
                "ChassisAssetTag",
                "ChassisSerialNumber",
                "ConfigurationFile",
                "ConfigurationID",
                "DebugChannelId",
                "DebugPort",
                "DebugPortEnabled",
                "IncrementalBackupEnabled",
                "IsSaved",
                "LogDataRoot",
                "LowMmioGapSize",
                "Notes",
                "PauseAfterBootFailure",
                "SecureBootEnabled",
                "SuspendDataRoot",
                "SwapFileDataRoot",
                "Version",
                "VirtualNumaEnabled",
                "VirtualSystemType",
                "VirtualSystemSubType"
            };

            if (vssd.VirtualSystemSubType != "Microsoft:Hyper-V:SubType:1")
            {
                stringList.Add("NetworkBootPreferredProtocol");
            }
            ManagementObject managementObject1 = vssd.Object;
            ManagementObject managementObject2 = systemSettingData.Object;

            foreach (string index in stringList)
            {
                try
                {
                    managementObject2[index] = managementObject1[index];
                }
                catch (Exception ex)
                {
                }
            }
            systemSettingData.ConfigurationDataRoot = location;
            systemSettingData.SnapshotDataRoot      = location;
            systemSettingData.SuspendDataRoot       = location;
            systemSettingData.SwapFileDataRoot      = location;
            return(systemSettingData);
        }
Esempio n. 8
0
        public static IEnumerable <IVirtualSystemSettingData> Query(ManagementScope scope, string condition, params object[] args)
        {
            string condition1 = string.Format(condition, args);

            return(VirtualSystemSettingData.Query(scope, condition1));
        }
Esempio n. 9
0
 public IVirtualSystemSettingData GetVirtualSystemSettingData()
 {
     return(VirtualSystemSettingData.GetVirtualSystemSettingDataByVmUuid(this._Instance.Scope, this.Name));
 }