virsh status report: VMs, state, resources and IPs in one table
A read-only bash script that prints a libvirt/KVM host report: virtual networks, storage pools, and every VM with its state, vCPUs, memory, autostart flag and IP addresses.
Not checked by a trusted editor yet Last edited Outdated?
What this changes
Read-only: it lists networks, storage pools and domains through virsh and prints them; no VM or setting is changed.
How to undo it
Nothing to undo.
Run this on a KVM host to get a quick overview before maintenance or when someone asks “what is running on this box?”. It only uses read-only virsh commands (uri, net-list, pool-list, list, dominfo, domstate, domifaddr), so it is safe to run at any time.
Script
#!/usr/bin/env bash
# virsh-report.sh - read-only status report for a libvirt/KVM host
# License: CC0-1.0
set -euo pipefail
URI="${LIBVIRT_DEFAULT_URI:-qemu:///system}"
usage() {
echo "Usage: $0 [-c URI]"
echo " -c URI libvirt connection URI (default: $URI)"
}
while getopts ":c:h" opt; do
case "$opt" in
c) URI="$OPTARG" ;;
h) usage; exit 0 ;;
*) usage >&2; exit 2 ;;
esac
done
if ! command -v virsh >/dev/null 2>&1; then
echo "virsh not found. Install the libvirt client tools first." >&2
exit 1
fi
v() { virsh -c "$URI" "$@"; }
if ! v uri >/dev/null 2>&1; then
echo "Cannot connect to $URI (is libvirtd running, and do you have permission?)" >&2
exit 1
fi
echo "== Host: $(hostname) URI: $(v uri) Date: $(date '+%Y-%m-%d %H:%M:%S')"
echo
echo "== Virtual networks"
v net-list --all
echo "== Storage pools"
v pool-list --all --details
echo "== Domains"
printf '%-24s %-12s %5s %10s %-9s %s\n' NAME STATE VCPUS MEM_MIB AUTOSTART IP_ADDRESSES
mapfile -t domains < <(v list --all --name | sed '/^$/d')
if [ "${#domains[@]}" -eq 0 ]; then
echo "(no domains defined)"
exit 0
fi
running=0
stopped=0
for dom in "${domains[@]}"; do
info="$(v dominfo "$dom")"
state="$(v domstate "$dom" | head -n 1)"
vcpus="$(awk -F: '/^CPU\(s\)/ {gsub(/ /,"",$2); print $2}' <<<"$info")"
mem_kib="$(awk -F: '/^Max memory/ {gsub(/[^0-9]/,"",$2); print $2}' <<<"$info")"
autostart="$(awk -F: '/^Autostart/ {gsub(/ /,"",$2); print $2}' <<<"$info")"
ips="-"
if [ "$state" = "running" ]; then
running=$((running + 1))
ips="$(v domifaddr "$dom" 2>/dev/null | awk 'NR>2 && $4 {print $4}' | paste -sd, -)"
[ -n "$ips" ] || ips="unknown"
else
stopped=$((stopped + 1))
fi
printf '%-24s %-12s %5s %10s %-9s %s\n' \
"$dom" "$state" "${vcpus:-?}" "$(( ${mem_kib:-0} / 1024 ))" "${autostart:-?}" "$ips"
done
echo
echo "Total: ${#domains[@]} domains, $running running, $stopped not running"
Usage
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
chmod +x virsh-report.sh
sudo ./virsh-report.sh
./virsh-report.sh -c qemu:///session
./virsh-report.sh > /tmp/kvm-report-$(date +%F).txt
Notes
- The default connection is
LIBVIRT_DEFAULT_URIif set, otherwiseqemu:///system, which usually needs root or libvirt group membership. - Memory is the domain’s maximum memory from
virsh dominfo, converted from KiB to MiB. - IP addresses come from
virsh domifaddr, whose default source is the libvirt DHCP lease table. VMs on a host bridge or with static IPs may showunknown; on those,domifaddr --source agentworks if the QEMU guest agent runs in the guest. - The script parses human-readable virsh output, which is stable in practice but not a formal API. For monitoring systems, consider
virsh domstatsor the libvirt API bindings instead.
Official documentation
See also
- 0098 What Proxmox VE is: KVM virtual machines, LXC containers and a web UI Proxmox VE is an open source virtualization platform based on Debian. It runs KVM virtual machines and LXC containers and is managed… Beginner · Concept · Linux Article Basics · Installation & first steps 15 Oct 2026
- 0100 Create a VM and a cloud-init template with qm Create KVM virtual machines from the command line with qm, then build a reusable cloud-init template from a cloud image and clone… Intermediate · How-to · Linux Article Basics · VMs & containers 15 Oct 2026
- 0123 KVM vs QEMU vs libvirt: how Linux virtualization fits together KVM is the Linux kernel part that uses CPU virtualization extensions, QEMU is the program that emulates a full machine around it,… Beginner · Concept · Linux Article Basics 15 Oct 2026
- 0124 virsh cheat sheet: everyday libvirt commands A quick reference for virsh: list, start, stop and inspect VMs, change CPUs and memory, manage networks, storage pools and snapshots, with… Intermediate · Reference · Linux Article Basics 15 Oct 2026
- 0125 Create a KVM virtual machine with virt-install Use virt-install to create libvirt/KVM guests: install from an ISO, run a text-only install over the serial console, or import a cloud… Intermediate · How-to · Linux Article Basics 15 Oct 2026
- 0126 KVM snapshots and cloning with virsh and virt-clone Take, list, revert and delete libvirt snapshots with virsh, understand internal vs external and disk-only snapshots, and clone a stopped VM with… Advanced · How-to · Linux Article Basics 15 Oct 2026
Repos in the wild
- proxmox/qemu-server: the Proxmox VE virtual machine manager proxmox/qemu-server Read-only GitHub mirror of qemu-server, the Proxmox VE package that manages QEMU/KVM virtual machines and provides the qm and qmrestore commands. GitHub
- libvirt: virtualization API and daemon (GitHub mirror) libvirt/libvirt libvirt is the toolkit that manages KVM/QEMU and other hypervisors through one API, plus the virsh client. The GitHub repository is a read-only mirror; development happens on GitLab. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,685 chars