feat: household recording destinations and verified Baidu segment archive
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Run as root on the standard Linux installation before restarting services.
|
||||
|
||||
Creates a dedicated bounded spool without moving or deleting existing recordings.
|
||||
The sparse image consumes physical blocks only as recordings are written.
|
||||
"""
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pwd
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
BASE = Path('/var/lib/zhaovision')
|
||||
IMAGE = BASE/'recording-spool.ext4'
|
||||
MOUNT = BASE/'recording-spool'
|
||||
SIZE = 5_000_000_000
|
||||
|
||||
|
||||
def main():
|
||||
if os.name != 'posix' or os.geteuid()!=0: raise SystemExit('Requires Linux root')
|
||||
if BASE.resolve()!=BASE or not (BASE/'vision.db').is_file(): raise SystemExit('Unexpected installation')
|
||||
if IMAGE.is_symlink() or MOUNT.is_symlink(): raise SystemExit('Unexpected symlink')
|
||||
if not IMAGE.exists():
|
||||
if shutil.disk_usage(BASE).free < 10_000_000_000: raise SystemExit('Insufficient free disk for safe provisioning')
|
||||
with IMAGE.open('xb') as output: output.truncate(SIZE)
|
||||
IMAGE.chmod(0o600)
|
||||
subprocess.run(['mkfs.ext4','-q','-m','0','-T','largefile4','-F',str(IMAGE)],check=True)
|
||||
if IMAGE.stat().st_size != SIZE: raise SystemExit('Existing spool size differs; no changes made')
|
||||
MOUNT.mkdir(mode=0o700,exist_ok=True)
|
||||
if not MOUNT.is_mount():
|
||||
if any(MOUNT.iterdir()): raise SystemExit('Mount point is not empty; original files preserved')
|
||||
subprocess.run(['mount','-o','loop,nodev,nosuid,noexec',str(IMAGE),str(MOUNT)],check=True)
|
||||
info=json.loads(subprocess.check_output(['findmnt','--json','--target',str(MOUNT)],text=True))['filesystems'][0]
|
||||
loops=json.loads(subprocess.check_output(['losetup','--json','-j',str(IMAGE)],text=True))['loopdevices']
|
||||
if info['target']!=str(MOUNT) or info['fstype']!='ext4' or info['source'] not in [x['name'] for x in loops]:
|
||||
raise SystemExit('Mount does not refer to the expected spool image')
|
||||
if shutil.disk_usage(MOUNT).total > SIZE: raise SystemExit('Spool exceeds its hard limit')
|
||||
owner=pwd.getpwnam('zhaovision');os.chown(MOUNT,owner.pw_uid,owner.pw_gid);MOUNT.chmod(0o700)
|
||||
stamp=datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
|
||||
fstab=Path('/etc/fstab');text=fstab.read_text()
|
||||
entries=[line for line in text.splitlines() if line.strip() and not line.lstrip().startswith('#') and len(line.split())>1 and line.split()[1]==str(MOUNT)]
|
||||
line=f'{IMAGE} {MOUNT} ext4 loop,nodev,nosuid,noexec 0 0'
|
||||
if entries and entries!=[line]: raise SystemExit('Existing mount configuration differs')
|
||||
if not entries:
|
||||
shutil.copy2(fstab,Path('/etc/fstab.pre-zhao-recording-'+stamp))
|
||||
fstab.write_text(text.rstrip()+'\n'+line+'\n')
|
||||
env=Path('/etc/zhaovision.env');raw=env.read_text()
|
||||
shutil.copy2(env,Path('/etc/zhaovision.env.pre-recording-'+stamp))
|
||||
wanted=dict(VISION_RECORDINGS=str(MOUNT),VISION_LEGACY_RECORDINGS=str(BASE/'recordings'),VISION_RECORDING_QUOTA_BYTES=str(SIZE))
|
||||
kept=[line for line in raw.splitlines() if line.split('=',1)[0] not in wanted]
|
||||
temporary=env.with_suffix('.recording-tmp');temporary.write_text('\n'.join(kept+[k+'='+v for k,v in wanted.items()])+'\n');temporary.chmod(0o600);os.replace(temporary,env)
|
||||
for service in ('zhaovision','zhaovision-media'):
|
||||
folder=Path('/etc/systemd/system')/(service+'.service.d');folder.mkdir(exist_ok=True)
|
||||
(folder/'recording-volume.conf').write_text('[Unit]\nRequiresMountsFor='+str(MOUNT)+'\nConditionPathIsMountPoint='+str(MOUNT)+'\n')
|
||||
subprocess.run(['systemctl','daemon-reload'],check=True)
|
||||
print('Bounded recording spool ready:',SIZE,'bytes; legacy recordings preserved')
|
||||
|
||||
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user