#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Аккуратно добавляет rootdir в хранилище local (для диска контейнера).""" import io import shutil import subprocess import time P = "/etc/pve/storage.cfg" BAK = "/root/storage.cfg.before-%s" % time.strftime("%Y%m%d_%H%M%S") print("=== 1. КОПИЯ ===") shutil.copy2(P, BAK) print(" %s" % BAK) print() print("=== 2. ЧИТАЮ ФАЙЛ ЦЕЛИКОМ ===") lines = io.open(P, encoding="utf-8").read().splitlines() for i, l in enumerate(lines): print(" %2d | %r" % (i, l)) print() print("=== 3. ПРАВЛЮ БЛОК local ===") out = [] in_local = False changed = False for line in lines: stripped = line.strip() if stripped == "dir: local": in_local = True out.append(line) continue if in_local and stripped.startswith("content"): if "rootdir" not in stripped: indent = line[:len(line) - len(line.lstrip())] line = "%scontent %s,rootdir" % (indent, stripped.split(None, 1)[1]) changed = True print(" строка содержимого: %r" % line) else: print(" rootdir уже был") in_local = False out.append(line) continue if in_local and stripped == "": in_local = False out.append(line) if not changed: print(" ничего не изменил") else: io.open(P, "w", encoding="utf-8").write("\n".join(out) + "\n") print(" записано") print() print("=== 4. ЧТО СТАЛО ===") text = io.open(P, encoding="utf-8").read() for line in text.splitlines()[:12]: print(" %r" % line) print() print("=== 5. ВИДИТ ЛИ PVE ИЗМЕНЕНИЕ ===") r = subprocess.run(["pvesm", "status"], capture_output=True, text=True) for line in r.stdout.splitlines()[:4]: print(" " + line) print() print("=== 6. ПРОБУЮ СОЗДАТЬ CT ===") r = subprocess.run( "pct create 161 local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst " "--hostname gateway-lxc --cores 1 --memory 256 --swap 128 " "--rootfs local:2 " "--net0 name=eth0,bridge=vmbr0,ip=192.168.1.161/24,gw=192.168.1.1,type=veth " "--ostype debian --unprivileged 1 --onboot 1 --features nesting=1 " "--description 'Шлюз в доме: DNS домашних доменов (этап 3)'", shell=True, capture_output=True, text=True) print(" код: %d" % r.returncode) print(" %s" % (r.stdout + r.stderr).strip()[-300:]) if r.returncode != 0: print() print(" ОТКАТ storage.cfg") shutil.copy2(BAK, P) print(" возвращено") else: s = subprocess.run(["pct", "status", "161"], capture_output=True, text=True) print(" статус: %s" % s.stdout.strip())