From 2b7c8328474b5f5cb9b6c79ec5578da592f9b3dc Mon Sep 17 00:00:00 2001 From: David Teigland Date: Tue, 7 Nov 2023 14:30:48 -0600 Subject: [PATCH] lvmdevices: print changes for product_uuid or hostname used by the recently added refresh feature --- lib/commands/toolcontext.c | 6 +--- lib/device/device_id.c | 58 ++++++++++++++++++++++++++------------ tools/lvmdevices.c | 43 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 23 deletions(-) diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 250af3f28..b91a88522 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -634,18 +634,14 @@ static void _init_device_ids_refresh(struct cmd_context *cmd) check_hostname = 1; } - /* product_uuid is preferred */ - if (check_product_uuid) { const char *sysfs_dir = cmd->device_id_sysfs_dir ?: dm_sysfs_dir(); if (dm_snprintf(path, sizeof(path), "%sdevices/virtual/dmi/id/product_uuid", sysfs_dir) < 0) return; if (get_sysfs_value(path, uuid, sizeof(uuid), 0) && uuid[0]) cmd->product_uuid = dm_pool_strdup(cmd->libmem, uuid);; - if (cmd->product_uuid) { + if (cmd->product_uuid) cmd->device_ids_check_product_uuid = 1; - return; - } } if (check_hostname && cmd->hostname) diff --git a/lib/device/device_id.c b/lib/device/device_id.c index 497aeb8f4..948d153b4 100644 --- a/lib/device/device_id.c +++ b/lib/device/device_id.c @@ -44,6 +44,10 @@ static char _devices_lockfile[PATH_MAX]; static char _devices_file_version[VERSION_LINE_MAX]; static const char *_searched_file = DEFAULT_RUN_DIR "/searched_devnames"; +/* Only for displaying in lvmdevices command output. */ +char devices_file_hostname_orig[PATH_MAX]; +char devices_file_product_uuid_orig[PATH_MAX]; + char *devices_file_version(void) { return _devices_file_version; @@ -1044,13 +1048,15 @@ int device_ids_read(struct cmd_context *cmd) return 1; /* - * The use_devices list should rarely if ever be non-empty at this - * point, it means device_ids_read has been called twice. - * If we wanted to redo reading the file, we'd need to - * free_dus(&cmd->use_devices) and clear the MATCHED_USE_ID flag in all - * dev->flags. + * Note: lvmdevices calls device_ids_read() a second + * time to get the original entries to compare with + * updated entries. Prior to calling it again, it + * moves the cmd->use_devices entries out of the way. + * Otherwise, device_ids_read() should only be called + * once at the start of a command. */ if (!dm_list_empty(&cmd->use_devices)) { + /* shouldn't happen */ log_debug("device_ids_read already done"); return 1; } @@ -1071,11 +1077,17 @@ int device_ids_read(struct cmd_context *cmd) continue; if (!strncmp(line, "HOSTNAME", 8)) { - if (!cmd->device_ids_check_hostname) - continue; - hostname_found = 1; _copy_idline_str(line, check_id, sizeof(check_id)); log_debug("read devices file hostname %s", check_id); + + /* Save original for lvmdevices output. */ + if (!strcmp(cmd->name, "lvmdevices")) + strncpy(devices_file_hostname_orig, check_id, PATH_MAX-1); + + if (!cmd->device_ids_check_hostname) + continue; + + hostname_found = 1; if (cmd->hostname && strcmp(cmd->hostname, check_id)) { log_debug("Devices file hostname %s vs local %s.", check_id[0] ? check_id : "none", cmd->hostname ?: "none"); @@ -1085,11 +1097,17 @@ int device_ids_read(struct cmd_context *cmd) } if (!strncmp(line, "PRODUCT_UUID", 12)) { - if (!cmd->device_ids_check_product_uuid) - continue; - product_uuid_found = 1; _copy_idline_str(line, check_id, sizeof(check_id)); log_debug("read devices file product_uuid %s", check_id); + + /* Save original for lvmdevices output. */ + if (!strcmp(cmd->name, "lvmdevices")) + strncpy(devices_file_product_uuid_orig, check_id, PATH_MAX-1); + + if (!cmd->device_ids_check_product_uuid) + continue; + + product_uuid_found = 1; if ((!cmd->product_uuid && check_id[0]) || (cmd->product_uuid && strcmp(cmd->product_uuid, check_id))) { log_debug("Devices file product_uuid %s vs local %s.", @@ -1163,13 +1181,16 @@ int device_ids_read(struct cmd_context *cmd) } if (fclose(fp)) stack; - - if (!product_uuid_found && !hostname_found && - (cmd->device_ids_check_product_uuid || cmd->device_ids_check_hostname)) { - cmd->device_ids_refresh_trigger = 1; - log_debug("Devices file refresh due to no product_uuid or hostname."); - } + if (!product_uuid_found && cmd->device_ids_check_product_uuid) { + cmd->device_ids_refresh_trigger = 1; + log_debug("Devices file refresh: missing product_uuid"); + } else if ((!product_uuid_found && !hostname_found) && + (cmd->device_ids_check_product_uuid || cmd->device_ids_check_hostname)) { + cmd->device_ids_refresh_trigger = 1; + log_debug("Devices file refresh: missing product_uuid and hostname"); + } + return ret; } @@ -1274,9 +1295,10 @@ int device_ids_write(struct cmd_context *cmd) fprintf(fp, "# LVM uses devices listed in this file.\n"); fprintf(fp, "# Created by LVM command %s pid %d at %s", cmd->name, getpid(), ctime(&t)); + /* if product_uuid is included, then hostname is unnecessary */ if (cmd->product_uuid && cmd->device_ids_check_product_uuid) fprintf(fp, "PRODUCT_UUID=%s\n", cmd->product_uuid); - if (cmd->hostname && cmd->device_ids_check_hostname) + else if (cmd->hostname && cmd->device_ids_check_hostname) fprintf(fp, "HOSTNAME=%s\n", cmd->hostname); if (dm_snprintf(version_buf, VERSION_LINE_MAX, "VERSION=%u.%u.%u", DEVICES_FILE_MAJOR, DEVICES_FILE_MINOR, df_counter+1) < 0) diff --git a/tools/lvmdevices.c b/tools/lvmdevices.c index 5bc14fce8..8c56f8eae 100644 --- a/tools/lvmdevices.c +++ b/tools/lvmdevices.c @@ -21,6 +21,9 @@ /* coverity[unnecessary_header] needed for MuslC */ #include +extern char devices_file_hostname_orig[PATH_MAX]; +extern char devices_file_product_uuid_orig[PATH_MAX]; + static void _search_devs_for_pvids(struct cmd_context *cmd, struct dm_list *search_pvids, struct dm_list *found_devs) { struct dev_iter *iter; @@ -151,11 +154,51 @@ static void _print_check(struct cmd_context *cmd) dm_list_init(&done_old); dm_list_init(&done_new); + /* + * Move the entries that have been processed out of the way so + * original entries can be added to use_devices by device_ids_read(). + * The processed entries are moved back to cmd->use_devices at the + * end of this function. + */ dm_list_splice(&use_new, &cmd->use_devices); device_ids_read(cmd); dm_list_splice(&use_old, &cmd->use_devices); dm_list_init(&cmd->use_devices); + /* + * Check if system identifier is changed. + */ + + if (cmd->device_ids_refresh_trigger) { + int include_product_uuid = 0; + int include_hostname = 0; + + if (cmd->product_uuid && cmd->device_ids_check_product_uuid) { + include_product_uuid = 1; + if (devices_file_product_uuid_orig[0] && + strcmp(cmd->product_uuid, devices_file_product_uuid_orig)) + log_print_unless_silent("PRODUCT_UUID=%s (old %s): update", + cmd->product_uuid, devices_file_product_uuid_orig); + else if (!devices_file_product_uuid_orig[0]) + log_print_unless_silent("PRODUCT_UUID=%s: add", cmd->product_uuid); + } + if (!include_product_uuid && devices_file_product_uuid_orig[0]) + log_print_unless_silent("PRODUCT_UUID=%s: remove", devices_file_product_uuid_orig); + + /* hostname is only updated or added if product_uuid is not included */ + if (cmd->hostname && cmd->device_ids_check_hostname && !include_product_uuid) { + include_hostname = 1; + if (devices_file_hostname_orig[0] && + strcmp(cmd->hostname, devices_file_hostname_orig)) + log_print_unless_silent("HOSTNAME=%s (old %s): update", + cmd->hostname, devices_file_hostname_orig); + else if (!devices_file_hostname_orig[0]) + log_print_unless_silent("HOSTNAME=%s: add", cmd->hostname); + } + if (!include_hostname && devices_file_hostname_orig[0]) + log_print_unless_silent("HOSTNAME=%s: remove", devices_file_hostname_orig); + } + /* * Check entries with proper id types. */