Search




EverythingVM.com




We are Moving

Thank you to everyone who have supported us since our inception. We will be moving to our new site in the coming months. So please check it out at: www.idkrtm.com We will be migrating all the content over, so you don't have to worry about losing anything.



Installing and Configuring Kernel Virtual Machines (KVM)

KVM Stands for Kernel Virtual Machine.  KVM is a virtualization technology built into the Linux Kernel based on QEMU (Quick Emulator).  QEMU allows for virtualizing hardware, all the way from a complete hardware stack to a single device.  KVM basically takes QEMU and allows it to use hardware extensions to achieve better performance.

In this article I will outline the steps it takes to get a two node KVM farm running using Open Filer for your storage and Fedora Core 14 for your KVM hosts.  By the end of this tutorial you should know how to install and configure KVM, create a virtual machine and perform a live migration between two hosts.

Throughout these instructions I will use gedit for the text editor.  you can of course use any text editor you are comfortable with.  For example, if you are doing this through SSH you will want to use vi or nano.

Part 1

 

Part  2

 

Part 3

Install Components

The first step is to install the various components we will be using.  We will need an NFS client to connect to OpenFiler.  Unfortunately each architecture has a slightly different NFS client.  to find the NFS client appropriate for your system, you can run: yum list | grep nfs the previous command will search the online repository and return all the packages with the word NFS in them.  In my case I am using the i686 package, so to install my NFS client I will type: yum install nfs-utils.i686

Next I need to install the various virtualization components.  
virt-manager is the utility we will use for managing our virtual machines.  
virt-viewer is what allows you to see the console of your virtual machine
libvirt is the actual hypervisor
qemu-kvm is the package that will enable kvm
to install these packages, simply enter the following command: yum install virt-manager virt-viewer libvirt qemu-kvm

At this point I typically restart to ensure all the components load properly.

Configure DNS

After entering the above command you should now have all the components installed.  Next we need to configure our system.  You will need to make sure that DNS is properly setup on each of your host systems, if you do not have a DNS server properly configured, you can work around that using the hosts file on your system.  To add a host file entry, you can enter the following at your command prompt: gedit /etc/hosts

You will want to add a new line for each host you are using.  You will want to enter the IP address of your host system, press the tab key once then enter the host name.  After you have entered all your host machines, each on their own line, save the changes and exit.

Create NFS Share

For step by step instructions on n how to  create a  an NFS share on Open Filer, see my tutorial on configuring Open Filer.  The main step we need to worry about in this section si is that we change the root_squash variable on the NFS share to no_root_squash.

To configure this variable.  You will need to browse to the share configuration screen by  clicking on the Shares menu at the top of the screen, then click on your share.

Scroll to the bottom of the page. Right beneath the NFS section of your Network ACL you will have an option to click Edit.

Click Edit
change UID/GID mapping to no_root_squash.

Click the update button.

For my examples, I will assume you have called your:
volume group volgroup1
volume name   volume1
your share share1.

Mount NFS Share

First thing we need to do is create a directory to mount the NFS share to:
mkdir /var/lib/libvirt/images/kvmshared
The next step is to actually mount the share:
mount -t nfs 10.0.0.28:/mnt/volgroup1/volume1/Share1/ /var/lib/libvirt/images/kvmshared

notice I mounted the share under /var/lib/libvirt/images.  I did this because of something called SELinux.  SELinux is a tool meant to improve the security of a given system.  One of the roles is to ensure the virtual machine does not compromise the host system; or each other.  By default the /var/lib/libvirt/images folder is configured  with the appropriate settings to allow for us to store our virtual machine images in that location, so we will stick to that location so we don't have to mess around with SELinux settings at all.

now that we have mounted our file system, we need to add it to the fstab file, that way the share will stick around after we reboot.  To edit the fstab file, enter gedit /etc/fstab
you will want to insert the following line:
10.0.0.28:/mnt/volgroup1/volume1/share1/ /var/lib/libvirt/images/kvmshared nfs

Now that our storage is mounted, we need to set the appropriate permissions so we can actually use the storage. Enter chmod 755 -R var/lib/libvirt/images

The last step is to tell SELinux that we want to be able to use NFS storage with KVM.  simply type the following in your terminal window: setsebool -P virt_use_nfs on

Configure Firewall

In order to remotely connect to your KVM host machine, you will need to open a few ports on your firewall, including Port 22 for SSH access and Ports 49152-49261 to allow for live migrations of your virtual machines.

Below are instructions on how to do that on Fedora Core 14
Open a Terminal Window
type su -
type your root password
You are now running as root
Type the following to open the Firewall utility: /usr/bin/system-config-firewall
You may be prompted to re-type your root password
Now that the firewall utility is open, click on Other Ports
Click Add
Check User Defined, select TCP as the Protocol
Enter 49152 - 49261 into the Port/Port Range field
Click Ok
Click Apply

Configure Hypervisor

Now that we have our DNS and storage configured, we are ready to start the hyper visor.  To start the hypervisor, type service libvirtd start After the service has started, you can start the management application by type virt-manager  Now that the services have been started, and virt-manager has been initilized, all the applicable configuration files should be available.

First thing we need to change our default storage location to be on our NFS Share, that way it will be accessible from the other virtual host machines.  Type: gedit /etc/libvirt/storage/default.xml

Change the path variable from /var/lib/libvirt/images to var/lib/libvirt/images/kvmshared
Be sure to save your changes

Lastly we need to restart the hypervisor by typing: service libvirtd restart and restart virt-manager by closing out of the window, then typing virt-manager in your terminal window.

Creating virtual machines


Before you can create your virtual machine, you will need to locate a CD or ISO.  In this tutorial I will be selecting an ISO.  After you have your ISO File, be sure to copy it to /var/lib/libvirt/images/kvmshared that way it will be accessible by all hosts.

  1. Launch Virt-Manager
  2. Click on the New button in the top left corner of the window
  3. Enter the name of your virtual machine, Example Test3
  4. Select Local Media (ISO Image or CD ROM)
  5. Click Forward
  6. Select Use ISO Image, Click the Browse button
  7. Double click on your ISO File
  8. Click Forward
  9. Select how much memory and how many CPU’s you would like to allocate
  10. Click Forward
  11. Enter how large you would like the virtual hard disk to be.  

I typically un-check the box next to “Allocate Entire Disk Now” since not checking this option is a much more efficient use of your disk space.  However, you will see better disk performance if you leave this option checked; it is up to you which option you wish to check.  It will take some time to allocate the disk image if you leave it checked.

  1. Click Forward
  2. Click Finish



Your Virtual Machine has now been created, and should automatically power on; and open the console window.  If you close the console window the machine will continue running.  If you wish to re-open the console window, Right click on the virtual machine within virtual machine manager, then select Open.

If you find that you can’t move your mouse out of the virtual machine, that is because the virtual machine has your mouse locked.  You can unlock your mouse/keyboard from the virtual machine by pressing Ctrl+Alt keys on the left side of your keyboard.

Live Migration of Virtual Machines

Before you can migrate your virtual machine between hosts, you will need to register both the host and destination computers with virt-manager.

*Note* before proceeding you will need to ensure SSH is configured on your remote system.

To register a machine with Virt-Manager:

Launch Virt-Manager
Click File -> Add connection
check the box next to Connect to remote Host
from the drop down, ensure QEMU/KVM is selected
select SSH as the Method
select root as the user-name
enter the host-name of the remote host machine
Click connect
You will be prompted to accept a certificate if this is your first time connecting
you will be prompted for your root password to complete the connection.
Repeat the process for as many hosts you have.

Perform Migration

Now that you have the hosts registered, it should show what virtual machines are running on which host.  In our case we have a single virtual machine running on one host.  The other host machine is sitting idle.

To migrate the virtual machine, right click on the virtual machine, select migrate.  Select the destination host from the drop down menu, click the migrate button.

The running virtual machine will now be moved from the current host onto the destination hostt without interruption.  After the migration you will see the machine is now running on the destination host.  You can now right click on the powered off machine on the original host and delete the virtual machine.  If you don’t do this, you won’t be able to do a live migration back to the original host, because the machine will already exist.

To delete the virtual machine, simply right click on it and select Delete. When Prompted DO NOT DELETE ASSOCIATED STORAGE FILES.  If you delete the storage files, your virtual machine will be gone.

Summary

At this point you have have a virtual machine which can easily be migrated between KVM Host machines.  The virtual machine is stored on an NFS Share, so you can easily scale this to include additional host machines.

In a future article I will cover KVM more in depth including the CLI commands to do much of what you have learned to do via virt-manager and I will include instructions on creating bridged network connections so your virtual machines won’t be stuck behind a NAT.

Troubleshooting

If you find you are getting errors while performing a live migration, try entering the following at the cli:
iptables -I INPUT -j ACCEPT

You can find logs at the following location: /var/log/libvirt

You can list all the running virtual machines by entering the following command:
virsh -c qemu:///system list

you can boot a virtual machine without using the virt-manager by issueing the following command:
qemu-kvm -m <How many MB of Memory> -cdrom <Path to ISO> -boot d <Path to  Hard Disk Image>

config files are stored under /etc/libvirt