start-colima.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 = int(subprocess.run(("sysctl", "-n", "hw.ncpu"), check=True, capture_output=True).stdout)
  13. memsize_bytes = int(
  14. subprocess.run(("sysctl", "-n", "hw.memsize"), check=True, capture_output=True).stdout
  15. )
  16. args = [
  17. "--cpu",
  18. f"{cpus//2}",
  19. "--memory",
  20. f"{memsize_bytes//(2*1024**3)}",
  21. ]
  22. if APPLE_ARM64:
  23. args = [*args, "--vm-type=vz", "--vz-rosetta", "--mount-type=virtiofs"]
  24. return subprocess.call(
  25. (
  26. "colima",
  27. "start",
  28. f"--mount=/var/folders:w,/private/tmp/colima:w,{os.path.expanduser('~')}:r",
  29. *args,
  30. )
  31. )
  32. if __name__ == "__main__":
  33. raise SystemExit(main())