From 92ca3793197dd1059aa9f4f77e80c6def34395e5 Mon Sep 17 00:00:00 2001 From: Klention Mali <45871249+klention@users.noreply.github.com> Date: Mon, 11 Aug 2025 06:43:47 +0200 Subject: [PATCH] lvm_pv - Fixes #10444 - Partition device not found (#10596) * Skip rescan for partition devices in LVM PV module Adds a check to prevent unnecessary rescan attempts on partition devices in the LVM physical volume module. When a device is actually a partition, attempting to rescan it via sysfs would fail since partitions don't have a rescan interface. This change improves error handling by gracefully skipping the rescan operation when dealing with partition devices, avoiding misleading warning messages. * Rewrote device rescan logic Added changelog fragment * Add issue reference to lvm_pv changelog entry --- changelogs/fragments/lvm_pv.yml | 3 +++ plugins/modules/lvm_pv.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/lvm_pv.yml diff --git a/changelogs/fragments/lvm_pv.yml b/changelogs/fragments/lvm_pv.yml new file mode 100644 index 0000000000..d0198d7ffb --- /dev/null +++ b/changelogs/fragments/lvm_pv.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - lvm_pv - properly detect SCSI or NVMe devices to rescan (https://github.com/ansible-collections/community.general/issues/10444, https://github.com/ansible-collections/community.general/pull/10596). diff --git a/plugins/modules/lvm_pv.py b/plugins/modules/lvm_pv.py index 15740db8c1..6cc4c4ebde 100644 --- a/plugins/modules/lvm_pv.py +++ b/plugins/modules/lvm_pv.py @@ -91,9 +91,22 @@ def get_pv_size(module, device): def rescan_device(module, device): """Perform storage rescan for the device.""" - # Extract the base device name (e.g., /dev/sdb -> sdb) base_device = os.path.basename(device) - rescan_path = "/sys/block/{0}/device/rescan".format(base_device) + is_partition = "/sys/class/block/{0}/partition".format(base_device) + + # Determine parent device if partition exists + parent_device = base_device + if os.path.exists(is_partition): + parent_device = ( + base_device.rpartition('p')[0] if base_device.startswith('nvme') + else base_device.rstrip('0123456789') + ) + + # Determine rescan path + rescan_path = "/sys/block/{0}/device/{1}".format( + parent_device, + "rescan_controller" if base_device.startswith('nvme') else "rescan" + ) if os.path.exists(rescan_path): try: @@ -102,9 +115,8 @@ def rescan_device(module, device): return True except IOError as e: module.warn("Failed to rescan device {0}: {1}".format(device, str(e))) - return False else: - module.warn("Rescan path not found for device {0}".format(device)) + module.warn("Rescan path does not exist for device {0}".format(device)) return False