Posts Tagged ‘Virtualization on Linux’

There are CPUs with and without virtualization support. To find whether the CPU you work with supports virtualization, run this command:

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

If it returns anything, the CPU you are using supports virtualization. However even if this is the case, virtualization support may be disabled at BIOS level.

If your computer has a CPU that supports virtualization and you don’t mind messing with BIOS switches, you would be better off if you use KVM or Xen for virtualization. Those solutions make use of the hardware virtualization support and as a result the guest OSs are almost as fast as if they were running without virtualization on a bare metal. If you expect to use the guest OS for any sort of “heavylifting” (e.g. running a build process), you should better use hardware that supports virtualization. You can find instructions on how to install KVM in this my post.

However there are many cases when you can use virtualization without hardware support. For example, you have a computer with x86 CPU architecture and you want to develop for a device which uses CPU with ARM or Atom architecture. Or you want to do something which can break the OS you are experimenting with. And so on. For such cases I would recommend using QEMU emulator. It allows emulating computers with various CPU architectures on x86 host system. It also provides a number of virtual devices. Besides this it uses a number of techniques to make the emulation faster; sometimes the execution speed is even close to what KVM virtualization provides.

QEMU is broadly used, it is a sort of standard de-facto. In fact, KVM uses QEMU virtual machines as well. Another example is Android device emulator. It uses QEMU “under the hood”.

Overall, QEMU is quite amazing piece of software if we consider complexity of the task it performs. It was developed initially by Fabrice Bellard, who is quite remarkable individual as well in my opinion. You can read a good overview of QEMU and its history here.

I have an old 2005 computer running Ubuntu Linux 9.04 which does not have hardware virtualization support; the steps described below were performed on that computer.

A “crash course” on qemu can be found here.

First install QEMU:

 sudo apt-get install qemu

If you are planning to run guest OS with x86 architecture on x86 host system, I highly recommend you installing QEMU accelerator. It increases the speed of QEMU emulation when a PC is emulated on a PC. It does it by running most of the target application code directly on the host processor to achieve near native performance. It is very useful when you want to run another x86 OS (for example Windows) on a Linux desktop. The accelerator is optional and it is not installed as a dependency for qemu package. You have to install it explicitly:

sudo apt-get install kqemu-common

If you are going to provide network access for the virtual machine, you have to install bridge-utils package. Its description says:

This package contains utilities for configuring the Linux Ethernet bridge in Linux 2.4 or later. The Linux Ethernet bridge can be used for connecting multiple Ethernet devices together. The connecting is fully transparent: hosts connected to one Ethernet device see hosts connected to the other Ethernet devices directly.

Type this:

sudo apt-get install bridge-utils

Enabling network connectivity requires certain additional steps. Please look at this post, sections Configuring network access and Troubleshooting.

You can also add recommended packages:

sudo apt-get install ubuntu-vm-builder qemu-kvm-extras

ubuntu-vm-builder is a script which automates the process of creating a ready to use VM based on Ubuntu. You can pass command line options to add extra packages, remove packages, choose which version of Ubuntu, which mirror to use etc.

qemu package contains only x86 CPU support as a default option. If you need yo emulate computers with other CPU architectures, you need to install qemu_kvm_extras. The package supports ARM, CRIS, i386, M68k (ColdFire), MIPS, PowerPC, SH4, SPARc and x86-64 emulation. It uses so-called dynamic translation of executed binaries and improves execution speed substantially when you execute non-x86 code on x86 machine. You can find more about QEMU dynamic translation here.

To see what architectures are supported, type this:

 ls -l /usr/bin/qemu*

You should see something like this as a result:

