Example #1
0
        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);
                }
            }
        }
Example #2
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);
            }
        }