594167
From d424adce45d593d41e52294bd8f32fd33c625498 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Mon, 7 Mar 2022 18:54:50 +0100
594167
Subject: [PATCH] basic: add new variable $SYSTEMD_OS_RELEASE to override
594167
 location of os-release
594167
594167
The test for the variable is added in test-systemctl-enable because there we
594167
can do it almost for free, and the variable is most likely to be used with
594167
'systemctl enable --root' anyway.
594167
594167
(cherry picked from commit df78419d107662dd49892d76a745c294d7031d66)
594167
594167
Related: #2082131
594167
---
594167
 docs/ENVIRONMENT.md           |  5 +++++
594167
 src/basic/os-util.c           | 16 +++++++++++-----
594167
 test/test-systemctl-enable.sh | 12 ++++++++++--
594167
 3 files changed, 26 insertions(+), 7 deletions(-)
594167
594167
diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md
594167
index 71d6c55010..5e9548449c 100644
594167
--- a/docs/ENVIRONMENT.md
594167
+++ b/docs/ENVIRONMENT.md
594167
@@ -43,6 +43,11 @@ All tools:
594167
   debugging, in order to test generators and other code against specific kernel
594167
   command lines.
594167
 
594167
+* `$SYSTEMD_OS_RELEASE` — if set, use this path instead of `/etc/os-release` or
594167
+  `/usr/lib/os-release`. When operating under some root (e.g. `systemctl
594167
+  --root=…`), the path is taken relative to the outside root. Only useful for
594167
+  debugging.
594167
+
594167
 * `$SYSTEMD_FSTAB` — if set, use this path instead of `/etc/fstab`. Only useful
594167
   for debugging.
594167
 
594167
diff --git a/src/basic/os-util.c b/src/basic/os-util.c
594167
index a6e4d09473..38b2179e48 100644
594167
--- a/src/basic/os-util.c
594167
+++ b/src/basic/os-util.c
594167
@@ -170,13 +170,19 @@ int open_extension_release(const char *root, const char *extension, char **ret_p
594167
                         }
594167
                 }
594167
         } else {
594167
-                FOREACH_STRING(p, "/etc/os-release", "/usr/lib/os-release") {
594167
-                        r = chase_symlinks(p, root, CHASE_PREFIX_ROOT,
594167
+                const char *var = secure_getenv("SYSTEMD_OS_RELEASE");
594167
+                if (var)
594167
+                        r = chase_symlinks(var, root, 0,
594167
                                            ret_path ? &q : NULL,
594167
                                            ret_fd ? &fd : NULL);
594167
-                        if (r != -ENOENT)
594167
-                                break;
594167
-                }
594167
+                else
594167
+                        FOREACH_STRING(path, "/etc/os-release", "/usr/lib/os-release") {
594167
+                                r = chase_symlinks(path, root, CHASE_PREFIX_ROOT,
594167
+                                                   ret_path ? &q : NULL,
594167
+                                                   ret_fd ? &fd : NULL);
594167
+                                if (r != -ENOENT)
594167
+                                        break;
594167
+                        }
594167
         }
594167
         if (r < 0)
594167
                 return r;
594167
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
594167
index 30ba6532e7..769341129c 100644
594167
--- a/test/test-systemctl-enable.sh
594167
+++ b/test/test-systemctl-enable.sh
594167
@@ -518,6 +518,14 @@ check_alias z 'z' && { echo "Expected failure because %z is not known" >&2; exit
594167
 
594167
 # FIXME: if there's an invalid Alias=, we shouldn't preach about empty [Install]
594167
 
594167
-exit 0  # yes, this is needed because the last test above fails
594167
-
594167
 # TODO: repeat the tests above for presets
594167
+
594167
+: -------SYSTEMD_OS_RELEASE relative to root------------------
594167
+# check that os-release overwriting works as expected with root
594167
+test -e "$root/etc/os-release"
594167
+
594167
+cat >"$root/etc/os-release2" <
594167
+ID='the-id2'
594167
+EOF
594167
+
594167
+SYSTEMD_OS_RELEASE="$root/etc/os-release2" check_alias o 'the-id2'