-rwxr-xr-x 1 root root 1824540 2011-02-11 19:28 /usr/bin/qemu
-rwxr-xr-x 1 root root  741992 2011-02-11 19:28 /usr/bin/qemu-alpha
-rwxr-xr-x 1 root root 1032764 2011-02-11 19:28 /usr/bin/qemu-arm
-rwxr-xr-x 1 root root 1036860 2011-02-11 19:28 /usr/bin/qemu-armeb
-rwxr-xr-x 1 root root  479604 2011-02-11 19:28 /usr/bin/qemu-cris
-rwxr-xr-x 1 root root  902284 2011-02-11 19:28 /usr/bin/qemu-i386
-rwxr-xr-x 1 root root     270 2011-02-11 18:46 /usr/bin/qemu-ifdown
-rwxr-xr-x 1 root root     138 2011-02-11 18:46 /usr/bin/qemu-ifup
-rwxr-xr-x 1 root root  177860 2011-02-11 19:28 /usr/bin/qemu-img
-rwxr-xr-x 1 root root  186080 2011-02-11 19:28 /usr/bin/qemu-io
-rwxr-xr-x 1 root root  663924 2011-02-11 19:28 /usr/bin/qemu-m68k
-rwxr-xr-x 1 root root  913896 2011-02-11 19:28 /usr/bin/qemu-mips
-rwxr-xr-x 1 root root  913896 2011-02-11 19:28 /usr/bin/qemu-mipsel
-rwxr-xr-x 1 root root  173796 2011-02-11 19:28 /usr/bin/qemu-nbd
-rwxr-xr-x 1 root root 1521992 2011-02-11 19:28 /usr/bin/qemu-ppc
-rwxr-xr-x 1 root root 2281576 2011-02-11 19:28 /usr/bin/qemu-ppc64
-rwxr-xr-x 1 root root 2264844 2011-02-11 19:28 /usr/bin/qemu-ppc64abi32
-rwxr-xr-x 1 root root  553428 2011-02-11 19:28 /usr/bin/qemu-sh4
-rwxr-xr-x 1 root root  553428 2011-02-11 19:28 /usr/bin/qemu-sh4eb
-rwxr-xr-x 1 root root  704840 2011-02-11 19:28 /usr/bin/qemu-sparc
-rwxr-xr-x 1 root root 1048912 2011-02-11 19:28 /usr/bin/qemu-sparc32plus
-rwxr-xr-x 1 root root 1086124 2011-02-11 19:28 /usr/bin/qemu-sparc64
-rwxr-xr-x 1 root root 2119672 2011-02-11 19:28 /usr/bin/qemu-system-arm
-rwxr-xr-x 1 root root 1145884 2011-02-11 19:28 /usr/bin/qemu-system-cris
-rwxr-xr-x 1 root root 1289116 2011-02-11 19:28 /usr/bin/qemu-system-m68k
-rwxr-xr-x 1 root root 1828124 2011-02-11 19:28 /usr/bin/qemu-system-mips
-rwxr-xr-x 1 root root 1963292 2011-02-11 19:28 /usr/bin/qemu-system-mips64
-rwxr-xr-x 1 root root 1959200 2011-02-11 19:28 /usr/bin/qemu-system-mips64el
-rwxr-xr-x 1 root root 1824028 2011-02-11 19:28 /usr/bin/qemu-system-mipsel
-rwxr-xr-x 1 root root 2260536 2011-02-11 19:28 /usr/bin/qemu-system-ppc
-rwxr-xr-x 1 root root 2794492 2011-02-11 19:28 /usr/bin/qemu-system-ppc64
-rwxr-xr-x 1 root root 2272828 2011-02-11 19:28 /usr/bin/qemu-system-ppcemb
-rwxr-xr-x 1 root root 1249368 2011-02-11 19:28 /usr/bin/qemu-system-sh4
-rwxr-xr-x 1 root root 1249372 2011-02-11 19:28 /usr/bin/qemu-system-sh4eb
-rwxr-xr-x 1 root root 1373404 2011-02-11 19:28 /usr/bin/qemu-system-sparc
-rwxr-xr-x 1 root root 1748348 2011-02-11 19:28 /usr/bin/qemu-system-sparc64
-rwxr-xr-x 1 root root 2025544 2011-02-11 19:28 /usr/bin/qemu-system-x86_64
-rwxr-xr-x 1 root root 1136420 2011-02-11 19:28 /usr/bin/qemu-x86_64

Quite impressive, isn’t it?

At this point you are ready to run a guest OS as VM using QEMU.

You can download a number of pre-built OS images from here (we’ll discuss creating and managing images in another post). Let’s use a small Linux image for testing our QEMU installation. Type this to download the image:

curl http://wiki.qemu.org/download/linux-0.2.img.bz2 >linux-0.2.img.bz2

(if you don’t have curl install it by typing sudo apt-get install curl or just download the file using e.g. a browser; whatever works for you will do).

Decompress it:

bzip2 -d linux-0.2.img.bz2

Launch qemu:

qemu linux-0.2.img

After this you should see VM console window; something like this:

You can use it as a regular Linux machine console. Type this for example:

ls -al /

and you will see filesystems mounted on the VM:

Later in other posts we are going to look at creating and managing QEMU VM images and troubleshooting.

That’s all for now.


Read Full Post »

