In a previous article we discussed how to install the KVM hypervisor and create a virtual machine. In a single article we were not able to cover the KVM virtual machine management in detail. Today we will try to show how to manage virtual machines from the command line with virsh
: how to change VM settings, add additional virtual devices, and which commands to use to manage KVM virtual machines on Linux.
Using Virsh (Virtual Shell) Commands to Manage KVM Virtual Machines
Let’s take a look on how to view the registered KVM virtual machines, how to start, stop or delete them. Virsh
(based on libvirt API) can be used to manage KVM virtual machines in the Linux console.
# virsh list
– shows the list of running VMs
# virsh list --all
– shows the list of all registered virtual machines (including those that are shut down)
As you can see in the screenshot, in the first case a shutdown VM was not displayed.
# virsh shutdown <vm name>
— shutdown the virtual machine
# virsh start <vm name>
— start the virtual machine
# virsh suspend <vm name>
— suspend the VM
# virsh resume <vm name>
— start a suspended virtual machine
# virsh reboot <vm name>
— restart VM
# virsh destroy <vm name>
— initiates an immediate ungraceful shutdown and stops the VM (use this command when the guest virtual machine is unresponsive)
# virsh undefine <vm name>
— unregister the VM and delete its storage volumes
# virsh vcpuinfo <vm name>
— shows the information about the vCPU of the VM
Here are some other commands to get different information about a virtual machine:
# virsh domid <vm name>
— get the virtual machine ID
# virsh domuuid <vm name>
— show the UUID of a virtual machine
# virsh dominfo <vm name>
— displays information about a virtual machine
# virsh domstate <vm name>
— view VM state
# virsh dumpxml <vm name>
— list the configuration file of a virtual machine in the XML format
How to Add Memory and vCPU to a KVM Virtual Machine?
In the KVM console, you can add or reduce CPU or memory resources assigned to a VM in two ways:
- Using virsh
- Via a VM configuration XML file
If a virtual machine is running, shut it down:
# virsh shutdown test-centos
Domain test-centos is being shutdown
Then use virsh to change the number of virtual CPUs (vCPU) to 6:
# virsh setvcpus <vm name> <vcpu_count> --config
For example:
# virsh setvcpus test-centos 6 --config
After I had run the command, an error appeared:
error: invalid argument: requested vcpus is greater than max allowable vcpus for the persistent domain: 6 > 4
You cannot set the number of cores greater than the maximum number. To increase the maximum allowed number of VM vCPUs, run this command:
# virsh setvcpus test-centos 6 --config --maximum
Then run the previous command and start the virtual machine:
Let’s check the number of vCPUs in the VM settings:
# virsh dumpxml test-centos
<domain type='kvm'> <name>test-centos</name> <uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>6</vcpu>
In the same way, you can add additional RAM to the virtual machine:
# virsh setmem <vm_name> <memsize> --config
For example:
# virsh setmem test-centos 4G --config
An error occurred on the same reason:
error: invalid argument: cannot set memory higher than max memory.
Let’s increase the maximum amount of memory:
# virsh setmaxmem test-centos 6G --config
Then you can add memory to the VM.
Before making any changes, be sure to shutdown the virtual machine and then start it.
Also, you can change the amount of resources of a KVM VM in its XML configuration file. You can edit it online or backup the VM XML file, modify it and apply to the virtual machine.
Let’s edit the VM XML file online:
# virsh edit <vm_name>
In the vi editor that appears, make the changes you want by pressing the Insert button.
Change the following block:
<domain type='kvm'> <name>test-centos</name> <uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid> <memory unit='KiB'>6291456</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>6</vcpu> <os>
For example, let’s allocate 2 processor cores and 1 GB of memory to the VM:
Note that the memory size is shown in kilobytes.
Save the changes in the file and reboot your VM:
# virsh reboot <vm_name>
You can do the same if you backup the XML file:
# virsh dumpxml <vm_name> > /root/test.xml
# vi /root/test.xml
Change the settings you want, save the file and apply it to the virtual machine:
# virsh shutdown test-centos
Domain test-centos is being shutdown
# virsh define /root/test.xml
Domain test-centos defined from /root/test.xml
# virsh start test-centos
Domain test-centos started
Add a Disk to an Existing KVM VM
In one of our articles we described how to extend or reduce the disk size of KVM virtual machine. But we did not tell how to add an additional disk.
First of all, create a new disk file for a virtual machine:
# qemu-img create -f qcow2 -o size=20G /vz/disk/test.img
Instead of qcow2, you can use the disk format you want. Also specify the file path. My disk storage is located in /vz/disk/.
Then you can add a virtual disk device to the VM:
# virsh attach-disk <vm_name> /vz/disk/test.img vdb --type disk --persistent
Shutdown and reboot your VM, then see what you have got:
# virsh shutdown test-centos
Domain test-centos is being shutdown
# virsh start test-centos
Domain test-centos started
# virsh dumpxml test-centos
<domain type='kvm' id='14'> <name>test-centos</name> <uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>6</vcpu> <resource> <partition>/machine</partition> </resource> <os> <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> <boot dev='cdrom'/> <boot dev='hd'/> <bootmenu enable='yes'/> </os> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/vz/disk/test.img'/> <backingStore/> <target dev='vdb' bus='virtio'/> <alias name='virtio-disk1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/> </disk>
As you can see, the disk has been added.
How to Add a Second NIC to the KVM Virtual Machine?
Let’s try to add another network interface to a VM. First of all, check the network interfaces on the Linux host:
# brctl show
I have one virtual machine with one network interface on my KVM host. I want to add another virtual network interface to br0. Run these commands:
# virsh shutdown test-centos
# virsh attach-interface test-centos --type bridge --source br0 --persistent
# virsh start test-centos
Make sure that another network interface has appeared in your VM:
<interface type='bridge'> <mac address='52:54:00:7e:c1:9f'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='bridge'> <mac address='52:54:00:2f:23:79'/> <source bridge='br0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/> </interface>
Also you can change network settings of your virtual machine directly via the XML file:
# virsh edit test-centos
After the first network interface, add these lines:
<interface type='bridge'> <source bridge='br0'/> </interface>
Save the file and start the VM. KVM will add the rest configuration (MAC address, etc.) itself.
In this article we have covered the main things you may need to manage KVM virtual machines from the Linux server console. In the next article we will show how to manage virtual machines through the virt-manager
GUI.