summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarald Seiler <hws@denx.de>2020-09-06 21:35:33 +0200
committerHarald Seiler <hws@denx.de>2020-10-19 16:28:22 +0200
commitc261a5d01469a3f5b6c448c066ea7ad6d14a5300 (patch)
tree0c127218ac212eca53d6fbd7f5a77f7735ea7c31
parent3023f2fead7df9f219acf3c8595d974e31ad23cc (diff)
downloadsystemd-c261a5d01469a3f5b6c448c066ea7ad6d14a5300.tar.gz
machine-id-setup: sync before committing machine-id
sync() before committing a transient machine-id to disk. This will ensure that any filesystem changes made by first-boot units will have been persisted before the first boot is marked as completed.
-rw-r--r--src/core/machine-id-setup.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 4e9a8d266e..c55e0b5321 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -202,10 +202,22 @@ finish:
int machine_id_commit(const char *root) {
_cleanup_close_ int fd = -1, initial_mntns_fd = -1;
- const char *etc_machine_id;
+ const char *etc_machine_id, *sync_path;
sd_id128_t id;
int r;
+ /* Before doing anything, sync everything to ensure any changes by first-boot units are persisted.
+ *
+ * First, explicitly sync the file systems we care about and check if it worked. */
+ FOREACH_STRING(sync_path, "/etc/", "/var/") {
+ r = syncfs_path(AT_FDCWD, sync_path);
+ if (r < 0)
+ return log_error_errno(r, "Cannot sync %s: %m", sync_path);
+ }
+
+ /* Afterwards, sync() the rest too, but we can't check the return value for these. */
+ sync();
+
/* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
* in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
* original mount namespace, thus revealing the file that was just created. */