They offer different flash templates with latest features.
Login

Forgot password
Register
Back
Written by:
misko_2083
Score: 5
votes: 7
Format: Article

 KVM Kernel-based Virtual Machine


Kernel Based Virtual Machine

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (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.

Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.

64-bit kernel is recommended

It is recomended that you use a 64-bit system for your host OS because a 64-bit system can host both 32-bit and 64-bit guests, and 32-bit system can only host 32-bit guests . Also a 32-bit system limits maximum amount of RAM you assing to a guest OS to 2GB.

To check if your kernel is 64-bit execute:

$ uname -a

Output might look similar to this:

3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

x86_64 tells you are runnig 64-bit kernel. If you have i386, i486, i586 or i686 in output, then you are running a 32-bit kernel.

To check if your processor supports 64-bit instructions execute:

$ egrep -c ' lm ' /proc/cpuinfo

If 0 is printed, it means that your CPU is not 64-bit.

If 1 or higher, it is.

 

Check that your CPU supports hardware virtualization

To run KVM, you need a processor that supports hardware virtualization. If hardware virtuaslisation is not supported, you can still run virtual machines, but it will be much slower without the KVM extension.

Intel and AMD both have developed extensions for their processors, deemed respectively Intel VT-x  and AMD-V.

To see if your processor supports one of these, ecxecute:

$ egrep -c '(vmx|svm)' /proc/cpuinfo

If 0 it means that your CPU doesn't support hardware virtualization.

If 1 or more it does - but you still need to make sure that virtualization is enabled in the BIOS.

You can also execute:

$ kvm-ok

If the output is:

INFO: /dev/kvm exists
KVM acceleration can be used

Hardware virtualisation is supported.

 

Install KVM

Install KVM and necessary user-space tools with apt-get command:

$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils

If you wish to install GUI tool run command:

$ sudo apt-get install virt-manager

I don't recommend you to install qtemu.

Add user to libvirtd group, so that the user can launch VMs without root privilege.

$ sudo adduser username libvirt

Log out and log back in as the user to make the group membership change effective.

Run the following command.

$ virsh -c qemu:///system list
 
If you see an empty list of VMs, that means KVM is set up successfully.
Id    Name                           State
----------------------------------------------------

Create a hard disk image

You can create two tipes of images. raw and qcow2. A hard disk image can be raw, so that it is literally byte-by-byte the same as what the guest sees and will always use the full capacity of the guest hard drive on the host. This method provides the least I/O overhead, but can waste a lot of space, as not-used space on the guest cannot be used on the host.

Alternatively, the hard disk image can be in a format such as qcow2 which only allocates space to the image file when the guest operating system actually writes to those sectors on its virtual hard disk. The image appears as the full size to the guest operating system, even though it may take up only a very small amount of space on the host system. Using this format instead of raw will likely affect performance.

QEMU provides the qemu-img command to create hard disk images. For example, to create a 10 GB image in the raw format execute:

$ qemu-img create -f raw image_name.img 10G

To create a 10 GB image in the qcow2 format execute:

$ qemu-img create -f qcow2 image_name.qcow2 10G

Optionally, you can simply create a raw image by creating a file of the needed size using dd or fallocate.

First, make sure you have sufficient disk space to create an image file using df:

$ df -h

$ fallocate -l 10G image_name.img
Verify image:
$ ls -lh image_name.img
or

$ dd if=/dev/zero of=image_name.img bs=1 count=0 seek=10G
Verify file size/blocks after this.
$ stat image_name.img

The qemu-img executable has the resize option, which enables easy resizing of a hard drive image. It works for both raw and qcow2. For example, to increase image space by 5 GB, run:

$ qemu-img resize disk_image +5G

 

Converting a qcow image to a raw file

To convert a qcow image, 'test.img', to a raw 'test.raw' file , run

$ qemu-img convert test.img -O raw test.raw

 

Cenverting raw image to qcow file

To convert a raw 'test.img' file to a qcow formatted 'test.qcow', run

$ qemu-img convert test.img -O qcow test.qcow


Overlay storage images

Instead of using the original image, you can create a storage image once (the 'backing' image) and have QEMU keep mutations to this image in an overlay image. This allows you to revert to a previous state of this storage image. You could revert by creating a new overlay image at the time you wish to revert, based on the original backing image.

To create an overlay image, run command:

$ qemu-img create -f qcow2 -b image_name.qcow2 image_name.ovl

After that you can run your QEMU VM as usual:

$ qemu-system-i386 -enable-kvm image_name.ovl

The backing image will then be left intact and mutations to this storage will be recorded in the overlay image file.

 


Run virtualized system

qemu-system-* binaries (for example qemu-system-i386 or qemu-system-x86_64, depending on guest's architecture) are used to run the virtualized guest. The general usage is:

$ qemu-system-i386 options disk_image

To see available options read the manual:
$ man qemu

Here is an example how to start linux Mint 17 without a virtual hard drive:
$ qemu-system-i386 -enable-kvm -soundhw ac97 -m 1024 -cdrom linuxmint-17-xfce-d
vd-32bit.iso -vga vmware &

For 64-bit version:
$ kvm -soundhw ac97 -m 2048 -cdrom linuxmint-17-xfce-dvd-64bit.iso -vmware std &

With virtual hard drive attached:
$ kvm -hda myhda.qcow2 -soundhw ac97 -m 2048 -cdrom linuxmint-17-xfce-dvd-64
bit.iso -boot d -vga vmware &
 
kvm          for 64-bit systems you can use kvm wrapper sript.
             the script executes qemu-system-x86_64 -enable-kvm
-enable-kvm  enables KVM extensions
-soundhw     E nables audio and selected sound hardware.
-m megs      Set virtual RAM size to megs megabytes.
             Default is 128 MiB. Set virtual RAM size to megs megabytes.
             Examples:   -m 1024     sets virtual RAM to 1 GB
                         -m 2G       sets virtual RAM to 2 GB
                         -m 1200M    sets   virtual RAM to 1200 MB
-cdrom       Use file as CD-ROM image.
-vga         Selects type of VGA card to emulate. 
             vmware works best in this case.
-boot        Specify boot order drives as a string of drive letters.
             Valid drive letters depend on the target achitecture.
             The x86 PC uses: a, b (floppy 1 and 2), c (first hard disk),
             d (first CD-ROM)

When QEMU starts you can use ctrl+alt+f to toggle fullscreen and ctrl+alt to exit mouse grab.

 

Option -device is used to add a device to your virtual machine. To see a list of all avaiable devices use:

$ qemu-system-i386 -device \?

Usermode Networking

In user mode networking traffic is NATed through the host interface to the outside network.

The rtl8139 is the default network adapter in qemu-kvm if no other device is specified. Change it to whatever you need:
$ qemu-system-i386 -netdev user,id=user.0 -device virtio-net-pci,netdev=user.0-device &

$ qemu-system-i386 -netdev user,id=user.0 -device e1000,netdev=user.0-device &

By default, the guest OS will get an IP address in the 10.0.2.0/24 address space and the host OS will be reachable at 10.0.2.2. You can change it with:

$ qemu-system-i386 -enable-kvm -soundhw ac97 -m 1024 -hda disk.img -boot c
 -vga std -netdev user,id=mynet0,net=192.168.2.0/24,dhcpstart=192.168.2.4 &

You can ssh into the host OS from inside the guest OS, install xnest on
the host and then:

#On guest OS
$ ssh -XC -c blowfish user @10.0.2.2 

to start firefox from your host os use -display :1 on guest os.

#guest

$ ssh firefox -display :1 &

Other application work without this option.

If you get this :

/usr/bin/X11/xauth: /home/me/.Xauthority not writable, changes will be ignored
fix it with

#host
$ sudo chown user:user /home/user/.Xau*


 

You can use scp to copy files from host and backwards.

To copy a file from host os.

#guest

$ scp -c blowfish your_username@10.0.2.2:/home/file.txt  /home/user/Downloads

Copy a directory from host.

$ scp -c blowfish -r user@10.0.2.2:/path/  /target/directory

Copy files to a host os.

$ scp -c blowfish file1.txt file2.txt user@10.0.2.2:/target/directory

To increase performance use -c blowfish option. Default is Triple DES and takes so much time to copy some files.

Port forwarding

You can use your guest as a web server for your host. Set your server in a virtual machine and redirect port:

-redir tcp:host_ip::guest ip

$ qemu-system-i386 -m 1G -hda disk.img -redir tcp:5555::80 &

Access your web server content with:

#Host

$ firefox http://localhost:5555/ &

File sharing

You can share files with a Windows guest.

$ qemu-system-i386 -m 1G -hda winxp.img -redir tcp:5556::445 -vga std &

Mount shares:
#host
$ mkdir -p /mnt/qemu
#host
$ mount -t cifs //localhost/someshare /mnt/qemu -o user=test,pass=test,dom=workgroup,port=5556

Password is necessary or you'll get I/O error.


Tags: Virtalisation QEMU KVM Virtual Machine
Created: 4 years ago.
Last edited: 4 years ago.
Reviewed: 4 years ago.


Comments
3 years ago

Kalyk
Thanks!!
My vmware workstation is no longer functional, but using this tutorial I've got an excellent replacement to test Mint 17.3 for updating the english and dutch manuals!!
But indeed you might want to correct the "sudo adduser username libvirtd" (include the "d")
And for the "kvm-ok" command to work I had to do "sudo apt-get install cpu-checker" (which Mint told me by itself :-)
 

Other tutorials from misko_2083