≡ Menu

How to Install Linux KVM and Create Guest VM with Examples

Linux KVMKVM stands for Kernel-based Virtual Machine.

As the name suggests, this is kernel based virtualization technology for Linux OS on hardware that supports virtualization.

The guest operating systems can be fully virtualized or para virtualized.

Para-virtualization enables several operating systems to run on one set of hardware by effectively using resources such as processors and memory. In Para virtualization, the operating system is modified to work with a virtual machine, which will minimize the execution time required to perform the operations that are otherwise difficult to run in a virtual environment.

This is the 1st article in an on-going series of articles on KVM.

1. Tools to Manage VM

The KVM package provides qemu-kvm, a hypervisor specific tool to manage virtual machines.

Instead of this tool, you can use the software package known as libvirt which is more convenient and recommended way to manage virtual machines. This software includes an API library, a daemon (libvirtd), and a command line utility (virsh).

You can use this libvirt tool to manage any existing virtual machines running on KVM, Xen, VMWARE ESX, etc. Full list of supported hypervisors, is listed on libvirt.

There are few other tools available that you can use to manage your guest VM.

  • virt-manager (Virtual Machine Manager): A graphical management tool for VM Guests.
  • vm-install: A script based menu driven utility that define a VM Guest and install its operating system.
  • virt-viewer: An X viewer client for VM Guests which supports TLS/SSL encryption of x509 certificate authentication and SASL authentication.

2. Check Virtualization Support on your Hardware

To check whether your CPU supports the hardware virtualization, execute the following command.

# egrep '(vmx|svm)' /proc/cpuinfo

If you see vmx or svm in the output under flags section, then your hardware (CPU) supports the virtualization.

flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov 
pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm 
constant_tsc archrfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni 
dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 sse4_2 
popcnt lahf_lm tpr_shadow vnmi flexprioritpt vpid
.

3. Install KVM Packages

Install KVM packages using your distro specific package management tool (for example, yum on RedHat and CentOS).

# yum install kvm

Once you install the main kvm package, install the following KVM related packages that will help you to manage the VMs on your system.

# yum install qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools

The following are the KVM related packages that are installed on my machine. The version number on your system might vary slightly.

# rpm -qa | egrep "virt|kvm|qemu"
python-virtinst-0.600.0-18.el6.noarch
qemu-img-0.12.1.2-2.415.el6.x86_64
libvirt-0.10.2-29.el6.x86_64
virt-viewer-0.5.6-8.el6.x86_64
qemu-kvm-0.12.1.2-2.415.el6.x86_64
libvirt-python-0.10.2-29.el6.x86_64
virt-manager-0.9.0-19.el6.x86_64
virt-top-1.0.4-3.15.el6.x86_64
libvirt-client-0.10.2-29.el6.x86_64
gpxe-roms-qemu-0.9.7-6.10.el6.noarch
virt-what-1.11-1.2.el6.x86_64

After the packages are installed, it is recommended to reboot your machine to load all the KVM and libvirt modules, even though you can reload it using modprobe command.

You can install the Guest OS using Virtul machine manager which vm-install (for SUSE Linux) and virt-install (for RedHat).

When your display is set (by exceed, reflectionX or some other display managers), it provides a graphical interface to create virtual machine Wizard that guides you through the installation process. If the display is not set, i.e when connecting the machine using ssh with no X11-forwarding, vm-install offers a command line wizard to interactively setup a VM guest for installation.

4. Network Pre-req to Create Guest VM

By default, VMs will only have network access to other VMs on the same server (and to the host itself). If you want the VMs to have access to your VLAN, then you must create a network bridge on the host as explained here.

Edit /etc/sysconfig/network-scripts/ifcfg-eth0 and add the line “BRIDGE=br0” (make sure to remove any static IPs).

Create the file /etc/sysconfig/network-scripts/ifcfg-br0 and add the entries as shown below. You can use static or dhcp. I used static IP in this case.

DEVICE="br0"
BOOTPROTO="static"
IPADDR="xxx.xxx.xxx.xxx"
NETMASK="255.255.255.0"
ONBOOT="yes"
TYPE="Bridge"
NM_CONTROLLED="no"

