mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-11-01 10:22:08 +00:00
Name only physical volumes in a restricted LVM scan and keep its own reads from counting as activity
- An explicit --devices list makes LVM open every device in it, while a plain scan opens only known volumes; the list now names only LVM physical volumes, read from udev by lsblk, so no other disk is touched - RAID, multipath and encrypted volumes follow the disks they are built on, instead of turning the whole check off - After each pvs, lvs or vgs run, the disks it read take their counters as the new baseline, so an unused disk can become idle
This commit is contained in:
@@ -526,6 +526,16 @@ def disk_read_policy(disk_name: str, excluded: Optional[set] = None) -> str:
|
||||
return IDLE
|
||||
|
||||
|
||||
def settle_own_reads(disk_names) -> None:
|
||||
"""A reader that has just read these disks itself takes their counters as
|
||||
the new baseline, so its own I/O never counts as activity."""
|
||||
stats = _read_diskstats()
|
||||
with _cache_lock:
|
||||
for name in disk_names:
|
||||
if name in _last_io and name in stats:
|
||||
_last_io[name] = stats[name]
|
||||
|
||||
|
||||
def is_disk_idle(disk_name: str) -> bool:
|
||||
"""True while the disk is being left alone for having no I/O."""
|
||||
ts = _idle_state.get(disk_name)
|
||||
|
||||
@@ -4188,13 +4188,16 @@ def get_system_disks():
|
||||
pass
|
||||
|
||||
try:
|
||||
# 4. Check LVM physical volumes. `--devices` (added when a disk
|
||||
# is parked) keeps pvs from scanning — and waking — standby HDDs.
|
||||
# 4. Check LVM physical volumes. `--devices` keeps pvs off the disks
|
||||
# that are excluded, idle or asleep.
|
||||
dev_args = _lvm_device_args()
|
||||
result = subprocess.run(
|
||||
['pvs', *dev_args, '--noheadings', '-o', 'pv_name,vg_name'],
|
||||
capture_output=True, text=True, timeout=5
|
||||
) if dev_args is not None else None
|
||||
if result is not None:
|
||||
from lvm_device_scope import settle_scan_reads
|
||||
settle_scan_reads()
|
||||
if result is not None and result.returncode == 0:
|
||||
for line in result.stdout.strip().split('\n'):
|
||||
if line.strip():
|
||||
|
||||
@@ -2286,6 +2286,8 @@ class HealthMonitor:
|
||||
text=True,
|
||||
timeout=3
|
||||
)
|
||||
from lvm_device_scope import settle_scan_reads
|
||||
settle_scan_reads()
|
||||
|
||||
if result.returncode != 0:
|
||||
return {'status': 'WARNING', 'reason': 'lvs command failed'}
|
||||
@@ -2310,6 +2312,7 @@ class HealthMonitor:
|
||||
text=True,
|
||||
timeout=3
|
||||
)
|
||||
settle_scan_reads()
|
||||
if vg_result.returncode == 0 and vg_result.stdout.strip():
|
||||
return {'status': 'WARNING', 'reason': 'No active LVM volumes detected'}
|
||||
else:
|
||||
|
||||
@@ -11,11 +11,56 @@
|
||||
# Description:
|
||||
# Applies the automatic disk-read policy to LVM metadata scans in both
|
||||
# Monitor readers. Excluded and idle disks receive no power-mode probe;
|
||||
# their partitions are excluded too. No eligible devices means no scan.
|
||||
# every device built on them is excluded too, and the scan's own reads
|
||||
# are not taken as activity. No eligible devices means no scan.
|
||||
# ==========================================================
|
||||
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import threading
|
||||
|
||||
_NAME = re.compile(r"[A-Za-z0-9._+-]+")
|
||||
_scan_lock = threading.Lock()
|
||||
_scanned_disks = set()
|
||||
|
||||
|
||||
def _topology():
|
||||
"""Every block device by kernel name: its type, its parents and whether
|
||||
it is an LVM physical volume. lsblk reads this from udev, not the disks.
|
||||
A RAID or multipath device appears once per member."""
|
||||
result = subprocess.run(["lsblk", "-J", "-l", "-o", "KNAME,TYPE,PKNAME,FSTYPE"],
|
||||
capture_output=True, text=True, timeout=5)
|
||||
if result.returncode:
|
||||
return None
|
||||
types, parents, volumes = {}, {}, set()
|
||||
for device in json.loads(result.stdout).get("blockdevices") or []:
|
||||
name, parent = device.get("kname"), device.get("pkname")
|
||||
if not isinstance(name, str) or not _NAME.fullmatch(name):
|
||||
return None
|
||||
types[name] = device.get("type")
|
||||
parents.setdefault(name, set())
|
||||
if parent is not None:
|
||||
if not isinstance(parent, str) or not _NAME.fullmatch(parent):
|
||||
return None
|
||||
parents[name].add(parent)
|
||||
if device.get("fstype") == "LVM2_member" and device.get("type") != "lvm":
|
||||
volumes.add(name)
|
||||
return types, parents, volumes
|
||||
|
||||
|
||||
def _disks_under(name, parents, seen=None):
|
||||
"""The disks a device is built on: itself, or its parents' disks."""
|
||||
seen = seen if seen is not None else set()
|
||||
if name in seen:
|
||||
return set()
|
||||
seen.add(name)
|
||||
if not parents.get(name):
|
||||
return {name}
|
||||
found = set()
|
||||
for parent in parents[name]:
|
||||
found |= _disks_under(parent, parents, seen)
|
||||
return found
|
||||
|
||||
|
||||
def lvm_device_args(standby_check):
|
||||
@@ -27,22 +72,12 @@ def lvm_device_args(standby_check):
|
||||
"""
|
||||
try:
|
||||
from disk_temperature_history import disk_read_policy, excluded_disk_names
|
||||
result = subprocess.run(["lsblk", "-rno", "NAME,TYPE,PKNAME"],
|
||||
capture_output=True, text=True, timeout=5)
|
||||
if result.returncode:
|
||||
topology = _topology()
|
||||
if topology is None:
|
||||
return None
|
||||
nodes = []
|
||||
for line in result.stdout.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) < 2 or parts[1] not in ("disk", "part"):
|
||||
continue
|
||||
name = parts[0]
|
||||
base = name if parts[1] == "disk" else parts[2] if len(parts) > 2 else ""
|
||||
if not re.fullmatch(r"[A-Za-z0-9._+-]+", name) or not re.fullmatch(r"[A-Za-z0-9._+-]+", base):
|
||||
return None
|
||||
nodes.append((name, base))
|
||||
disks = {name for name, base in nodes if name == base}
|
||||
if not disks or any(base not in disks for _, base in nodes):
|
||||
types, parents, volumes = topology
|
||||
disks = {name for name, kind in types.items() if kind == "disk"}
|
||||
if not disks:
|
||||
return None
|
||||
excluded = excluded_disk_names()
|
||||
blocked = set()
|
||||
@@ -51,13 +86,31 @@ def lvm_device_args(standby_check):
|
||||
# excluded/idle disk. Some USB bridges do not honour -n standby.
|
||||
if disk_read_policy(disk, excluded) != "read" or standby_check(disk):
|
||||
blocked.add(disk)
|
||||
allowed = sorted(f"/dev/{name}" for name in volumes if not (_disks_under(name, parents) & blocked))
|
||||
with _scan_lock:
|
||||
_scanned_disks.clear()
|
||||
_scanned_disks.update(disks - blocked)
|
||||
if not blocked:
|
||||
return []
|
||||
devices = [f"/dev/{name}" for name, base in nodes if base not in blocked]
|
||||
if not devices:
|
||||
# An explicit list makes LVM open every device in it, so only the
|
||||
# physical volumes are named: a RAID, multipath or encrypted volume
|
||||
# built on a blocked disk would read that disk too and stays out.
|
||||
if not allowed:
|
||||
return None
|
||||
return ["--devices", ",".join(dict.fromkeys(devices))]
|
||||
return ["--devices", ",".join(allowed)]
|
||||
except Exception:
|
||||
# Unavailable exclusions, topology or policy are not permission to
|
||||
# open devices that may have been deliberately excluded.
|
||||
return None
|
||||
|
||||
|
||||
def settle_scan_reads():
|
||||
"""The LVM scan reads the disks it is allowed to. Those reads are its
|
||||
own, so they must not make an unused disk look busy at the next check."""
|
||||
try:
|
||||
from disk_temperature_history import settle_own_reads
|
||||
with _scan_lock:
|
||||
disks = set(_scanned_disks)
|
||||
settle_own_reads(disks)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user