In  an earlier post we looked at how to install KVM virtualization on Ubuntu Linux. Now we are going to learn how to change parameters of an existing virtual machine.

Configuration of KVM virtual machine (VM) is stored as a usual xml file. By default it is located in /etc/libvirt/qemu directory.

I assume you have already installed KVM and created one or several virtual machines, so the configuration files already exist.

Type this command:

ls -l /etc/libvirt/qemu

The command will result in output like this:

total 16
drwxr-xr-x 3 root root 4096 2011-01-25 06:00 networks
-rw------- 1 root root 2011 2011-02-02 18:00 testVM.xml

networks directory contains a network bridge configuration file. testVM.xml is a configuration file of testVM virtual machine. Type this:

sudo cat /etc/libvirt/qemu/testVM.xml

(of course, use a name of a virtual machine which exists on your computer instead of testVM.xml).

This will print the configuration file. Here is an example:

<domain type='kvm'>
    <type arch='x86_64' machine='pc-0.12'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/testVM.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <interface type='network'>
      <mac address='52:54:00:47:d3:96'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <serial type='pty'>
      <target port='0'/>
    <console type='pty'>
      <target type='serial' port='0'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes'/>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     <model type='cirrus' vram='9216' heads='1'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
   <memballoon model='virtio'>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

The file is pretty self-explanatory. For example the xml snippet below defines a size of RAM allocated to the virtual machine:


Please note, the memory is allocated in kilobytes, so the number above corresponds to 512MB (524288 = 512 * 1024).

The xml snippet instructs VM to use one CPU core:


Another xml snippet defines that VM uses file /var/lib/libvirt/images/testVM.img as a hard drive:

    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/testVM.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>

Of course, it is possible to edit the file in place. But this is rather a risky method since we can make a mistake etc. The recommended method includes 3 steps. First export the VM xml file via virsh, then edit it and finally import back.

To export VM configuration file we have to type:

virsh dumpxml testVM > /tmp/testVM.xml

Please note, we must put only a name of the VM after dumpxml parameter, without a path or an extension.

Now we can edit the exported xml. Let’s make a mistake in it for a purpose so that to see how an import procedure is going to handle this later. Let’s suppose we changed the snippet which defines a number of CPU to something like this:


Now type:

virsh define /tmp/testVM.xml

Let’s verify that the testVM.xml file has been updated indeed. Type:

ls -l /etc/libvirt/qemu

Notice, file timestamp has been updated and now reflects the time when we imported the file.

How about the wrong number of CPU? Type:

sudo cat /etc/libvirt/qemu/testVM.xml | grep vcpu

You should see this:


which means that virsh silently corrected our mistake.

Read Full Post »

Recently I found that I need 32-bit Ubuntu for certain experiments I wanted to perform. My laptop is running 64-bit Ubuntu. As a solution I decided to install Ubuntu 10.10 32-bit on my laptop as a guest system in a virtual machine (VM). You can use the instructions to install another guest OS, e.g. Windows. The differences will be minimal and obvious.

There are two virtualization solutions available on Ubuntu: KVM and Xen. Both could do what I needed, but after some deliberation I decided to use KVM.

The post below is not a manual on KVM installation; it is just a description of how I installed it. If you look for more in-depth info on KVM installation, I recommend reading Ubuntu documentation. It is slightly outdated for Ubuntu 10.10, but still very helpful.


Check if virtialization is supported by CPU of your computer. Run this command:

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

If the command returns nothing, the CPU does not support virtualization. Even if the CPU supports virtualization, it may be turned off. To turn it on, you’ll need to change the setting in BIOS. However on my laptop it was turned on by default. If it is turned off on your computer, you’ll get this error message while trying to run KVM:

KVM: disabled by BIOS

If your hardware does not support virtualization or virtualization is disabled at BIOS level and you don’t want to mess with BIOS, you can still have a virtualization solution using QEMU emulator. For instructions look at this post.

Install necessary packages:

 sudo apt-get install kvm libvirt-bin

The user you are going to use to run VMs has to be added to the libvirtd group. Doing so will grant the user access to the advanced networking options:

sudo adduser $USER libvirtd

Restart computer. Now we are going to install two graphical utility for managing and accessing virtual machines.

The virt-manager package contains a graphical utility to manage local and remote virtual machines. To install virt-manager enter:

 sudo apt-get install virt-manager

The virt-viewer application allows you to connect to a virtual machine’s console. virt-viewer does require a Graphical User Interface (GUI) to interface with the virtual machine.

  sudo apt-get install virt-viewer

