public void AddChild(VMSnapshot snapshot) { ///Insert child snapshot to sorted collection of children by creation time foreach (VMSnapshot child in this.ChildSnapshots) { if (child.CreationTime < snapshot.CreationTime) {//jump to the next child continue; } if (child.CreationTime > snapshot.CreationTime) {//insert before child with larger CreationTime property value this.childSnapshots.Insert(this.ChildSnapshots.IndexOf(child), snapshot); return; } } //the snapshot has the largest CreationTime property value so append it to the end this.ChildSnapshots.Add(snapshot); }
public static VMJob RemoveVirtualSystemSnapshotTree(VM vm, VMSnapshot snapshot) { ManagementScope scope = new ManagementScope(@"\\" + vm.Host.Name + @"\root\virtualization", vm.Host.HostConnectionOptions); ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService"); ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("RemoveVirtualSystemSnapshotTree"); ManagementObject vmObj = Utility.GetTargetComputer(vm.GUID, scope); ManagementObject vmSnapshotObj = snapshot.SnapshotInstance; inParams["SnapshotSettingData"] = vmSnapshotObj.Path.Path; ManagementBaseObject outParams = virtualSystemService.InvokeMethod("RemoveVirtualSystemSnapshotTree", inParams, null); VMJob ThisJob = new VMJob(new ManagementObject(scope, new ManagementPath((string)outParams["Job"]), null)); inParams.Dispose(); outParams.Dispose(); vmSnapshotObj.Dispose(); vmObj.Dispose(); virtualSystemService.Dispose(); return(ThisJob); }
public static VMSnapshot GetVMSnapshotTree(VM vm) { //Getting the Management object containing snapshots array of current VM // ManagementBaseObject snapshots = Utility.GetSummaryInformation(vm, new uint[] { VMRequestedInformation.Snapshots }); ManagementObjectCollection VirtualSystemSettingsCollection = vm.Instance.GetRelated("Msvm_VirtualSystemSettingData"); VMSnapshot SnapshotTree = null; try { //Getting snapshots array of current VM //ManagementBaseObject[] VirtualSystemSettingsCollection = (ManagementBaseObject[])snapshots["Snapshots"]; if (VirtualSystemSettingsCollection != null) { Dictionary <string, VMSnapshot> AvailableSnapshots = new Dictionary <string, VMSnapshot> (); //Building snapshots tree foreach (ManagementObject snapshotsObj in VirtualSystemSettingsCollection) { //Check if the setting is snapshot (SettingType = 5). If the setting represents current settings, then SettingType = 3 if ((UInt16)snapshotsObj["SettingType"] == 5) { //current VMSnapshots instance VMSnapshot child; //check if instance of this snapshot is not already exist if (!AvailableSnapshots.ContainsKey((string)snapshotsObj["InstanceID"])) { child = new VMSnapshot(snapshotsObj); //Adding new snapshot instance to the Dictionary AvailableSnapshots.Add((string)child.SnapshotInstance["InstanceID"], child); } else { //this snapshot is already exist in the Dictionary child = AvailableSnapshots[(string)snapshotsObj["InstanceID"]]; } if (snapshotsObj["Parent"] == null) { //Current snapshot is root snapshot SnapshotTree = child; } else { //Current Snapshot is not a root snapshot ManagementObject parentObj = new ManagementObject((string)snapshotsObj["Parent"]); if (!AvailableSnapshots.ContainsKey((string)parentObj["InstanceID"])) { //Parent VMSnapshot snapshot instance is not already present. So we create it VMSnapshot parent = new VMSnapshot(parentObj); //.. and add it to the Dictionary AvailableSnapshots.Add((string)parentObj["InstanceID"], parent); } ///Adding current child to children array of parent snapshot if child is not already exist. This because ///GetRelated() function returns some snapshots several times as snapshot, last applied snapshot ///and previous settings if (!AvailableSnapshots[(string)parentObj["InstanceID"]].ChildSnapshots.Contains(child)) { AvailableSnapshots[(string)parentObj["InstanceID"]].AddChild(child); } } } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "GetVMSnapshotTree"); } return(SnapshotTree); }
public VMJob RemoveSnapshotTree(VMSnapshot snapshot) { return(Utility.RemoveVirtualSystemSnapshotTree(this, snapshot)); }
public VMJob ApplySnapshot(VMSnapshot snapshot) { return(Utility.ApplyVirtualSystemSnapshot(this, snapshot)); }
public VMDetailsViewModel(VM vm) : base(vm) { view = new VMDetailsView(); this.vmModel = VMModel.GetInstance(); this.IsActiveVM = true; //lock (vmModel.ActiveVMList) //{ // vmModel.ActiveVMList.Clear(); // vmModel.ActiveVMList.Add(this.VirtualMachine); //} VMName = this.VirtualMachine.Name; vmstatusString = this.VirtualMachine.StatusString; VMDomainName = this.VirtualMachine.GetDomainName(); GuestOS guestOS = this.VirtualMachine.GetGuestOS(); if (guestOS == null) { guestOS = new GuestOS(string.Empty, string.Empty, string.Empty, string.Empty); } OSName = guestOS.OSName; OSVersion = guestOS.OSVersion; CSDVersion = guestOS.CSDVersion; IPv4Address = guestOS.IPv4Address; VMSnapshot snapshotTree = this.VirtualMachine.SnapshotTree; if (snapshotTree != null) { VMSnapshots = new VMSnapshotViewModel[] { new VMSnapshotViewModel(snapshotTree, this.VirtualMachine, vmModel) }; } else { VMSnapshots = new VMSnapshotViewModel [] {}; } foreach (VMSnapshotViewModel snapshotVM in VMSnapshots) { snapshotVM.Selected += (o, e) => { this.SelectedSnapshotItem = snapshotVM.SnapshotInstance; }; } this.VirtualMachine.VMSnapshotsChanged += (o, e) => { snapshotTree = this.VirtualMachine.SnapshotTree; if (snapshotTree != null) { VMSnapshots = new VMSnapshotViewModel[] { new VMSnapshotViewModel(snapshotTree, this.VirtualMachine, vmModel) }; } }; ThreadStart backgroundThreadStart = new ThreadStart(() => { while (true) { GCHandle pinnedArray = GCHandle.Alloc(this.VirtualMachine.MediumThumbnailImage, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); Bitmap BI = new Bitmap(160, 120, 320, System.Drawing.Imaging.PixelFormat.Format16bppRgb565, ptr); MemoryStream st = new MemoryStream(); BI.Save(st, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(st.ToArray()); bmp.EndInit(); bmp.Freeze(); ThumbnailImage = bmp; Thread.Sleep(2000); pinnedArray.Free(); } }); if ((this.VirtualMachine.Status != VMState.Unknown) && (this.VirtualMachine.Status != VMState.Disabled)) { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } this.VirtualMachine.VMStatusChanged += (o, e) => { this.VMName = this.VirtualMachine.Name; this.VMStatusString = this.VirtualMachine.GetStatusString(); this.VMDomainName = this.VirtualMachine.GetDomainName(); guestOS = this.VirtualMachine.GetGuestOS(); if (guestOS == null) { guestOS = new GuestOS(string.Empty, string.Empty, string.Empty, string.Empty); } OSName = guestOS.OSName; OSVersion = guestOS.OSVersion; CSDVersion = guestOS.CSDVersion; IPv4Address = guestOS.IPv4Address; if ((this.VirtualMachine.Status != VMState.Unknown) && (this.VirtualMachine.Status != VMState.Disabled)) { if (BackgroundThread != null) { if (!BackgroundThread.IsAlive) { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } } else { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } } else { if (BackgroundThread != null) { BackgroundThread.Abort(); BackgroundThread.Join(10); } } }; view.SetViewModel(this); }