From 41ba6536cf1a02b6560a73a92c2e8951efbc83d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Knecht?= Date: Fri, 28 Jun 2024 15:40:14 +0200 Subject: [PATCH] facts/hardware: Fix support_discard block device fact (#83480) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, `support_discard` simply returned the value of `/sys/block/{device}/queue/discard_granularity`. When its value is `0`, then the block device doesn't support discards; _however_, it being greater than zero doesn't necessarily mean that the block device _does_ support discards. But another indication that a block device doesn't support discards is `/sys/block/{device}/queue/discard_max_hw_bytes` being equal to `0` (with the same caveat as above). So if either of those are `0`, set `support_discard` to zero, otherwise set it to the value of `discard_granularity` for backwards compatibility. Signed-off-by: BenoƮt Knecht --- .../fragments/83480-fix-support-discard.yml | 6 +++++ .../module_utils/facts/hardware/linux.py | 22 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/83480-fix-support-discard.yml diff --git a/changelogs/fragments/83480-fix-support-discard.yml b/changelogs/fragments/83480-fix-support-discard.yml new file mode 100644 index 00000000000..8cedf8206b6 --- /dev/null +++ b/changelogs/fragments/83480-fix-support-discard.yml @@ -0,0 +1,6 @@ +--- +bugfixes: + - facts - `support_discard` now returns `0` if either `discard_granularity` + or `discard_max_hw_bytes` is zero; otherwise it returns the value of + `discard_granularity`, as before + (https://github.com/ansible/ansible/pull/83480). diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py index cd0f41dcc26..abd8dd5c617 100644 --- a/lib/ansible/module_utils/facts/hardware/linux.py +++ b/lib/ansible/module_utils/facts/hardware/linux.py @@ -773,10 +773,24 @@ class LinuxHardware(Hardware): if serial: d['serial'] = serial - for key, test in [('removable', '/removable'), - ('support_discard', '/queue/discard_granularity'), - ]: - d[key] = get_file_content(sysdir + test) + d['removable'] = get_file_content(sysdir + '/removable') + + # Historically, `support_discard` simply returned the value of + # `/sys/block/{device}/queue/discard_granularity`. When its value + # is `0`, then the block device doesn't support discards; + # _however_, it being greater than zero doesn't necessarily mean + # that the block device _does_ support discards. + # + # Another indication that a block device doesn't support discards + # is `/sys/block/{device}/queue/discard_max_hw_bytes` being equal + # to `0` (with the same caveat as above). So if either of those are + # `0`, set `support_discard` to zero, otherwise set it to the value + # of `discard_granularity` for backwards compatibility. + d['support_discard'] = ( + '0' + if get_file_content(sysdir + '/queue/discard_max_hw_bytes') == '0' + else get_file_content(sysdir + '/queue/discard_granularity') + ) if diskname in devs_wwn: d['wwn'] = devs_wwn[diskname]