[systemd-devel] systemctl switch-root /sysroot without deleting old_root

Herbert Groll herbert.groll at so-logic.net
Sat Nov 7 04:29:51 PST 2015

Am 2015-11-07 12:33, schrieb Richard Maw:
> On Fri, Nov 06, 2015 at 05:53:53PM +0100, Herbert Groll wrote:
>> Hi,
>> is there an easy way in initrd mode to keep old root when switching to
>> new root? Switching root is done in initrd-switch-root.target with
>> /bin/systemctl --no-block --force switch-root /sysroot
> You can remove /etc/initrd-release to stop it removing the old rootfs 
> tmpfs,
> I'm not sure what other side-effects this may have.

This was my first attempt too but switch-root is checking the existance 
of /etc/initrd-release via in_initrd() which saves its result as a 
static variable. Therefore I would need to patch with something like:

diff --git a/src/shared/switch-root.c b/src/shared/switch-root.c
index 813641a..307b7f3 100644
--- a/src/shared/switch-root.c
+++ b/src/shared/switch-root.c
@@ -54,7 +54,7 @@ int switch_root(const char *new_root, const char 
*oldroot, bool detach_oldroot,
          temporary_old_root = strjoina(new_root, oldroot);
          mkdir_p_label(temporary_old_root, 0755);

-        old_root_remove = in_initrd();
+        old_root_remove = in_initrd() && access("/etc/initrd-release", 
F_OK) >= 0;

          if (stat(new_root, &new_root_stat) < 0)
                  return log_error_errno(errno, "Failed to stat directory 
%s: %m", new_root);

Or maybe an uncached version of in_initrd() e.g. _in_initrd() could fit. 
With a patched switch-root.c and deleting /etc/initrd it seems to work 
for me. But is it possible without patching it? Maybe I'm missing 
something here. Thanks.

More information about the systemd-devel mailing list