Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
persist: fix vmtemplate storage leak
Browse files Browse the repository at this point in the history
Fix VM template storage leak by adding delete operations, we need to
delete sandbox storage dirs when stop VM.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Nov 26, 2019
1 parent 27b677e commit 16fa202
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion virtcontainers/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}()

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
}()

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 16fa202

Please sign in to comment.