start-colima.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. import os
  3. import platform
  4. import subprocess
  5. import sys
  6. from typing import Sequence
  7. def main(argv: Sequence[str] | None = None) -> int:
  8. # platform.processor() changed at some point between these:
  9. # 11.2.3: arm
  10. # 12.3.1: arm64
  11. APPLE_ARM64 = sys.platform == "darwin" and platform.processor() in {"arm", "arm64"}
  12. cpus = os.cpu_count()
  13. if cpus is None:
  14. raise SystemExit("failed to determine cpu count")
  15. # SC_PAGE_SIZE is POSIX 2008
  16. # SC_PHYS_PAGES is a linux addition but also supported by more recent MacOS versions
  17. SC_PAGE_SIZE = os.sysconf("SC_PAGE_SIZE")
  18. SC_PHYS_PAGES = os.sysconf("SC_PHYS_PAGES")
  19. if SC_PAGE_SIZE == -1 or SC_PHYS_PAGES == -1:
  20. raise SystemExit("failed to determine memsize_bytes")
  21. memsize_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
  22. args = [
  23. "--cpu",
  24. f"{cpus//2}",
  25. "--memory",
  26. f"{memsize_bytes//(2*1024**3)}",
  27. ]
  28. if APPLE_ARM64:
  29. args = [*args, "--vm-type=vz", "--vz-rosetta", "--mount-type=virtiofs"]
  30. return subprocess.call(
  31. (
  32. "colima",
  33. "start",
  34. f"--mount=/var/folders:w,/private/tmp/colima:w,{os.path.expanduser('~')}:r",
  35. *args,
  36. )
  37. )
  38. if __name__ == "__main__":
  39. raise SystemExit(main())