facts/hardware: Fix support_discard block device fact (#83480)

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 <bknecht@protonmail.ch>
pull/81947/merge
Benoît Knecht 4 months ago committed by GitHub
parent 2930a4664c
commit 41ba6536cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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).

@ -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]

Loading…
Cancel
Save