From b289a6b384ac5ba474dd477b2a92b89244b27c24 Mon Sep 17 00:00:00 2001 From: zhangxiaoyu Date: Mon, 13 Jun 2022 14:35:46 +0800 Subject: [PATCH 20/30] fix shm size set invalid when reboot Signed-off-by: zhangxiaoyu --- .../modules/service/service_container.c | 8 +++ src/daemon/modules/spec/specs_mount.c | 63 +++++++++++++++---- src/daemon/modules/spec/specs_mount.h | 2 + .../image/oci/oci_config_merge/CMakeLists.txt | 1 + test/specs/specs/CMakeLists.txt | 1 + test/specs/specs_extend/CMakeLists.txt | 1 + 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c index c3c4fc1c..43a4a0c9 100644 --- a/src/daemon/modules/service/service_container.c +++ b/src/daemon/modules/service/service_container.c @@ -43,6 +43,7 @@ #include "events_sender_api.h" #include "image_api.h" #include "specs_api.h" +#include "specs_mount.h" #include "isulad_config.h" #include "verify.h" #include "plugin_api.h" @@ -735,6 +736,13 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo goto close_exit_fd; } + nret = setup_ipc_dirs(cont->hostconfig, cont->common_config); + if (nret != 0) { + ERROR("Failed to setup ipc dirs"); + ret = -1; + goto close_exit_fd; + } + // embedded conainter is readonly, create mtab link will fail // kata-runtime container's qemu donot support to create mtab in host if (strcmp(IMAGE_TYPE_EMBEDDED, cont->common_config->image_type) != 0 && strcmp(KATA_RUNTIME, cont->runtime) != 0) { diff --git a/src/daemon/modules/spec/specs_mount.c b/src/daemon/modules/spec/specs_mount.c index b35061d8..e55832c5 100644 --- a/src/daemon/modules/spec/specs_mount.c +++ b/src/daemon/modules/spec/specs_mount.c @@ -49,6 +49,7 @@ #include "utils_file.h" #include "utils_string.h" #include "utils_verify.h" +#include "utils_fs.h" #include "image_api.h" #include "volume_api.h" #include "parse_volume.h" @@ -2574,10 +2575,11 @@ static int chown_for_shm(const char *shm_path, const char *user_remap) static char *get_prepare_share_shm_path(const char *truntime, const char *cid) { -#define SHM_MOUNT_FILE_NAME "/mounts/shm/" +#define SHM_MOUNT_FILE_NAME "/mounts/shm" char *c_root_path = NULL; size_t slen = 0; char *spath = NULL; + char real_root_path[PATH_MAX] = { 0 }; int nret = 0; if (truntime == NULL) { @@ -2588,19 +2590,24 @@ static char *get_prepare_share_shm_path(const char *truntime, const char *cid) goto err_out; } - // c_root_path + "/" + cid + "/mounts/shm" - if (strlen(c_root_path) > (((PATH_MAX - strlen(cid)) - 1) - strlen(SHM_MOUNT_FILE_NAME)) - 1) { + if (realpath(c_root_path, real_root_path) == NULL) { + ERROR("Failed to get %s realpath", c_root_path); + goto err_out; + } + + // real_root_path + "/" + cid + "/mounts/shm" + if (strlen(real_root_path) > (((PATH_MAX - strlen(cid)) - 1) - strlen(SHM_MOUNT_FILE_NAME)) - 1) { ERROR("Too large path"); goto err_out; } - slen = strlen(c_root_path) + 1 + strlen(cid) + strlen(SHM_MOUNT_FILE_NAME) + 1; + slen = strlen(real_root_path) + 1 + strlen(cid) + strlen(SHM_MOUNT_FILE_NAME) + 1; spath = util_smart_calloc_s(sizeof(char), slen); if (spath == NULL) { ERROR("Out of memory"); goto err_out; } - nret = snprintf(spath, slen, "%s/%s/mounts/shm/", c_root_path, cid); + nret = snprintf(spath, slen, "%s/%s/mounts/shm", real_root_path, cid); if (nret < 0 || nret >= slen) { ERROR("Sprintf failed"); goto err_out; @@ -2637,7 +2644,7 @@ out: return ret; } -static int prepare_share_shm(host_config *host_spec, container_config_v2_common_config *v2_spec) +int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2_spec) { #define MAX_PROPERTY_LEN 64 char shmproperty[MAX_PROPERTY_LEN] = { 0 }; @@ -2650,14 +2657,26 @@ static int prepare_share_shm(host_config *host_spec, container_config_v2_common_ char *p = NULL; char *userns_remap = NULL; #endif - // has mount for /dev/shm - if (has_mount_shm(host_spec, v2_spec)) { + + // ignore shm of system container + if (host_spec->system_container) { + return 0; + } + // setup shareable dirs + if (host_spec->ipc_mode != NULL && !namespace_is_shareable(host_spec->ipc_mode)) { return 0; } spath = get_prepare_share_shm_path(host_spec->runtime, v2_spec->id); if (spath == NULL) { - goto out; + return -1; + } + + // container shm has been mounted + if (util_detect_mounted(spath)) { + DEBUG("shm path %s has been mounted", spath); + free(spath); + return 0; } nret = util_mkdir_p(spath, 0700); @@ -2683,7 +2702,6 @@ static int prepare_share_shm(host_config *host_spec, container_config_v2_common_ goto out; } - v2_spec->shm_path = spath; #ifdef ENABLE_USERNS_REMAP userns_remap = conf_get_isulad_userns_remap(); @@ -2716,7 +2734,6 @@ static int prepare_share_shm(host_config *host_spec, container_config_v2_common_ } #endif - spath = NULL; ret = 0; out: if (ret != 0 && has_mount) { @@ -2777,8 +2794,22 @@ out_free: return ret; } +static int set_share_shm(const host_config *host_spec, container_config_v2_common_config *v2_spec) +{ + char *spath = NULL; + + spath = get_prepare_share_shm_path(host_spec->runtime, v2_spec->id); + if (spath == NULL) { + return -1; + } + + v2_spec->shm_path = spath; + + return 0; +} + #define SHM_MOUNT_POINT "/dev/shm" -static int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2_spec) +static int set_shm_path(host_config *host_spec, container_config_v2_common_config *v2_spec) { int ret = 0; container_t *cont = NULL; @@ -2791,7 +2822,7 @@ static int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_con } // setup shareable dirs if (host_spec->ipc_mode == NULL || namespace_is_shareable(host_spec->ipc_mode)) { - return prepare_share_shm(host_spec, v2_spec); + return set_share_shm(host_spec, v2_spec); } if (namespace_is_container(host_spec->ipc_mode)) { @@ -3334,6 +3365,12 @@ int merge_conf_mounts(oci_runtime_spec *oci_spec, host_config *host_spec, contai goto out; } + if (set_shm_path(host_spec, v2_spec) != 0) { + ERROR("Failed to set shm path"); + ret = -1; + goto out; + } + /* add ipc mount */ if (v2_spec->shm_path != NULL) { // check whether duplication diff --git a/src/daemon/modules/spec/specs_mount.h b/src/daemon/modules/spec/specs_mount.h index b1e987e5..07c07a37 100644 --- a/src/daemon/modules/spec/specs_mount.h +++ b/src/daemon/modules/spec/specs_mount.h @@ -41,4 +41,6 @@ bool mount_run_tmpfs(oci_runtime_spec *container, const host_config *host_spec, int merge_conf_device(oci_runtime_spec *oci_spec, host_config *host_spec); +int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2_spec); + #endif diff --git a/test/image/oci/oci_config_merge/CMakeLists.txt b/test/image/oci/oci_config_merge/CMakeLists.txt index 36dc3ead..88047fde 100644 --- a/test/image/oci/oci_config_merge/CMakeLists.txt +++ b/test/image/oci/oci_config_merge/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/utils_convert.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/utils_file.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/utils_timestamp.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/utils_fs.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/map/map.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/map/rb_tree.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/util_atomic.c diff --git a/test/specs/specs/CMakeLists.txt b/test/specs/specs/CMakeLists.txt index e0031e08..b730959c 100644 --- a/test/specs/specs/CMakeLists.txt +++ b/test/specs/specs/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_timestamp.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/util_atomic.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_mount_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_fs.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256/sha256.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/path.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/map.c diff --git a/test/specs/specs_extend/CMakeLists.txt b/test/specs/specs_extend/CMakeLists.txt index 45b21ecd..7d05deb4 100644 --- a/test/specs/specs_extend/CMakeLists.txt +++ b/test/specs/specs_extend/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_timestamp.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/util_atomic.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_mount_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_fs.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256/sha256.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/path.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/map.c -- 2.32.1 (Apple Git-133)