index.html :: rss :: github :: telegram :: email

virsh: survival guide

08 May 2025

practical, copy-paste-friendly collection of virsh command examples.

install os from the .iso:

virt-install \
    --name=fedora42 \
    --ram=2048 \
    --vcpus=2 \
    --os-variant=fedora42 \
    --network network=default \
    --graphics=vnc \
    --disk path=fedora42.qcow2,size=40 \
    --cdrom Fedora-Server-dvd-x86_64-42-1.1.iso

import provided .qcow2 file:

virt-install --import \
    --memory 2048 \
    --osinfo fedora41 \
    --disk Fedora-Server-Guest-Generic-42-1.1.x86_64.qcow2

virsh 101:

virsh list --all
virsh autostart --disable <name>
virsh start fedora42
virsh managedsave --domain alpine0 # save state and shutdown
virt-viewer --connect qemu:///system --wait alpine0
virsh screenshot alpine0 /tmp/console.bmp
virsh shutdown fedora42
virsh destroy alpine0
virsh reboot alpine0 # ctrl-alt-del at tty1
virsh reset alpine0 # hard reboot

create isolated netword:

virsh net-dumpxml default > network.xml
vim network.xml
# change <name> and <uuid>
# remove <forward/> tag
# increase <bridge name="virbrN+1">
# optinally change ip address and dhcp range

virsh net-create network.xml
virsh edit debian12
# change <source network='default'/> to a name of a network above

virsh start debian12 # check resut of "ip a"

listen traffic between wm and internet(works with default network):

tcpdump -i virbr0 -n

create/revert machine's snapshot:

virsh snapshot-create-as --domain alpine0 --name a0-base0
virsh snapshot-revert --domain alpine0 --snapshotname a0-base0 --running

make a full copy(virt-clone will update everything that have to be updated, e.g. mac addresses):

virsh shutdown alpine0
virt-clone \
        --original alpine0 \
        --name alpine1 \
        --file virt/alpine1.qcow2
virsh start alpine0
virsh start alpine1

edit raw vm options (runs $EDITOR ):

virsh edit alpine0

delete machine and all artifacts:

virsh undefine \
    --delete-storage-volume-snapshots \
    --remove-all-storage \
    alpine0

list machine interfaces:

virsh domiflist --domain alpine0

show memory stats (used, swap, page faults):

virsh dommemstat alpine0

rename a vm:

virsh domrename alpine0 alpine-000

describe state shortly:

virsh domstate alpine0 --reason

machine-readable stats:

virsh domstats --raw

show or modify guest interface parameters:

virsh domiftune --domain alpine0 --interface vnet0

save or restore vm's memory snapshot:

virsh save/restore alpine0 path/to/state.file

man 1 virsh is a document of the great quality.

see the useful exmaples section in man 1 virt-install