From f6ecb887312ba540996a9619910831bf5e50617c Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Sat, 31 Aug 2019 20:23:37 +0700 Subject: [PATCH 1/2] fix possible dm leak by re-arranging defers Signed-off-by: Chanwit Kaewkasi --- pkg/dmlegacy/vm.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/dmlegacy/vm.go b/pkg/dmlegacy/vm.go index d483723b3..6bcfecead 100644 --- a/pkg/dmlegacy/vm.go +++ b/pkg/dmlegacy/vm.go @@ -83,16 +83,17 @@ func AllocateAndPopulateOverlay(vm *api.VM) error { } func copyToOverlay(vm *api.VM) error { - if err := ActivateSnapshot(vm); err != nil { + err := ActivateSnapshot(vm) + defer cleanup.DeactivateSnapshot(vm) + if err != nil { return err } - defer cleanup.DeactivateSnapshot(vm) mp, err := util.Mount(vm.SnapshotDev()) + defer mp.Umount() if err != nil { return err } - defer mp.Umount() // Copy the kernel files to the VM. TODO: Use snapshot overlaying instead. if err := copyKernelToOverlay(vm, mp.Path); err != nil { From 6704273040a474c066715cd60510666bdd6124cb Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Sat, 31 Aug 2019 23:08:40 +0700 Subject: [PATCH 2/2] deactive dm snapshots if a vm is removed Signed-off-by: Chanwit Kaewkasi --- pkg/operations/remove.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/operations/remove.go b/pkg/operations/remove.go index 3dc449269..49b38aae9 100644 --- a/pkg/operations/remove.go +++ b/pkg/operations/remove.go @@ -6,6 +6,7 @@ import ( log "github.com/sirupsen/logrus" api "github.com/weaveworks/ignite/pkg/apis/ignite" "github.com/weaveworks/ignite/pkg/client" + "github.com/weaveworks/ignite/pkg/dmlegacy/cleanup" "github.com/weaveworks/ignite/pkg/logs" "github.com/weaveworks/ignite/pkg/providers" "github.com/weaveworks/ignite/pkg/runtime" @@ -28,7 +29,7 @@ func DeleteVM(c *client.Client, vm *api.VM) error { // CleanupVM removes the resources of the given VM func CleanupVM(vm *api.VM) error { // Inspect the container before trying to stop it and it gets auto-removed - result, _ := providers.Runtime.InspectContainer(util.NewPrefixer().Prefix(vm.GetUID())) + inspectResult, _ := providers.Runtime.InspectContainer(util.NewPrefixer().Prefix(vm.GetUID())) // If the VM is running, try to kill it first so we don't leave dangling containers if vm.Running() { @@ -38,7 +39,13 @@ func CleanupVM(vm *api.VM) error { } // Remove the VM container if it exists - RemoveVMContainer(result) + // TODO should this function return a proper error? + RemoveVMContainer(inspectResult) + + // After removal is successful, remove the dm snapshots + if err := cleanup.DeactivateSnapshot(vm); err != nil { + return err + } if logs.Quiet { fmt.Println(vm.GetUID())