diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index 0e63d388f..86cd6afe0 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -209,6 +209,16 @@ int uuid_generate_from_str(uuid_t out, const std::string &data, const uuid_t ns) /** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */ int uuid_generate_from_json(uuid_t out, json_t *json, const uuid_t ns); +/** Check if process is running inside a Docker container */ + +bool is_docker(); + +/** Check if process is running inside a Kubernetes container */ +bool is_kubernetes(); + +/** Check if process is running inside a containerized environment */ +bool is_container(); + namespace base64 { using byte = std::uint8_t; diff --git a/common/lib/kernel/kernel.cpp b/common/lib/kernel/kernel.cpp index 775bb1a9c..c48db41f8 100644 --- a/common/lib/kernel/kernel.cpp +++ b/common/lib/kernel/kernel.cpp @@ -260,14 +260,12 @@ int villas::kernel::get_nr_hugepages() int villas::kernel::set_nr_hugepages(int nr) { FILE *f; - int ret; f = fopen(PROCFS_PATH "/sys/vm/nr_hugepages", "w"); if (!f) { auto logger = logging.get("kernel"); - ret = access("/.dockerenv", F_OK); - if (ret != -1) { + if (is_container()) { logger->warn("This functionality is unavailable in this mode. Please run the Docker container in the privileged mode:"); logger->warn(" $ docker run --privilged ..."); } diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index f2fc3b17d..8afb6efb7 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -420,5 +420,20 @@ int uuid_generate_from_json(uuid_t out, json_t *json, const uuid_t ns) return ret; } +bool is_docker() +{ + return access("/.dockerenv", F_OK) != -1; +} + +bool is_kubernetes() +{ + return access("/var/run/secrets/kubernetes.io", F_OK) != -1 || + getenv("KUBERNETES_SERVICE_HOST") != nullptr; +} + +bool is_container() { + return is_docker() || is_kubernetes(); +} + } /* namespace utils */ } /* namespace villas */