[systemd-devel] [PATCH] machine-id-setup: generate /etc/machine-id if it doesn't exist

Ramkumar Ramachandra artagnon at gmail.com
Fri Nov 30 02:24:00 PST 2012


/etc/machine-id does not exist on all distributions, and DBus puts its
machine-id in /var/lib/dbus/machine-id.  One test in test-id128 fails
if the file isn't present.  Make systemd-machine-id-setup create the
file if it doesn't exist, and fix a failing test in test-id128.

Signed-off-by: Ramkumar Ramachandra <artagnon at gmail.com>
---
 Ramkumar Ramachandra wrote:
 > $ ./test-id128
 > random: a08ea8ed34594d4bbd953dd182ec86f9
 > Assertion 'sd_id128_get_machine(&id) == 0' failed at
 > src/test/test-id128.c:41, function main(). Aborting.
 > [1]    8017 abort (core dumped)  ./test-id128
 
 Okay, this test fails because I don't have a /etc/machine-id -- I
 thought systemd is supposed to create it?  However, from the logic in
 src/core/machine-id-setup.c, it looks like although open() is called
 with O_CREAT on /etc/machine-id, systemd barfs if the file isn't
 present.  How about changing this?

 src/core/machine-id-setup.c |   12 +++++-------
 src/test/test-id128.c       |    6 ++++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 7f4c23b..3f21d58 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -168,12 +168,8 @@ int machine_id_setup(void) {
                 writable = true;
         else {
                 fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
-                if (fd < 0) {
-                        umask(m);
-                        log_error("Cannot open /etc/machine-id: %m");
-                        return -errno;
-                }
-
+                if (fd < 0)
+                        goto generate;
                 writable = false;
         }
 
@@ -192,7 +188,9 @@ int machine_id_setup(void) {
                 }
         }
 
-        /* Hmm, so, the id currently stored is not useful, then let's
+generate:
+        /* Hmm, so, either /etc/machine-id doesn't exist, the id
+         * currently stored is not useful, then let's
          * generate one */
 
         r = generate(id);
diff --git a/src/test/test-id128.c b/src/test/test-id128.c
index bfd743e..60902d0 100644
--- a/src/test/test-id128.c
+++ b/src/test/test-id128.c
@@ -38,8 +38,10 @@ int main(int argc, char *argv[]) {
         assert_se(sd_id128_from_string(t, &id2) == 0);
         assert_se(sd_id128_equal(id, id2));
 
-        assert_se(sd_id128_get_machine(&id) == 0);
-        printf("machine: %s\n", sd_id128_to_string(id, t));
+        if (sd_id128_get_machine(&id) < 0)
+                printf("machine: run systemd-machine-id-setup first\n");
+        else
+                printf("machine: %s\n", sd_id128_to_string(id, t));
 
         assert_se(sd_id128_get_boot(&id) == 0);
         printf("boot: %s\n", sd_id128_to_string(id, t));
-- 
1.7.8.1.362.g5d6df.dirty



More information about the systemd-devel mailing list