@@ -3,13 +3,15 @@ package hostAgentService
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "strings"
7
+ "sync"
8
+ "time"
9
+
6
10
agentModel "github.com/easysoft/zagent/internal/host/model"
7
11
kvmService "github.com/easysoft/zagent/internal/host/service/kvm"
8
12
consts "github.com/easysoft/zagent/internal/pkg/const"
9
13
"github.com/easysoft/zagent/internal/pkg/job"
10
14
"github.com/gofrs/uuid"
11
- "sync"
12
- "time"
13
15
14
16
v1 "github.com/easysoft/zagent/cmd/host/router/v1"
15
17
hostRepo "github.com/easysoft/zagent/internal/host/repo"
@@ -44,6 +46,36 @@ func (s *SnapService) ListSnap(vm string) (ret []v1.SnapItemResp, err error) {
44
46
return
45
47
}
46
48
49
+ // RemoveSnapsByVmName 删除虚拟机下所有快照
50
+ func (s * SnapService ) RemoveSnapsByVmName (vm string ) (err error ) {
51
+ snaps := s .GetVmSnaps (vm )
52
+
53
+ for _ , snap := range snaps {
54
+ err = s .RemoveSnap (& v1.SnapTaskReq {Vm : vm , Name : snap })
55
+ }
56
+
57
+ return
58
+ }
59
+
60
+ func (s * SnapService ) GetVmSnaps (vm string ) (snaps []string ) {
61
+ cmd := fmt .Sprintf ("virsh snapshot-list %s | awk '{print $1}' | tail -n +3" , vm )
62
+
63
+ out , err := _shellUtils .ExeShell (cmd )
64
+ if err != nil {
65
+ _logUtils .Infof ("list snap '%s' err, output %s, error %s" , cmd , out , err .Error ())
66
+ return
67
+ }
68
+ snapNames := strings .Split (out , "\n " )
69
+ for _ , name := range snapNames {
70
+ name = strings .TrimSpace (name )
71
+ if name != "" {
72
+ snaps = append (snaps , name )
73
+ }
74
+ }
75
+
76
+ return
77
+ }
78
+
47
79
func (s * SnapService ) AddTasks (req []v1.SnapTaskReq ) (err error ) {
48
80
for _ , item := range req {
49
81
if item .Vm == "" || item .Name == "" {
0 commit comments