From 45c3d6c89fa895f147e74a4388ab604c6c5ad804 Mon Sep 17 00:00:00 2001
From: jingrui <jingrui@huawei.com>
Date: Mon, 1 Jul 2019 21:17:07 +0800
Subject: [PATCH] docker:  fix reboot dirty data in containerd

Signed-off-by: jingrui <jingrui@huawei.com>
---
 components/engine/cmd/dockerd/daemon.go |  2 +-
 components/engine/daemon/daemon.go      | 30 ++++++++++++-------------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/components/engine/cmd/dockerd/daemon.go b/components/engine/cmd/dockerd/daemon.go
index 9c2b6602b3..78cd41ac59 100644
--- a/components/engine/cmd/dockerd/daemon.go
+++ b/components/engine/cmd/dockerd/daemon.go
@@ -151,6 +151,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 				return fmt.Errorf("Failed to generate containerd options: %v", err)
 			}
 
+			daemon.CleanupContainerdDBs(cli.Config.ExecRoot, cli.Config.Root)
 			r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...)
 			if err != nil {
 				cancel()
@@ -185,7 +186,6 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 		return fmt.Errorf("Error starting daemon: %v", err)
 	}
 
-	d.CleanupContainerdDBs()
 	d.StoreHosts(hosts)
 
 	// validate after NewDaemon has restored enabled plugins. Dont change order.
diff --git a/components/engine/daemon/daemon.go b/components/engine/daemon/daemon.go
index c36cc6395f..6c5eafd7c5 100644
--- a/components/engine/daemon/daemon.go
+++ b/components/engine/daemon/daemon.go
@@ -509,7 +509,7 @@ func (daemon *Daemon) restore() error {
 		logrus.Errorf("removeRedundantMounts failed %v", err)
 	}
 
-	daemon.cleanupLocalDBs()
+	daemon.cleanupLocalDBs(daemon.configStore.ExecRoot, daemon.configStore.Root)
 
 	containerIDs := make(map[string]struct{})
 	for cid, _ := range containers {
@@ -611,7 +611,7 @@ func (daemon *Daemon) restore() error {
 	return nil
 }
 
-func (daemon *Daemon) cleanupLocalDB(db string) {
+func cleanupLocalDB(db string) {
 	_, err := os.Stat(db)
 	if err == nil {
 		err = os.Remove(db)
@@ -619,25 +619,25 @@ func (daemon *Daemon) cleanupLocalDB(db string) {
 	}
 }
 
-func (daemon *Daemon) CleanupContainerdDBs() {
+func CleanupContainerdDBs(run, root string) {
 	// check db lock is exist, do nothing if file is existed
-	dbLockPath := filepath.Join(daemon.configStore.ExecRoot, "dblock")
+	dbLockPath := filepath.Join(run, "dblock")
 	_, err := os.Stat(dbLockPath)
 	if err == nil {
 		return
 	}
 	if !os.IsNotExist(err) {
-		logrus.Errorf("stat dblock failed %v", err)
+		logrus.Errorf("stat DB dblock failed %v", err)
 		return
 	}
-	daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "containerd/daemon/io.containerd.metadata.v1.bolt/meta.db"))
+	cleanupLocalDB(filepath.Join(root, "containerd/daemon/io.containerd.metadata.v1.bolt/meta.db"))
 }
 
 // DB files may corrupted on exception poweroff but can be rebuild at run time,
 // so we can remove DB files on OS starts avoid daemon can not startup.
-func (daemon *Daemon) cleanupLocalDBs() {
+func (daemon *Daemon) cleanupLocalDBs(run, root string) {
 	// check db lock is exist, do nothing if file is existed
-	dbLockPath := filepath.Join(daemon.configStore.ExecRoot, "dblock")
+	dbLockPath := filepath.Join(run, "dblock")
 	_, err := os.Stat(dbLockPath)
 	if err == nil {
 		return
@@ -649,13 +649,13 @@ func (daemon *Daemon) cleanupLocalDBs() {
 	ioutil.WriteFile(dbLockPath, []byte{}, 0600)
 
 	removeAllDB := func() {
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "builder/fscache.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "volumes/metadata.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "network/files/local-kv.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "accelerator/accel.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "buildkit/metadata.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "buildkit/cache.db"))
-		daemon.cleanupLocalDB(filepath.Join(daemon.configStore.Root, "buildkit/snapshots.db"))
+		cleanupLocalDB(filepath.Join(root, "builder/fscache.db"))
+		cleanupLocalDB(filepath.Join(root, "volumes/metadata.db"))
+		cleanupLocalDB(filepath.Join(root, "network/files/local-kv.db"))
+		cleanupLocalDB(filepath.Join(root, "accelerator/accel.db"))
+		cleanupLocalDB(filepath.Join(root, "buildkit/metadata.db"))
+		cleanupLocalDB(filepath.Join(root, "buildkit/cache.db"))
+		cleanupLocalDB(filepath.Join(root, "buildkit/snapshots.db"))
 	}
 
 	if daemon.containers == nil {
-- 
2.17.1

