Add ledctl locate/off LED controls to drive detail modal

This commit is contained in:
2026-03-07 04:57:35 +00:00
parent 51e6b49830
commit 927a5ccf3a
5 changed files with 119 additions and 2 deletions

29
services/leds.py Normal file
View File

@@ -0,0 +1,29 @@
import asyncio
import re
async def set_led(device: str, state: str) -> dict:
"""Set the locate LED on a drive via ledctl.
Args:
device: Block device name (e.g. "sda"). Must be alphanumeric.
state: "locate" to turn on the locate LED, "off" to turn it off.
"""
if not re.fullmatch(r"[a-zA-Z0-9]+", device):
raise ValueError(f"Invalid device name: {device}")
if state not in ("locate", "off"):
raise ValueError(f"Invalid state: {state}")
proc = await asyncio.create_subprocess_exec(
"ledctl",
f"{state}=/dev/{device}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
err = stderr.decode().strip() or stdout.decode().strip()
raise RuntimeError(f"ledctl failed (rc={proc.returncode}): {err}")
return {"device": device, "state": state}