"""Copy the Arch Linux bootstrap tarball's root, read as a plain tar on stdin, to stdout as a tar whose root is the image's: every member of `root.x86_64/` at the same path without that prefix, with its owner, mode, times, extended headers and link targets kept. The files beside that root (the tarball's package list) are left out. `docker import` and `podman import` take it as an image's file system. Run as `zstd -dc TARBALL | python3 -I bootstrap_root.py | docker import - TAG`.""" import sys import tarfile PREFIX = "root.x86_64/" def main() -> None: source = tarfile.open(fileobj=sys.stdin.buffer, mode="r|") image = tarfile.open(fileobj=sys.stdout.buffer, mode="w|", format=tarfile.PAX_FORMAT) for member in source: if member.name.rstrip("/") == PREFIX.rstrip("/"): continue if not member.name.startswith(PREFIX): if member.isreg() and "/" not in member.name: continue sys.exit(f"bootstrap_root: {member.name!r} is outside {PREFIX}") member.name = member.name[len(PREFIX) :] if member.islnk(): # A hard link names its target by its path in the archive. if not member.linkname.startswith(PREFIX): sys.exit(f"bootstrap_root: {member.name!r} links outside {PREFIX}") member.linkname = member.linkname[len(PREFIX) :] image.addfile(member, source.extractfile(member) if member.isreg() else None) image.close() source.close() main()