Basic operations to manage a KVM virtual machine

Today we will see basic operations to manage a KVM virtual machine.

Information and requirements

These elements are to be taken into consideration to follow this article:

State

Save virtual machine state

This command stops the virtual machine mail.illuad.local and saves the data to a file called state-mail.illuad.local. This will take a few moments depending upon the amount of memory in use by your virtual machine.

sudo virsh save mail.illuad.local state-mail.illuad.local

Domain mail.illuad.local saved to state-mail.illuad.local

Restore virtual machine state

To restore the previously saved state of a virtual machine, just specify the file name like this.

sudo virsh restore state-mail.illuad.local
Domain restored from state-mail.illuad.local

Power management

Suspend or pause

sudo virsh suspend mail.illuad.local
Domain 'mail.illuad.local' suspended

Resume

sudo virsh resume mail.illuad.local
Domain 'mail.illuad.local' resumed

Stop active virtual machines

To forcibly stop an active virtual machine, and leave it in the inactive state, execute this command.

sudo virsh destroy mail.illuad.local
Domain 'mail.illuad.local' destroyed

You can also gracefully stop the virtual machine instead of forcing it.

sudo virsh destroy mail.illuad.local --graceful
Domain 'mail.illuad.local' destroyed

Snapshot

Create

I don’t describe the use of a snapshot, but I do advise you to do one at the beginning. First, shutdown your virtual machine.

sudo virsh shutdown mail.illuad.local

Then, you can create it.

sudo virsh snapshot-create-as --domain mail.illuad.local --name "Start" --description "Virutal machine initial state"
Domain snapshot Start created

You can list snapshots like this.

sudo virsh snapshot-list mail.illuad.local
 Name    Creation Time               State
----------------------------------------------
 Start   2021-06-03 00:40:14 +0200   shutoff

Restore

sudo virsh shutdown mail.illuad.local
sudo virsh snapshot-revert --domain mail.illuad.local --snapshotname Start

Delete

sudo virsh snapshot-delete --domain mail.illuad.local --snapshotname Start
Domain snapshot Start deleted

Network

Retrieve virtual interfaces

sudo virsh domiflist mail.illuad.local
 Interface   Type      Source    Model    MAC
-------------------------------------------------------------
 vnet0       network   default   virtio   52:54:00:81:61:4e

Retrieve IP address(es)

sudo virsh domifaddr mail.illuad.local
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:81:61:4e    ipv4         192.168.122.238/24

Retrieve network interface stats

The virtual interface’s name of my virtual machine is vnet0, replace it by yours.

sudo virsh domifstat mail.illuad.local vnet0
vnet0 rx_bytes 11214
vnet0 rx_packets 181
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 4160
vnet0 tx_packets 49
vnet0 tx_errs 0
vnet0 tx_drop 0

Cleaning

Remove a virtual machine and delete its storage

Make sure your virtual machine is shutdown, otherwise you’ll have the following message.

error: Storage volume deletion is supported only on stopped domains

Make sure no snapshot belongs to your virtual machine, otherwise you’ll have the following message.

error: Requested operation is not valid: cannot delete inactive domain with 1 snapshots
sudo virsh undefine mail.illuad.local --remove-all-storage
Domain 'mail.illuad.local' has been undefined
Volume 'vda'(/var/lib/libvirt/images/mail.illuad.local.qcow2) removed.

General

Virtual machine information

sudo virsh dominfo mail.illuad.local
Id:             1
Name:           mail.illuad.local
UUID:           c712dfc7-fd1a-497c-b49e-4f45b0b58273
OS Type:        hvm
State:          running
CPU(s):         4
CPU time:       46.0s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

Dump virtual machine’s XML configuration

This command will display the complete configuration details (software and hardware) of the virtual machine.

sudo virsh dumpxml mail.illuad.local
<domain type='kvm' id='1'>
  <name>mail.illuad.local</name>
  <uuid>c712dfc7-fd1a-497c-b49e-4f45b0b58273</uuid>
  <metadata>
[...]

Libguestfs

You need the libguestfs tools installed to execute those commands.

Read file content

You can as well cat a file without doing SSH to the virtual machine or accessing it via the console.

sudo virt-cat --domain mail.illuad.local /etc/redhat-release
Rocky Linux release 8.4 (Green Obsidian)

Edit a file

Using virt-edit on live virtual machines, or concurrently with other disk editing tools, can be dangerous, potentially causing disk corruption. The virtual machine must be shut down before you use this command, and disk images must not be edited concurrently.

sudo virt-edit --domain mail.illuad.local /etc/hosts

Display virtual machine disk usage

sudo virt-df --domain mail.illuad.local
Filesystem                           1K-blocks       Used  Available  Use%
mail.illuad.local:/dev/sda1            1038336     138160     900176   14%
mail.illuad.local:/dev/cl/root        17811456    1410328   16401128    8%

List filesystems, partitions, block devices, LVM in a virtual machine

sudo virt-filesystems --all --uuid --human-readable --long --domain mail.illuad.local
Name         Type       VFS  Label MBR Size  Parent    UUID
/dev/sda1    filesystem xfs  -     -   1014M -         1ea2d7be-bfbb-4d61-90e2-ef8baee369ee
/dev/cl/root filesystem xfs  -     -   17G   -         5106b919-2692-4e50-9040-251795b68913
/dev/cl/swap filesystem swap -     -   2.0G  -         46cc4821-647b-4d83-8b62-4285b54ad56b
/dev/cl/root lv         -    -     -   17G   /dev/cl   HF2PvO-ecs6-W7ds-zENe-aK5t-Cqrz-MRJ1Kt
/dev/cl/swap lv         -    -     -   2.0G  /dev/cl   0YaYHy-aaEw-kTLM-sza1-CkDI-m66s-hqSdpR
/dev/cl      vg         -    -     -   19G   /dev/sda2 IoAFjZc5FuYYbmOERrJEX47bRL2JvmuZ
/dev/sda2    pv         -    -     -   19G   -         LgDuy214LzfntxBDFtTPDAFaXW5TB1Ii
/dev/sda1    partition  -    -     83  1.0G  /dev/sda  -
/dev/sda2    partition  -    -     8e  19G   /dev/sda  -
/dev/sda     device     -    -     -   20G   -         -

Volume and pool

List pool

The default pool name (and if you have followed my articles) is images. Otherwise, you can execute the following command to list pools.

sudo virsh pool-list
 Name     State    Autostart
------------------------------
 images   active   yes
sudo virsh vol-list --details --pool images
 Name                      Path                                              Type   Capacity    Allocation
------------------------------------------------------------------------------------------------------------
 centos_8_stream.qcow2     /var/lib/libvirt/images/centos_8_stream.qcow2     file   20.00 GiB   2.02 GiB
 mail.illuad.local.qcow2   /var/lib/libvirt/images/mail.illuad.local.qcow2   file   20.00 GiB   2.12 GiB

Remove a volume

sudo virsh vol-delete --pool images --vol mail.illuad.local.qcow2
Vol mail.illuad.local.qcow2 deleted

Refresh the pool

sudo virsh pool-refresh images
Pool images refreshed