Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

virsh: list, stop and diagnose your KVM VMs

33 min read
Improve this page

You run virsh shutdown, the command answers that the shutdown is in progress, and the VM stays running. Or virsh list no longer shows a VM you just stopped, or returns an empty list on your account while the list is full under sudo. virsh is the command line of libvirt: it starts, stops, describes and edits KVM VMs, and these three situations come down to how it chooses its connection and what it displays.

This lesson has you use virsh first without any VM, on a simulated hypervisor, then shows what changes on real VMs. Create a KVM VM (in French) already uses list, start, shutdown, destroy, undefine and autostart for the VM you have just created; this lesson explains what they really do (a shutdown that is requested or forced, what undefine deletes), the connection they use and the XML configuration. The pages Snapshots, clones and backups (in French), QEMU Guest Agent (in French) and Remote access to libvirt (in French) go deeper into the commands it only opens.

What you will learn

  • List VMs with virsh list --all and read their state
  • Start, pause, resume and stop a VM, and know why shutdown may do nothing
  • Save the state of a VM with save and restore it with restore
  • Tell destroy, which stops, from undefine, which deletes
  • Choose the qemu:///session or qemu:///system connection and read the URI with virsh uri
  • Export, redefine and edit the XML configuration, and set autostart

Prerequisites

You have installed KVM and libvirt (Installation (in French)) and, for the part on real VMs, created a machine with Create a KVM VM (in French). The exercises in this lesson need no VM: they run on a simulated hypervisor, introduced just below, with the virsh client alone (tested with libvirt-clients on Ubuntu 26.04, virsh 12.0.0).

Practice without a VM: the simulated hypervisor

The libvirt library that virsh loads ships with a demonstration driver, test, which simulates a hypervisor with one VM named test, with no daemon and no KVM. You connect to it with the -c option, which chooses the connection:

Terminal window
virsh --version
virsh -c test:///default uri
virsh -c test:///default list
Output
10.0.0
test:///default
Id Name State
----------------------
1 test running

The first line is the version of virsh. The second is the URI of the connection, and the list shows the test VM, active, with Id 1. The simulator outputs come from libvirt 10.0.0 (Ubuntu 24.04) and those of the real VMs from libvirt 11.3.0 (Debian 13). The latest stable version is 12.9.0, released on 2 November 2026: your outputs may differ slightly.

The simulator forgets everything at the end of each virsh command: every call starts again from a new, active test VM. To chain several actions in the same session, pass them as a single argument, separated by ;:

Terminal window
virsh -c test:///default 'suspend test; domstate test'
Output
Domain 'test' suspended
paused

domstate sees the suspension done just before, because the two commands share the session. All the simulation examples in this lesson are written this way, between single quotes. The files they create go in a throwaway folder: mkdir -p ~/virsh-lab && cd ~/virsh-lab.

libvirt vocabulary

virsh messages talk about domains, pools and volumes, never about "VMs". The table translates: it helps you read errors, because failed to get domain means "I cannot find this machine" and has nothing to do with a DNS domain name.

libvirt termMeaningCommon equivalent
domainVirtual machineVM
poolStorage locationFolder or LVM volume group
volumeDisk in a poolqcow2 file
networkVirtual networkBridge or NAT
shut offVM stopped, still definedPowered off

Mental model: how virsh works

virsh does not control QEMU directly: it talks to libvirt, which starts and monitors one QEMU process per VM. The diagram follows the path of a command, from your terminal down to the processor. Look at the top first: it is the connection (the URI given to -c) that chooses the daemon virsh talks to. Each daemon only starts and sees its own VMs: web and db for the host daemon, personal for your account's. Below the VMs, the path is the same in both cases.

The connection of virsh chooses the daemon: qemu:///system leads to the host daemon (libvirtd or virtqemud), which starts the VMs web and db; qemu:///session leads to the user daemon, which starts the VM personal. Each VM is a QEMU process; all three rely on the KVM kernel module, itself on the Intel VT-x or AMD-V processor.     virsh     the connection decidesHost daemonlibvirtd or virtqemud qemu:///system User daemonstarted for you qemu:///session      VM web     QEMU     VM db     QEMU     KVM     kernel module     VM personal     QEMUProcessorIntel VT-x or AMD-VHostUsercommand you rundaemon, kernel or hardwarerunning VMconnection to libvirtstarts, or relies on
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

