[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v3 2/2] daemon: add option to read host uuid from /etc/machine-id



Daemon config parameter switch between reading host uuid
either from smbios or machine-id:

host_uuid_source = "smbios|machine-id"

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy virtuozzo com>
---
 daemon/libvirtd-config.c    |  2 ++
 daemon/libvirtd-config.h    |  1 +
 daemon/libvirtd.aug         |  1 +
 daemon/libvirtd.c           | 38 +++++++++++++++++++++++++++++++++++---
 daemon/libvirtd.conf        | 15 +++++++++++----
 daemon/test_libvirtd.aug.in |  1 +
 6 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/daemon/libvirtd-config.c b/daemon/libvirtd-config.c
index 7a448f9..45280e9 100644
--- a/daemon/libvirtd-config.c
+++ b/daemon/libvirtd-config.c
@@ -374,6 +374,7 @@ daemonConfigFree(struct daemonConfig *data)
     VIR_FREE(data->crl_file);
 
     VIR_FREE(data->host_uuid);
+    VIR_FREE(data->host_uuid_source);
     VIR_FREE(data->log_filters);
     VIR_FREE(data->log_outputs);
 
@@ -463,6 +464,7 @@ daemonConfigLoadOptions(struct daemonConfig *data,
     GET_CONF_UINT(conf, filename, audit_logging);
 
     GET_CONF_STR(conf, filename, host_uuid);
+    GET_CONF_STR(conf, filename, host_uuid_source);
 
     GET_CONF_UINT(conf, filename, log_level);
     GET_CONF_STR(conf, filename, log_filters);
diff --git a/daemon/libvirtd-config.h b/daemon/libvirtd-config.h
index 3e1971d..672e9ad 100644
--- a/daemon/libvirtd-config.h
+++ b/daemon/libvirtd-config.h
@@ -28,6 +28,7 @@
 
 struct daemonConfig {
     char *host_uuid;
+    char *host_uuid_source;
 
     int listen_tls;
     int listen_tcp;
diff --git a/daemon/libvirtd.aug b/daemon/libvirtd.aug
index a70aa1d..6561480 100644
--- a/daemon/libvirtd.aug
+++ b/daemon/libvirtd.aug
@@ -86,6 +86,7 @@ module Libvirtd =
                              | bool_entry "admin_keepalive_required"
 
    let misc_entry = str_entry "host_uuid"
+                  | str_entry "host_uuid_source"
 
    (* Each enty in the config is one of the following three ... *)
    let entry = network_entry
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 7ec02ad..a489136 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1076,6 +1076,39 @@ static int migrateProfile(void)
     return ret;
 }
 
+static int
+daemonSetupHostUUID(const struct daemonConfig *config)
+{
+    static const char *machine_id = "/etc/machine-id";
+    char buf[VIR_UUID_STRING_BUFLEN];
+    const char *uuid;
+
+    if (config->host_uuid) {
+        uuid = config->host_uuid;
+    } else if (!config->host_uuid_source ||
+               STREQ(config->host_uuid_source, "smbios")) {
+        /* smbios UUID is fetched on demand in virGetHostUUID */
+        return 0;
+    } else if (STREQ(config->host_uuid_source, "machine-id")) {
+        if (virFileReadBufQuiet(machine_id, buf, sizeof(buf)) < 0) {
+            VIR_ERROR(_("Can't read %s"), machine_id);
+            return -1;
+        }
+
+        uuid = buf;
+    } else {
+        VIR_ERROR(_("invalid UUID source: %s"), config->host_uuid_source);
+        return -1;
+    }
+
+    if (virSetHostUUIDStr(uuid)) {
+        VIR_ERROR(_("invalid host UUID: %s"), uuid);
+        return -1;
+    }
+
+    return 0;
+}
+
 /* Print command-line usage. */
 static void
 daemonUsage(const char *argv0, bool privileged)
@@ -1295,9 +1328,8 @@ int main(int argc, char **argv) {
         exit(EXIT_FAILURE);
     }
 
-    if (config->host_uuid &&
-        virSetHostUUIDStr(config->host_uuid) < 0) {
-        VIR_ERROR(_("invalid host UUID: %s"), config->host_uuid);
+    if (daemonSetupHostUUID(config) < 0) {
+        VIR_ERROR(_("Can't setup host uuid"));
         exit(EXIT_FAILURE);
     }
 
diff --git a/daemon/libvirtd.conf b/daemon/libvirtd.conf
index d2c439c..1c1fa7f 100644
--- a/daemon/libvirtd.conf
+++ b/daemon/libvirtd.conf
@@ -410,10 +410,16 @@
 
 ###################################################################
 # UUID of the host:
-# Provide the UUID of the host here in case the command
-# 'dmidecode -s system-uuid' does not provide a valid uuid. In case
-# 'dmidecode' does not provide a valid UUID and none is provided here, a
-# temporary UUID will be generated.
+# Host UUID is read from one of the sources specified in host_uuid_source.
+#
+# - 'smbios': fetch the UUID from 'dmidecode -s system-uuid'
+# - 'machine-id': fetch the UUID from /etc/machine-id
+#
+# The host_uuid_source default is 'smbios'. If 'dmidecode' does not provide
+# a valid UUID a temporary UUID will be generated.
+#
+# Another option is to specify host UUID in host_uuid.
+#
 # Keep the format of the example UUID below. UUID must not have all digits
 # be the same.
 
@@ -421,6 +427,7 @@
 # it with the output of the 'uuidgen' command and then
 # uncomment this entry
 #host_uuid = "00000000-0000-0000-0000-000000000000"
+#host_uuid_source = "smbios"
 
 ###################################################################
 # Keepalive protocol:
diff --git a/daemon/test_libvirtd.aug.in b/daemon/test_libvirtd.aug.in
index b0cb7eb..7a03603 100644
--- a/daemon/test_libvirtd.aug.in
+++ b/daemon/test_libvirtd.aug.in
@@ -55,6 +55,7 @@ module Test_libvirtd =
         { "audit_level" = "2" }
         { "audit_logging" = "1" }
         { "host_uuid" = "00000000-0000-0000-0000-000000000000" }
+        { "host_uuid_source" = "smbios" }
         { "keepalive_interval" = "5" }
         { "keepalive_count" = "5" }
         { "keepalive_required" = "1" }
-- 
1.8.3.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]