summaryrefslogtreecommitdiffstats
path: root/src/basic/initrd-util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/basic/initrd-util.c')
-rw-r--r--src/basic/initrd-util.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/basic/initrd-util.c b/src/basic/initrd-util.c
new file mode 100644
index 0000000..03ccfbe
--- /dev/null
+++ b/src/basic/initrd-util.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <unistd.h>
+
+#include "env-util.h"
+#include "errno-util.h"
+#include "initrd-util.h"
+#include "parse-util.h"
+#include "stat-util.h"
+#include "string-util.h"
+
+static int saved_in_initrd = -1;
+
+bool in_initrd(void) {
+ int r;
+
+ if (saved_in_initrd >= 0)
+ return saved_in_initrd;
+
+ /* If /etc/initrd-release exists, we're in an initrd.
+ * This can be overridden by setting SYSTEMD_IN_INITRD=0|1.
+ */
+
+ r = getenv_bool_secure("SYSTEMD_IN_INITRD");
+ if (r < 0 && r != -ENXIO)
+ log_debug_errno(r, "Failed to parse $SYSTEMD_IN_INITRD, ignoring: %m");
+
+ if (r >= 0)
+ saved_in_initrd = r > 0;
+ else {
+ r = RET_NERRNO(access("/etc/initrd-release", F_OK));
+ if (r < 0 && r != -ENOENT)
+ log_debug_errno(r, "Failed to check if /etc/initrd-release exists, assuming it does not: %m");
+ saved_in_initrd = r >= 0;
+ }
+
+ return saved_in_initrd;
+}
+
+void in_initrd_force(bool value) {
+ saved_in_initrd = value;
+}