86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
import asyncio
|
|
import os
|
|
import logging
|
|
import re
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Allow overriding the zpool binary path via env (for bind-mounted host tools)
|
|
ZPOOL_BIN = os.environ.get("ZPOOL_BIN", "zpool")
|
|
|
|
|
|
async def get_zfs_pool_map() -> dict[str, dict]:
|
|
"""Return a dict mapping device names to ZFS pool and vdev info.
|
|
|
|
e.g. {"sda": {"pool": "tank", "vdev": "raidz2-0"},
|
|
"sdb": {"pool": "fast", "vdev": "mirror-0"}}
|
|
"""
|
|
pool_map = {}
|
|
try:
|
|
# When running in a container with pid:host, use nsenter to run
|
|
# zpool in the host mount namespace so it finds its own libs.
|
|
use_nsenter = os.environ.get("ZFS_USE_NSENTER", "").lower() in ("1", "true")
|
|
if use_nsenter:
|
|
cmd = ["nsenter", "-t", "1", "-m", "--", "zpool", "status", "-P"]
|
|
else:
|
|
cmd = [ZPOOL_BIN, "status", "-P"]
|
|
|
|
proc = await asyncio.create_subprocess_exec(
|
|
*cmd,
|
|
stdout=asyncio.subprocess.PIPE,
|
|
stderr=asyncio.subprocess.PIPE,
|
|
)
|
|
stdout, _ = await proc.communicate()
|
|
if proc.returncode != 0:
|
|
return pool_map
|
|
|
|
current_pool = None
|
|
current_vdev = None
|
|
in_config = False
|
|
|
|
for line in stdout.decode().splitlines():
|
|
stripped = line.strip()
|
|
|
|
if stripped.startswith("pool:"):
|
|
current_pool = stripped.split(":", 1)[1].strip()
|
|
current_vdev = None
|
|
in_config = False
|
|
continue
|
|
|
|
if stripped.startswith("NAME") and "STATE" in stripped:
|
|
in_config = True
|
|
continue
|
|
|
|
if stripped.startswith("errors:") or stripped == "":
|
|
if stripped.startswith("errors:"):
|
|
in_config = False
|
|
continue
|
|
|
|
if not in_config or not current_pool:
|
|
continue
|
|
|
|
# Indentation: 1 tab = pool name, 1 tab + 2 spaces = vdev,
|
|
# 1 tab + 4 spaces = device. Count chars before content.
|
|
leading = len(line) - len(line.lstrip())
|
|
|
|
if "/dev/" not in stripped and leading == 3:
|
|
# This is a vdev line (mirror-0, raidz2-0, etc.)
|
|
current_vdev = stripped.split()[0]
|
|
elif "/dev/" in stripped:
|
|
parts = stripped.split()
|
|
dev_path = parts[0]
|
|
try:
|
|
real = os.path.realpath(dev_path)
|
|
dev_name = os.path.basename(real)
|
|
# Strip partition numbers (sda1 -> sda)
|
|
dev_name = re.sub(r'\d+$', '', dev_name)
|
|
pool_map[dev_name] = {
|
|
"pool": current_pool,
|
|
"vdev": current_vdev or current_pool,
|
|
}
|
|
except Exception:
|
|
pass
|
|
except FileNotFoundError:
|
|
logger.debug("zpool not available")
|
|
return pool_map
|