There are different ways to manage virtual machines (VMs) running on KVM hypervisor. For example, virt-manager is a popular GUI-based front-end for VM management. However, if you would like to use KVM on a headless server, GUI-based solutions will not be ideal. That is when virsh comes in handy. virsh is a command-line tool for managing guest VMs. Underneath it, virsh relies on libvirtd service which can control several different hypervisors including KVM, Xen, QEMU, LXC and OpenVZ.
A command-line management interface such as virsh is also useful when you would like to automate the provisioning and management of VMs. Also, the fact that virsh supports multiple hypervisors means you can manage different hypervisors via the same virsh interface.
In this tutorial, I will demonstrate how to run KVM from the command line by using virsh on Debian or Ubuntu.
Step One: Verify Hardware Virtualization Support
As a first step, verify that the host CPU is equipped with hardware virtualization extensions (e.g., Intel VT or AMD-V), which are required for KVM. The following command will do.
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi ept vpid fsgsbase smep
If the output does not contain vmx or svm flag, it means the host CPU does not have hardware virtualization support. Thus you cannot use KVM on the host. After verifying that the host CPU comes with vmx or svm, proceed to install KVM next.
Step Two: Install KVM
Using apt-get, install KVM and related user-space tools.
Next, add your userID to libvirt (or libvirtd) group, so that you can manage VMs as a non-root regular user. The libvirt (or libvirtd) group must have been created as part of KVM installation above.
Reload updated group membership info as follows. Upon asked for a password, enter your login password.
At this point, you should be able to run virsh as a regular user. As a test, try the command below, which will show a list of available VMs (currently none). If you do not encounter a permission error, it means everything is okay so far.
Id Name State ----------------------------------------------------
Step Three: Configure Bridged Networking
One way to enable VMs to access external networks is via a Linux bridge which is provisioned on your Linux host. This is called bridged networking. Here is how to create and configure a Linux bridge br0 for bridged networking with KVM.
First, install a necessary package, and create a Linux bridge from the command line.
$ sudo brctl addbr br0
The next step is to configure Linux bridge in /etc/network/interfaces, so that the bridge is configured automatically upon boot. To use /etc/network/interfaces, you need to disable Network Manager on your system (if you are using it). Follow the instruction here to disable Network Manager.
After disabling Network Manager, go ahead and configure Linux bridge br0 in /etc/network/interfaces as follows.
#auto eth0 #iface eth0 inet dhcp auto br0 iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0
Here I assume that eth0 is the primary network interface that can access external networks. Also, I assume that eth0 is getting its IP address via DHCP. Note that there is no configuration for eth0 in /etc/network/interface. The Linux bridge br0 takes up the configuration of eth0 as eth0 is enslaved to bridge br0.
Restart network service, and verify that Linux bridge is configured successfully. If successful, br0 should be assigned the eth0's DHCP IP address, and eth0 should have no IP address assigned.
Step Four: Create a VM from the Command Line
With KVM, the configuration of a VM is stored in a domain XML file. Thus, the first step to create a VM is to prepare its domain XML file.
The following is a sample domain XML file of a VM. You can use and customize it as needed.
<domain type='kvm'> <name>alice</name> <uuid>f5b8c05b-9c7a-3211-49b9-2bd635f7e2aa</uuid> <memory>1048576</memory> <currentMemory>1048576</currentMemory> <vcpu>1</vcpu> <os> <type>hvm</type> <boot dev='cdrom'/> </os> <features> <acpi/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <disk type="file" device="disk"> <driver name="qemu" type="raw"/> <source file="/home/dev/images/alice.img"/> <target dev="vda" bus="virtio"/> <address type="pci" domain="0x0000" bus="0x00" slot="0x04" function="0x0"/> </disk> <disk type="file" device="cdrom"> <driver name="qemu" type="raw"/> <source file="/home/dev/iso/ubuntu-13.10-server-amd64.iso"/> <target dev="hdc" bus="ide"/> <readonly/> <address type="drive" controller="0" bus="1" target="0" unit="0"/> </disk> <controller type="ide" index="0"> <address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/> </controller> <interface type='bridge'> <mac address='52:54:aa:00:f0:51'/> <source bridge='br0'/> </interface> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport="yes" listen='127.0.0.1'/> </devices> </domain>
The above domain XML file defines the following VM.
- 1GB memory, one vCPU and one hard drive.
- Disk image: /home/dev/images/alice.img.
- Boot from CD-ROM (/home/dev/iso/ubuntu-13.10-server-amd64.iso).
- Networking: bridged networking via br0.
The UUID string inside <uuid></uuid> can be randomly generated. To get a random UUID, you can use uuid command-line tool.
Another way to create a domain XML file is to dump the domain information of an existing VM as follows.
Step Five: Start VM from the Command Line
Before starting a VM, you need to create its initial disk image. For that, you can use qemu-img command, which comes with qemu-kvm package you installed.
The advantage of using qcow2 as a disk image format is that the disk image is not created as a full size (5GB) initially, but grows as the disk gets populated.
Now you are ready to start a VM using the domain XML file you created earlier. The following command will start a VM.
Domain alice created from alice.xml
Verify that a new domain has been created successfully.
Id Name State ---------------------------------------------------- 3 alice running
Also, verify that the virtual interface for the VM (e.g., vnet0) is successfully added to the Linux bridge br0 that you created earlier.
Step Six: Remote Access a VM
To access the console of a running VM remotely, you can use any VNC client.
First, find out the VNC port number for the VM as follows.
In this example, the VNC port number for alice VM is 5900.
Then launch a VNC client, and connect to a VNC server running at <KVM-host-IP>:5900.
Manage VMs with virsh
The following lists common usages of virsh command.
To create a new guest domain and start a VM:
To stop a VM and destroy a guest domain:
To shutdown a VM (without destroying a domain):
To suspend a VM:
To resume a suspended VM:
To autostart a VM upon host booting:
To get domain information of a VM:
You can also manage VMs from within a virsh session. To create and enter a new virsh session, simply run:
At the virsh prompt, you can use any virsh commands.
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.