hostnamectl; # tested on 
   Static hostname: lenovo
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-17-amd64
      Architecture: x86-64

# with
virsh --version

libvirtd --version
libvirtd (libvirt) 5.0.0

/usr/bin/kvm --version
QEMU emulator version 3.1.0 (Debian 1:3.1+dfsg-8+deb10u8)
Copyright (c) 2003-2018 Fabrice Bellard and the QEMU Project developers

kvm qemu shutdown all vms

cat /scripts/

echo "=== shutting down all kvm vms ==="
for i in $(virsh list | grep running | awk '{print $2}'); do virsh shutdown $i; done
virsh list --all

# example:

start all vms with keyword in name

cat /scripts/
echo "=== starting all kvm vms that contain the word $1 ==="
for i in $(virsh list --all | grep $1 | awk '{print $2}'); do virsh start $i; done
virsh list --all

# example:
/scripts/ keyword

snapshot all vms:

cat /scripts/
echo "=== virsh: snapshot all vms ==="
for i in $(virsh list --all | awk '(NR>2)' | awk '{print $2}'); do virsh snapshot-create $i; done
virsh list --all

# example:
=== virsh: snapshot all vms ===
Domain snapshot 1628801587 created
Domain snapshot 1628801587 created
Domain snapshot 1628801587 created
Domain snapshot 1628801587 created
Domain snapshot 1628801587 created
 Id   Name               State
 -    ansible.master     shut off
 -    ansible.srv1       shut off
 -    ansible.srv2       shut off
 -    ansible.srv3       shut off
 -    debian10template   shut off

# list all snapshots of a vmname
virsh snapshot-list --domain vmname
# revert vmname to snapshotname
virsh snapshot-revert vmname snapshotname

automatically revert to snapshot, start vm and connect to vm:

vim /scripts/kvm-qemu/

(needs to be run as root)


  1. vmname
  2. snapshotname
  3. user is the user that is running the x11 desktop 🙂
echo "=== revert to working snapshot ==="
virsh snapshot-revert vmname snapshotname

echo "=== starting vm ==="
virsh start vmname
export DISPLAY=':0.0'
/usr/bin/cp -fv /home/user/.Xauthority /root/.Xauthority
virt-viewer 'vmname'

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!