This is a comprehensive virsh commands cheatsheet: Virsh is a management user interface for virsh guest domains. Virsh can be used to create, pause, restart, and shutdown domains. In addition, virsh can be used to list current domains available in your Virtualization hypervisor platform.
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 ESX.
For installation guide of KVM on most Linux distributions, check the link below:
In this virsh commands cheatsheet, I’ll show you most used virsh commands to manage Guest Virtual Machines running on KVM or Xen Hypervisor.
The basic structure of most virsh usage is:
$ virsh [OPTION]... [ARG]...
Virsh display node information:
This is the first item on our virsh commands cheatsheet. This displays the host node information and the machines that support the virtualization process.
$ sudo virsh nodeinfo
CPU model: x86_64
CPU(s): 8
CPU frequency: 2200 MHz
CPU socket(s): 1
Core(s) per socket: 4
Thread(s) per core: 2
NUMA cell(s): 1
Memory size: 12238908 KiB
List os variants
To get the value for the --os-variant run the following command:
$ osinfo-query os
Virsh list all domains
To list both inactive and active domains, use the command:
$ sudo virsh list --all
Id Name State
----------------------------------------------------
- admin shut off
- cloudstack shut off
- hyperv shut off
- ipa shut off
- katello shut off
- node1 shut off
- node2 shut off
- node3 shut off
- server1 shut off
- server2 shut off
- test shut off
- ubuntu14.04 shut off
- win12k shut off
- xen shut off
- zenoss shut off
List only active domains
To list only active domains with virsh command, use:
$ sudo virsh list
Id Name State
----------------------------------------------------
Virsh rename domain
Syntax:
virsh domrename currentnamenewname
List available domains
$ sudo virsh list --all
Id Name State
----------------------------
4 centos-8.2 running
$ sudo virsh domrename centos-8.2 apacheserver01
Domain successfully renamed
We’ve changed the name of the domain from centos-8.2 to apacheserver01
Virsh change domain boot disk
Edit the domain by passing its name:
$ sudo virsh edit domain
Scroll down until section and modify values for the line below:
Example
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:
$ sudo virsh start test
Domain test started
$ sudo virsh list
Id Name State
----------------------------------------------------
3 test running
Virsh autostart vm
To set a vm to start automatically on system startup, do:
$ sudo virsh autostart test
Domain test marked as autostarted
$ sudo virsh dominfo test
Id: 9
Name: test
UUID: a943ed42-ba62-4270-a41d-7f81e793d754
OS Type: hvm
State: running
CPU(s): 2
CPU time: 144.6s
Max memory: 2048 KiB
Used memory: 2048 KiB
Persistent: yes
Autostart: enable
Managed save: no
Security model: none
Security DOI: 0
Keep an eye on the option Autostart: enable.
Virsh autostart disable
To disable autostart feature for a vm:
$ sudo virsh autostart --disable test
Domain test unmarked as autostarted
$ sudo virsh dominfo test
Id: -
Name: test
UUID: a943ed42-ba62-4270-a41d-7f81e793d754
OS Type: hvm
State: shut off
CPU(s): 2
Max memory: 2048 KiB
Used memory: 2048 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
Virsh stop vm, virsh shutdown vm
To shutdown a running vm gracefully use:
$ sudo virsh shutdown test
Domain test is being shutdown
$ sudo virsh list
Id Name State
----------------------------------------------------
Virsh force shutdown vm
You can do a forceful shutdown of active domain using the command:
$ sudo virsh destroy test
Virsh stop all running vms
In case you would like to shutdown all running domains, just issue the command below:
for i in `sudo virsh list | grep running | awk 'print $2'` do
sudo virsh shutdown $i
done
Virsh reboot vm
To restart a vm named test, the command used is:
sudo virsh reboot test
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
In this example, storage volume is named /var/lib/libvirt/images/test.qcow2
You can also use undefine with–remove-all-storage option:
sudo virsh undefine test --remove-all-storage
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.
This will return a fail message if an active console session exists for the provided domain. To solve this run:
sudo virsh console test --force
Virsh edit vm xml file
To edit a vm xml file, use:
export EDITOR=vim
sudo virsh edit test
To use nano text editor
export EDITOR=nano
sudo virsh edit test
Virsh suspend vm, virsh resume vm
To suspend a guest called testwith virsh command, run:
$ sudo virsh suspend test
Domain test suspended
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.
Resuming a guest vm:
To restore a suspended guest with virsh using the resume option:
$ sudo virsh resume test
Domain test resumed
Virsh save vm
To save the current state of a vm to a file using the virsh command :
The syntax is:
$ sudo virsh save test test.saved
Domain test saved to test.save
$ ls -l test.save
-rw------- 1 root root 328645215 Mar 18 01:35 test.saved
Restoring a saved vm
To restore saved vm from the file:
$ sudo virsh restore test.save
Domain restored from test.save
$ sudo virsh list
Id Name State
----------------------------------------------------
7 test running
Virsh Manage Volumes
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.
Virsh create volume
To create a 2GB volume named test_vol2 on the default storage pool, use:
$ sudo virsh vol-create-as default test_vol2.qcow2 2G
Vol test_vol2.qcow2 created
$ sudo du -sh /var/lib/libvirt/images/test_vol2.qcow2
2.0G/var/lib/libvirt/images/test_vol2.qcow2
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
[[email protected] ~]#
[[email protected] ~]# 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
vdb 2G disk
Example showing how to create and attach secondary virtual disk:
To detach above volume test_vol2 from the vm test:
$ sudo virsh detach-disk --domain test --persistent --live --target vdb
Disk detached successfully
$ ssh test
Last login: Sat Mar 18 01:52:33 2017 from gateway
[[email protected] ~]#
[[email protected] ~]# 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
[[email protected] ~]#
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:
Here we’ll create another snapshot called test_vm_snapshot2, then revert to snapshot test_vm_snapshot1
$ sudo virsh snapshot-create-as \
--domain test --name "test_vm_snapshot2" \
--description "test vm snapshot 2-working"
Domain snapshot test_vm_snapshot2 created
Let’s revert the snapshot we created before:
$ sudo virsh snapshot-list test
Name Creation Time State
------------------------------------------------------------
1489689679 2017-03-16 21:41:19 +0300 shutoff
test_fresh 2017-03-16 22:11:48 +0300 shutoff
test_vm_snapshot1 2017-03-18 02:15:58 +0300 running
test_vm_snapshot2 2017-03-18 02:23:29 +0300 running
$ sudo virsh snapshot-revert --domain test --snapshotname test_vm_snapshot1 --running
Virsh delete snapshot
Let’s delete the two snapshots we created:
$ sudo virsh snapshot-delete --domain test --snapshotname test_vm_snapshot2
Domain snapshot test_vm_snapshot2 deleted
$ sudo virsh snapshot-delete --domain test --snapshotname test_vm_snapshot1
Domain snapshot test_vm_snapshot1 deleted
$ sudo virsh snapshot-list test
Name Creation Time State
------------------------------------------------------------
1489689679 2017-03-16 21:41:19 +0300 shutoff
test_fresh 2017-03-16 22:11:48 +0300 shutoff
Virsh clone a vm
Domain with devices to clone must be paused or shutoff. So let’s shut it down:
$ sudo virsh destroy test
Domain test destroyed
Then clone a vm, do it as shown below:
$ sudo virt-clone --connect qemu:///system \
--original test \
--name test_clone \
--file /var/lib/libvirt/images/test_clone.qcow2
Allocating 'test_clone.qcow2' | 10 GB 00:00:06
Clone 'test_clone' created successfully.
$ sudo virsh dominfo test_clone
Id: -
Name: test_clone
UUID: be0621fd-51b5-4d2b-a05c-ce76e59baafa
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
Virsh manage VM vcpus
This virsh commands cheatsheet section covers how to add additional virtual cpus to a virtual machine:
sudo virsh setvcpus --domain test --maximum 2 --config
sudo virsh setvcpus --domain test --count 2 --config
sudo virsh shutdown test
sudo virsh start test
Confirm that the number of vcpu has changed, the previous was 1, the current value is 2:
$ sudo virsh dominfo test
Id: -
Name: test
UUID: a943ed42-ba62-4270-a41d-7f81e793d754
OS Type: hvm
State: shut off
CPU(s): 2
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
Virsh manage VM memory
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.
$ sudo virsh dominfo test
Id: 9
Name: test
UUID: a943ed42-ba62-4270-a41d-7f81e793d754
OS Type: hvm
State: running
CPU(s): 2
CPU time: 70.7s
Max memory: 2048 KiB
Used memory: 2048 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
Notice that the current ram allocated to the VM is 2048
Mount Virtual Disk
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:
To ls a directory on a running VM, you need the libguestfs-tools installed.
The run:
$ sudo virt-ls -l -d
E.g
$ sudo virsh list
Id Name State
----------------------------------------------------
10 allot_netxplorer_01 running
19 sg-ve-01 running
$ sudo virt-ls -l -d allot_netxplorer_01 /root
total 28
dr-xr-x---. 2 root root 135 Mar 22 14:26 .
dr-xr-xr-x. 17 root root 224 Mar 21 10:37 ..
-rw-------. 1 root root 385 Mar 22 14:26 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 1447 Mar 21 10:38 anaconda-ks.cfg
Common options are:
-R |--recursive --> Recursive listing
-h |--human-readable --> Human-readable sizes in output
-d |--domain guest --> Add disks from libvirt guest
-l |--long --> Long listing
cat a file in a virtual machine
You can as well cat a file without doing ssh to the VM or accessing it via the console. You need the libguestfs tools installed on the hypervisor for this to work.
Use virt-top to display stats of virtualized domains. You can filter by CPU, RAM e.t.c and save the output to a CSV file.
$ 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:
sudo virt-top --debug >(logger -t virt-top)
Enable VNC on existing VM instance
List domains on KVM
$ virsh list --all
Stop the instance
virsh shutdown
Edit the VM domain config using virsh edit command.
$ virsh edit
Add below XML contents within block (Accessible from outside)
Local loopback listen – (Accessible only locally on the host)
Display log files from a virtual machine
“virt-log” is a command line tool to display the log files from the named virtual machine (or disk image). This tool understands and displays both plain text log files (eg. /var/log/messages) and binary formats such as the systemd journal.
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.
I would like to thank you for taking your time to read this post. Please share and comment if you have any issue.
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.