Creating a Virtual Machine on Linux with KVM, QEMU and Virt

Now a days there are quite a few different options available for virtualization on linux, the most famous ones being:

This blog post will be taking about KVM.

A quick summary of which software will be covered here:

KVM – Kernel Virtual Machine
QEMU – Quick Emulator
Virt – The virtualization API

When KVM and QEMU are used in conjunction, the KVM takes care of virtualizing the CPU and memory management while QEMU emulates all the other hardware resources, such as hard-drives, video, cd-rom, peripherals, etc.

Virt is built on top of libvirt, it provides a set of features to manage virtual machines.

1. Checking for support

Before installing any of the software listed above, you first need to check if your hardware supports virtualization.

[sourcecode language=”bash”]
egrep ‘(vmx|svm)’ –color=always /proc/cpuinfo

That should output a list of flags if virtualization is enabled on your hardware:

flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constanttsc archperfmon pebs bts repgood aperfmperf pni dtes64 monitor dscpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse41 lahflm dts tprshadow vnmi flexpriority
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant
tsc archperfmon pebs bts repgood aperfmperf pni dtes64 monitor dscpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse41 lahflm dts tprshadow vnmi flexpriority

If the VMX flag is enabled it means your CPU is Intel, SVM means AMD

2. Installing KVM

After checking if the processor supports virtualization, you can start by installing KVM

[sourcecode language=”bash”]
yum install kvm kmod-kvm

There are several version of KVM, here is a list explaining which version is suitable for which need

3. Installing QEMU

QEMU is not available on the default repositories enabled on CentOS, you need to enable the rpmforge-extras repository to have access to the QEMU package with yum.

To enable the repository:

[sourcecode language=”bash”]
rpm -Uhv rpmforge*

Then modify the file:

[sourcecode language=”bash”]
sudo vim /etc/yum.repos.d/rpmforge.repo

Set the enabled key for the [rpmforge-extrs] to 1

[sourcecode language=”bash”]
### Name: RPMforge RPM Repository for RHEL 6 – dag
### URL:
name = RHEL $releasever – – dag
baseurl =$basearch/rpmforge
mirrorlist =
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1

name = RHEL $releasever – – extras
baseurl =$basearch/extras
mirrorlist =
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge-extras
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1

Now you should be able to run:

[sourcecode language=”bash”]
sudo yum install qemu qemu-kvm

And QEMU should be installed

4. Loading the module

With KVM and QEMU installed, it is time to load the kvm module to start playing with the virtualization tools:

[sourcecode language=”bash”]
sudo modprobe kvm-intel

You might get the error:


FATAL: Error inserting kvmintel (/lib/modules/2.6.32-279.11.1.el6.x8664/kernel/arch/x86/kvm/kvm-intel.ko): Operation not supported

Well, but we checked before and the CPU supports virtualization, right?
Actually most times the BIOS disable virtualization by default, so you need to modify the BIOS settings yourself.
To enable virtualization is very simple, here is a good tutorial explaining the steps.

**Just restarting the computer didn’t work for me.
I had to shutdown my computer and wait a few minutes for the new BIOS settings to take effect.

After enabaling VT for your CPU, you can go ahead and load the modules again:

[sourcecode language=”bash”]
sudo modprobe kvm-intel

To check if they were successfully loaded:

[sourcecode language=”bash”]
lsmod | grep kvm

You should see something like:

kvmintel 52890 0
kvm 314739 1 kvm

5. Adding your user to the KVM group

There are two ways to add your user to the KVM group.
The simples and fastest:

[sourcecode language=”bash”]
sudo usermod -G kvm -a diogogmt

Or if you prefer:

Side Note:
To check the script that will automatically load the KVM module every time the computer is booted:
[sourcecode language=”bash”]
cat /etc/sysconfig/modules/kvm.modules

The contents of the file:
[sourcecode language=”bash”]

if [ $(grep -c vmx /proc/cpuinfo) -ne 0 ]; then
modprobe -b kvm-intel >/dev/null 2>&1

