Virsh commands cheatsheet
Virsh commands cheatsheet
virtual machines
By Josphat Mutai - December 23, 2024 215980 3
Virsh interacts with Libvirt which is a library aimed at providing a long-term stable C API.
It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox and VMware vSphere
ESX.
For installation guide of KVM on most Linux distributions, check the link below:
List os variants
Virsh autostart vm
Virsh reboot vm
Virsh remove vm
Virsh create a vm
Virsh save vm
Restoring a saved vm
Virsh create volume
Virsh clone a vm
Virsh manage VM memory
ls a directory in a virtual machine
Conclusion
This is the first item on our virsh commands cheatsheet. This displays the host node
information and the machines that support the virtualization process.
List os variants
To get the value for the --os-variant run the following command:
osinfo-query os
Syntax:
Scroll down until <devices> section and modify values for the line below:
<source file=''/>
Example
<source file='/var/lib/libvirt/images/apacheserver01.qcow2'/>
Virsh start vm
This is an example on how to use virsh command to start a guest virtual machine. We’re
going to start test domain displayed above:
Virsh autostart vm
In case you would like to shutdown all running domains, just issue the command below:
Virsh reboot vm
Virsh remove vm
To cleanly remove a vm including its storage columes, use the commands shown below.
The domain test should be replaced with the actual domain to be removed.
sudo virsh destroy test 2> /dev/null
sudo virsh undefine test
sudo virsh pool-refresh default
sudo virsh vol-delete --pool default test.qcow2
Virsh create a vm
If you would like to create a new virtual machine with virsh, the relevant command to
use is virt-install. This is crucial and can’t miss on virsh commands cheatsheet
arsenal. The example below will install a new operating system from CentOS 7 ISO
Image.
sudo virt-install \
--name centos7 \
--description "Test VM with CentOS 7" \
--ram=1024 \
--vcpus=2 \
--os-type=Linux \
--os-variant=rhel7 \
--disk path=/var/lib/libvirt/images/centos7.qcow2,bus=virtio,size
--graphics none \
--location $HOME/iso/CentOS-7-x86_64-Everything-1611.iso \
--network bridge:virbr0 \
--console pty,target_type=serial -x 'console=ttyS0,115200n8 seria
This will return a fail message if an active console session exists for the provided
domain. To solve this run:
export EDITOR=vim
sudo virsh edit test
NOTE: When a domain is in a suspended state, it still consumes system RAM. Disk and
network I/O will not occur while the guest is suspended.
Virsh save vm
$ ls -l test.save
-rw------- 1 root root 328645215 Mar 18 01:35 test.saved
Restoring a saved vm
Here we’ll cover how to create a storage volume , attach it to a vm , detach it from a vm
and how to delete a volume.
To create a 2GB volume named test_vol2 on the default storage pool, use:
List volumes
You can confirm that the volume was added to the vm as a block device /dev/vdb
$ ssh test
Last login: Fri Mar 17 19:30:54 2017 from gateway
[root@test ~]#
export VM=test-vm
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/${VM}-disk2.q
$ ssh test
Last login: Sat Mar 18 01:52:33 2017 from gateway
[root@test ~]#
[root@test ~]# lsblk --output NAME,SIZE,TYPE
NAME SIZE TYPE
sr0 1024M rom
vda 10G disk
├─vda1 1G part
└─vda2 9G part
├─cl_test-root 8G lvm
└─cl_test-swap 1G lvm
[root@test ~]#
You can indeed confirm from this output that the device /dev/vdb has been detached
Please note that you can directly grow disk image for the vm using qemu-img
command, this will look something like this:
The main shortcoming of above command is that you cannot resize an image which has
snapshots.
# List volumes
$ sudo virsh vol-list --pool default
In this second last section of managing kvm guest machines with virsh command, we’ll
have a look at managing VM snapshots.
Here we’ll create another snapshot called test_vm_snapshot2, then revert to snapshot
test_vm_snapshot1
Virsh clone a vm
Domain with devices to clone must be paused or shutoff. So let’s shut it down:
This virsh commands cheatsheet section covers how to add additional virtual cpus to a
virtual machine:
Confirm that the number of vcpu has changed, the previous was 1, the current value is
2:
Also on virsh commands cheatsheet is managing RAM with virsh. To adjust the total ram
used by the guest operating system, the following commands are used:
# In MB
sudo virsh setmaxmem test 2048 --config
sudo virsh setmem test 2048 --config
sudo virsh shutdown test
sudo virsh start test
# Same in GB
sudo virsh setmaxmem test 16G --config
sudo virsh setmem test 16G --config
sudo virsh shutdown test
sudo virsh start test
Check domain info to confirm the current RAM allocated to the VM.
You can mount a virtual disk on KVM for offline administration. For this, we have a
ready article which you can reference from the link below:
The run:
E.g
$ sudo virsh list
Id Name State
----------------------------------------------------
10 allot_netxplorer_01 running
19 sg-ve-01 running
$ virt-top
$ sudo virt-top --csv file.csv
You can also send debug and error messages to a filename. To send error messages to
syslog you can do:
Add below XML contents within <devices> block (Accessible from outside)
Conclusion
Our virsh commands cheatsheet is now complete. In our next tutorial
on virsh commands, I’ll share with you my bash functions that come in handy when
managing Guest machines on KVM.