summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-04-28 14:15:36 +0200
committerLennart Poettering <lennart@poettering.net>2021-04-29 16:39:09 +0200
commitb4be4ff8ae0c7ab54a60f77b95f967131e1fcc6b (patch)
treecfa394d481c209162b5fb64a78c0a107600856f5 /src
parentf813b62316395205f4c744cde43885081b5f88ae (diff)
downloadsystemd-b4be4ff8ae0c7ab54a60f77b95f967131e1fcc6b.tar.gz
id128-util: use common implementation of helper to get/validate product ID
Diffstat (limited to 'src')
-rw-r--r--src/hostname/hostnamed.c20
-rw-r--r--src/libsystemd/sd-id128/id128-util.c22
-rw-r--r--src/libsystemd/sd-id128/id128-util.h2
-rw-r--r--src/shared/machine-id-setup.c12
4 files changed, 35 insertions, 21 deletions
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 2ece8b2fbd..92af4d1913 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -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)
diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c
index a3f6da6381..2074771a41 100644
--- a/src/libsystemd/sd-id128/id128-util.c
+++ b/src/libsystemd/sd-id128/id128-util.c
@@ -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;
+}
diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h
index 6b09bcd96a..053ef0a6a8 100644
--- a/src/libsystemd/sd-id128/id128-util.h
+++ b/src/libsystemd/sd-id128/id128-util.h
@@ -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);
diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c
index 6d15f9cd09..dd4c74b19c 100644
--- a/src/shared/machine-id-setup.c
+++ b/src/shared/machine-id-setup.c
@@ -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;
}