From 508101bc0fcd9f73a48566ba9795273b9dba36f2 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Tue, 26 Nov 2019 12:21:26 +0800 Subject: [PATCH] persist: fix vmtemplate storage leak Fix VM template storage leak by adding delete operations, we need to delete sandbox storage dirs when stop VM. Signed-off-by: Wei Zhang --- virtcontainers/vm.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/virtcontainers/vm.go b/virtcontainers/vm.go index 292cf877d3..99fda8b179 100644 --- a/virtcontainers/vm.go +++ b/virtcontainers/vm.go @@ -14,6 +14,8 @@ import ( "time" pb "github.com/kata-containers/runtime/protocols/cache" + "github.com/kata-containers/runtime/virtcontainers/persist" + persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" "github.com/kata-containers/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/runtime/virtcontainers/store" "github.com/sirupsen/logrus" @@ -34,6 +36,8 @@ type VM struct { memory uint32 cpuDelta uint32 + + store persistapi.PersistDriver } // VMConfig is a collection of all info that a new blackbox VM needs. @@ -155,9 +159,16 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { virtLog.WithField("vm", id).WithField("config", config).Info("create new vm") + store, err := persist.GetDriver("fs") + if err != nil { + return nil, err + } + defer func() { if err != nil { virtLog.WithField("vm", id).WithError(err).Error("failed to create new vm") + virtLog.WithField("vm", id).Errorf("Deleting store for %s", id) + store.Destroy(id) } }() @@ -219,6 +230,7 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { proxyURL: url, cpu: config.HypervisorConfig.NumVCPUs, memory: config.HypervisorConfig.MemorySize, + store: store, }, nil } @@ -231,9 +243,16 @@ func NewVMFromGrpc(ctx context.Context, v *pb.GrpcVM, config VMConfig) (*VM, err return nil, err } + store, err := persist.GetDriver("fs") + if err != nil { + return nil, err + } + defer func() { if err != nil { virtLog.WithField("vm", v.Id).WithError(err).Error("failed to create new vm from Grpc") + virtLog.WithField("vm", v.Id).Errorf("Deleting store for %s", v.Id) + store.Destroy(v.Id) } }() @@ -261,6 +280,7 @@ func NewVMFromGrpc(ctx context.Context, v *pb.GrpcVM, config VMConfig) (*VM, err cpu: v.Cpu, memory: v.Memory, cpuDelta: v.CpuDelta, + store: store, }, nil } @@ -318,7 +338,7 @@ func (v *VM) Stop() error { return err } - return nil + return v.store.Destroy(v.id) } // AddCPUs adds num of CPUs to the VM.