]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/virt.c
Merge pull request #12414 from keszybz/detect-podman
[thirdparty/systemd.git] / src / basic / virt.c
index 284d9b4d44cd7538359b9b17882d024197be7575..84b2d64b25621773bcab9fe21742691ef88a4f57 100644 (file)
@@ -428,7 +428,6 @@ finish:
 }
 
 int detect_container(void) {
-
         static const struct {
                 const char *value;
                 int id;
@@ -437,6 +436,7 @@ int detect_container(void) {
                 { "lxc-libvirt",    VIRTUALIZATION_LXC_LIBVIRT    },
                 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
                 { "docker",         VIRTUALIZATION_DOCKER         },
+                { "podman",         VIRTUALIZATION_PODMAN         },
                 { "rkt",            VIRTUALIZATION_RKT            },
                 { "wsl",            VIRTUALIZATION_WSL            },
         };
@@ -468,9 +468,15 @@ int detect_container(void) {
         }
 
         if (getpid_cached() == 1) {
-                /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
-
+                /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
+                 * We distinguish three cases:
+                 * - the variable is not defined → we jump to other checks
+                 * - the variable is defined to an empty value → we are not in a container
+                 * - anything else → some container, either one of the known ones or "container-other"
+                 */
                 e = getenv("container");
+                if (!e)
+                        goto check_sched;
                 if (isempty(e)) {
                         r = VIRTUALIZATION_NONE;
                         goto finish;
@@ -498,8 +504,9 @@ int detect_container(void) {
         if (r < 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
                 log_debug_errno(r, "Failed to read $container of PID 1, ignoring: %m");
 
-        /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. Hence, if the PID shown
-         * there is not 1, we know we are in a PID namespace. and hence a container. */
+        /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
+         * shown there is not 1, we know we are in a PID namespace and hence a container. */
+ check_sched:
         r = read_one_line_file("/proc/1/sched", &m);
         if (r >= 0) {
                 const char *t;
@@ -649,6 +656,7 @@ static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
         [VIRTUALIZATION_LXC] = "lxc",
         [VIRTUALIZATION_OPENVZ] = "openvz",
         [VIRTUALIZATION_DOCKER] = "docker",
+        [VIRTUALIZATION_PODMAN] = "podman",
         [VIRTUALIZATION_RKT] = "rkt",
         [VIRTUALIZATION_WSL] = "wsl",
         [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",