In the last few blogs, I covered how you can install different Operating systems in a KVM host. Now some of you are wondering I got this guest OS installed, but how do I manage it in the command line.
In this blog, let’s take a look at some of the useful KVM command line commands which you can run on the terminal.
The commands that is going to start is ‘virsh‘
The virsh commands require you to have root privileges. If you don’t have root access, you will have to run the command using sudo. For example, sudo virsh list, so please keep that in mind.
You may either type the whole command with virsh or you just type virsh then hit enter to start using the commands as shown below.
or you can type the whole command with the virsh at the beginning.
1. Manage KVM VM using XML.
KVM XML file is where you update all the system virtual hardware resources that the KVM guest is going to use.
Creation of KVM VM using XML
Once you have the XML created you can type the command below to create the VM.
virsh define win10.xml
Check the KVM xml configuration.
If you would like to go back and check the VM hardware properties, you may enter virsh dumpxml <vmname>
virsh dumpxml win10
This will display the read-only format of the XML file, basically system resources allocated to the VM in the XML format, and you cannot edit them.
Make changes to KVM VM resources using XML
If you would like to make some changes to the VM using the XML, you may enter
virsh edit win10
replace the win10 with your VM name. And The VM should be in shutdown mode, in order for the changes to take affect.
For example, you wanted to increase the CPU of the VM, you may type
virsh edit win10
Modify the line which start with vcpu placment, change the value to the required one, and save the file.
And when you start the VM you should have the VM with the new CPU configuration.
3. How to list KVM VM’s ?
To see the list of VM’s in the KVM host. You can enter the command
virsh list. this should show you the list of VM’s that are currently running.
What if you wanted to see the VM domains which are not only running but also shut down, then you can type
virsh list --all
This command will show you the VM’s that are shut also those VM’s that are running as well.
4. How do I shutdown and start KVM VM’s?
Gracefully shut the VM
To gracefully stop a VM you can type
virsh shutdown <vmname>
If you try to gracefully shut down a VM in KVM, it would take some time, so I use the below command handy until the VM gets shutdown.
watch virsh list --all
The above command would keep updating the terminal for every 2 seconds.
Force the KVM VM to shut
If you are in a hurry and you need to force the VM to shut, then you can use the command
virsh destroy <vmname> .
Some people get worried when they see the word ‘destroy’, which means force stop in the KVM environment.
Is it okay to use virsh destroy command?
It is not recommended to use virsh destroy command as it forces the VM to shut ungracefully, and you should use this command with caution as there are chances using virsh destroy command can cause damage to the VM file system.
And you should use this command if you are not able to shut the VM at all using the virsh shutdown command.
To start the the KVM VM.
To start a VM you can type
virsh start win10
How to reboot a KVM VM ?
If you would like to reboot the VM, then you just have to type
virsh reboot <vmname>
5. how to connect to the KVM host.
Though this command is not actually virsh commands, it will be useful when you work with KVM remotely and wanted to access GUI of the machine remotely, provided your host machine should have x11 enabled.
For the creation of the VM in GUI, you must have used something called virt-manager. After you completed the installation you don’t have to go back to the virt-manager again.
You can type the command
virt-viewer to access the guest VM, Provided you have VNC as the graphics added.
For example, below you can see I typed virt-viewer and it asked me to choose the virtual machine.
Since I have only one virtual machine, I can select the one that I have and click on Connect.
Note: If the VM is shut, you will not be able to access the VM using the virt viewer. So first check whether the VM is running or not using the list virsh list VM –all commands, if the VM is shut, the you have to use the virsh start command to start the VM and then use a virt-viewer to access it.
Connect the CLI based VM’s
The VNC based VM you can access by typing the virt-viewer, however, what if you wanted to access the VM which has only CLI?
Instead of using SSH to get into the KVM VM, you can also get into the VM using the virsh command.
The command to get into the VM is
virsh console <vm>
So if you have Centos VM, with domain name centos, then the command would be
virsh console centos
6. How to automatically start the KVM guest every time you reboot the host machine.
This is something useful if you are using the guest machine as your daily driver and you would like to use it all times.
So every time when you reboot the Ubuntu KVM host and you also wanted the VM to start at the boot you may enter the below command.
virsh autostart <vmname>
To disable the autostart of the VM, enter the command
virsh autostart <vmname> --disable
7.How to delete the KVM VM
if you decide to delete the KVM guest VM, you may enter the command below.
virsh undefine <vmname>
virsh undefine win10
8. To check KVM VM windows disk storage.
To check the VM disk storage, you can type
virsh domblklist win10.
The first Target device VDA is the virtual harddisk, and the second and third is the CDROM that I have attached on my virtula machine during the VM installation.
Let me remove the CDROM drive2 and check the status again.
First, I will shut down the VM using the command
virsh destroy win10 and then I use
virsh edit win10 to edit the VM, and in XML file under devices, you can see the above devices.
let me remove the third device which is the CDROM2 (SDC) and save the xml file.
You may now check the storage details, and you should be able to see only two devices at this time.
The first one is the virtual hardisk and the second one is the CDROM1.
One of the use cases for this command here is for expanding the KVM guest storage.
9. Check the host information.
One important part whenever you wanted to create a VM is that, you need to know the host machines resources and you could plane accordinlgy.
So to get the information about your host, you can type
10. How to backup and restore KVM VM?
The best way to take a backup and restore of KVM VM is by taking snapshots and there are some virsh snapshot commands that I thought I would cover here, then I realized it would take a whole blog when you start explaining it, so no worries, I have also covered how to take snapshots, restore it and deleting with all the commands here.