Useful KVM commands

Saifudheen SidheeqKVM, VirtualizationLeave a Comment

In the last few blogs, I covered how you can install Windows operating systems in a KVM host, be it Centos/Redhat KVM or Ubuntu. But some of you are wondering I got this guest OS installed but how do I manage it.

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

You may either type virsh and hit enter to start using the commands

kvm command line

or you can type the whole command with the virsh at the beginning.

1. Creation of KVM host using the XML.

KVM XML file is where you update all the system virtual hardware that the KVM guest is going to use. Once you have the XML created you can type the command below to create the VM.

virsh define win10.xml

If you would like to go back and check the VM hardware properties, you may enter

virsh dumpxml win10

This will display the hardware properties of the VM, but you cannot edit them.

If you would like to make some changes then you may enter virsh edit win10

For example, you wanted to increase the CPU of the VM, you may type virsh edit win10 and save the file. And when you start the VM you should have the VM with the new configuration.

3. How to list KVM Qemu 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.

virsh list vms

What if you wanted to see the VM 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.

virsh list all

4. How to start and stop the KVM VM’s.

To stop a VM you may type virsh destroy <vmname> .

Some people get worried when they see the word ‘destroy’, which means stop in the KVM environment.

virsh destroy

To start a VM you can type virsh start win10

virsh destroy

5. how to connect to the KVM host.

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, for example below you can see I typed virt-viewer and it asked me to choose the a 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 you have to use virsh start command to start the VM and then use virt-viewer to access it.

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>
virsh autostart enable

To disable the autostart of the VM, enter the command virsh autostart <vmname> --disable

virsh autostart 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

virsh undefine

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.

how to check virtual storage

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.

remove CDROM from the KVM vm

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.

Checking the virtual storage of KVM VM.

One of the use cases for this command here is for expanding the KVM guest storage.