Single-card flow; preflight sudo precondition + Ubuntu package install

This commit is contained in:
2026-09-26 17:27:35 +02:00
parent 4e53bd880c
commit 94818ebdec
3 changed files with 219 additions and 183 deletions
+109 -19
View File
@@ -13,7 +13,6 @@ from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
HARNESS_DIR = SCRIPT_DIR / "harness"
CARDS_DIR = SCRIPT_DIR / "cards"
PROGRESS_FILE = SCRIPT_DIR / "progress.json"
REPOS = {
"170th-Street": "https://github.com/amoghmunikote/170th-Street.git",
@@ -21,7 +20,6 @@ REPOS = {
}
VERIFY_SH = HARNESS_DIR / "cmpunlocker" / "verify.sh"
GPU_IDS = ("20c2", "2082")
DEFAULT_TOTAL = 23
VULKAN_TEST_SECONDS = 300
SPEED_TO_GEN = {"2.5GT/s": 1, "5GT/s": 2, "8GT/s": 3, "16GT/s": 4, "32GT/s": 5, "64GT/s": 6}
@@ -172,34 +170,120 @@ PKG_FOR = {"lspci": "pciutils", "setpci": "pciutils", "vulkaninfo": "vulkan-tool
"vkcube": "vulkan-tools", "vkmark": "vkmark", "git": "git"}
def os_release():
data = read_file("/etc/os-release") or ""
out = {}
for line in data.splitlines():
if "=" in line:
k, v = line.split("=", 1)
out[k] = v.strip().strip('"')
return out
def sudo_precondition(interactive=True):
"""Precondition for package installation (--preflight): root or working sudo.
Tries passwordless sudo first; falls back to an interactive `sudo -v` so the
credentials get cached, after which plain sudo -n calls succeed.
"""
if os.geteuid() == 0:
return True, "running as root"
if not shutil.which("sudo"):
return False, "sudo is not installed"
r = run(["sudo", "-n", "true"], timeout=15)
if r["rc"] == 0:
return True, "sudo available (credentials cached)"
if interactive and sys.stdin.isatty():
info("sudo authentication required for package installation - enter your password")
try:
p = subprocess.run(["sudo", "-v"])
except KeyboardInterrupt:
return False, "interrupted"
if p.returncode == 0:
return True, "sudo authenticated just now"
return False, "sudo authentication failed"
return False, "sudo requires a password (non-interactive session)"
def install_nvidia_driver():
"""Ubuntu: prefer 'ubuntu-drivers install', else the nvidia-driver metapackage."""
if which("ubuntu-drivers"):
info("installing NVIDIA driver via 'ubuntu-drivers install'")
r = as_root(["ubuntu-drivers", "install"], timeout=900)
else:
pm = detect_pm()
if not pm:
return False, "no package manager available for driver install"
info("installing NVIDIA driver metapackage 'nvidia-driver'")
r = as_root(pm + ["nvidia-driver"], timeout=900)
return r["rc"] == 0, (r["out"][-2000:] + r["err"][-1000:])
def preflight(auto_install=True, do_clone=True):
step("Preflight")
report = {"tools": {}, "root": have_root()}
critical_missing, optional_missing = [], []
rel = os_release()
report["os"] = {"id": rel.get("ID", ""), "version_id": rel.get("VERSION_ID", ""),
"pretty": rel.get("PRETTY_NAME", "unknown")}
info(f"OS: {report['os']['pretty']}")
for tool, (level, why) in TOOL_CHECKS.items():
path = which(tool)
report["tools"][tool] = {"found": bool(path), "path": path, "level": level, "why": why}
if not path:
(critical_missing if level.startswith("critical") else optional_missing).append(tool)
want_nvidia = False
if auto_install:
pkgs = sorted({PKG_FOR[t] for t in critical_missing + optional_missing if t in PKG_FOR})
if pkgs:
if detect_pm():
info(f"attempting to install: {' '.join(pkgs)}")
_, log = install_packages(pkgs)
report["install_log"] = log
if "nvidia-smi" in critical_missing and sys.stdin.isatty():
if ask("nvidia-smi missing - install the NVIDIA driver now? (y/N)", "n").lower().startswith("y"):
want_nvidia = True
if pkgs or want_nvidia:
ok_sudo, why = sudo_precondition()
report["sudo"] = {"ok": ok_sudo, "why": why}
if not ok_sudo:
err(f"sudo PRECONDITION FAILED: {why}")
err("--preflight package installation requires sudo privileges. Fix one of:")
err(" - run 'sudo -v' first, then re-run this command")
err(" - run the script as root")
err(" - re-run with --no-install to skip package installation")
report["install_skipped"] = why
else:
report["install_log"] = "no package manager - manual install required"
warn(f"no package manager found - install manually: {' '.join(pkgs)}")
for tool in list(critical_missing) + list(optional_missing):
if which(tool):
if tool in critical_missing:
critical_missing.remove(tool)
if tool in optional_missing:
optional_missing.remove(tool)
report["tools"][tool].update(found=True, path=which(tool))
info(f"sudo precondition OK: {why}")
pm = detect_pm()
if pkgs:
if pm and pm[0] == "apt-get":
info("apt-get update ...")
as_root(["apt-get", "update"], timeout=300)
if pm:
info(f"installing: {' '.join(pkgs)}")
_, log = install_packages(pkgs)
report["install_log"] = log
else:
report["install_log"] = "no package manager - manual install required"
warn(f"no package manager found - install manually: {' '.join(pkgs)}")
if want_nvidia:
if pm or which("ubuntu-drivers"):
ok_d, log_d = install_nvidia_driver()
report["nvidia_driver"] = {"ok": ok_d, "log": log_d}
if ok_d:
ok("NVIDIA driver installed - reboot, then re-run the check")
else:
warn("NVIDIA driver install failed (see report['nvidia_driver']['log'])")
else:
warn("no package manager - cannot install NVIDIA driver automatically")
for tool in list(critical_missing) + list(optional_missing):
if which(tool):
if tool in critical_missing:
critical_missing.remove(tool)
if tool in optional_missing:
optional_missing.remove(tool)
report["tools"][tool].update(found=True, path=which(tool))
else:
report["sudo"] = {"ok": True, "why": "nothing to install"}
if do_clone:
ensure_repos()
@@ -216,16 +300,22 @@ def preflight(auto_install=True, do_clone=True):
mark = f"{C.YELLOW}missing (optional){C.RESET}"
print(f" {t:<14} {mark} {C.DIM}{st['why']}{C.RESET}")
if "nvflash" in critical_missing:
if not which("nvflash"):
warn("nvflash missing - VBIOS backup will fall back to sysfs ROM, serial to manual entry")
print(" Download Linux x64 nvflash from https://www.nvidia.com/en-us/drivers/nvflash/")
print(" and pass --nvflash /path/to/nvflash, or place it on PATH.")
if "nvidia-smi" in critical_missing:
if not which("nvidia-smi"):
warn("nvidia-smi missing - cmpunlocker verify will be skipped (install nvidia-open driver)")
if not report["root"]:
warn("no sudo and not root: privileged steps (rescan, retrain, rom, dmesg) will be skipped")
ok("preflight complete")
blocking = [t for t in ("git", "lspci", "setpci") if not which(t)]
report["blocking_missing"] = blocking
report["preflight_ok"] = not blocking
if blocking:
err("blocking tools still missing: " + ", ".join(blocking))
else:
ok("preflight complete")
return report