]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine-id-setup: port machine_id_commit() to new id128-util.c APIs
authorLennart Poettering <lennart@poettering.net>
Thu, 21 Jul 2016 17:12:50 +0000 (19:12 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 22 Jul 2016 10:59:36 +0000 (12:59 +0200)
src/core/machine-id-setup.c
src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h
src/nspawn/nspawn.c

index 62f80833ddf9fcba35b7b94de9c1769f6606d7ef..423d4ff69dfc882fd891d9b19aafbda021e864ac 100644 (file)
@@ -295,9 +295,13 @@ int machine_id_setup(const char *root, sd_id128_t machine_id) {
 int machine_id_commit(const char *root) {
         _cleanup_close_ int fd = -1, initial_mntns_fd = -1;
         const char *etc_machine_id;
-        char id[34]; /* 32 + \n + \0 */
+        sd_id128_t id;
         int r;
 
+        /* 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. */
+
         etc_machine_id = prefix_roota(root, "/etc/machine-id");
 
         r = path_is_mount_point(etc_machine_id, 0);
@@ -313,10 +317,6 @@ int machine_id_commit(const char *root) {
         if (fd < 0)
                 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
 
-        r = read_machine_id(fd, id);
-        if (r < 0)
-                return log_error_errno(r, "We didn't find a valid machine ID in %s.", etc_machine_id);
-
         r = fd_is_temporary_fs(fd);
         if (r < 0)
                 return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
@@ -325,6 +325,10 @@ int machine_id_commit(const char *root) {
                 return -EROFS;
         }
 
+        r = id128_read_fd(fd, ID128_PLAIN, &id);
+        if (r < 0)
+                return log_error_errno(r, "We didn't find a valid machine ID in %s.", etc_machine_id);
+
         fd = safe_close(fd);
 
         /* Store current mount namespace */
@@ -343,15 +347,9 @@ int machine_id_commit(const char *root) {
                 return log_error_errno(errno, "Failed to unmount transient %s file in our private namespace: %m", etc_machine_id);
 
         /* Update a persistent version of etc_machine_id */
-        fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
-        if (fd < 0)
-                return log_error_errno(errno, "Cannot open for writing %s. This is mandatory to get a persistent machine-id: %m", etc_machine_id);
-
-        r = write_machine_id(fd, id);
+        r = id128_write(etc_machine_id, ID128_PLAIN, id, true);
         if (r < 0)
-                return log_error_errno(r, "Cannot write %s: %m", etc_machine_id);
-
-        fd = safe_close(fd);
+                return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
 
         /* Return to initial namespace and proceed a lazy tmpfs unmount */
         r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
index c1742cab0e55dee35e28034f39401d0a4ab0d640..aaac838b5945621cdc8ad92f0745406d21f6b1b9 100644 (file)
@@ -18,6 +18,7 @@
 ***/
 
 #include <fcntl.h>
+#include <unistd.h>
 
 #include "fd-util.h"
 #include "hexdecoct.h"
@@ -140,9 +141,10 @@ int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
         return id128_read_fd(fd, f, ret);
 }
 
-int id128_write_fd(int fd, Id128Format f, sd_id128_t id) {
+int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
         char buffer[36 + 2];
         size_t sz;
+        int r;
 
         assert(fd >= 0);
         assert(f < _ID128_FORMAT_MAX);
@@ -157,15 +159,24 @@ int id128_write_fd(int fd, Id128Format f, sd_id128_t id) {
                 sz = 37;
         }
 
-        return loop_write(fd, buffer, sz, false);
+        r = loop_write(fd, buffer, sz, false);
+        if (r < 0)
+                return r;
+
+        if (do_sync) {
+                if (fsync(fd) < 0)
+                        return -errno;
+        }
+
+        return r;
 }
 
-int id128_write(const char *p, Id128Format f, sd_id128_t id) {
+int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
         _cleanup_close_ int fd = -1;
 
         fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
         if (fd < 0)
                 return -errno;
 
-        return id128_write_fd(fd, f, id);
+        return id128_write_fd(fd, f, id, do_sync);
 }
index 73e4c710c45c8a8a8761ff511a41c25e0abeb1d2..3ba59acbcab33ae3074f198c2b4bfb0efadc6d6c 100644 (file)
@@ -41,5 +41,5 @@ typedef enum Id128Format {
 int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret);
 int id128_read(const char *p, Id128Format f, sd_id128_t *ret);
 
-int id128_write_fd(int fd, Id128Format f, sd_id128_t id);
-int id128_write(const char *p, Id128Format f, sd_id128_t id);
+int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync);
+int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync);
index 4c1d79418dff683b5b850ed87e5cec095eb62335..da8bee32441937b272355c9ce14e6c25f160d77f 100644 (file)
@@ -1287,7 +1287,7 @@ static int setup_boot_id(const char *dest) {
         if (r < 0)
                 return log_error_errno(r, "Failed to generate random boot id: %m");
 
-        r = id128_write(from, ID128_UUID, rnd);
+        r = id128_write(from, ID128_UUID, rnd, false);
         if (r < 0)
                 return log_error_errno(r, "Failed to write boot id: %m");