if [ $(grep -c svm /proc/cpuinfo) -ne 0 ]; then
modprobe -b kvm-amd >/dev/null 2>&1

modprobe -b vhost_net >/dev/null 2>&1

exit 0

As you can see it checks if the CPU is Intel or AMD and then loads the appropriate module.

6. Installing Virt

The last step is to install Virt, the software that will allow us to manipulate and configure the virtual machine from a nice rich feature API.

[sourcecode language=”bash”]
sudo yum install lib-virt python-virtinst virt-manager virt-viewer

After installing the packages above you could restart the computer so the changes take effect or start the libvirt service yourself:

[sourcecode language=”bash”]
sudo service libvirtd start

7. Running a VM

Now that everything is installed you can test it out by creating a Virtual Machine.


Installing MongoDB on Ubuntu

This tutorial will cover the basics to get MongoDB running on Ubuntu

I’ll break down the tutorial in 6 parts:

  • 1 - Setting up the environment
  • 2 - Adding repo key
  • 3 - Adding repo source
  • 4 - Installing mongo
  • 5 - Running Mongo
  • 6 - Tips

1 - Setting up the environment

If you tried to install mongo before and wasn’t successful, the best option is to uninstall all the existing mongo packages,. To do that you can run:

[email protected]:~$ dpkg -l | grep mongo

If you see mongodb-10gen installed, then you have the right version, if you see mongodb-server, then you’ve installed from Ubuntu’s repository.
10gen repo is always up to date, and contains all mongo’s updates. So its better to install mongo using their repo.

If mongodb-server is installed, to remove the package run:

dpkg mongodb-server -P

A small description of dpkg:

dpkg is a tool to install, build, remove and manage Debian packages. The primary and more user-friendly front-end for dpkg is aptitude. dpkg itself is controlled entirely via command line parameters, which consist of exactly one action and zero or more options. The action-parameter tells dpkg what to do and options control the behavior of the action in some way.

**Some extra info, on how Ubuntu handles deb packages:
There are several tools to install a deb package on Ubuntu. The base tool that actually do the installation is the dpkg command.
Before the dpkg, is the apt system, which serves as a front end for dpkg. The synapitc, aptitute are a front end for the apt system, which is contained in the apt (Debian package). From apt that all the commands, apt-get, apt-update, apt-key comes from.

This blog has some very good information on how deb packages are handle :

2- Adding repo key

On this tutorial we’ll install mongo using 10gen official repo.

To be able to download mongo with apititude from 10gen repo, a key must be added first. That will verify if the repository is trusted.
The key can be added using apt-key
A quick description for the command:

apt-key is used to manage the list of keys used by apt to authenticate packages. Packages which have been authenticated using these keys will be considered trusted.

Here is the command to add the key:

sudo apt-key adv –keyserver –recv 7F0CEB10

Breaking down the command:
Another command used in the authentication of the key is gpg, because apt-key is called passing adv as an option, gpg will be invoked
GDP quick description:

gpg is the OpenPGP part of the GNU Privacy Guard (GnuPG). It is a tool to provide digital encryption and signing services using the OpenPGP standard. gpg features complete key management and all bells and whistles you can expect from a decent OpenPGP implementation.

More information on GnuPG :
More info on OpenGP :

3- Adding repo source

After you added the key, you can go and add the repository to your list.

On mongo’s website, it says to add

deb dist 10gen

as an apt source.
If you add the source manually by editing the /etc/apt/sources.list like they recommend on the website it will work. However if you go to the Ubuntu Software Centre GUI and add the repo there. Two entries will be made to the /etc/apt/sources.list one as deb repourl and the other as deb-src repo url
For some reason, having the db-src will fail to get the updates.


Manually enter

deb dist 10gen



or use the Ubuntu Software Centre GUI, and after
deb dist 10gen
is added, uncheck deb-src.

**Sysvinit and upstart.

