From ba7bca853f2b2ec1cdfabe37a464776745cbb0ab Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Sun, 28 May 2023 19:26:19 -0700
Subject: [PATCH 76/79] fix don't cleanup when remote ro is enabled

Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
 src/cmd/isulad/main.c                                    | 2 +-
 src/daemon/modules/api/leftover_cleanup_api.h            | 4 +++-
 .../container/leftover_cleanup/leftover_cleanup_api.c    | 9 ++++++++-
 .../layer_store/graphdriver/overlay2/driver_overlay2.c   | 6 ++++++
 4 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index f5ded220..0cb7b50e 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1249,7 +1249,7 @@ static int isulad_server_init_common()
 
 #ifndef DISABLE_CLEANUP
     // to cleanup leftover, init clean module before other modules.
-    if (clean_module_init() != 0) {
+    if (clean_module_init(args->json_confs) != 0) {
         ERROR("Failed to init clean module");
         goto out;
     }
diff --git a/src/daemon/modules/api/leftover_cleanup_api.h b/src/daemon/modules/api/leftover_cleanup_api.h
index 32a41258..26c4509b 100644
--- a/src/daemon/modules/api/leftover_cleanup_api.h
+++ b/src/daemon/modules/api/leftover_cleanup_api.h
@@ -15,6 +15,8 @@
 #ifndef DAEMON_MODULES_API_LEFTOVER_CLEANUP_API_H
 #define DAEMON_MODULES_API_LEFTOVER_CLEANUP_API_H
 
+#include <isula_libutils/isulad_daemon_configs.h>
+
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
 #endif
@@ -23,7 +25,7 @@ typedef enum {
     BROKEN_ROOTFS = 1
 } cleanup_ctx_data_t;
 
-int clean_module_init();
+int clean_module_init(const isulad_daemon_configs *args);
 
 void clean_module_fill_ctx(cleanup_ctx_data_t data_type, void *data);
 
diff --git a/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
index 7bdaef22..8f077916 100644
--- a/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
+++ b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
@@ -20,8 +20,15 @@
 struct clean_ctx *g_clean_ctx = NULL;
 struct cleaners *g_clns = NULL;
 
-int clean_module_init()
+int clean_module_init(const isulad_daemon_configs *args)
 {
+    if (args->storage_enable_remote_layer) {
+        // need to disable cleanup
+        // cause cleanup may cleanup local broken RO layer
+        // while this RO layer is valid for remote 
+        return 0;
+    }
+
     // create cleaners and clean_ctx
     g_clns = cleaners_init();
     if (g_clns == NULL) {
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
index eedbeef2..5ad487f9 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
@@ -1204,6 +1204,12 @@ int overlay2_rm_layer(const char *id, const struct graphdriver *driver)
     }
 
 #ifdef ENABLE_REMOTE_LAYER_STORE
+    if (!util_fileself_exists(layer_dir)) {
+        WARN("layer direcotry is already removed, can't remove twice");
+        ret = 0;
+        goto out;
+    }
+
     if (lstat(layer_dir, &stat_buf) < 0) {
         SYSERROR("Failed to lstat path: %s", layer_dir);
         ret = -1;
-- 
2.25.1

