VulpesOS/tundra/devices/sargo/prepare-update.py

57 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Extract the upgrade payload from the clean installation image (Linux)."""
import argparse
import gzip
import hashlib
import json
from pathlib import Path
import subprocess
import tarfile
import tempfile
def pack(tree, destination):
# A canonical uncompressed tar has the same digest with any gzip implementation.
with destination.open("wb") as file, gzip.GzipFile(filename="", fileobj=file, mode="wb", compresslevel=2, mtime=0) as compressed, tarfile.open(fileobj=compressed, mode="w|", format=tarfile.GNU_FORMAT) as archive:
for path in sorted(tree.rglob("*")):
name = path.relative_to(tree).as_posix()
header = archive.gettarinfo(str(path), arcname=name)
if not (header.isdir() or header.isfile()):
raise ValueError(f"Unexpected entry {name}")
header.uid = header.gid = header.mtime = 0
header.uname = header.gname = ""
if header.isfile():
with path.open("rb") as source:
archive.addfile(header, source)
else:
archive.addfile(header)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--work-dir", type=Path, default=Path.cwd(), help="Filesystem with 10 GiB available")
args = parser.parse_args()
base = Path(__file__).resolve().parent
with tempfile.TemporaryDirectory(dir=args.work_dir.resolve()) as temp:
work = Path(temp)
raw = work / "volume.ext4"
sparse = base / "userdata.img"
manifest = json.loads((base / "package.json").read_text())
with sparse.open("rb") as src:
if hashlib.file_digest(src, "sha256").hexdigest() != manifest["partitions"]["userdata"]["sha256"]:
raise ValueError("Invalid userdata.img digest")
subprocess.run(["simg2img", sparse, raw], check=True)
subprocess.run(["debugfs", "-R", 'rdump /tundra/engines/p26-161 .', raw], cwd=work, check=True)
tree = work / "p26-161"
if not (tree / "firefox/platform.ini").is_file():
raise ValueError("Missing runtime in installation image")
packed = work / "engine.tar.gz"
pack(tree, packed)
with gzip.open(packed, "rb") as data:
digest = hashlib.file_digest(data, "sha256").hexdigest()
if digest != (base / "engine-tar.sha256").read_text().strip():
raise ValueError("Reconstructed engine checksum differs")
import shutil
shutil.copyfile(packed, base / "engine.tar.gz")
print("engine.tar.gz ready for the recovery")
if __name__ == "__main__":
main()