1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

kernel: check if process is running in a containerized environment

This commit is contained in:
Steffen Vogel 2021-03-17 10:53:42 -04:00
parent 3a47bfa870
commit 2ccda908c9
3 changed files with 26 additions and 3 deletions

View file

@ -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;

View file

@ -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 ...");
}

View file

@ -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 */