From d98b217dee02f436c1c3fe99e2c87b92a1019d9d Mon Sep 17 00:00:00 2001 From: David Teigland Date: Dec 12 2023 19:46:26 +0000 Subject: sanlock: use product_uuid for host name When there is no host name specified on the command line or in the config file, use the value from /sys/devices/virtual/dmi/id/product_uuid instead of generating a random UUID each time the daemon is started. If no product_uuid exists, the random UUID will be generated for the host name. --- diff --git a/src/main.c b/src/main.c index a9ceee5..4e7f3c0 100644 --- a/src/main.c +++ b/src/main.c @@ -1473,37 +1473,64 @@ int get_rand(int a, int b) return a + (int) (((float)(b - a + 1)) * rv / (RAND_MAX+1.0)); } +static void read_product_uuid(char *buf, size_t buf_size) +{ + FILE *fp; + size_t len; + + if (!(fp = fopen("/sys/devices/virtual/dmi/id/product_uuid", "r"))) + return; + + if (!fgets(buf, buf_size, fp)) + return; + + if ((len = strlen(buf)) && buf[len - 1] == '\n') + buf[--len] = '\0'; + + fclose(fp); +} + static void setup_host_name(void) { - struct utsname name; + char product_uuid[SANLK_NAME_LEN+1] = { 0 }; char uuid[37]; + struct utsname name; uuid_t uu; int ret; - memset(rand_state, 0, sizeof(rand_state)); - initstate(time(NULL), rand_state, sizeof(rand_state)); - - /* use host name from command line */ + /* + * Get host name value from: + * 1. command line + * 2. config file + * 3. /sys/devices/virtual/dmi/id/product_uuid + * 4. generate random uuid that won't collide with another host + */ if (com.our_host_name[0]) { memcpy(our_host_name_global, com.our_host_name, SANLK_NAME_LEN); return; } - /* make up something that's likely to be different among hosts */ - memset(&our_host_name_global, 0, sizeof(our_host_name_global)); + memset(product_uuid, 0, sizeof(product_uuid)); memset(&name, 0, sizeof(name)); memset(&uuid, 0, sizeof(uuid)); + read_product_uuid(product_uuid, sizeof(product_uuid)); uname(&name); - uuid_generate(uu); - uuid_unparse_lower(uu, uuid); - ret = snprintf(our_host_name_global, NAME_ID_SIZE, "%s.", uuid); + if (product_uuid[0]) { + ret = snprintf(our_host_name_global, SANLK_NAME_LEN, "%s.", product_uuid); + } else { + memset(rand_state, 0, sizeof(rand_state)); + initstate(time(NULL), rand_state, sizeof(rand_state)); + uuid_generate(uu); + uuid_unparse_lower(uu, uuid); + ret = snprintf(our_host_name_global, SANLK_NAME_LEN, "%s.", uuid); + } - if (ret < NAME_ID_SIZE) - memcpy(our_host_name_global+ret, name.nodename, NAME_ID_SIZE-ret); + if (ret < SANLK_NAME_LEN) + memcpy(our_host_name_global+ret, name.nodename, SANLK_NAME_LEN-ret); } static void setup_limits(void) diff --git a/src/sanlock.8 b/src/sanlock.8 index 0776b8f..5295bf5 100644 --- a/src/sanlock.8 +++ b/src/sanlock.8 @@ -120,9 +120,9 @@ delta lease disk sector, reading it back after a delay, and verifying it is the same. .IP \[bu] -If a unique host name is not specified, sanlock generates a uuid to use -as the host's name. The delta lease algorithm depends on hosts using -unique names. +If a unique host name is not specified, sanlock uses the product_uuid +if one is available, otherwise generates a uuid to use as the host's +name. The delta lease algorithm depends on hosts using unique names. .IP \[bu] The application on each host should be configured with a unique host_id,