From 8decdef5000bca376c75160c6420ff94d7539242 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Mon, 5 Oct 2026 13:30:54 -0600 Subject: [PATCH] fix: pin Pimox VMs to compatible cores --- bootstrap/provisioning/README.md | 13 ++- lib/jeannie/pipeline.sh | 149 ++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 5 deletions(-) diff --git a/bootstrap/provisioning/README.md b/bootstrap/provisioning/README.md index 27888eb..b85eb11 100644 --- a/bootstrap/provisioning/README.md +++ b/bootstrap/provisioning/README.md @@ -180,9 +180,16 @@ command under host CPU set `4-7`. This keeps the guest's secondary CPU on the compatible RK3588 cores during PSCI boot. The same setting is used for the template build and worker clones, and each worker must report its configured CPU count through SSH before it is added to the Kubernetes worker inventory. -Set `LAB_PIMOX_KVM_CPUSET=` to use the normal `qm start` path on a different -Pimox host, or override template and worker values separately with -`LAB_PIMOX_TEMPLATE_KVM_CPUSET` and `LAB_PIMOX_WORKER_KVM_CPUSET`. +Jeannie also installs a persistent Proxmox hookscript at +`local:snippets/pin.pl` and attaches it to the Pimox control-plane and worker +VMs so normal Proxmox starts/restarts keep KVM pinned to the same compatible +core set. Set `LAB_PIMOX_KVM_CPUSET=` to use the normal `qm start` path on a +different Pimox host, or override template, control-plane, and worker values +separately with `LAB_PIMOX_TEMPLATE_KVM_CPUSET`, +`LAB_PIMOX_CONTROL_PLANE_KVM_CPUSET`, and `LAB_PIMOX_WORKER_KVM_CPUSET`. +Set `LAB_PIMOX_PIN_HOOKSCRIPT_ENABLED=false` to disable the persistent +hookscript, or override its VMID mapping with +`LAB_PIMOX_PIN_CPU_MAP="9001=4-7 9010=4-7 9011=4-7"`. Static guest-network validation skips the extra LAN ICMP probe by default because the ISP modem may not answer worker VM pings. Set `LAB_PIMOX_WORKER_REQUIRE_LAN_PROBE=true` only when the gateway/probe host diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 0c02c83..5813ffb 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -1286,6 +1286,14 @@ pimox_ssh() { ssh -i "${key_path}" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${user}@${host}" "$@" } +pimox_scp() { + local key_path="$1" + local source_path="$2" + local target="$3" + + scp -i "${key_path}" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${source_path}" "${target}" +} + pimox_guest_ipv4() { local guest_json local host="$1" @@ -2046,6 +2054,89 @@ fi sudo taskset -c \"\$cpuset\" bash \"\$start_script\"" } +ensure_pimox_pin_hookscript() { + local host="$1" + local user="$2" + local key_path="$3" + local qm_bin="$4" + local storage="$5" + local script_name="$6" + local cpu_map="$7" + local script_ref="${storage}:snippets/${script_name}" + local snippets_dir="${LAB_PIMOX_PIN_HOOKSCRIPT_SNIPPETS_DIR:-/var/lib/vz/snippets}" + local remote_tmp="/tmp/homelab-${script_name}" + local local_tmp + local entry + local vmid + local cpuset + local map_lines="" + + if [[ -z "${cpu_map}" ]]; then + return 0 + fi + if ! [[ "${storage}" =~ ^[A-Za-z0-9_.:-]+$ ]]; then + echo "LAB_PIMOX_PIN_HOOKSCRIPT_STORAGE must be a valid Pimox storage identifier." >&2 + exit 1 + fi + if ! [[ "${script_name}" =~ ^[A-Za-z0-9_.-]+$ ]]; then + echo "LAB_PIMOX_PIN_HOOKSCRIPT_NAME must be a safe file name." >&2 + exit 1 + fi + + for entry in ${cpu_map}; do + vmid="${entry%%=*}" + cpuset="${entry#*=}" + if [[ "${vmid}" == "${entry}" || -z "${vmid}" || -z "${cpuset}" ]]; then + echo "Invalid LAB_PIMOX_PIN_CPU_MAP entry '${entry}'. Use VMID=CPUSET, for example 9001=4-7." >&2 + exit 1 + fi + if ! [[ "${vmid}" =~ ^[0-9]+$ ]]; then + echo "Invalid LAB_PIMOX_PIN_CPU_MAP VMID '${vmid}'." >&2 + exit 1 + fi + if ! cpuset_cpu_count "${cpuset}" >/dev/null; then + echo "Invalid LAB_PIMOX_PIN_CPU_MAP CPU set '${cpuset}' for VM ${vmid}. Use CPU IDs or ranges, such as 4-7." >&2 + exit 1 + fi + map_lines+=" ${vmid} => \"${cpuset}\","$'\n' + done + + local_tmp="$(mktemp "${TMPDIR:-/tmp}/homelab-pimox-pin.XXXXXX")" + # shellcheck disable=SC2016 + { + printf '%s\n' '#!/usr/bin/perl' + printf '%s\n' 'use strict; use warnings;' + printf '%s\n' 'my ($vmid, $phase) = @ARGV;' + printf '%s\n' 'my %cpus = (' + printf '%s' "${map_lines}" + printf '%s\n' ');' + printf '%s\n' "if (\$phase eq 'post-start' && \$cpus{\$vmid}) {" + printf '%s\n' ' my $pid_file = "/var/run/qemu-server/$vmid.pid";' + printf '%s\n' ' exit(0) unless -r $pid_file;' + printf '%s\n' ' chomp(my $pid = `cat $pid_file`);' + printf '%s\n' ' system("taskset -a -cp $cpus{$vmid} $pid");' + printf '%s\n' '}' + printf '%s\n' 'exit(0);' + } >"${local_tmp}" + + echo "Installing Pimox KVM CPU pin hookscript ${script_ref} with map: ${cpu_map}" + pimox_ssh "${host}" "${user}" "${key_path}" "mkdir -p /tmp" + pimox_scp "${key_path}" "${local_tmp}" "${user}@${host}:${remote_tmp}" + rm -f "${local_tmp}" + pimox_ssh "${host}" "${user}" "${key_path}" "set -eu +sudo mkdir -p '${snippets_dir}' +sudo install -m 0755 '${remote_tmp}' '${snippets_dir}/${script_name}' +rm -f '${remote_tmp}'" + + for entry in ${cpu_map}; do + vmid="${entry%%=*}" + pimox_ssh "${host}" "${user}" "${key_path}" "set -eu +if sudo '${qm_bin}' status '${vmid}' >/dev/null 2>&1; then + sudo '${qm_bin}' set '${vmid}' --hookscript '${script_ref}' +fi" + done +} + pimox_shutdown_vm_gracefully() { local host="$1" local user="$2" @@ -2106,6 +2197,7 @@ ensure_pimox_worker_node() { local worker_replace_existing="${20}" local worker_cpu_affinity="${21}" local worker_kvm_cpuset="${22}" + local worker_hookscript_ref="${23}" local worker_network_mode="${LAB_PIMOX_WORKER_NETWORK_MODE:-static}" local worker_static_ips="${LAB_PIMOX_WORKER_STATIC_IPS:-192.168.100.66 192.168.100.67 192.168.100.76}" local worker_gateway="${LAB_PIMOX_WORKER_GATEWAY:-192.168.100.1}" @@ -2177,6 +2269,9 @@ sudo '${qm_bin}' set '${vmid}' \ if [ \"\$had_legacy_args\" = true ]; then sudo '${qm_bin}' set '${vmid}' --delete args fi +if [ -n '${worker_hookscript_ref}' ]; then + sudo '${qm_bin}' set '${vmid}' --hookscript '${worker_hookscript_ref}' +fi if [ -n '${worker_cpu_affinity}' ]; then affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${worker_cpu_affinity}' 2>&1)\" || { case \"\$affinity_output\" in @@ -2243,6 +2338,9 @@ sudo '${qm_bin}' set '${vmid}' \ if sudo '${qm_bin}' config '${vmid}' | grep -q '^args:'; then sudo '${qm_bin}' set '${vmid}' --delete args fi +if [ -n '${worker_hookscript_ref}' ]; then + sudo '${qm_bin}' set '${vmid}' --hookscript '${worker_hookscript_ref}' +fi if [ -n '${worker_cpu_affinity}' ]; then affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${worker_cpu_affinity}' 2>&1)\" || { case \"\$affinity_output\" in @@ -2346,6 +2444,7 @@ ensure_pimox_control_plane_node() { local memory="${17}" local cpu_affinity="${18}" local kvm_cpuset="${19}" + local hookscript_ref="${20}" local network_mode="${LAB_PIMOX_CONTROL_PLANE_NETWORK_MODE:-${LAB_PIMOX_WORKER_NETWORK_MODE:-static}}" local gateway="${LAB_PIMOX_CONTROL_PLANE_GATEWAY:-${LAB_PIMOX_WORKER_GATEWAY:-192.168.100.1}}" local dns_servers="${LAB_PIMOX_CONTROL_PLANE_DNS_SERVERS:-${LAB_PIMOX_WORKER_DNS_SERVERS:-192.168.100.89 1.1.1.1}}" @@ -2401,6 +2500,9 @@ sudo '${qm_bin}' set '${vmid}' \ if [ \"\$had_legacy_args\" = true ]; then sudo '${qm_bin}' set '${vmid}' --delete args fi +if [ -n '${hookscript_ref}' ]; then + sudo '${qm_bin}' set '${vmid}' --hookscript '${hookscript_ref}' +fi if [ -n '${cpu_affinity}' ]; then affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${cpu_affinity}' 2>&1)\" || { case \"\$affinity_output\" in @@ -2468,6 +2570,9 @@ sudo '${qm_bin}' set '${vmid}' \ if sudo '${qm_bin}' config '${vmid}' | grep -q '^args:'; then sudo '${qm_bin}' set '${vmid}' --delete args fi +if [ -n '${hookscript_ref}' ]; then + sudo '${qm_bin}' set '${vmid}' --hookscript '${hookscript_ref}' +fi if [ -n '${cpu_affinity}' ]; then affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${cpu_affinity}' 2>&1)\" || { case \"\$affinity_output\" in @@ -3077,6 +3182,11 @@ run_pimox_pipeline() { local pimox_kvm_cpuset="${LAB_PIMOX_KVM_CPUSET:-4-7}" local template_kvm_cpuset="${LAB_PIMOX_TEMPLATE_KVM_CPUSET:-${pimox_kvm_cpuset}}" local worker_kvm_cpuset="${LAB_PIMOX_WORKER_KVM_CPUSET:-${pimox_kvm_cpuset}}" + local pin_hookscript_enabled="${LAB_PIMOX_PIN_HOOKSCRIPT_ENABLED:-true}" + local pin_hookscript_storage="${LAB_PIMOX_PIN_HOOKSCRIPT_STORAGE:-local}" + local pin_hookscript_name="${LAB_PIMOX_PIN_HOOKSCRIPT_NAME:-pin.pl}" + local pin_hookscript_ref="" + local pin_cpu_map="${LAB_PIMOX_PIN_CPU_MAP:-}" local control_plane_enabled="${LAB_PIMOX_CONTROL_PLANE_ENABLED:-auto}" local control_plane_vmid="${LAB_PIMOX_CONTROL_PLANE_VMID:-${LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID:-}}" local control_plane_name="${LAB_PIMOX_CONTROL_PLANE_NAME:-${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-pimox-cp-01}}" @@ -3106,6 +3216,7 @@ run_pimox_pipeline() { local cpuset_name local cpuset_value local required_cores + local worker_vmid if disabled_value "${mode}"; then return 0 @@ -3158,6 +3269,27 @@ run_pimox_pipeline() { exit 1 fi done + if ! truthy "${pin_hookscript_enabled}" && ! disabled_value "${pin_hookscript_enabled}"; then + echo "LAB_PIMOX_PIN_HOOKSCRIPT_ENABLED must be true or false." >&2 + exit 1 + fi + if truthy "${pin_hookscript_enabled}"; then + if [[ -z "${pin_cpu_map}" ]]; then + if truthy "${control_plane_enabled}" && [[ -n "${control_plane_kvm_cpuset}" ]]; then + pin_cpu_map+="${control_plane_vmid}=${control_plane_kvm_cpuset} " + fi + for ((index = 1; index <= worker_count; index++)); do + if worker_index_is_skipped "${index}" "${worker_skip_indexes}" || [[ -z "${worker_kvm_cpuset}" ]]; then + continue + fi + worker_vmid=$((worker_base_vmid + index - 1)) + pin_cpu_map+="${worker_vmid}=${worker_kvm_cpuset} " + done + fi + if [[ -n "${pin_cpu_map// /}" ]]; then + pin_hookscript_ref="${pin_hookscript_storage}:snippets/${pin_hookscript_name}" + fi + fi if ! [[ "${timeout_seconds}" =~ ^[0-9]+$ && "${timeout_seconds}" -gt 0 ]]; then echo "LAB_PIMOX_GUEST_TIMEOUT_SECONDS must be a positive integer." >&2 exit 1 @@ -3285,6 +3417,17 @@ fi" 2>&1)" pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' set '${template_vmid}' --agent enabled=1" echo "Worker VM clones will be created on Pimox storage ${worker_storage}; template VM ${template_vmid} stays on its configured template storage." + if [[ -n "${pin_hookscript_ref}" ]]; then + ensure_pimox_pin_hookscript \ + "${pimox_host}" \ + "${pimox_user}" \ + "${pimox_key}" \ + "${qm_bin}" \ + "${pin_hookscript_storage}" \ + "${pin_hookscript_name}" \ + "${pin_cpu_map}" + fi + if truthy "${control_plane_enabled}"; then echo "Ensuring Pimox control-plane VM ${control_plane_vmid} (${control_plane_name}) on storage ${control_plane_storage}..." ensure_pimox_control_plane_node \ @@ -3306,7 +3449,8 @@ fi" 2>&1)" "${control_plane_cores}" \ "${control_plane_memory}" \ "${control_plane_cpu_affinity}" \ - "${control_plane_kvm_cpuset}" + "${control_plane_kvm_cpuset}" \ + "${pin_hookscript_ref}" fi if ((worker_count == 0)); then @@ -3347,7 +3491,8 @@ fi" 2>&1)" "${worker_storage}" \ "${worker_replace_existing}" \ "${worker_cpu_affinity}" \ - "${worker_kvm_cpuset}" + "${worker_kvm_cpuset}" \ + "${pin_hookscript_ref}" done write_cluster_worker_var_file "${var_file}" "${REPO_ROOT}/.lab/manual-workers.tsv" "${spec_file}"