mirror of
https://github.com/morgan9e/systemd
synced 2026-04-15 17:06:39 +09:00
in_initrd() was really doing two things: checking if we're in the initrd, and also verifying that the initrd is set up correctly. But this second check is complicated, in particular it would return false for overlayfs, even with an upper tmpfs layer. It also doesn't support the use case of having an initial initrd with tmpfs, and then transitioning into an intermediate initrd that is e.g. a DDI, i.e. a filesystem possibly with verity arranged as a disk image. We don't need to check if we're in initrd in every program. Instead, concerns are separated: - in_initrd() just does a simple check for /etc/initrd-release. - When doing cleanup, pid1 checks if it's on a tmpfs before starting to wipe the old root. The only case where we want to remove the old root is when we're on a plain tempory filesystem. With an overlay, we'd be creating whiteout files, which is not very useful. (*) This should resolve https://bugzilla.redhat.com/show_bug.cgi?id=2137631 which is caused by systemd refusing to treat the system as an initrd because overlayfs is used. (*) I think the idea of keeping the initrd fs around for shutdown is outdated. We should just have a completely separate exitrd that is unpacked when we want to shut down. This way, we don't waste memory at runtime, and we also don't transition to a potentially older version of systemd. But we don't have support for this yet. This replaces 0fef5b0f0bd9ded1ae7bcb3e4e4b2893e36c51a6.
43 lines
1.1 KiB
C
43 lines
1.1 KiB
C
/* 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;
|
|
}
|