Checking Virtual Machine Resource Allocations with Libvirt (BASH)
Occasionally you find you need to allocate more resources to a VM, but have lost track of what's actually been allocated on the KVM host system.
Checking RAM and CPU allocations with libvirt/virsh is relatively straightforward. This documentation provides various oneliners to dump out allocations for the VMs on a host
Details
- Language: BASH
Snippet
# List RAM Allocations for all *running* machines
virsh list | awk '{print $2}' | egrep -v -e '^$|^-|^Name$' | while read vm; do mem=$(virsh dominfo ${vm} | grep "Max memory" | awk '{print $3,$4}'); echo "$vm ${mem}"; done
# List RAM allocations for all machines
virsh list --all | awk '{print $2}' | egrep -v -e '^$|^-|^Name$' | while read vm; do mem=$(virsh dominfo ${vm} | grep "Max memory" | awk '{print $3,$4}'); echo "$vm ${mem}"; done
# List total allocation vs Host RAM for all *running* machines
virsh list | awk '{print $2}' | egrep -v -e '^$|^-|^Name$' | while read vm; do mem=$(virsh dominfo ${vm} | grep "Max memory" | awk '{print $3}'); echo "$mem"; done | awk 'BEGIN{a=0}{a=a+$1}END{print a}'; grep "MemTotal" /proc/meminfo
# List CPU allocations for all *running* machines
virsh list | awk '{print $2}' | egrep -v -e '^$|^-|^Name$' | while read vm; do cnt=$(virsh dominfo ${vm} | grep "CPU.s" | awk '{print $2}'); echo "$vm $cnt"; done
# List total allocated CPUs vs host logical cores
virsh list | awk '{print $2}' | egrep -v -e '^$|^-|^Name$' | while read vm; do virsh dominfo ${vm} | grep "CPU.s"; done | awk 'BEGIN {a=0;} {a=a+$2;} END {print a;}'; egrep "^processor" /proc/cpuinfo | wc -l