Using Kernel Samepage Merging with KVM

For the original and writeup of the article, do look at Using KSM (Kernel Samepage Merging) with KVM. There is a correponding pdf article Increasing Virtual Machine Density with KSM (pdf) by QUMRANET

In short, from the article

Kernel SamePage Merging is a recent linux kernel feature which combines identical memory pages from multiple processes into one copy on write memory region. Because kvm guest virtual machines run as processes
under linux, this feature provides the memory overcommit feature to kvm so important to hypervisors for more efficient use of memory……

Pointer 1. Verifying Kernel KSM Support

# grep KSM /boot/config -'uname -r'

You should see something like this if KSM is enabled


You should also see a directory for KSM in

Pix taken from Linux-KVM
Pointer 2: By default, KSM is limited to 2000 kernel pages.

To verify, type the following command

# cat /sys/kernel/mm/ksm/max_kernel_pages
You should see

Pointer 3: Verifying KVM Support for Samepage Merging

From the article…..
In order for your KVM guests to take advantage of KSM, your version of qemu-kvm must explicitly request from the kernel that identical pages be merged using the new madvise interface. The patch for this feature was added to the kvm development tree just recently following the kvm-88 release. If you’re compiling kvm yourself you can verify whether your version of kvm will support KSM by inspecting exec.c source file for the following lines of code

If you don’t see these lines in your exec.c file then your kvm process will still run fine but but it won’t take advantage of KSM.

        madvise(new_block->host, size, MADV_MERGEABLE);

Pointer 4 – Run multiple simiar guests

…….With multiple virtual machines running, you can verify that KSM is working by inspecting the following file to see how many pages are being shared between your kvm guests.

If the value is greateer than zero, KSM is used

# cat /sys/kernel/mm/KSM/pages_sharing

Installing Linux Kernal-Based Virtual Machine (KVM) on CentOS 5.4 Server (Part II)

This is a continuation from Installing Linux Kernal-Based Virtual Machine (KVM) on CentOS 5.4 Server (Part I)

Step 8: Prepare the Virtual Machine

Prepare a Installation Script for easier management.

a. For CentOS Virtual Machine

# vim
virt-install \
--connect qemu:///system \
--name centos5.4_n01 \
--vcpus=2 \
--ram 1024 \
--disk path=/nfs_shared/vms/centos5.4_n01.img,size=40, \
--cdrom=/nfs_shared/ISO/CentOS-5.4-x86_64-bin-DVD.iso \
--network=bridge:br0 \
--accelerate \
--vnc \
--noautoconsole \
--os-type=linux \
--os-variant=rhel5.4 \

b. For Windows XP Machine

# vim
virt-install \
--connect qemu:///system \
--name winxp_n01 \
--vcpus=1 \
--ram 1024 \
--disk path=/nfs_shared/vms/winxp_n01.img,size=25, \
--cdrom=/nfs_shared/ISO/Windows_XP2.iso \
--network=bridge:br0 \
--accelerate \
--vnc \
--noautoconsole \
--os-type=windows \
--os-variant=winxp \

Some notes:

  • Do note that the disk path should be on a shared drive if you are planning to use “migration” from physical nodes to another physical node.
  • noautoconsole -> No connection to it is started by default although you can make a connection to it via virt-manager.
  • accelerate -> The VM will runin using kernel acceleration if available.
  • os-type and os-variant -> Please check the man virt-install for more information on the exact paramters.

Step 9: Run the script

You should see something like this

Starting install...
Creating storage file...                                 |  40 GB     00:00
Creating domain...                                       |    0 B     00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

Step 10: Continue the installation through Virt-Manager Console.

Remember to start the libvert daemon first

# service libvertd start

If you are on the graphical console, just type

# virt-manager

you should be able to see the node name. double-clicked the node icon, you should be able to continue the rest of the installation

Installing Linux Kernal-Based Virtual Machine (KVM) on CentOS 5.4 Server (Part I)

Taken from the KVM main site:

