From b7106af465f1d7fbe4a6695f5b7f7eb8c109d622 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 19 May 2023 14:21:21 +0200 Subject: [PATCH 1/2] tree-wide: remove (void) cast around sync() The call is void anyway, it doesn't return an failure indication. Hence, no need to cast void to (void)... (We got this right in most cases, but forgot some) --- src/basic/async.c | 2 +- src/systemctl/systemctl-util.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/async.c b/src/basic/async.c index 73de889a2b..5e347dde8b 100644 --- a/src/basic/async.c +++ b/src/basic/async.c @@ -72,7 +72,7 @@ int asynchronous_sync(pid_t *ret_pid) { return r; if (r == 0) { /* Child process */ - (void) sync(); + sync(); _exit(EXIT_SUCCESS); } diff --git a/src/systemctl/systemctl-util.c b/src/systemctl/systemctl-util.c index b6b28a113d..074bb8d83b 100644 --- a/src/systemctl/systemctl-util.c +++ b/src/systemctl/systemctl-util.c @@ -921,7 +921,7 @@ int halt_now(enum action a) { /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need * to be synced explicitly in advance. */ if (!arg_no_sync && !arg_dry_run) - (void) sync(); + sync(); /* Make sure C-A-D is handled by the kernel from this point on... */ if (!arg_dry_run) From 5221b55bb3b78d6dafcc33a609513b594fb62dbf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 19 May 2023 14:22:14 +0200 Subject: [PATCH 2/2] switch-root: sync() before MNT_DETACH unmounting all old mounts As discussed here: https://github.com/systemd/systemd/pull/27670#discussion_r1196716794 --- src/shared/switch-root.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/shared/switch-root.c b/src/shared/switch-root.c index 2431facba0..d5b3d169bf 100644 --- a/src/shared/switch-root.c +++ b/src/shared/switch-root.c @@ -63,6 +63,13 @@ int switch_root(const char *new_root, (void) mkdir_p_label(resolved_old_root_after, 0755); } + /* We are about to unmount various file systems with MNT_DETACH (either explicitly via umount() or + * indirectly via pivot_root()), and thus do not synchronously wait for them to be fully sync'ed — + * all while making them invisible/inaccessible in the file system tree for later code. That makes + * sync'ing them then difficult. Let's hence issue a manual sync() here, so that we at least can + * guarantee all file systems are an a good state before entering this state. */ + sync(); + /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted * MS_SHARED. Hence remount them MS_PRIVATE here as a work-around. *