Now we are ready to create a virtual machine. This can be done either through command line or GUI. In both cases you’ll need an image of a guest OS you want to install on VM.

Since I was installing Ubuntu 10.10 32-bit, I download Ubuntu iso image from here.

Here is an example of a command to create VM:

sudo virt-install -n testvm -r 1024 --disk ~/bin/vm/testdisk,size=20 \

-c ~/Downloads/ubuntu-10.10-desktop-i386.iso --accelerate \

--connect=qemu:///system --vnc -v

In this example:

  • testvm – name of the VM to be created
  • 1024 – size of memory in MB
  • ~/ bin/vm/testdisk,size=20 – path to a file that will contain hard drive image of                       VM. The size of the hard drive is 20GB
  • ~/Downloads/ubuntu-10.10-desktop-i386.iso – path to Ubuntu disk image to                           install. Of course, this can be an installer for another OS, e.g. Windows.

VM can be created through GUI as well. Go to Application -> System Tools -> Virtual Machine Manager. This will launch VM Manager:

Double-click on localhost (QEMU) to establish a connection. Then click leftmost button on VM Manager toolbar (the one with tooltip saying Create a new virtual machine). This will bring New VM wizard:

The wizard screens are self-explanatory. Enter a desired name for the VM (e.g. testVM), select Local install and click Forward. On the next screen select ISO disk image you want to install from, select OS type (Linux in our case. Of course, this can be an installer for another OS, e.g. Windows.) and Version (Ubuntu in our case):

Click Forward. On the next screen select VM RAM size and a number of CPU cores the VM will be using (of course, the latter is possible if you use a computer with multi-core CPU). Click Forward again.

On the next screen you have to choose a size of VM hard drive. After choosing, click Forward. This brings the final review screen:

Here you can either click Finish or review/set advanced options. If you select Customize configuration before install, this will bring an additional customization screen after you click Finish:

You can either customize further your VM and click Apply or just close the screen without customization. After the wizrd finishes, the VM will start and OS installation on VM will begin automatically:

This is a regular installation process of the OS (Ubuntu 10.10 in the given case). AT the end you will have a fully functional VM with OS installed. With one caveat: it does not have a network access. Since we do need network access for most practical uses of VM, we’ll configure it.

Configuring network access

I have followed instructions here. They worked well with one correction – I did not need to edit interfaces file to enable the bridge as the instruction suggest. It was enabled automatically by the installation process.

Please note, KVM network bridge does not work with most wireless network drivers; therefore the most likely you will need to use ethernet connection. Make sure, all cables are plugged in properly and there is connectivity through ethernet network interface indeed.

Install qemu package:

 sudo apt-get install qemu

Install Linux capabilities tools:

 sudo apt-get install libcap2-bin

Ubuntu 10.4 (Lucid) and later – Grant specific users the CAP_NET_ADMIN capability. This capability should be assigned cautiously, as it will allow those users to disrupt all networking on the system.

Give qemu the inheritable CAP_NET_ADMIN capability, for 64-bit:

 sudo setcap cap_net_admin=ei /usr/bin/qemu-system-x86_64

Allow specific users to gain the inheritable CAP_NET_ADMIN capability by editing /etc/security/capability.conf file. Add this line to the file:

 cap_net_admin          vlad

Of course, use your user name instead of vlad.

Install the bridge-utils package:

 sudo apt-get install bridge-utils

The document which I have been using suggests as a next step setting up a bridge interface. To do this it recommends to edit /etc/network/interfaces. I didn’t need to do it; everything was configured automatically.

If you want to change VM configuration after you created it, take a look here.


I have encountered two problems while setting KVM networking.

Problem #1. If you computer has a wireless connection with a wireless router, connecting it simultaneously with the same router by an ethernet cable may result in no connectivity at all even for your host OS. To fix this disable a wireless network on your computer so that only ethernet connection was enabled.

Problem #2. If your host OS has network connectivity, but your guest does not have, there may be a problem with IPtables settings. To check this, use following command:

sudo iptables -t nat -L

You should see something like this (be patient, it may take around 1 minute to get to this line, the output may be slow):

Chain POSTROUTING (policy ACCEPT)target     prot opt source               destination
MASQUERADE  tcp  --    !    masq ports: 1024-65535

If this is not present, those entries must be added. You can do this by typing following commands:

virsh net-destroy default
virsh net-start default

This should fix the problem.

Read Full Post »