If you are running a firewall, (like iptables), create a rule in to allow bridged traffic.

5. Additional Pre-reqs to Create Guest VM

The default location for the VM image files are under /var/lib/libvirt/images. Please ensure adequate space is available under this directory. If not, you can point the image file to different directory during the creation of VM.

Enable IP forwarding in /etc/sysctl.conf by adding the following line:

inet.ipv3.ip_forward=1

During the testing phase, if you have SELinux running, disable it by setting SELinux to permissive mode.

Reboot the system after the above changes are done.

6. Creating a new Guest VM using virt-install

virt-install tool is used to create the VM. This tool can be used in both interactive or non-interactive mode.

In the following example, I passed all the required values to create an VM as command line parameters to the virt-install command.

# virt-install \
 -n myRHELVM1 \
 --description "Test VM with RHEL 6" \
 --os-type=Linux \
 --os-variant=rhel6 \
 --ram=2048 \
 --vcpus=2 \
 --disk path=/var/lib/libvirt/images/myRHELVM1.img,bus=virtio,size=10 \
 --graphics none \
 --cdrom /var/rhel-server-6.5-x86_64-dvd.iso \
 --network bridge:br0

In the above virt-install command:

  • n Name of your virtual machine
  • description Some valid description about your VM. For example: Application server, database server, web server, etc.
  • os-type OS type can be Linux, Solaris, Unix or Windows.
  • os-variant Distribution type for the above os-type. For example, for linux, it can be rhel6, centos6, ubuntu14, suse11, fedora6 , etc. For windows, this can be win2k, win2k8, win8, win7
  • ram Memory for the VM in MB
  • vcpu Total number of virtual CPUs for the VM.
  • disk path=/var/lib/libvirt/images/myRHELVM1.img,bus=virtio,size=10 Path where the VM image files is stored. Size in GB. In this example, this VM image file is 10GB.
  • graphics none This instructs virt-install to use a text console on VM serial port instead of graphical VNC window. If you have the xmanager set up, then you can ignore this parameter.
  • cdrom Indicates the location of installation image. You can specify the NFS or http installation location (instaed of –cdrom). For example, –location=http://.com/pub/rhel6/x86_64/
  • network bridge:br0 This example uses bridged adapter br0. It is also possible to create your own network on any specific port instead of bridged adapter. If you want to use the NAT then use something like below for the network parameter with the virtual network name known as VMnetwork1. All the network configuration files are located under /etc/libvirt/qemu/networks/ for the virtual machines. For example: –network network=VMnetwork1

7. List Virtual Machines

You can edit the grub menu with “console=tty0 console=ttyS0,115200” to display the console for your screen during installation process on your SSH window. This will let you to install the OS through the guided installation. Else you can use X server for the display.

The following command will display all the virtual machines that are installed on your OS. In this example, we have two VMs installed on this system.

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     dev-dev-vm                     running
 2     myRHELVM1                      running

8. Edit VM Config File

After the VM is created, you’ll see the VM configuration file in the following location. The name of the configuration file will be the name of the VM with .xml extension.

# ls -l /etc/libvirt/qemu/myRHELVM1.xml
-rw-------. 1 root root 2109 Oct 15 12:30 /etc/libvirt/qemu/myRHELVM1.xml

You should avoid editing this file manually and instead use the command virsh edit. To edit the domain (virtual machine) do the following:

virsh edit myRHELVM1

9. Connect to VM Console

To connect to the console of the virtual machine use the following command. You can use “ctrl + ]” to exit out of the VM console.

virsh console myRHELVM1

If you do not have an X server running on your host, connecting to a VMs serial console might be the only way to login to a VM if networking is not available.

Setting up access to a VM’s console is no different than a physical server, where you simply add the proper kernel boot parameters to the VM.

For example, for a RHEL VM (or CentOS VM), append the following parameters to the kernel boot line in /etc/grub.conf and then reboot the VM.

console=tty0 console=ttyS0,115200

Alternatively, you can also add the following entries to the /etc/grub.conf file.

serial --unit=0 --speed=115200
terminal --timeout=5 serial console

10. Display VM Information

To display the VM information, use the following command:

# virsh dominfo myRHELVM1
Id:             5
Name:           myRHELVM1
UUID:           58083ae7-51db-50c3-64d8-bc4c49f642d0
OS Type:        Linux
State:          running
CPU(s):         2
CPU time:       207.6s
Max memory:     2097152 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c698,c788 (permissive)

11. Display VM CPU and Memory Uasge

To display the virtual machine CPU and memory usage, use the virt-top command as shown below.

# virt-top
virt-top 07:14:44 - x86_64 8/8CPU 1600MHz 32094MB
3 domains, 2 active, 2 running, 0 sleeping, 0 paused, 1 inactive D:0 O:0 X:0
CPU: 0.1%  Mem: 4096 MB (4096 MB by guests)
   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
    5 R    0    0 1546    0  0.0  6.0   3:29.55 dev-dev-vm
    8 R    0    0 1546    0  0.0  6.0   1:42.17 myRHELVM2
    -

12. Shutdown, Reboot, or Start VM using virsh

To shutdown the VM, do the following:

# virsh shutdown myRHELVM1
Domain myRHELVM1 is being shutdown

To reboot the VM, do the following:

# virsh reboot myRHELVM1

To start or power on the virtual machine, do the following. Once the VM is started, execute “virsh list –all” to view all the running VMs.

# virsh start myRHELVM1
Domain myRHELVM1 started

In the next article of the KVM series, we’ll cover more details on how to edit and manipulate configurations on an individual VM.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Kyle Amadio October 21, 2014, 12:18 am

    The biggest bug bear with KVM used to be set to setting up iptables. This seems to be a lot better now with later releases

  • yogesh October 21, 2014, 2:04 am

    how to use yum command???
    (Yum install xyz ; gives error)

  • Harshad October 21, 2014, 3:03 am

    If you could provide same for Ubuntu

  • sudi October 21, 2014, 6:21 pm

    sir, You dint mentioned about virt-viewer
    once our vm is ready and after we start our vm thru virsh
    we need to connect to vm console also
    thru $ virt-viewer –connect qemu:///myRHELVM1

  • naphix October 21, 2014, 8:50 pm

    yogesh, you might have an apt-based distro rather than a yum based one. Try replacing yum with apt-get.

  • Bert October 22, 2014, 2:54 am

    I think You have a type mistake:
    inet.ipv3.ip_forward=1
    Should be ipv4 ?
    Best Regards

  • JohnP October 22, 2014, 2:05 pm

    Nice post for RPM-type users.

    Debian/Ubuntu:
    * install packages
    * setup bridge
    * use virt-manager (GUI – like vbox, vsphere, fusion, player, workstation have)
    Be happy.

    No need to enable ipv4-forwarding.
    No need to disable/add filewall rules for VMs.

    Of course, using virt-install and virsh still work, if you insist.
    virt-manager can connect through ssh+libvirt to a remote KVM server and manage everything that way, securely. ssh-keys are hononed.

    There are many presentations on how to do kvm + virt-manager on the internet. My rpm-fu is weak, so I’ll point folks at this page going forward, but at my stuff for debian/ubuntu users which has many fewer steps.

  • Jalal Hajigholamali November 7, 2014, 11:59 pm

    Hi,
    Very nice article
    Thanks a lot…

  • Rollo June 24, 2015, 10:50 am

    After performing an KVM install and trying to access the console I only see the first line “Escape character is^]

    Any help will be appreciated.

  • Rahul August 25, 2015, 12:17 am

    In some articles I have seen that before installing VM using KVM, logical volume is created on OS by using “cfdisk” command and on this logical volume VM-OS is installed.
    Can you please let me know why this logical volume is created to install VM?
    What are the pros/cons of this logical volume to install VM?

  • sharwari phadnis September 18, 2015, 1:40 am

    Is there any provision in the virt-install command to enable ssh on the guest virtual machine during installation?

  • sufian May 3, 2016, 12:37 pm

    I got this error after
    yum install kvm

    “No package kvm available”

    I have Centos 7.

  • Julian Vasquez January 11, 2017, 6:25 am

    Hi,

    Great job,

    why “make sure to remove any static IPs” – /etc/sysconfig/network-scripts/ifcfg-eth0

    I need this ip to be static.

    Thank you!