The virsh command in detail

A virsh command is written in this order: the connection (optional), the action, then the name of the VM. Without -c, virsh uses the default connection of your account, explained below. The action is a word such as list or domstate: virsh help lists them, and most take the domain name as an argument.

Anatomy of the command

Syntaxvirsh [options] action [domain]

  1. virsh the programThe libvirt command line.
  2. -c test:///default the connectionoptionalThe targeted hypervisor. Without -c, the default connection of the account: virsh uri displays it.
  3. domstate the actionHere, give the state.
  4. test the domainThe name of the VM.

Outputrunning

How virsh chooses what it displays

Every command follows the same cycle: open the connection, act on the domain, then display. Before clicking on the card, predict what virsh -c test:///default 'shutdown test; list' displays, then what list --all gives in place of list. Then check with the card.

Mental model

Why a VM disappears from virsh list

open the connectionact on the domainlist the domains each command follows this order

$ virsh -q -c test:///default 'list'

  1. virsh connects to the simulated hypervisor. A domain named test is active there.
  2. No stop request: the domain stays active.
  3. The domain is active: list displays it, with or without --all.

What the animation shows

  1. A stopped domain still exists, but list alone does not show it.
  2. The number changes at each start; the name stays.
  3. Everything depends on the connection: each URI has its own domains.
  4. Stopping is not removing: start runs it again.

Golden rules

Always list with --all

Otherwise stopped VMs are invisible:

virsh list --all

Practice without risk

The simulated hypervisor forgets everything at the end:

virsh -c test:///default

Essential vocabulary

Look · without changing anything

list
The active domainsvirsh -q -c test:///default list → test, active
domstate
The state of a domainvirsh -q -c test:///default domstate test → running
domstate
An unknown domainvirsh -q -c test:///default domstate absent; echo $? → code 1
net-list
The networksvirsh -q -c test:///default net-list --all → default

Act · on the domain

shutdown
Stopvirsh -q -c test:///default 'shutdown test; domstate test' → shut off
start
Run again: the number changesvirsh -q -c test:///default 'shutdown test; start test; list' → number 2
suspend
Pausevirsh -q -c test:///default 'suspend test; domstate test' → paused
start
Start an active domainvirsh -q -c test:///default start test; echo $? → code 1

List VMs: list and --all

virsh list only displays active domains. A stopped VM still exists, but only appears if you add --all, with no message to tell you.

Terminal window
virsh -c test:///default list
virsh -c test:///default list --all
Output
Id Name State
----------------------
1 test running
Id Name State
----------------------
1 test running

The Id column only exists for an active domain: a stopped VM has none and shows -. The two lists are identical here because the only VM is active; they differ as soon as a VM is stopped. Make virsh list --all your reflex. The --name option keeps only the names, which is useful in a script, but it has the same trap (see below).

Read the state of a VM: domstate and dominfo

domstate returns the state in one word, dominfo gives the VM's record.

Terminal window
virsh -c test:///default domstate test
virsh -c test:///default dominfo test
Output
running
Id: 1
Name: test
UUID: 6695eb01-f6a4-8304-79aa-97f2502e193f
OS Type: linux
State: running
CPU(s): 2
CPU time: 1795617439.9s
Max memory: 8388608 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
...

Look at four fields. Max memory is the memory ceiling of the VM and Used memory what it uses now, both in KiB. Persistent: yes means the VM is defined in libvirt (it survives a shutdown), whereas a transient VM disappears as soon as it stops. Autostart says whether it starts with the host. The test driver makes up some values: CPU time and Security model mean nothing here, and on a real VM OS Type is hvm.

Session or system: which list do you see?

libvirt has two separate spaces. qemu:///system is the host daemon's, which runs VMs as a service (disks in /var/lib/libvirt). qemu:///session is your account's, with its VMs in your home directory. A VM created in one does not appear in the other. Without -c, virsh looks for the URI in this order: the LIBVIRT_DEFAULT_URI environment variable, the uri_default option of the libvirt.conf file (/etc/libvirt/libvirt.conf for root, ~/.config/libvirt/libvirt.conf for an ordinary account), then a try of each hypervisor until one answers. virsh also reads VIRSH_DEFAULT_CONNECT_URI, an old variable whose documentation advises against its use. Do not guess the result, read it with virsh uri.

