Skip to content

Instantly share code, notes, and snippets.

@BlackDex
Forked from ayufan/taskset.sh
Created November 22, 2016 09:47
Show Gist options
  • Save BlackDex/7b1cf00d4220ecfd3ee0d966930ead40 to your computer and use it in GitHub Desktop.
Save BlackDex/7b1cf00d4220ecfd3ee0d966930ead40 to your computer and use it in GitHub Desktop.
KVM CPU pinning for Proxmox VE
#!/bin/bash
set -eo pipefail
VMID=200
cpu_tasks() {
expect <<EOF | sed -n 's/^.* CPU .*thread_id=\(.*\)$/\1/p' | tr -d '\r' || true
spawn qm monitor $VMID
expect ">"
send "info cpus\r"
expect ">"
EOF
}
VCPUS=($(cpu_tasks))
VCPU_COUNT="${#VCPUS[@]}"
if [[ $VCPU_COUNT -eq 0 ]]; then
echo "* No VCPUS for VM$VMID"
exit 1
fi
echo "* Detected ${#VCPUS[@]} assigned to VM$VMID..."
echo "* Resetting cpu shield..."
for CPU_INDEX in "${!VCPUS[@]}"
do
CPU_TASK="${VCPUS[$CPU_INDEX]}"
echo "* Assigning $CPU_INDEX to $CPU_TASK..."
taskset -pc "$CPU_INDEX" "$CPU_TASK"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment