Created
August 21, 2025 21:19
-
-
Save nwjsmith/ebd3f8c78a7e86f777abb3bf3b3de16f to your computer and use it in GitHub Desktop.
Revisions
-
nwjsmith created this gist
Aug 21, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,89 @@ #!/bin/bash usage() { echo "Usage: $0 [PERCENT]" echo "Start Colima with dynamically allocated memory and CPU" echo "" echo "Arguments:" echo " PERCENT Percentage of system memory and CPU to allocate (default: 75)" echo "" echo "Examples:" echo " $0 # Use 75% of memory and CPU" echo " $0 50 # Use 50% of memory and CPU" echo " $0 90 # Use 90% of memory and CPU" exit 1 } validate_percent() { local percent=$1 if ! [[ "${percent}" =~ ^[0-9]+$ ]] || [[ "${percent}" -lt 1 ]] || [[ "${percent}" -gt 100 ]]; then echo "Error: Percent must be a number between 1 and 100" >&2 exit 1 fi } calculate_cpus() { local percent=$1 local performance_cores performance_cores=$(sysctl -n hw.perflevel0.physicalcpu 2>/dev/null || sysctl -n hw.ncpu) local allocated_cpus=$((performance_cores * percent / 100)) if [[ "${allocated_cpus}" -lt 1 ]]; then echo 1 else echo "${allocated_cpus}" fi } calculate_memory() { local percent=$1 local memory_bytes memory_bytes=$(sysctl -n hw.memsize) local memory_gb=$((memory_bytes / 1024 / 1024 / 1024)) local allocated_memory=$((memory_gb * percent / 100)) if [[ "${allocated_memory}" -lt 4 ]]; then echo 4 else echo "${allocated_memory}" fi } start_colima() { local cpus=$1 local memory=$2 exec colima start \ --vm-type=vz \ --mount-type=virtiofs \ --cpu "${cpus}" \ --memory "${memory}" } main() { local percent=75 if [[ $# -gt 1 ]]; then echo "Error: Too many arguments" >&2 usage fi if [[ $# -eq 1 ]]; then if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then usage fi percent="$1" fi validate_percent "${percent}" local cpus cpus=$(calculate_cpus "${percent}") local memory memory=$(calculate_memory "${percent}") start_colima "${cpus}" "${memory}" } main "$@"