Commit 748f76e5 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

kernel: check if process is running in a containerized environment

parent 5d456e76
......@@ -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;
......
......@@ -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 ...");
}
......
......@@ -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 */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment