Use nsenter with pid:host to run zpool in host mount namespace

This commit is contained in:
2026-03-07 04:04:15 +00:00
parent 034219c75e
commit 10de5563b2
2 changed files with 10 additions and 11 deletions

View File

@@ -5,16 +5,13 @@ services:
container_name: jbod-monitor
restart: unless-stopped
privileged: true
pid: host
network_mode: host
volumes:
- /dev:/dev
- /sys:/sys:ro
- /run/udev:/run/udev:ro
- /usr/sbin/zpool:/host/zpool:ro
- /usr/sbin/zfs:/host/zfs:ro
- /lib/x86_64-linux-gnu:/host/lib:ro
environment:
- TZ=America/Denver
- UVICORN_LOG_LEVEL=info
- ZPOOL_BIN=/host/zpool
- ZFS_HOST_LIB=/host/lib
- ZFS_USE_NSENTER=true

View File

@@ -15,16 +15,18 @@ async def get_zfs_pool_map() -> dict[str, str]:
"""
pool_map = {}
try:
env = os.environ.copy()
host_lib = os.environ.get("ZFS_HOST_LIB")
if host_lib:
env["LD_LIBRARY_PATH"] = host_lib
# 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(
ZPOOL_BIN, "status", "-P",
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=env,
)
stdout, _ = await proc.communicate()
if proc.returncode != 0: