4141 echo " BPF JIT hardening configuration not available, skipping..."
4242fi
4343
44+ calculate_safe_memory () {
45+ local floor_mb=2048
46+ local host_reserve_mb=2048
47+
48+ local total_kb avail_kb total_mb avail_mb
49+ total_kb=$( awk ' /MemTotal:/ {print $2}' /proc/meminfo)
50+ avail_kb=$( awk ' /MemAvailable:/ {print $2}' /proc/meminfo)
51+ total_mb=$(( total_kb / 1024 ))
52+ avail_mb=$(( avail_kb > 0 ? avail_kb / 1024 : (total_mb * 60 / 100 ) ))
53+
54+ # cgroup v2 limit if any
55+ local cg_raw cg_mb=0
56+ if [[ -r /sys/fs/cgroup/memory.max ]]; then
57+ cg_raw=$( cat /sys/fs/cgroup/memory.max)
58+ [[ " $cg_raw " != " max" ]] && cg_mb=$(( cg_raw / 1024 / 1024 ))
59+ fi
60+
61+ local target=$(( avail_mb * 80 / 100 ))
62+ local total_cap=$(( total_mb * 90 / 100 ))
63+ (( target > total_cap )) && target=$total_cap
64+
65+ local max_allowed=$(( total_mb - host_reserve_mb ))
66+ if (( cg_mb > 0 )) ; then
67+ local cg_cap=$(( cg_mb - host_reserve_mb ))
68+ (( cg_cap < max_allowed )) && max_allowed=$cg_cap
69+ fi
70+
71+ # If the machine is too small, fail
72+ if (( max_allowed < floor_mb )) ; then
73+ echo " ERROR: Not enough RAM to auto-size (total=${total_mb} MB, allowed=${max_allowed} MB). Set MINIKUBE_MEM manually." >&2
74+ return 1
75+ fi
76+
77+ (( target < floor_mb )) && target=$floor_mb
78+ (( target > max_allowed )) && target=$max_allowed
79+
80+ echo " $target "
81+ }
82+
4483# --- NVIDIA GPU Setup ---
4584GPU_AVAILABLE=false
4685if command -v " $NVIDIA_SMI_PATH " > /dev/null 2>&1 ; then
5594 echo " No NVIDIA GPU detected. Will start minikube without GPU support."
5695fi
5796
97+ if [[ -z " ${MINIKUBE_MEM:- } " ]]; then
98+ MINIKUBE_MEM=" $( calculate_safe_memory) "
99+ fi
100+
58101if [ " $GPU_AVAILABLE " = true ]; then
59102 # Configure Docker for GPU support.
60103 echo " Configuring Docker runtime for GPU support..."
@@ -69,7 +112,7 @@ if [ "$GPU_AVAILABLE" = true ]; then
69112
70113 # Start minikube with GPU support.
71114 echo " Starting minikube with GPU support..."
72- minikube start --memory=max --driver=docker --container-runtime=docker --gpus=all --force --addons=nvidia-device-plugin
115+ minikube start --memory=" ${MINIKUBE_MEM} " --driver=docker --container-runtime=docker --gpus=all --force --addons=nvidia-device-plugin
73116
74117 # Update kubeconfig context.
75118 echo " Updating kubeconfig context..."
85128 echo " Starting minikube without GPU support..."
86129 # Fix potential permission issues.
87130 sudo sysctl fs.protected_regular=0
88- minikube start --memory=max --driver=docker --force
131+ minikube start --memory=" ${MINIKUBE_MEM} " --driver=docker --force
89132fi
90133
91134echo " Minikube cluster installation complete."
0 commit comments