snapshot.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "fmt"
  16. "os"
  17. "path"
  18. "github.com/vmware/govmomi/object"
  19. "github.com/vmware/govmomi/vim25/methods"
  20. "github.com/vmware/govmomi/vim25/mo"
  21. "github.com/vmware/govmomi/vim25/soap"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. type VirtualMachineSnapshot struct {
  25. mo.VirtualMachineSnapshot
  26. }
  27. func (v *VirtualMachineSnapshot) createSnapshotFiles() types.BaseMethodFault {
  28. vm := Map.Get(v.Vm).(*VirtualMachine)
  29. snapshotDirectory := vm.Config.Files.SnapshotDirectory
  30. if snapshotDirectory == "" {
  31. snapshotDirectory = vm.Config.Files.VmPathName
  32. }
  33. index := 1
  34. for {
  35. fileName := fmt.Sprintf("%s-Snapshot%d.vmsn", vm.Name, index)
  36. f, err := vm.createFile(snapshotDirectory, fileName, false)
  37. if err != nil {
  38. switch err.(type) {
  39. case *types.FileAlreadyExists:
  40. index++
  41. continue
  42. default:
  43. return err
  44. }
  45. }
  46. _ = f.Close()
  47. p, _ := parseDatastorePath(snapshotDirectory)
  48. vm.useDatastore(p.Datastore)
  49. datastorePath := object.DatastorePath{
  50. Datastore: p.Datastore,
  51. Path: path.Join(p.Path, fileName),
  52. }
  53. dataLayoutKey := vm.addFileLayoutEx(datastorePath, 0)
  54. vm.addSnapshotLayout(v.Self, dataLayoutKey)
  55. vm.addSnapshotLayoutEx(v.Self, dataLayoutKey, -1)
  56. return nil
  57. }
  58. }
  59. func (v *VirtualMachineSnapshot) removeSnapshotFiles(ctx *Context) types.BaseMethodFault {
  60. // TODO: also remove delta disks that were created when snapshot was taken
  61. vm := Map.Get(v.Vm).(*VirtualMachine)
  62. for idx, sLayout := range vm.Layout.Snapshot {
  63. if sLayout.Key == v.Self {
  64. vm.Layout.Snapshot = append(vm.Layout.Snapshot[:idx], vm.Layout.Snapshot[idx+1:]...)
  65. break
  66. }
  67. }
  68. for idx, sLayoutEx := range vm.LayoutEx.Snapshot {
  69. if sLayoutEx.Key == v.Self {
  70. for _, file := range vm.LayoutEx.File {
  71. if file.Key == sLayoutEx.DataKey || file.Key == sLayoutEx.MemoryKey {
  72. p, fault := parseDatastorePath(file.Name)
  73. if fault != nil {
  74. return fault
  75. }
  76. host := Map.Get(*vm.Runtime.Host).(*HostSystem)
  77. datastore := Map.FindByName(p.Datastore, host.Datastore).(*Datastore)
  78. dFilePath := path.Join(datastore.Info.GetDatastoreInfo().Url, p.Path)
  79. _ = os.Remove(dFilePath)
  80. }
  81. }
  82. vm.LayoutEx.Snapshot = append(vm.LayoutEx.Snapshot[:idx], vm.LayoutEx.Snapshot[idx+1:]...)
  83. }
  84. }
  85. vm.RefreshStorageInfo(ctx, nil)
  86. return nil
  87. }
  88. func (v *VirtualMachineSnapshot) RemoveSnapshotTask(ctx *Context, req *types.RemoveSnapshot_Task) soap.HasFault {
  89. task := CreateTask(v, "removeSnapshot", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  90. var changes []types.PropertyChange
  91. vm := Map.Get(v.Vm).(*VirtualMachine)
  92. Map.WithLock(vm, func() {
  93. if vm.Snapshot.CurrentSnapshot != nil && *vm.Snapshot.CurrentSnapshot == req.This {
  94. parent := findParentSnapshotInTree(vm.Snapshot.RootSnapshotList, req.This)
  95. changes = append(changes, types.PropertyChange{Name: "snapshot.currentSnapshot", Val: parent})
  96. }
  97. rootSnapshots := removeSnapshotInTree(vm.Snapshot.RootSnapshotList, req.This, req.RemoveChildren)
  98. changes = append(changes, types.PropertyChange{Name: "snapshot.rootSnapshotList", Val: rootSnapshots})
  99. if len(rootSnapshots) == 0 {
  100. changes = []types.PropertyChange{
  101. {Name: "snapshot", Val: nil},
  102. }
  103. }
  104. Map.Get(req.This).(*VirtualMachineSnapshot).removeSnapshotFiles(ctx)
  105. Map.Update(vm, changes)
  106. })
  107. Map.Remove(req.This)
  108. return nil, nil
  109. })
  110. return &methods.RemoveSnapshot_TaskBody{
  111. Res: &types.RemoveSnapshot_TaskResponse{
  112. Returnval: task.Run(),
  113. },
  114. }
  115. }
  116. func (v *VirtualMachineSnapshot) RevertToSnapshotTask(req *types.RevertToSnapshot_Task) soap.HasFault {
  117. task := CreateTask(v, "revertToSnapshot", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  118. vm := Map.Get(v.Vm).(*VirtualMachine)
  119. Map.WithLock(vm, func() {
  120. Map.Update(vm, []types.PropertyChange{
  121. {Name: "snapshot.currentSnapshot", Val: v.Self},
  122. })
  123. })
  124. return nil, nil
  125. })
  126. return &methods.RevertToSnapshot_TaskBody{
  127. Res: &types.RevertToSnapshot_TaskResponse{
  128. Returnval: task.Run(),
  129. },
  130. }
  131. }