1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/bin/bash
# desktop-assistant tool: virtual machine status through virsh (read-only).
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# vms.sh [NAME]: without NAME, every defined VM with its state; with NAME,
# that VM's state, vCPUs, memory, autostart and IP address when one is
# known. Only list, domstate, dominfo and domifaddr run here: nothing is
# started, stopped or edited. Uses libvirt's default URI (LIBVIRT_DEFAULT_URI
# overrides it).
set -uo pipefail
name="${1:-}"
if [ -z "$name" ]; then
vms=$(virsh list --all --name) || exit 1
[ -n "${vms//[[:space:]]/}" ] || { echo "no virtual machines defined"; exit 0; }
while read -r vm; do
[ -n "$vm" ] && echo "$vm: $(virsh domstate "$vm")"
done <<<"$vms"
exit 0
fi
# An unknown name leaves virsh's own error as the last stderr line.
info=$(virsh dominfo "$name") || exit 1
field() { sed -n "s/^$1: *//p" <<<"$info"; }
echo "$name: $(field State), $(field 'CPU(s)') vCPUs," \
"$(( $(field 'Max memory' | awk '{print $1}') / 1048576 )) GiB memory," \
"autostart $(field Autostart)"
# The DHCP lease is empty for bridged guests; ARP or the guest agent knows.
for source in lease arp agent; do
ip=$(virsh domifaddr "$name" --source "$source" 2>/dev/null \
| awk 'NR > 2 && $4 != "" && $4 !~ /^127\./ {sub(/\/.*/, "", $4); print $4}' | paste -sd, -)
[ -n "$ip" ] && { echo "IP address: $ip"; break; }
done
|