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 container_name: jbod-monitor
restart: unless-stopped restart: unless-stopped
privileged: true privileged: true
pid: host
network_mode: host network_mode: host
volumes: volumes:
- /dev:/dev - /dev:/dev
- /sys:/sys:ro - /sys:/sys:ro
- /run/udev:/run/udev: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: environment:
- TZ=America/Denver - TZ=America/Denver
- UVICORN_LOG_LEVEL=info - UVICORN_LOG_LEVEL=info
- ZPOOL_BIN=/host/zpool - ZFS_USE_NSENTER=true
- ZFS_HOST_LIB=/host/lib

View File

@@ -15,16 +15,18 @@ async def get_zfs_pool_map() -> dict[str, str]:
""" """
pool_map = {} pool_map = {}
try: try:
env = os.environ.copy() # When running in a container with pid:host, use nsenter to run
host_lib = os.environ.get("ZFS_HOST_LIB") # zpool in the host mount namespace so it finds its own libs.
if host_lib: use_nsenter = os.environ.get("ZFS_USE_NSENTER", "").lower() in ("1", "true")
env["LD_LIBRARY_PATH"] = host_lib if use_nsenter:
cmd = ["nsenter", "-t", "1", "-m", "--", "zpool", "status", "-P"]
else:
cmd = [ZPOOL_BIN, "status", "-P"]
proc = await asyncio.create_subprocess_exec( proc = await asyncio.create_subprocess_exec(
ZPOOL_BIN, "status", "-P", *cmd,
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
env=env,
) )
stdout, _ = await proc.communicate() stdout, _ = await proc.communicate()
if proc.returncode != 0: if proc.returncode != 0: