From f719512ab4681542b45de148105351913231835b Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Mon, 25 Jan 2016 22:12:35 -0800 Subject: [PATCH] Cleanup stale overlay sandboxes When the daemon is ungracefully shutdown, sometimes when we try to create the overlay sandbox after coming back up might get created in a different epoch count which will result in the vxlan interface not properly cleaned up. Fix this by explicitly cleaning up all the previous epoch sandboxes. Signed-off-by: Jana Radhakrishnan --- libnetwork/drivers/overlay/ov_network.go | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/libnetwork/drivers/overlay/ov_network.go b/libnetwork/drivers/overlay/ov_network.go index 0a891c0e5e..dc092ef359 100644 --- a/libnetwork/drivers/overlay/ov_network.go +++ b/libnetwork/drivers/overlay/ov_network.go @@ -5,6 +5,8 @@ import ( "fmt" "net" "os" + "path/filepath" + "strings" "sync" "syscall" @@ -298,6 +300,26 @@ func (n *network) initSubnetSandbox(s *subnet) error { return nil } +func (n *network) cleanupStaleSandboxes() { + filepath.Walk(filepath.Dir(osl.GenerateKey("walk")), + func(path string, info os.FileInfo, err error) error { + _, fname := filepath.Split(path) + + pList := strings.Split(fname, "-") + if len(pList) <= 1 { + return nil + } + + pattern := pList[1] + if strings.Contains(n.id, pattern) { + syscall.Unmount(path, syscall.MNT_DETACH) + os.Remove(path) + } + + return nil + }) +} + func (n *network) initSandbox() error { n.Lock() n.initEpoch++ @@ -311,6 +333,10 @@ func (n *network) initSandbox() error { } } + // If there are any stale sandboxes related to this network + // from previous daemon life clean it up here + n.cleanupStaleSandboxes() + sbox, err := osl.NewSandbox( osl.GenerateKey(fmt.Sprintf("%d-", n.initEpoch)+n.id), !hostMode) if err != nil {