#!/usr/bin/env python3 """Prepare only the isolated bridge and storage for the network sandbox. This runs on the Orange Pi as root. It deliberately does not create or start guests, change the cluster guard, or configure the existing host bridges. """ import os import subprocess import sys from pathlib import Path BRIDGE = 'vmbr9' ALIAS = 'jeannie-network-sandbox' STORAGE = 'jeannie-sandbox' STORAGE_PATH = Path('/data/jeannie/network-sandbox/lxc') EXPECTED_UUID = '19972338-9757-4f4b-89d7-c202df14a9aa' MARKER = '.jeannie-network-sandbox-storage' def run(*args): return subprocess.run(args, check=True, text=True, capture_output=True).stdout.strip() def verify_data(): uuid = run('findmnt', '-rn', '-M', '/data', '-o', 'UUID') opts = run('findmnt', '-rn', '-M', '/data', '-o', 'OPTIONS').split(',') if uuid != EXPECTED_UUID or 'rw' not in opts: raise RuntimeError('/data is not the expected writable SSD filesystem') for part in (Path('/data/jeannie'), Path('/data/jeannie/network-sandbox'), STORAGE_PATH): if part.is_symlink(): raise RuntimeError(f'Refusing symlink in sandbox storage path: {part}') def verify_bridge(): base = Path('/sys/class/net') / BRIDGE if not (base / 'bridge').is_dir(): raise RuntimeError(f'{BRIDGE} is not a bridge') if (base / 'ifalias').read_text().strip() != ALIAS: raise RuntimeError(f'{BRIDGE} is not marked as the Jeannie sandbox bridge') if (base / 'bridge/vlan_filtering').read_text().strip() != '1': raise RuntimeError(f'{BRIDGE} is not VLAN aware') members = sorted(p.name for p in (base / 'brif').iterdir()) if members: raise RuntimeError(f'Refusing to reuse nonempty sandbox bridge: {members}') def verify_storage_config(): config = Path('/etc/pve/storage.cfg').read_text(encoding='utf-8') blocks = config.split('\n\n') matching = [b for b in blocks if b.splitlines() and b.splitlines()[0].strip() == f'dir: {STORAGE}'] if len(matching) != 1: raise RuntimeError(f'Cannot verify storage {STORAGE}') values = {} for line in matching[0].splitlines()[1:]: fields = line.split(None, 1) if len(fields) == 2: values[fields[0]] = fields[1] if values.get('path') != str(STORAGE_PATH) or 'rootdir' not in values.get('content', '').split(','): raise RuntimeError(f'Storage {STORAGE} exists with unexpected configuration') if not (STORAGE_PATH / MARKER).is_file(): raise RuntimeError('Sandbox storage marker is missing') def main(): if os.geteuid() != 0 or sys.argv[1:] != ['prepare']: print('Run as root with the single argument: prepare', file=sys.stderr) return 2 try: verify_data() bridge_path = Path('/sys/class/net') / BRIDGE if bridge_path.exists(): verify_bridge() else: run('ip', 'link', 'add', 'name', BRIDGE, 'type', 'bridge', 'vlan_filtering', '1') run('ip', 'link', 'set', 'dev', BRIDGE, 'alias', ALIAS) run('ip', 'link', 'set', 'dev', BRIDGE, 'up') verify_bridge() status = run('pvesm', 'status') exists = any(line.split() and line.split()[0] == STORAGE for line in status.splitlines()) if exists: verify_storage_config() else: if STORAGE_PATH.exists() and not (STORAGE_PATH / MARKER).is_file(): raise RuntimeError('Existing sandbox directory is unmarked; refusing to adopt it') STORAGE_PATH.mkdir(parents=True, exist_ok=True) (STORAGE_PATH / MARKER).touch(exist_ok=True) run('pvesm', 'add', 'dir', STORAGE, '--path', str(STORAGE_PATH), '--content', 'rootdir,vztmpl') verify_storage_config() print(f'Prepared transient {BRIDGE} and SSD storage {STORAGE}. No guests were created.') except (OSError, RuntimeError, subprocess.CalledProcessError) as exc: detail = exc.stderr.strip() if isinstance(exc, subprocess.CalledProcessError) and exc.stderr else str(exc) print(f'Sandbox preparation failed: {detail}', file=sys.stderr) return 1 return 0 if __name__ == '__main__': sys.exit(main())