Linux Kernel-Based Virtual Machine (KVM) is a full virtualisation solution for Linux on x86 hardware containing virtualization extension (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. KVM also requires a modified QEMU although work is underway to get the required changes upstream. 

This guide is modified from the article from Howtoforge – “Virtualization With KVM On A CentOS 5.2 Server”  and IBM – Quick Start Guide for installing and running KVM. But we will be installing Virt-Manager on the CentOS. I hope to put in additional  note for us to consider as well. Similarly, the this write-up draw information from  

Step 1. Set SELINUX to “disabled”

# vim /etc/selinux/config

Step 2: Check that hardware support hardware virtualisation

# egrep '(vmx|svm)' --color=always /proc/cpuinfo

Your output should be something like this

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 syscall nx pdpe1gb rdtscp lm constant_tsc nonstop_tsc pni monitor
ds_cpl vmx smx est tm2 cx16 xtpr popcnt lahf_lm

If you do not get this output, then we can conclude that your hardware does not support virtualisation

Before you do a yum, make sure you  have EPEL Repository enabled. For more information see Red Hat Enterprise Linux / CentOS Linux Enable EPEL (Extra Packages for Enterprise Linux) Repository

Step 3: Install the KVM and virtinst (tools to create virtual machines)

yum install kvm kmod-kvm qemu libvirt python-virtinst

Reboot the System

# shutdown -r now

Step 4: Verify that the kernel has is loaded

# lsmod | grep kvm

the output should be something like

kvm_amd               50452  0
kvm                   109264  1 kvm_intel

Check that the KVM is installed

# virsh -c qemu:///system list
Id Name                 State

Step 5: Setting up of a Network Bridge so that the VM can be accessed from other hosts on the same network

a. Install the Bridge

# yum install bridge-utils

b. Configure the Bridge. Create the “bridge configuration file”. Ensure the BOOTPROTO, BROADCAST, IPADDR, NETMASK and NETWORK are the same as /etc/sysconfig/network-scripts

# vim /etc/sysconfig/network-scripts/ifcfg-br0

c. Modify the /etc/sysconfig/network-scripts/ifcfg-eth0. Amend as follows

# Chelsio Communications Inc T310 10GbE Single Port Adapter

d. Disable NetworkManager

# chkconfig NetworkManager off
# service NetworkManager stop
# chkconfig network on

e. Restart the Network

# service network start

f. Verify that the Network Bridge is working

# ifconfig
br0       Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::210:a7ff:fe05:afeb/64 Scope:Link
          RX packets:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1160 (1.1 KiB)  TX bytes:14875 (14.5 KiB)

eth0      Link encap:Ethernet  HWaddr yy:yy:yy:yy:yy:yy
          inet6 addr: fe80::210:a7ff:fe05:afeb/64 Scope:Link
          RX packets:13662 errors:7 dropped:160 overruns:4 frame:0
          TX packets:11646 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:15144608 (14.4 MiB)  TX bytes:1379942 (1.3 MiB)
          Interrupt:74 Base address:0xcc00

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:38 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4308 (4.2 KiB)  TX bytes:4308 (4.2 KiB)

virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:9987 (9.7 KiB)

Step 6: Installing Virt-Manager on CentOS on a remote or local server

# yum install virt-manager
 Package           Arch     Version                             Repository
 virt-manager      x86_64   0.6.1-12.el5                        base      1.5 M
Installing for dependencies:
 e4fsprogs-libs    x86_64   1.41.9-3.el5                        base      104 k
                   x86_64   2.16.0-3.el5                        base       17 k
 gtk-vnc           x86_64   0.3.8-3.el5                         base       81 k
 gtk-vnc-python    x86_64   0.3.8-3.el5                         base       12 k
 libvirt           x86_64   0.6.3-33.el5_5.3                    updates   2.0 M
 libvirt-python    x86_64   0.6.3-33.el5_5.3                    updates   137 k
 python-virtinst   noarch   0.400.3-9.el5_5.1                   updates   380 k
 xen-libs          x86_64   3.0.3-105.el5_5.5                   updates   156 k
 xz-libs           x86_64   4.999.9-0.3.beta.20091007git.el5    base       95 k

Transaction Summary
Install      10 Package(s)
Upgrade       0 Package(s)

Total download size: 4.5 M
Is this ok [y/N]:

Step 7: Install Virtual Machines using virt-install

a. Do put an ISO of the Operating System into Server so that you can build the Virtual Machine From. If you only have the disk but not the ISO, you may want to look at “Making Disc Images using mkisofs” from Linux Toolkit

b. Do also take a look at the Guest Support Status from KVM to know what is supported for the version of KVM.

Installing Linux Kernal-Based Virtual Machine (KVM) on CentOS 5.4 Server (Part II)