On mongo’s website, there is the option of choosing the upstart and sysvinit repos. If you are using a recent version of Ubuntu(6 >) you can select the upstart.
Sysvinit used to be the startup boot program for ubuntu. Since version 6 Ubuntu has been using upstart.
If you notice, on the /etc/init.d/ dir, a lot of files are links to an upstart job

More info on boot management:

4- Installing mongo

With the repository, and key added to your system, now is time to install mongo.

[sourcecode language=”powershell”]
apt-get install mongodb-10gen

Congratualitions, you have MongoDB installed in your system.

5- Running mongo

If you installed mongo in a new version of Ubuntu it will be possible to start and stop the it as a service. However, if you run the command start mongodb you’ll and get this message:

 [email protected]:~$ start mongodb  
 start: Rejected send message, 1 matched rules; type="method_call", sender=":1.62" (uid=1000 pid=6540 comm="start mongodb ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply=0 destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init"))  

don’t be afraid! Even though the message is not very user friendly, what happens is that you must be root to start/stop a service, so if you run:

[sourcecode language=”powershell”]
[email protected]:~$ sudo start mongodb
mongodb start/running, process 6482

It will work

To run mongo there are a couple of options.
You can start as a service
Or you can simple run the program

Both options have their benefits, some times you just want to create an instance for some project that you are testing

Others you want to have mongo running consistently on the background

If you start mongo as a service, you cannot pass any arguments in the command, example:

 [email protected]:~$ sudo start mongodb –port 27001  
 start: invalid option: –port  
 Try `start –help’ for more information.  

All the configuration for mongo will be in the /etc/mongodb.conf
So every time you start mongo as a service it will have the configuration specified on the mongo.conf file.

Now comparing to running an instance of mongo, every time you start that instance it will have the default configuration.
To change its configuration, you can then pass the options in the star up, for example:

 [email protected]:~$ mongod –port 27001 –dbpath /home/diogogmt/data  
 Mon Oct 24 01:19:03 [initandlisten] MongoDB starting : pid=6576 port=27001 dbpath=/home/diogogmt/data 64-bit host=diogogmt-ID54-Series  
 Mon Oct 24 01:19:03 [initandlisten] db version v2.0.1, pdfile version 4.5  
 Mon Oct 24 01:19:03 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd684  
 Mon Oct 24 01:19:03 [initandlisten] build info: Linux #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41  
 Mon Oct 24 01:19:03 [initandlisten] options: { dbpath: "/home/diogogmt/data", port: 27001 }  
 Mon Oct 24 01:19:03 [initandlisten] journal dir=/home/diogogmt/data/journal  
 Mon Oct 24 01:19:03 [initandlisten] recover : no journal files present, no recovery needed  

or if you want you can load a configuration file passing as an argument:

sudo mongod –config /etc/mongodb.conf

it will create an instance of mongo with the same configuration settings as starting mongo as a service.

6- Tips

Here are just a few tips, that maybe helpful if you’re getting started with mongo:

As you can see mongo gives you a lot of flexibility on how to run an configure your servers.

Like I said before, if you are testing a new project, you can create a new instance of mongo and give a different port and dbpath, so all the changes you make it wont effect the one running as a service.

Another difference, is that when you start mongo as a service, it won’t sit on your terminal listing all the interaction, to see the details of the server you can access http://localhost:28017/ or whatever port you decided to run it.

If you click on the listDatabases tab, it will say that REST is not enable, and you must start mongo with –rest option. However, you can’t pass arguments when you start mongo as a service, and if you check the /etc/mongodb.conf it doesn’t have any REST option.
To fix this is very simple, just add “rest = true” to the conf file.
For a list of all the posible configuration for mongo check their official website:

**You can also just download mongo from their website:
After you unzip, you will see a bin folder, there are all the commands that you need to run mongo.
This way doesn’t give you a lot of flexibility, but if you just want to give it a quick and fast try, it is an option.

In the end, there are several ways to download, install, and run mongo. Choose the one it suits you better.

Good references: