]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
id128-util: use common implementation of helper to get/validate product ID
authorLennart Poettering <lennart@poettering.net>
Wed, 28 Apr 2021 12:15:36 +0000 (14:15 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 29 Apr 2021 14:39:09 +0000 (16:39 +0200)
src/hostname/hostnamed.c
src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h
src/shared/machine-id-setup.c

index 2ece8b2fbd9bbf0f8ab697ae45f9f3811bd4b676..92af4d19137705192b165b3ecd1a87acc91e87c7 100644 (file)
@@ -932,27 +932,23 @@ static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *
 static int method_get_product_uuid(sd_bus_message *m, void *userdata, sd_bus_error *error) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         Context *c = userdata;
-        bool has_uuid = false;
         int interactive, r;
         sd_id128_t uuid;
 
         assert(m);
         assert(c);
 
-        r = id128_read("/sys/class/dmi/id/product_uuid", ID128_UUID, &uuid);
-        if (r == -ENOENT)
-                r = id128_read("/sys/firmware/devicetree/base/vm,uuid", ID128_UUID, &uuid);
-        if (r < 0)
-                log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
-                               "Failed to read product UUID, ignoring: %m");
-        else if (sd_id128_is_null(uuid) || sd_id128_is_allf(uuid))
-                log_debug("DMI product UUID " SD_ID128_FORMAT_STR " is all 0x00 or all 0xFF, ignoring.", SD_ID128_FORMAT_VAL(uuid));
-        else
-                has_uuid = true;
+        r = id128_get_product(&uuid);
+        if (r < 0) {
+                if (r == -EADDRNOTAVAIL)
+                        log_debug_errno(r, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
+                else
+                        log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
+                                       "Failed to read product UUID, ignoring: %m");
 
-        if (!has_uuid)
                 return sd_bus_error_set(error, BUS_ERROR_NO_PRODUCT_UUID,
                                         "Failed to read product UUID from firmware.");
+        }
 
         r = sd_bus_message_read(m, "b", &interactive);
         if (r < 0)
index a3f6da6381ca980606d84017a41bd165463a638e..2074771a41bfdb209cc4ecbde36815a10367e1ea 100644 (file)
@@ -210,3 +210,25 @@ sd_id128_t id128_make_v4_uuid(sd_id128_t id) {
 }
 
 DEFINE_HASH_OPS(id128_hash_ops, sd_id128_t, id128_hash_func, id128_compare_func);
+
+int id128_get_product(sd_id128_t *ret) {
+        sd_id128_t uuid;
+        int r;
+
+        assert(ret);
+
+        /* Reads the systems product UUID from DMI or devicetree (where it is located on POWER). This is
+         * particularly relevant in VM environments, where VM managers typically place a VM uuid there. */
+
+        r = id128_read("/sys/class/dmi/id/product_uuid", ID128_UUID, &uuid);
+        if (r == -ENOENT)
+                r = id128_read("/sys/firmware/devicetree/base/vm,uuid", ID128_UUID, &uuid);
+        if (r < 0)
+                return r;
+
+        if (sd_id128_is_null(uuid) || sd_id128_is_allf(uuid))
+                return -EADDRNOTAVAIL; /* Recognizable error */
+
+        *ret = uuid;
+        return 0;
+}
index 6b09bcd96a44ebccfea1b39d73feed169343b96c..053ef0a6a83f091a9aeddfa32b601d0e53dde0dd 100644 (file)
@@ -36,3 +36,5 @@ int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) _pure_;
 extern const struct hash_ops id128_hash_ops;
 
 sd_id128_t id128_make_v4_uuid(sd_id128_t id);
+
+int id128_get_product(sd_id128_t *ret);
index 6d15f9cd09cf7647e0195e73122b14533b745453..dd4c74b19c960ad17b807b351ac422c61e007aec 100644 (file)
@@ -62,16 +62,10 @@ static int generate_machine_id(const char *root, sd_id128_t *ret) {
 
                 } else if (detect_vm() == VIRTUALIZATION_KVM) {
 
-                        /* If we are not running in a container, see if we are
-                         * running in qemu/kvm and a machine ID was passed in
-                         * via -uuid on the qemu/kvm command line */
+                        /* If we are not running in a container, see if we are running in qemu/kvm and a
+                         * machine ID was passed in via -uuid on the qemu/kvm command line */
 
-                        if (id128_read("/sys/class/dmi/id/product_uuid", ID128_UUID, ret) >= 0) {
-                                log_info("Initializing machine ID from KVM UUID.");
-                                return 0;
-                        }
-                        /* on POWER, it's exported here instead */
-                        if (id128_read("/sys/firmware/devicetree/base/vm,uuid", ID128_UUID, ret) >= 0) {
+                        if (id128_get_product(ret) >= 0) {
                                 log_info("Initializing machine ID from KVM UUID.");
                                 return 0;
                         }