An ordinary account therefore does not get the same URI everywhere. On Ubuntu 24.04, the libvirt-clients package installs /etc/profile.d/libvirt-uri.sh, which exports LIBVIRT_DEFAULT_URI="qemu:///system" when a login shell opens: the account lands on system without any configuration. On Ubuntu 26.04, with the client alone, /etc/profile.d does not contain this file and LIBVIRT_DEFAULT_URI is empty. The Debian 13 libvirt-clients package does not contain it either: an account in the libvirt group, in a login session, gets qemu:///session, and system only works with -c or an exported variable.

Account alice in the libvirt group, Debian 13 (libvirt 11.3.0): sudo -iu alice virsh uri
qemu:///session
Ubuntu 26.04.1, virsh 12.0.0, client only: virsh uri
error: failed to connect to the hypervisor
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

With no libvirt daemon on the machine, virsh uri fails instead of answering: to display the default URI, virsh must first connect. If your account follows the Installation page (libvirt group, LIBVIRT_DEFAULT_URI exported as qemu:///system), virsh uri returns qemu:///system and virsh list --all gives you the system list.

Terminal window
virsh list --all
virsh --connect qemu:///session list --all
virsh --connect qemu:///system list --all
Output on Debian 13, libvirt 11.3.0, account outside the libvirt group, SSH connection
Id Name State
--------------------
Id Name State
--------------------
error: failed to connect to the hypervisor
error: authentication unavailable: no polkit agent available to authenticate action 'org.libvirt.unix.manage'

On Debian, the default list is empty: it matches the session, like the second one. Yet sudo virsh list --all showed the essai VM there, because sudo uses qemu:///system. The third command asks for system without having the right: for the org.libvirt.unix.manage action, polkit (the service that controls access to the daemon) requires an administrator to authenticate, and an SSH connection has no graphical agent to ask. There are two ways out: go through sudo, or belong to the libvirt group (sudo usermod -aG libvirt $USER, then a new session) and add -c qemu:///system or export LIBVIRT_DEFAULT_URI. On Debian 13, the group gives access but the default URI stays qemu:///session. This group gives access to the whole libvirt API, hence to the host: only open it to administration accounts (see Installation (in French)).

In a script, always pass -c qemu:///system explicitly, including in a loop (for vm in $(virsh -c qemu:///system list --all --name)): the variable changes from machine to machine, and the script would act on the wrong list.

Start, pause and resume

start starts a stopped VM. On an already active VM, it fails, with exit code 1:

Terminal window
virsh -c test:///default start test
virsh -c test:///default 'suspend test; list; resume test; list'
Output
error: Domain is already active
Domain 'test' suspended
Id Name State
---------------------
1 test paused
Domain 'test' resumed
Id Name State
----------------------
1 test running

suspend freezes the VM in memory: it keeps its Id and goes to paused, which is not the shut off state. resume puts it back to work exactly where it was. A typo in the action gives error: unknown command: 'shutdow', with code 1 as well.

Stop a VM: shutdown, and why it may do nothing

shutdown cuts nothing: it sends the VM a shutdown request, like a short press on the power button (ACPI signal), and it is the guest system that powers off. On the simulator, the VM stops at once, which hides an important behavior. On a real VM, virsh answers that the shutdown has started and checks nothing. To see it, define a 64 MiB VM with no disk and no system. First save this XML in /tmp/essai.xml, with your text editor:

<domain type='qemu'>
<name>essai</name>
<memory unit='MiB'>64</memory>
<vcpu>1</vcpu>
<os><type arch='x86_64' machine='q35'>hvm</type></os>
<devices/>
</domain>
Terminal window
sudo virsh define /tmp/essai.xml
sudo virsh start essai
sudo virsh shutdown essai
sudo virsh domstate essai
Output (libvirt 11.3.0, Debian 13)
Domain 'essai' defined from /tmp/essai.xml
Domain 'essai' started
Domain 'essai' is being shutdown
running

The essai VM has no operating system: nobody inside answers the signal, and domstate still returns running. On a VM that runs a real system, the shutdown completes when that system reacts to the signal, which takes time.

To see a shutdown that completes, create cirros1, a 256 MB VM that boots CirrOS (a tiny Linux image made for tests). The save, snapshot, domifaddr and domblklist examples below use it too, and the default network must be active (sudo virsh net-start default). If you prefer to keep the VM from Create a KVM VM (in French), ubuntu-test in its examples, replace cirros1 with its name. On your side, the name and disk path in domblklist, the size of the save file, the IP address and the shutdown time will be your own.

Terminal window
sudo sh -c "cd /var/lib/libvirt/images && curl -sSfLO https://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img && cp cirros-0.6.3-x86_64-disk.img vm1.qcow2"
sudo virt-install --name cirros1 --memory 256 --vcpus 1 --import --disk path=/var/lib/libvirt/images/vm1.qcow2,format=qcow2 --network network=default --osinfo detect=on,require=off --graphics none --noautoconsole

On cirros1, started shortly before, domstate still returned running more than 75 seconds after shutdown; restarted 75 seconds before the request, it returned shut off after 58 seconds:

sudo virsh shutdown cirros1, then domstate read every 2 seconds (excerpts)
Domain 'cirros1' is being shutdown
+0s running
...
+58s shut off

These durations are those of this VM, not a rule: read domstate again, wait, and do not assume it failed right away. On Linux, it is systemd-logind or acpid that receives the power button press and starts the shutdown. If the QEMU Guest Agent (in French) is configured in the VM, virsh shutdown prefers it to ACPI, and --mode acpi or --mode agent forces one channel or the other.

Save the state: save, restore and snapshot

save stops the VM while writing its state (memory included) to a file, restore starts it again exactly where it was. It is the command to use to free the machine without losing the work in progress. On cirros1, running (sudo virsh start cirros1 if it is stopped):

Terminal window
sudo virsh save cirros1 /var/tmp/x.sav
ls -lh /var/tmp/x.sav
sudo virsh domstate --reason cirros1
sudo virsh restore /var/tmp/x.sav
sudo virsh restore /var/tmp/x.sav
Output (libvirt 11.3.0, Debian 13)
Domain 'cirros1' saved to /var/tmp/x.sav
-rw------- 1 root root 135M Nov 25 16:00 /var/tmp/x.sav
shut off (saved)
Domain restored from /var/tmp/x.sav
error: Failed to restore domain from /var/tmp/x.sav
error: Requested operation is not valid: domain 'cirros1' is already active

After save, the VM is shut off (saved) and its state fits in a 135 MB file, for 256 MB of memory. restore puts it back to running. The second restore is refused while the VM is active, and the file stays in place: libvirt does not delete it. A file can only be restored once: once the VM has run again, its disks change, and the same file would bring it back with a memory that no longer matches the disks. Delete it after the restore, and keep it somewhere other than /tmp.

A snapshot, by contrast, keeps a return point without stopping the VM. On cirros1, whose disk is a qcow2 file, it succeeds; on essai, with no disk, it fails:

Terminal window
sudo virsh snapshot-create-as cirros1 s1
sudo virsh snapshot-list cirros1
sudo virsh snapshot-create-as essai snap1
Output (libvirt 11.3.0, Debian 13)
Domain snapshot s1 created
Name Creation Time State
---------------------------------------------
s1 2026-11-25 16:00:50 +0000 running
error: unsupported configuration: internal and full system snapshots require all disks to be selected for snapshot

On an active VM, snapshot-create-as creates an internal snapshot by default: the memory state is written into a disk of the VM, in qcow2 format. essai has no disk, so libvirt has nowhere to write it and refuses. It also refuses when a disk is excluded from the snapshot. A VM that has snapshots cannot be erased with undefine without --snapshots-metadata, which removes their metadata from libvirt. The conditions for a snapshot that succeeds, its types and the difference from a backup are in Snapshots, clones and backups (in French).

The shutdown that did not happen Exercise 1 of 2 · ~5 min · terminal

A colleague runs virsh -c test:///default 'shutdown test' on the simulator, then virsh -c test:///default list --all in a second command. They conclude that the shutdown failed, even though shutdown answered that it was in progress. Find out why, then write a single command that proves the shutdown.

Run the two commands again

Terminal window
virsh -c test:///default 'shutdown test'
virsh -c test:///default list --all

Note the state that list --all displays, and compare it with what shutdown has just answered.

Prove the shutdown in the same session

Write a single virsh command that stops test then reads its state back with the reason. The --reason option of domstate adds the cause of the state in parentheses.

A hint 1 / 2

Each virsh command run on its own opens a new simulator session.

A hint 2 / 2

Group shutdown and domstate in a single argument, separated by a semicolon.

I have tried: show the solution
Output of the two separate commands
Domain 'test' is being shutdown
Id Name State
----------------------
1 test running

The second command opens a new session where test is active again: the shutdown did not fail, it was forgotten. In a single session, the proof is virsh -c test:///default 'shutdown test; domstate --reason test', which returns shut off (shutdown). Before shutdown, the same option returns running (unknown). On a real VM, this trap does not exist: the state persists, and the risk is the opposite, a shutdown that is accepted but has no effect, which only domstate reveals.

Stop abruptly or delete: destroy and undefine

destroy cuts the VM at once, like pulling the plug: the guest system has no time to close its files, hence a risk of corruption. Keep it for a VM that no longer answers shutdown. Despite its name, destroy deletes nothing: the definition and the disks stay, and start starts the VM again.

With --graceful, destroy does not force: if the QEMU process does not stop after a reasonable delay, the command returns an error instead of killing the process (SIGKILL). Deletion is done with undefine, which removes the VM's definition from libvirt. The disks stay on the host. On a stopped VM, --remove-all-storage also deletes the volumes that libvirt manages in a pool, even if another VM uses one, and only those: a file placed outside any pool stays, and the base images of a qcow2 disk are kept. --storage vda targets a single volume, by the name that the domblklist command (see below) gives in Target. If a deletion fails, the definition may already be removed: check with domblklist before, and with vol-list after.

undefine refuses a VM that has snapshots until you add --snapshots-metadata, a VM that has an image created by managedsave (the variant of save where libvirt chooses the file) until you add --managed-save, and a UEFI VM until you add --nvram (erases the UEFI variables file) or --keep-nvram (keeps it). On an active VM, undefine does not stop it: it becomes transient (Persistent: no in dominfo) and disappears from libvirt when it stops.

Get the IP address of a VM

To connect to a VM over SSH, you need its IP address. virsh domifaddr (with -c qemu:///system or sudo, as for any system VM) looks for it in three places, depending on the --source option: lease reads the DHCP leases of the libvirt network, agent asks the QEMU Guest Agent inside the VM, and arp reads the host's ARP table.

On the cirros1 VM, attached to the default network:

Terminal window
sudo virsh domifaddr cirros1
sudo virsh domifaddr cirros1 --source arp
sudo virsh domifaddr cirros1 --source agent
sudo virsh net-dhcp-leases default
virsh domifaddr cirros1
Outputs (libvirt 11.3.0, Debian 13)
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet0 52:54:00:2c:53:73 ipv4 192.168.122.166/24
Name MAC address Protocol Address
-------------------------------------------------------------------------------
error: Failed to query for interfaces addresses
error: argument unsupported: QEMU guest agent is not configured
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
------------------------------------------------------------------------------------------------------------
2026-11-25 16:58:58 52:54:00:2c:53:73 ipv4 192.168.122.166/24 - 01:52:54:00:2c:53:73
error: failed to get domain 'cirros1'

The first command has no option: the default source, lease, reads the DHCP leases of the libvirt network and gives the address. --source arp queries the host's ARP table: on this VM, the table is empty and the exit code is 0, so an empty table does not signal an error. --source agent fails, because the QEMU Guest Agent (in French) is not configured in the VM. net-dhcp-leases default lists all the leases of the network with the MAC address; Hostname is - because this VM did not announce a name. The last command, without sudo, fails on failed to get domain: it queries the session, which does not contain cirros1.

domifaddr queries a single source at a time: an empty table only says that this one found nothing. Try the other two before concluding that the VM has no address. The default network must be active: on the Debian 13 tested, it was defined but inactive until sudo virsh net-start default (net-autostart default starts it with the host). The network is detailed in KVM networking (in French).

See the disks of a VM: domblklist

Before editing or deleting a VM, list its disks with sudo virsh domblklist cirros1. Target is the name of the disk as the VM sees it, Source the file on the host: it is the list to read before any --remove-all-storage. The net- and pool- prefixes apply the same operations to networks and pools, detailed in KVM/libvirt storage (in French).

Output (libvirt 11.3.0, Debian 13)
Target Source
---------------------------------------------
hda /var/lib/libvirt/images/vm1.qcow2

Export, redefine and edit the configuration

dumpxml saves the configuration, not the data: the disks remain files to copy separately. For a backup, add --inactive. Without it, you get the XML of the running VM, which libvirt has completed (the id, the resolved CPU models: host-model becomes custom) and which does not contain the changes planned for the next start.

Terminal window
virsh -c test:///default dumpxml test > actif.xml
virsh -c test:///default dumpxml --inactive test > inactif.xml
diff actif.xml inactif.xml
Output of diff
1c1
< <domain type='test' id='1'>
---
> <domain type='test'>

Here the only difference is the id attribute, which only an active VM has. define creates a VM from an XML file, without starting it, and refuses a truncated XML by giving the line:

Terminal window
head -5 inactif.xml > faux.xml
virsh -c test:///default define faux.xml
Output
error: Failed to define domain from faux.xml
error: (domain_definition):6: Premature end of data in tag domain line 1

Premature end of data signals a tag that is never closed. On an already defined VM, define replaces the definition, but not the running VM. With a maximum memory raised to 4194304 KiB in test2.xml:

Terminal window
sed -e 's#8388608#4194304#' actif.xml > test2.xml
virsh -c test:///default 'define test2.xml; dominfo test' | grep -E 'Max|Used'
virsh -c test:///default 'shutdown test; define test2.xml; start test; dominfo test' | grep -E 'Max|Used'
Output
Max memory: 8388608 KiB
Used memory: 2097152 KiB
Max memory: 4194304 KiB
Used memory: 2097152 KiB

The first command changes nothing while the VM runs: the new definition waits for the next start. The second stops, redefines and restarts, and Max memory takes the new value. virsh edit my-vm does the same without a file: it opens the XML in $EDITOR or $VISUAL and validates it when you save.

Copy a VM from its XML Exercise 2 of 2 · ~10 min · terminal

You want a second VM, web, with 1 GiB of maximum memory and 512 MiB in use, copied from the test VM of the simulator. Export test, change the name, define the new VM, start it and check its memory with dominfo. Work in ~/virsh-lab.

Export and try a define

Terminal window
virsh -c test:///default dumpxml test > web.xml
sed -e 's#<name>test</name>#<name>web</name>#' web.xml > memeuuid.xml
virsh -c test:///default 'define memeuuid.xml; list --all'

Read the error: it says why libvirt refuses to create a second VM.

Fix, define, start, check

Export again with --inactive into web2.xml, fix it with sed -i (name, memory, and what the error requires), then run define web2.xml; list --all; start web; list; dominfo web in a single session. The maximum memory is 8388608 KiB in test; the memory in use, 2097152.

A hint 1 / 2

Two VMs cannot have the same unique identifier.

A hint 2 / 2

Delete the identifier line: libvirt generates a new one at define. The maximum memory is the memory tag, the memory in use currentMemory; 1 GiB is 1048576 KiB.

I have tried: show the solution
Output of define memeuuid.xml
error: Failed to define domain from memeuuid.xml
error: operation failed: domain 'test' is already defined with uuid 6695eb01-f6a4-8304-79aa-97f2502e193f

The copied XML carries the <uuid> of test: libvirt refuses, and list --all shows that only test exists. The fix removes the <uuid> line and sets the memory:

Terminal window
virsh -c test:///default dumpxml --inactive test > web2.xml
sed -i -e 's#<name>test</name>#<name>web</name>#' -e '/<uuid>/d' -e 's#8388608#1048576#' -e 's#2097152#524288#' web2.xml
virsh -c test:///default 'define web2.xml; list --all; start web; list; dominfo web'
Output (dominfo shortened)
Domain 'web' defined from web2.xml
...
Domain 'web' started
Id Name State
----------------------
1 test running
2 web running
Max memory: 1048576 KiB
Used memory: 524288 KiB

web is defined stopped (shut off, with no Id, the first list --all showed it), then started with Id 2: Id 1 is taken by test. libvirt generated a new UUID at define.

A for vm in $(virsh list --name) loop skips stopped VMs, with no message, like list alone. In the same session, after define web2.xml (the file of exercise 2), list --name returns only test, and list --all --name returns test then web. In a script, always use --all.

setmem changes the memory in use of an active VM, setvcpus the number of active processors. On the simulator, setmem test 1048576 takes Used memory from 2097152 to 1048576 KiB, but Max memory stays at 8388608: setmem never exceeds this ceiling. Likewise, setvcpus test 4 fails with requested cpu amount exceeds maximum (4 > 2). To go higher, change the ceiling in the configuration (virsh edit, or setvcpus --maximum --config), then restart the VM. Without an option, these commands change the running VM (--live) and not its definition (--config).

Start with the host: autostart

autostart marks a VM to start at the same time as the host, autostart --disable cancels it. You check the result in dominfo.

Terminal window
virsh -c test:///default 'autostart test; dominfo test' | grep -i autostart
Output
Domain 'test' marked as autostarted
Autostart: enable

The Autostart field goes from disable to enable. With autostart --disable test, in the same session, dominfo sets it back to disable (Domain 'test' unmarked as autostarted). On a real VM, it is the same command with sudo or -c qemu:///system.

Logs and troubleshooting

When virsh fails or a VM does not start, the reason is in the libvirt daemon's journal. There are two layouts: a single daemon, libvirtd, or separate daemons (virtqemud, virtnetworkd, virtstoraged). systemctl is-active libvirtd virtqemud tells you which one runs.

On Debian 13, right after installation, both units returned inactive; on a machine already in use, libvirtd returned active and virtqemud inactive, and you then read the journal with journalctl -u libvirtd. On RHEL 9 and 10, a fresh installation uses the separate daemons, and it is journalctl -u virtqemud that contains the VM start errors (virtnetworkd for networks, virtstoraged for storage). An upgraded machine keeps the mode it used before the upgrade, and libvirtd remains usable on RHEL, where it is deprecated. The journal of each VM is in /var/log/libvirt/qemu/<name>.log. For broader failures (a VM that does not start, a missing network), see KVM troubleshooting (in French).

SymptomLikely causeSolution
failed to connect to the hypervisor with no polkit agent availableAccount with no access to qemu:///system, connection without an interfacesudo virsh ... or add the account to the libvirt group (Debian, Ubuntu)
failed to get domain 'x'The name does not exist in this connectionvirsh list --all then check the URI with virsh uri
Domain is already active or unknown command: 'shutdow'start on an already active VM, or a typo in the actionvirsh domstate x, virsh help
shutdown accepted, VM still runningThe guest system does not answer the signal, or has not finished bootingWait, read domstate again, then destroy as a last resort
unsupported configuration on snapshot-create-asVM with no disk, or disks incompatible with this type of snapshotSnapshots, clones and backups (in French)
failed to connect ... libvirt-sock: No such file or directoryNo libvirt daemon on this machine (client only)Install and start libvirt, or target a remote host with -c

Clean up

The simulator forgets its state, so only the files you created remain. From ~/virsh-lab, delete them one by one with rm actif.xml inactif.xml faux.xml test2.xml web.xml memeuuid.xml web2.xml, then the empty folder with cd ~ && rmdir ~/virsh-lab. On a real host, if you replayed the examples, stop essai with sudo virsh destroy essai, remove its definition with sudo virsh undefine essai, then delete /tmp/essai.xml. Do the same for cirros1, adding --snapshots-metadata to undefine since it has a snapshot, then delete with sudo rm the file /var/lib/libvirt/images/vm1.qcow2, the cirros-0.6.3-x86_64-disk.img image in the same folder and /var/tmp/x.sav if it still exists.

Key points

  • virsh list only displays active domains: a stopped VM still exists and shows up with list --all.
  • shutdown sends a request to the VM without checking: read domstate again and give the guest system time. destroy cuts without deleting, undefine removes the definition, --remove-all-storage also deletes the disks managed in a pool.
  • qemu:///session and qemu:///system each have their own VMs: the default URI depends on the configuration, read it with virsh uri.
  • save writes the state to a file and stops the VM, restore starts it again, once per file; a snapshot keeps a return point without stopping it.
  • dumpxml --inactive saves the configuration, not the disks; define and edit change the definition, which only applies to an active VM at the next start.
  • domifaddr queries a single source (lease, agent or arp): try the others before concluding; net-dhcp-leases default lists the leases of the network.

Knowledge check

Check that you have grasped the essentials of this lesson. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer