switch-root: when pivot_root() fails, but old root shall be mounted, do so

If pivot_root() fails, we'll fall back to switching root via MS_MOVE.
Unlike pivot_root() that won't place the old root fs anyway, but just
hide it. That's problematic during shutdown for example, since after all
we might still want to access it from the exitrd, to disassemble it
properly.

Hence let's make things somewhat systematic: regardless if pivot_root()
or MS_MOVE is used, always make the old root fs show up in the selected
new dir.
This commit is contained in:
Lennart Poettering
2023-09-02 19:47:58 +08:00
committed by Mike Yuan
parent c2d62118f7
commit 95648f9ed0
+6
View File
@@ -173,6 +173,12 @@ int switch_root(const char *new_root,
if (r < 0) {
log_debug_errno(r, "Pivoting root file system failed, moving mounts instead: %m");
if (resolved_old_root_after) {
r = mount_nofollow_verbose(LOG_ERR, "/", resolved_old_root_after, NULL, MS_BIND|MS_REC, NULL);
if (r < 0)
return r;
}
/* If we have to use MS_MOVE let's first try to get rid of *all* mounts we can, with the
* exception of the path we want to switch to, plus everything leading to it and within
* it. This is necessary because unlike pivot_root() just moving the mount to the root via